FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.14] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) by vstinner · Pull Request #157373 · python/cpython · GitHub

Repository navigation

[3.14] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) - #157373

Merged
vstinner merged 2 commits into
python:3.14from
vstinner:take_bytes14
Sep 13, 2026
Merged

vstinner merged 2 commits into
python:3.14from
vstinner:take_bytes14

Conversation

vstinner commented Sep 12, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

On Free Threading, _PyBytes_Resize(&obj, 1) no longer returns a single byte singleton is the current thread is different than the thread which created the object.

(cherry picked from commit fd569ea)

On Free Threading, _PyBytes_Resize(&obj, 1) no longer returns a
single byte singleton if the current thread is different than the
thread which created the object.

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit fd569ea)

Copy link
Copy Markdown
Member Author

I failed to find a function in Python 3.14 stdlib which still mutates the bytes object after calling _PyBytes_Resize(). The common pattern is more like the following code which is not affected:

bytes = PyBytes_FromStringAndSize(NULL, size);
... write into bytes ...
_PyBytes_Resize(&bytes, n);
return bytes;  // bytes no longer mutated from this point

There is _io.BytesIO.truncate() which may be affected by the issue, but its resize function allocates "size + 1" and so is not affected at the end.

Copy link
Copy Markdown
Member Author

This change should be backported to Python 3.13, but the fix there is different because _PyBytes_Resize() doesn't test _PyObject_IsUniquelyReferenced() but tests Py_REFCNT(v) == 1 instead.

vstinner merged commit fec2734 into python:3.14 Sep 13, 2026
49 checks passed
vstinner deleted the take_bytes14 branch September 13, 2026 14:26
vstinner added a commit to vstinner/cpython that referenced this pull request Sep 13, 2026
…on#156996) (python#157373)

_PyBytes_Resize(&obj, 1) no longer returns a single byte singleton if
the reference count is greater than 1.

(cherry picked from commit fd569ea)

Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>

bedevere-app Bot commented Sep 13, 2026

Copy link
Copy Markdown

GH-157436 is a backport of this pull request to the 3.14 branch.

bedevere-app Bot commented Sep 13, 2026

Copy link
Copy Markdown

GH-157436 is a backport of this pull request to the 3.13 branch.

vstinner added a commit that referenced this pull request Sep 13, 2026
…157373) (#157436)

[3.14] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) (#157373)

_PyBytes_Resize(&obj, 1) no longer returns a single byte singleton if
the reference count is greater than 1.

(cherry picked from commit fd569ea)

Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
@@ -0,0 +1,3 @@
On Free Threading, ``_PyBytes_Resize(&obj, 1)`` no longer returns a single byte
singleton is the current thread is different than the thread which created the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
singleton is the current thread is different than the thread which created the
singleton if the current thread is different than the thread which created the

nit, is -> if

vstinner Sep 14, 2026 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ooops, I wrote #157477 to fix the typo. Thanks for the post-merge review :-)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL