bytearray.resize() must not clear the object on error (memory allocation failure) #157242
Copy link
Copy link
Closed
Closed
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Python 3.15 reimplemented bytearray using an internal bytes object (to implement the bytearray.take_bytes() method). _PyBytes_Resize() is used internally to resize the bytearray object. Problem: if _PyBytes_Resize() fails, the bytearray object is cleared.
On Python 3.14, the bytearray is not cleared on resize failure.
The following example works on Python 3.14 but fails on Python 3.15.
import _testcapi
data = b'some data'
ba = bytearray(data)
try:
try:
_testcapi.set_nomemory(0)
ba.resize(1024)
except MemoryError:
pass
else:
raise AssertionError("expect MemoryError")
finally:
_testcapi.remove_mem_hooks()
if ba != data:
raise AssertionError(f"{ba} != {data}")Linked PRs
- gh-157242: Fix PyBytesWriter_Resize() on MemoryError #157243
- gh-157242: Leave bytearray unchanged if resize() fails #157340
- gh-157242: Do not close io.BytesIO on MemoryError #157344
- gh-157242: Fix test_capi on Py_TRACE_REFS build #157350
- gh-157242: Fix set_nomemory() on Py_TRACE_REFS build #157351
- gh-157242: Add more PyBytesWriter tests #157352
- gh-157242: Add _PyBytes_IsMutable() assertion #157371
- gh-157242: Allow negative grow in PyBytesWriter_Grow() #157375
- gh-157242: Add support.inject_memory_error() function #157431
- gh-157242: Fix typo in _PyBytes_Resize() #157619
- gh-157242: Add byteswriter_check_consistency() #157723
- gh-157242: Use support.inject_memory_error_cm() in tests #157811
- [3.15] gh-157242: Fix PyBytesWriter_Resize() on MemoryError (#157243) #159260
- [3.15] gh-157242: Leave bytearray unchanged if resize() fails (#157340) #159274
- [3.15] gh-157242: Do not close io.BytesIO on MemoryError (GH-157344) #159275
- [3.15] gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) #159292
- [3.14] gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) #159528
- [3.14] gh-157242: Do not close io.BytesIO on MemoryError #159529
- gh-157242: Use support.inject_memory_error_cm() in test_dict #159543
- [3.15] gh-157242: Add more PyBytesWriter tests #159546
- [3.15] gh-157242: Add support.inject_memory_error() function #159553
- [3.14] gh-157242: Add support.inject_memory_error() function #159582
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
pythongh-157242: Leave bytearray unchanged if resize() fails
vstinner commentedon Sep 10, 2026
The
PyBytesWriter_Resize()function has a similar issue: on failure,writer->objis set to NULL and the writer is left in an inconsistent state.vstinner commentedon Sep 10, 2026
3rd bug:
bytearray.take_bytes()also clears thebytearrayif_PyBytes_Resize()fails. At least, it's not a regression since the method is new in Python 3.15 :-)gh-157242: Fix PyBytesWriter_Resize() on MemoryError (#157243)
pythongh-157242: Do not close io.BytesIO on MemoryError
pythongh-157242: Do not close io.BytesIO on MemoryError
gh-157242: Leave bytearray unchanged if resize() fails (#157340)
gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351)
gh-157242: Add more PyBytesWriter tests (#157352)
pythongh-157242: Fix PyBytesWriter_Resize() on MemoryError (python#15…
pythongh-157242: Leave bytearray unchanged if resize() fails (python#…
pythongh-157242: Fix set_nomemory() on Py_TRACE_REFS build (python#15…
pythongh-157242: Add more PyBytesWriter tests (python#157352)
20 remaining items
pythongh-157242: Fix PyBytesWriter_Resize() on MemoryError (python#15…
[3.15] gh-157242: Fix PyBytesWriter_Resize() on MemoryError (#157243) (…
pythongh-157242: Leave bytearray unchanged if resize() fails (python#…
[3.15] gh-157242: Do not close io.BytesIO on MemoryError (GH-157344) (#…
[3.15] gh-157242: Leave bytearray unchanged if resize() fails (#157340)…
[3.15] gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) (…
pythongh-157242: Fix set_nomemory() on Py_TRACE_REFS build (python#15…
[3.14] gh-157242: Do not close io.BytesIO on MemoryError (#159529)
[3.14] gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) (…
gh-157242: Use support.inject_memory_error_cm() in test_dict (#159543)
[3.15] gh-157242: Add more PyBytesWriter tests (#159546)
[3.15] gh-157242: Add support.inject_memory_error() function (#159553)
[3.15] pythongh-157242: Add support.inject_memory_error() function (p…
[3.14] gh-157242: Add support.inject_memory_error() function (#159582)