Skip to content

bytearray.take_bytes() accepts negative n and raises BufferError with memoryview - both undocumented #148710

@zhouxjpypl

Description

@zhouxjpypl

Bug report

Bug description:

Python 3.15.0a8 bytearray.take_bytes() Test Report

Test Environment

  • Python Version: 3.15.0a8
  • Compiler: Clang 13.0.0 (clang-1300.0.29.30)
  • Operating System: macOS (Darwin)
  • Test Date: 2026-04-18

Issues Found

Issue 1: Negative Argument Behavior Is Undocumented

Description

The bytearray.take_bytes(n) method accepts negative values for n. When a negative value is provided, the behavior is equivalent to take_bytes(len(ba) + n), similar to Python slice semantics. However, this behavior is not documented.

Reproduction Code

ba = bytearray(b'hello')
result = ba.take_bytes(-1)
print(result)  # b'hell'
print(ba)      # bytearray(b'o')

Test Results

Input Return Value Remaining bytearray Explanation
-1 b'hell' b'o' Takes len-1 bytes
-2 b'hel' b'lo' Takes len-2 bytes
-3 b'he' b'llo' Takes len-3 bytes
-4 b'h' b'ello' Takes len-4 bytes
-5 b'' b'hello' Takes 0 bytes
-6 IndexError - Out of range

Expected Behavior

Option A: Document the negative argument behavior explicitly
Option B: Reject negative values and raise ValueError


Issue 2: BufferError Raised When Active memoryview Exists

Description

When there is an active memoryview reference to the bytearray, calling take_bytes() raises a BufferError. This limitation is not documented.

Reproduction Code

ba = bytearray(b'12345')
mv = memoryview(ba)
ba.take_bytes(3)  # BufferError: Existing exports of data: object cannot be re-sized

Test Results

Scenario Result
Active memoryview exists BufferError
After releasing memoryview Works normally
Multiple memoryviews exist BufferError

Expected Behavior

Option A: Document this limitation in the official documentation
Option B: Make take_bytes() work even when memoryview references exist (e.g., create a copy instead of in-place modification)


Expected vs Actual Behavior Summary

Scenario Expected Behavior Actual Behavior Status
take_bytes(-1) Should be documented Slice-like behavior ⚠️ Undocumented
Negative n out of range Should be documented IndexError ⚠️ Undocumented
Call with active memoryview Should be documented BufferError ⚠️ Undocumented

Recommendations

  1. Update Documentation: Add the following to the bytearray.take_bytes() documentation:

    • Explanation of negative argument behavior
    • Note about the limitation with memoryview references
  2. Or Change Behavior:

    • Reject negative arguments (raise ValueError)
    • Make take_bytes() compatible with active memoryview references

Attachments

  • Test script: test_take_bytes_negative_detail.py
  • Test script: test_memoryview.py

CPython versions tested on:

3.15

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions