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

gh-157242: Add _PyBytes_IsMutable() assertion by vstinner · Pull Request #157371 · python/cpython · GitHub

Repository navigation

gh-157242: Add _PyBytes_IsMutable() assertion - #157371

Merged
vstinner merged 3 commits into
python:mainfrom
vstinner:bytes_mutable
Sep 12, 2026
Merged

vstinner merged 3 commits into
python:mainfrom
vstinner:bytes_mutable

Conversation

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

Copy link
Copy Markdown
Member

Elaborate mutability in _PyBytes_Resize() and PyBytesWriter documentation.

Check _PyBytes_IsMutable() in functions which require a mutable bytes object like PyBytesWriter_Resize().

Elaborate mutability in _PyBytes_Resize() and PyBytesWriter
documentation.

Check _PyBytes_IsMutable() in functions which require a mutable bytes
object like PyBytesWriter_Resize().

Copy link
Copy Markdown
Member Author

I also wanted to add these assertions:

assert(_PyObject_IsUniquelyReferenced(v));
assert(get_ob_shash((PyBytesObject*)v) == -1);
  • Check that a bytes object is not shared between threads while it's being modified.
  • Check that a bytes object has not been used a dictionary key (check that its hash value has not been computed yet).

But IMO these checks are too strict and can fail in use cases which work currently.

The PyBytesWriter has this warning:

The API is not thread safe. A PyBytesWriter object must only be used in a single thread, it must not be shared between threads.

But there is no check at runtime, even in debug mode.

Copy link
Copy Markdown
Member Author

cc @cmaloney

Comment thread Doc/c-api/bytes.rst Outdated

read-the-docs-community Bot commented Sep 12, 2026 •
edited
Loading

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34528345 | 📁 Comparing 176a305 against main (575fe39)

  🔍 Preview build  

2 files changed
± c-api/bytes.html
± whatsnew/changelog.html

vstinner enabled auto-merge (squash) September 12, 2026 19:48
vstinner merged commit 121e27c into python:main Sep 12, 2026
54 checks passed
vstinner deleted the bytes_mutable branch September 12, 2026 20:22
johnslavik pushed a commit to johnslavik/cpython that referenced this pull request Sep 14, 2026
Elaborate on mutability in _PyBytes_Resize() and PyBytesWriter
documentation.

Check _PyBytes_IsMutable() in functions which require a mutable bytes
object like PyBytesWriter_Resize().
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL