FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-139871: Fix 3.15 bytearray.take_bytes example by cmaloney · Pull Request #149520 · python/cpython · GitHub

/ cpython Public

gh-139871: Fix 3.15 bytearray.take_bytes example - #149520

Merged
JelleZijlstra merged 1 commit into
python:mainfrom
cmaloney:cmaloney/take_bytes_broken_example
May 9, 2026
Merged

gh-139871: Fix 3.15 bytearray.take_bytes example#149520
JelleZijlstra merged 1 commit into
python:mainfrom
cmaloney:cmaloney/take_bytes_broken_example

Conversation

cmaloney commented May 7, 2026
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Currently:

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 newline both match:

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')

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')
```
cmaloney added the 3.15 pre-release feature fixes, bugs and security fixes label May 7, 2026
cmaloney requested a review from AA-Turner as a code owner May 7, 2026 23:53
bedevere-app Bot added docs Documentation in the Doc dir skip news labels May 7, 2026
github-project-automation Bot moved this to Todo in Docs PRs May 7, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32595407 | 📁 Comparing f69ca1c against main (49918f5)

  🔍 Preview build  

1 file changed
± whatsnew/3.15.html

cmaloney added needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed 3.15 pre-release feature fixes, bugs and security fixes labels May 9, 2026
JelleZijlstra merged commit cc5cf14 into python:main May 9, 2026
46 checks passed

Copy link
Copy Markdown

Thanks @cmaloney for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

github-project-automation Bot moved this from Todo to Done in Docs PRs May 9, 2026

bedevere-app Bot commented May 9, 2026

Copy link
Copy Markdown

GH-149622 is a backport of this pull request to the 3.15 branch.

bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 9, 2026
JelleZijlstra pushed a commit that referenced this pull request May 9, 2026
…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 May 9, 2026 22:15

cmaloney commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @JelleZijlstra !

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL