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

[3.15] gh-156939: Fix two bugs in PyBytesWriter (#158404) by vstinner · Pull Request #158409 · python/cpython · GitHub

Repository navigation

[3.15] gh-156939: Fix two bugs in PyBytesWriter (#158404) - #158409

Merged
hugovk merged 2 commits into
python:3.15from
vstinner:fix_writer15
Sep 30, 2026
Merged

hugovk merged 2 commits into
python:3.15from
vstinner:fix_writer15

Conversation

vstinner commented Sep 29, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Fix PyBytesWriter_Format() error handling: restore the writer to its previous state on error. Add an unit test injecting MemoryError.

Fix PyBytesWriter_FinishWithSize() to return an empty bytearray when use_bytearray is set and size is 0, instead of always returning an empty bytes object.

Fix test_capi.test_bytes: always check writer finish result type, to make sure that _PyBytesWriter_CreateByteArray() always create bytearray objects.

(cherry picked from commit 24ea85a)

Fix PyBytesWriter_Format() error handling: restore the writer to its
previous state on error. Add an unit test injecting MemoryError.

Fix PyBytesWriter_FinishWithSize() to return an empty bytearray when
use_bytearray is set and size is 0, instead of always returning an
empty bytes object.

Fix test_capi.test_bytes: always check writer finish result type, to
make sure that _PyBytesWriter_CreateByteArray() always create
bytearray objects.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit 24ea85a)
bedevere-app Bot added the type-feature A feature request or enhancement label Sep 29, 2026
vstinner removed the type-feature A feature request or enhancement label Sep 29, 2026

Copy link
Copy Markdown
Member Author

Without this fix, bytearray.fromhex() and bytearray.__mod__() can return a bytes object instead of a bytearray object.

Current behavior (wrong):

$ ./python
>>> bytearray.fromhex('')
b''
>>> bytearray(b'') % ()
b''

Correct behavior (with this fix):

$ ./python
>>> bytearray.fromhex('')
bytearray(b'')
>>> bytearray(b'') % ()
bytearray(b'')

Copy link
Copy Markdown
Member Author

@cmaloney: Would you mind to review this fix?

@hugovk: It would be nice to have this fix in Python 3.15.0 final release.

Copy link
Copy Markdown
Member Author

I was curious so I checked the the Git history to see when the bug was introduced: the bug is as old as the introduction of the PyBytesWriter C API (one year ago), commit adb4140.

cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good to me. Feels like maybe should have been a couple different NEWS entries for the different bugs

bedevere-app Bot added the type-feature A feature request or enhancement label Sep 29, 2026

Copy link
Copy Markdown
Member Author

@cmaloney:

Feels like maybe should have been a couple different NEWS entries for the different bugs

Oh right, I added a second NEWS entry.

vstinner removed the type-feature A feature request or enhancement label Sep 29, 2026

Copy link
Copy Markdown
Member Author

test_tkinter failed on "Tests / Windows (free-threading) / Build and test (x64, switch-case)" CI job, but it passed on other Windows CI jobs. It loooks like a flaky test.

FAIL: test_boundary_values_accepted (test.test_tkinter.test_simpledialog.QueryDialogTest.test_boundary_values_accepted)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_tkinter\test_simpledialog.py", line 246, in test_boundary_values_accepted
    self.assertEqual(d.result, 10)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
AssertionError: None != 10

hugovk merged commit 3740f74 into python:3.15 Sep 30, 2026
98 of 102 checks passed

Copy link
Copy Markdown
Member Author

@hugovk Thanks for the merge.

vstinner deleted the fix_writer15 branch September 30, 2026 11:51
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL