[3.15] gh-156939: Fix two bugs in PyBytesWriter (#158404) - #158409
Conversation
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)
|
Without this fix, Current behavior (wrong): Correct behavior (with this fix): |
|
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
There was a problem hiding this comment.
Looks good to me. Feels like maybe should have been a couple different NEWS entries for the different bugs
Oh right, I added a second NEWS entry. |
|
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. |
|
@hugovk Thanks for the merge. |
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)