-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-116738: Make lzma module thread-safe #142947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yoney
wants to merge
4
commits into
python:main
Choose a base branch
from
yoney:ft_lzma_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−11
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2025-12-19-12-38-01.gh-issue-116738.iMt3Ol.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Make the attributes in :mod:`lzma` thread-safe on the :term:`free threaded | ||
| <free threading>` build. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||
| #include "pycore_long.h" // _PyLong_UInt32_Converter() | ||||||
| // Blocks output buffer wrappers | ||||||
| #include "pycore_blocks_output_buffer.h" | ||||||
| #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_*_RELAXED | ||||||
|
|
||||||
| #if OUTPUT_BUFFER_MAX_BLOCK_SIZE > SIZE_MAX | ||||||
| #error "The maximum block size accepted by liblzma is SIZE_MAX." | ||||||
|
|
@@ -948,10 +949,10 @@ decompress_buf(Decompressor *d, Py_ssize_t max_length) | |||||
| goto error; | ||||||
| } | ||||||
| if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK) { | ||||||
| d->check = lzma_get_check(&d->lzs); | ||||||
| FT_ATOMIC_STORE_INT_RELAXED(d->check, lzma_get_check(&d->lzs)); | ||||||
| } | ||||||
| if (lzret == LZMA_STREAM_END) { | ||||||
| d->eof = 1; | ||||||
| FT_ATOMIC_STORE_CHAR_RELAXED(d->eof, 1); | ||||||
| break; | ||||||
| } else if (lzs->avail_out == 0) { | ||||||
| /* Need to check lzs->avail_out before lzs->avail_in. | ||||||
|
|
@@ -1038,13 +1039,14 @@ decompress(Decompressor *d, uint8_t *data, size_t len, Py_ssize_t max_length) | |||||
| } | ||||||
|
|
||||||
| if (d->eof) { | ||||||
| d->needs_input = 0; | ||||||
| FT_ATOMIC_STORE_CHAR_RELAXED(d->needs_input, 0); | ||||||
| if (lzs->avail_in > 0) { | ||||||
| Py_XSETREF(d->unused_data, | ||||||
| PyBytes_FromStringAndSize((char *)lzs->next_in, lzs->avail_in)); | ||||||
| if (d->unused_data == NULL) { | ||||||
| PyObject *unused_data = PyBytes_FromStringAndSize( | ||||||
| (char *)lzs->next_in, lzs->avail_in); | ||||||
| if (unused_data == NULL) { | ||||||
| goto error; | ||||||
| } | ||||||
| Py_XSETREF(d->unused_data, unused_data); | ||||||
| } | ||||||
| } | ||||||
| else if (lzs->avail_in == 0) { | ||||||
|
|
@@ -1054,17 +1056,17 @@ decompress(Decompressor *d, uint8_t *data, size_t len, Py_ssize_t max_length) | |||||
| /* (avail_in==0 && avail_out==0) | ||||||
| Maybe lzs's internal state still have a few bytes can | ||||||
| be output, try to output them next time. */ | ||||||
| d->needs_input = 0; | ||||||
| FT_ATOMIC_STORE_CHAR_RELAXED(d->needs_input, 0); | ||||||
|
|
||||||
| /* If max_length < 0, lzs->avail_out always > 0 */ | ||||||
| assert(max_length >= 0); | ||||||
| } else { | ||||||
| /* Input buffer exhausted, output buffer has space. */ | ||||||
| d->needs_input = 1; | ||||||
| FT_ATOMIC_STORE_CHAR_RELAXED(d->needs_input, 1); | ||||||
| } | ||||||
| } | ||||||
| else { | ||||||
| d->needs_input = 0; | ||||||
| FT_ATOMIC_STORE_CHAR_RELAXED(d->needs_input, 0); | ||||||
|
|
||||||
| /* If we did not use the input buffer, we now have | ||||||
| to copy the tail from the caller's buffer into the | ||||||
|
|
@@ -1314,15 +1316,33 @@ PyDoc_STRVAR(Decompressor_needs_input_doc, | |||||
| PyDoc_STRVAR(Decompressor_unused_data_doc, | ||||||
| "Data found after the end of the compressed stream."); | ||||||
|
|
||||||
| static PyObject * | ||||||
| Decompressor_unused_data_get(PyObject *op, void *Py_UNUSED(ignored)) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I always think that Py_UNUSED(ignored) is not helpful. It's more meaningful to indicate what the parameter was meant to represent. |
||||||
| { | ||||||
| Decompressor *self = Decompressor_CAST(op); | ||||||
| if (!FT_ATOMIC_LOAD_CHAR_RELAXED(self->eof)) { | ||||||
| return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); | ||||||
| } | ||||||
| PyMutex_Lock(&self->mutex); | ||||||
| assert(self->unused_data != NULL); | ||||||
| PyObject *result = Py_NewRef(self->unused_data); | ||||||
| PyMutex_Unlock(&self->mutex); | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| static PyGetSetDef Decompressor_getset[] = { | ||||||
| {"unused_data", Decompressor_unused_data_get, NULL, | ||||||
| Decompressor_unused_data_doc}, | ||||||
| {NULL}, | ||||||
| }; | ||||||
|
|
||||||
| static PyMemberDef Decompressor_members[] = { | ||||||
| {"check", Py_T_INT, offsetof(Decompressor, check), Py_READONLY, | ||||||
| Decompressor_check_doc}, | ||||||
| {"eof", Py_T_BOOL, offsetof(Decompressor, eof), Py_READONLY, | ||||||
| Decompressor_eof_doc}, | ||||||
| {"needs_input", Py_T_BOOL, offsetof(Decompressor, needs_input), Py_READONLY, | ||||||
| Decompressor_needs_input_doc}, | ||||||
| {"unused_data", Py_T_OBJECT_EX, offsetof(Decompressor, unused_data), Py_READONLY, | ||||||
| Decompressor_unused_data_doc}, | ||||||
| {NULL} | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -1332,6 +1352,7 @@ static PyType_Slot lzma_decompressor_type_slots[] = { | |||||
| {Py_tp_new, _lzma_LZMADecompressor}, | ||||||
| {Py_tp_doc, (char *)_lzma_LZMADecompressor__doc__}, | ||||||
| {Py_tp_members, Decompressor_members}, | ||||||
| {Py_tp_getset, Decompressor_getset}, | ||||||
| {0, 0} | ||||||
| }; | ||||||
|
|
||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.