Skip to content

gh-153419: Fix several issues around bytearray __init__#153498

Open
stestagg wants to merge 1 commit into
python:mainfrom
stestagg:gh-153419
Open

gh-153419: Fix several issues around bytearray __init__#153498
stestagg wants to merge 1 commit into
python:mainfrom
stestagg:gh-153419

Conversation

@stestagg

@stestagg stestagg commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Introduce a bytearray_new function to ensure that
ob_bytes_object is always set on a bytearray.

Resizing a bytearray to 0 length explicitly sets
the ob_bytes_object to the empty constant immortal.

Add a check in the 'bytearray init from string'
fast path to ensure there are no active exports.

This fixes asserts/crashes on the following:

  • bytearray(1).__init__()
  • bytearray().__new__(bytearray).append(1)
  • a = bytearray(); b = memoryview(a); a.__init__('x', 'ascii')

I realise that the explicit setting of bytes_object to Py_CONSTANT_EMPTY_BYTES is not strictly necessary, as this is also done by the _PyBytes_Resize too, but this seemed to mark the intent of what's happening more clearly, given we rely and assert on that exact behaviour.

Introduce a bytearray_new function to ensure that
ob_bytes_object is always set on a bytearray.

Resizing a bytearray to 0 length explicitly sets
the ob_bytes_object to the empty constant immortal.

Add a check in the 'bytearray init from string'
fast path to ensure there are no active exports.

This fixes asserts/crashes on the following:
 - bytearray(1).__init__()
 - bytearray().__new__(bytearray).append(1)
 - a = bytearray(); b = memoryview(a); a.__init__('x', 'ascii')
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.

1 participant