Skip to content

[3.15] gh-139871: Fix 3.15 bytearray.take_bytes example (GH-149520) - #149622

Merged
JelleZijlstra merged 1 commit into
python:3.15from
miss-islington:backport-cc5cf14-3.15
May 9, 2026
Merged

[3.15] gh-139871: Fix 3.15 bytearray.take_bytes example (GH-149520)#149622
JelleZijlstra merged 1 commit into
python:3.15from
miss-islington:backport-cc5cf14-3.15

Conversation

@miss-islington

@miss-islingtonmiss-islington commented May 9, 2026

Copy link
Copy Markdown
Contributor

Currently:

buffer=bytearray(b'abc\ndef')
n=buffer.find(b'\n')
data=bytes(buffer[:n+1])
delbuffer[:n+1]
assertdata==b'abc'Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>assertdata==b'abc'^^^^^^^^^^^^^^AssertionError

Adding in the \n makes the two match:

buffer=bytearray(b'abc\ndef')
n=buffer.find(b'\n')
data=bytes(buffer[:n+1])
delbuffer[:n+1]
assertdata==b'abc\n'assertbuffer==bytearray(b'def')
buffer=bytearray(b'abc\ndef')
n=buffer.find(b'\n')
data=buffer.take_bytes(n+1)
assertdata==b'abc\n'assertbuffer==bytearray(b'def')

(cherry picked from commit cc5cf14)

Co-authored-by: Cody Maloney cmaloney@users.noreply.github.com

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>
@bedevere-appbedevere-appBot added docs Documentation in the Doc dir skip news labels May 9, 2026
@JelleZijlstra
JelleZijlstra enabled auto-merge (squash) May 9, 2026 21:43
@JelleZijlstra
JelleZijlstra merged commit 6ba3ea4 into python:3.15May 9, 2026
38 checks passed
@github-project-automationgithub-project-automationBot moved this from Todo to Done in Docs PRsMay 9, 2026
@miss-islington
miss-islington deleted the backport-cc5cf14-3.15 branch May 9, 2026 21:47
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocumentation in the Doc dirskip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@miss-islington@JelleZijlstra@cmaloney