You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Treat bytes objects as immutable in C code #155742
Currently, in the CPython C code, multiple functions treat bytes objects as mutable. For example, they use char* to access their data, instead of const char*.
In the worst case, it can lead to bad bugs like issue gh-155702 in sqlite3 which modifies an immutable bytes object.
Some functions can be rewritten using PyBytesWriter C API to avoid that.
I modified Python locally with following patch to replace char* with const char* in functions PyBytes_AS_STRING(), PyBytes_AsString() and PyBytes_AsStringAndSize(). So I can detect when a bytes object is treated as mutable.
Note that the bug was introduced by rewriting using PyBytesWriter C API which can return a shared immutable bytes object. Before this, the code used PyBytes_FromStringAndSize(NULL, size) which returns unique bytes object. More rewrites can introduce more bugs.
Note that the bug was introduced by rewriting using PyBytesWriter C API which can return a shared immutable bytes object. Before this, the code used PyBytes_FromStringAndSize(NULL, size) which returns unique bytes object. More rewrites can introduce more bugs.
The sqlite3 bug gh-155702 existed before the PyBytesWriter rewrite, but before it couldn't be reached since the old code didn't use the empty bytes singleton.
You're right that we should be careful about such bug when modifying more code to use PyBytesWriter.
@akx: In short, Python 3.15 already implements the optimization that you expect, thanks to the PyBytesWriter conversion of int.to_bytes().
On Python 3.15, PyBytesWriter_Finish() already returns 1-byte singleton if the small buffer is used (writer size up to 256 bytes). For example, (1).to_bytes() calls PyBytesWriter_Create(1): writer of 1 byte (use the small buffer).
On Python 3.14, int.to_bytes() is implemented with PyBytes_FromStringAndSize(NULL, length). It doesn't return a singleton, but always a fresh bytes object.
$ python3.14
>>> i=259; x=(i &255).to_bytes(); y=bytes((i &255,)); x is y
False
On Python 3.15, the 1-byte singleton is used:
$ python3.16
>>> i=259; x=(i & 255).to_bytes(); y=bytes((i & 255,)); x is y
True
The #155795 change is for a more special case: when a large writer was allocated (more than 256 bytes) but then PyBytesWriter_FinishWithSize(writer, 1) is called: truncated to a single byte.
In short, Python 3.16 returns a singleton in all cases. Python 3.15 returns a singleton in the common cases.
Activity
pythongh-155742: Use PyBytesWriter in _textiowrapper_writeflush()
pythongh-155742: Use PyBytesWriter in _io _textiowrapper_writeflush()
pythongh-155742: Use PyBytesWriter in Python/assemble.c
vstinner commentedon Aug 13, 2026
I modified Python locally with following patch to replace
char*withconst char*in functions PyBytes_AS_STRING(), PyBytes_AsString() and PyBytes_AsStringAndSize(). So I can detect when a bytes object is treated as mutable.[-]Treat immutable bytes as immutable in C code[/-][+]Treat bytes objects as immutable in C code[/+]vstinner commentedon Aug 13, 2026
TODO: more code can/should use
PyBytesWriterI may propose PRs for these ones, once most of other
PyBytesWriterPRs will be merged.serhiy-storchaka commentedon Aug 13, 2026
Note that the bug was introduced by rewriting using
PyBytesWriterC API which can return a shared immutable bytes object. Before this, the code usedPyBytes_FromStringAndSize(NULL, size)which returns unique bytes object. More rewrites can introduce more bugs.vstinner commentedon Aug 13, 2026
The sqlite3 bug gh-155702 existed before the
PyBytesWriterrewrite, but before it couldn't be reached since the old code didn't use the empty bytes singleton.You're right that we should be careful about such bug when modifying more code to use
PyBytesWriter.serhiy-storchaka commentedon Aug 13, 2026
It did not exist before the
PyBytesWriterrewrite.gh-155742: Use PyBytesWriter in _zstd.train_dict() (#155745)
pythongh-155742: Use PyBytesWriter in _zstd.train_dict() (python#155745)
gh-155742: Get PyBytes_AS_STRING() as `const char*` (#155751)
40 remaining items
pythongh-155742: Use PyBytesWriter in CJK codecs
gh-155742: Use PyBytesWriter in CJK codecs (#157535)
pythongh-155742: Check singletons consistencty at Python exit
pythongh-155742: Check singletons consistencty at Python exit
pythongh-155742: Check singletons consistencty at Python exit
vstinner commentedon Sep 15, 2026
@akx: In short, Python 3.15 already implements the optimization that you expect, thanks to the
PyBytesWriterconversion ofint.to_bytes().On Python 3.15,
PyBytesWriter_Finish()already returns 1-byte singleton if the small buffer is used (writer size up to 256 bytes). For example,(1).to_bytes()callsPyBytesWriter_Create(1): writer of 1 byte (use the small buffer).On Python 3.14, int.to_bytes() is implemented with
PyBytes_FromStringAndSize(NULL, length). It doesn't return a singleton, but always a fresh bytes object.On Python 3.15, the 1-byte singleton is used:
The #155795 change is for a more special case: when a large writer was allocated (more than 256 bytes) but then
PyBytesWriter_FinishWithSize(writer, 1)is called: truncated to a single byte.In short, Python 3.16 returns a singleton in all cases. Python 3.15 returns a singleton in the common cases.
gh-155742: Use PyMem_Malloc() in decode_unicode_with_escapes() (#157587)
gh-155742: Check singletons consistency at Python exit (#157572)
vstinner commentedon Sep 17, 2026
The main issue has been solved by many changes. I close the issue.
gh-155742: Use PyBytesWriter in _Py_strhex_impl() (#157734)
gh-155742: Use PyBytesWriter in PySSL_RAND() (#157731)
gh-155742: Use PyMem_Malloc() in memoryview.hex() (#158582)
[3.15] gh-158445: Add C API tests on build information functions (#15…
[3.14] gh-158445: Add C API tests on build information functions (#15…