FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bytearray.resize() must not clear the object on error (memory allocation failure) · Issue #157242 · python/cpython · GitHub

Repository navigation

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

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

Activity

  1. added a commit that references this issue on Sep 10, 2026
  2. vstinner commented on Sep 10, 2026

    MemberAuthor

    The PyBytesWriter_Resize() function has a similar issue: on failure, writer->obj is set to NULL and the writer is left in an inconsistent state.

  3. vstinner commented on Sep 10, 2026

    MemberAuthor

    3rd bug: bytearray.take_bytes() also clears the bytearray if _PyBytes_Resize() fails. At least, it's not a regression since the method is new in Python 3.15 :-)

  4. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 10, 2026
  5. added 6 commits that reference this issue on Sep 11, 2026
  6. added 4 commits that reference this issue on Sep 12, 2026
  7. 6 remaining items

  8. added a commit that references this issue on Sep 12, 2026
  9. added 7 commits that reference this issue on Sep 14, 2026
  10. added 6 commits that reference this issue on Sep 15, 2026
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


      Back | FazBrowse Home | New Git URL