gh-139871: Fix 3.15 bytearray.take_bytes example by cmaloney · Pull Request #149520 · python/cpython
Currently:
```python
buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = bytes(buffer[:n + 1])
del buffer[:n + 1]
assert data == b'abc'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
assert data == b'abc'
^^^^^^^^^^^^^^
AssertionError
```
Adding in the `\n` makes the two match:
```python
buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = bytes(buffer[:n + 1])
del buffer[:n + 1]
assert data == b'abc\n'
assert buffer == bytearray(b'def')
buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = buffer.take_bytes(n + 1)
assert data == b'abc\n'
assert buffer == bytearray(b'def')
```
JelleZijlstra pushed a commit that referenced this pull request
…149622) gh-139871: Fix 3.15 bytearray.take_bytes example (GH-149520) Currently: ```python buffer = bytearray(b'abc\ndef') n = buffer.find(b'\n') data = bytes(buffer[:n + 1]) del buffer[:n + 1] assert data == b'abc' Traceback (most recent call last): File "<stdin>", line 1, in <module> assert data == b'abc' ^^^^^^^^^^^^^^ AssertionError ``` Adding in the `\n` makes the two match: ```python buffer = bytearray(b'abc\ndef') n = buffer.find(b'\n') data = bytes(buffer[:n + 1]) del buffer[:n + 1] assert data == b'abc\n' assert buffer == bytearray(b'def') buffer = bytearray(b'abc\ndef') n = buffer.find(b'\n') data = buffer.take_bytes(n + 1) assert data == b'abc\n' assert buffer == bytearray(b'def') ``` (cherry picked from commit cc5cf14) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
cmaloney
deleted the
cmaloney/take_bytes_broken_example
branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters