Skip to content

bytearray.resize() must not clear the object on error (memory allocation failure) #157242

Description

@vstinner

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions