GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [3.14] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) by vstinner · Pull Request #157373 · python/cpython

Conversation

@vstinner

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

Uh oh!

There was an error while loading. Please reload this page.

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)

  • Issue: bytearray.take_bytes() can corrupt shared single-byte bytes objects #156995

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@StanFromIreland @cmaloney @vstinner
pythongh-156995: Fix _PyBytes_Resize() in Free Threading (python#156996)
722324d
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 <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
(cherry picked from commit fd569ea)
@bedevere-app bedevere-app Bot mentioned this pull request Sep 12, 2026
bytearray.take_bytes() can corrupt shared single-byte bytes objects #156995
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Sep 12, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Sep 12, 2026
gh-156995: Fix bytearray.take_bytes() corrupting shared single-byte bytes objects #156996
Merged
@vstinner

vstinner commented Sep 12, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Add Changelog entry
75afa46
@vstinner

vstinner commented Sep 12, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit fec2734 into python:3.14 Sep 13, 2026
49 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner deleted the take_bytes14 branch September 13, 2026 14:26
@bedevere-app bedevere-app Bot removed the awaiting core review label Sep 13, 2026
vstinner added a commit to vstinner/cpython that referenced this pull request Sep 13, 2026
@vstinner @StanFromIreland @cmaloney
[3.14] pythongh-156995: Fix _PyBytes_Resize() in Free Threading (pyth…
ae17016
…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 <[email protected]>
Co-authored-by: Cody Maloney <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@bedevere-app

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner added a commit that referenced this pull request Sep 13, 2026
@vstinner @StanFromIreland @cmaloney
[3.13] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) (#…
a728f1c
…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 <[email protected]>
Co-authored-by: Cody Maloney <[email protected]>
cmaloney
cmaloney reviewed Sep 14, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/C_API/2026-09-12-21-00-42.gh-issue-156995.UVjqQe.rst
@@ -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

@cmaloney cmaloney Sep 14, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 14, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cmaloney reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@cmaloney cmaloney mentioned this pull request Sep 14, 2026
Use _PyObject_IsUniquelyReferenced instead of Py_REFCNT(x) == 1 for free-threaded compatibility #140061
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@cmaloney cmaloney cmaloney left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@vstinner @cmaloney @StanFromIreland
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.