]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.15] gh-139871: Fix 3.15 bytearray.take_bytes example (GH-149520) (#149622)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 9 May 2026 21:47:21 +0000 (23:47 +0200)
committerGitHub <noreply@github.com>
Sat, 9 May 2026 21:47:21 +0000 (21:47 +0000)
commit6ba3ea43a032ceedaf917fd0f1952afb238756eb
tree640b65a59997a4308121ba30f4c01e07ea934953
parentf488c7d45f5186be2bb52054b4dd3bc59a45fde5
[3.15] gh-139871: Fix 3.15 bytearray.take_bytes example (GH-149520) (#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 cc5cf14ae0a3665ba9d192cc4152c0a46a9dab2f)

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Doc/whatsnew/3.15.rst