Skip to content

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

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

gh-157242: Add _PyBytes_IsMutable() assertion#157371
vstinner merged 3 commits into
python:mainfrom
vstinner:bytes_mutable

Conversation

@vstinner

@vstinner vstinner commented Sep 12, 2026

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().
@vstinner

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.

@vstinner

Copy link
Copy Markdown
Member Author

cc @cmaloney

Comment thread Doc/c-api/bytes.rst Outdated
be mutated using :c:func:`PyBytesWriter_GetData`; except if *newsize* is
zero in which case it returns the immutable empty bytes string.

This API is now soft depreacated and the :c:type:`PyBytesWriter` API should

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This just re-iterates the .. soft-deprecated:: note below.

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.

Ah yes, you're right, it's redundant. I removed the paragraph.

@read-the-docs-community

read-the-docs-community Bot commented Sep 12, 2026

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
vstinner enabled auto-merge (squash) September 12, 2026 19:48
@vstinner
vstinner merged commit 121e27c into python:main Sep 12, 2026
54 checks passed
@vstinner
vstinner deleted the bytes_mutable branch September 12, 2026 20:22
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