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
-
Update Documentation: Add the following to the bytearray.take_bytes() documentation:
- Explanation of negative argument behavior
- Note about the limitation with
memoryview references
-
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
Bug report
Bug description:
Python 3.15.0a8
bytearray.take_bytes()Test ReportTest Environment
Issues Found
Issue 1: Negative Argument Behavior Is Undocumented
Description
The
bytearray.take_bytes(n)method accepts negative values forn. When a negative value is provided, the behavior is equivalent totake_bytes(len(ba) + n), similar to Python slice semantics. However, this behavior is not documented.Reproduction Code
Test Results
-1b'hell'b'o'-2b'hel'b'lo'-3b'he'b'llo'-4b'h'b'ello'-5b''b'hello'-6IndexErrorExpected Behavior
Option A: Document the negative argument behavior explicitly
Option B: Reject negative values and raise
ValueErrorIssue 2: BufferError Raised When Active memoryview Exists
Description
When there is an active
memoryviewreference to thebytearray, callingtake_bytes()raises aBufferError. This limitation is not documented.Reproduction Code
Test Results
BufferErrorBufferErrorExpected 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
take_bytes(-1)IndexErrorBufferErrorRecommendations
Update Documentation: Add the following to the
bytearray.take_bytes()documentation:memoryviewreferencesOr Change Behavior:
ValueError)take_bytes()compatible with activememoryviewreferencesAttachments
test_take_bytes_negative_detail.pytest_memoryview.pyCPython versions tested on:
3.15
Operating systems tested on:
macOS