-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Report bytes out-of-range elements as CPython does #8679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -813,3 +813,22 @@ def cannot(fn, message): | |
| lambda: bytearray(b"ab").__setitem__(slice(0, 2), "ab"), | ||
| "can assign only bytes, buffers, or iterables of ints in range(0, 256)", | ||
| ) | ||
|
|
||
|
|
||
| def out_of_range(fn, message): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Remove this new helper and its associated assertions from AGENTS.md reference: AGENTS.md:L273-L279 Useful? React with 👍 / 👎. |
||
| try: | ||
| fn() | ||
| except ValueError as e: | ||
| assert str(e) == message, e | ||
| else: | ||
| raise AssertionError(f"expected ValueError: {message}") | ||
|
|
||
|
|
||
| # `bytes` is the one entry point that does not name a single byte, and both the | ||
| # sized and unsized iterator paths report it that way. | ||
| out_of_range(lambda: bytes([256]), "bytes must be in range(0, 256)") | ||
| out_of_range(lambda: bytes(iter([256])), "bytes must be in range(0, 256)") | ||
| out_of_range(lambda: bytes([-1]), "bytes must be in range(0, 256)") | ||
| out_of_range(lambda: bytearray([256]), "byte must be in range(0, 256)") | ||
| out_of_range(lambda: bytearray(iter([256])), "byte must be in range(0, 256)") | ||
| out_of_range(lambda: bytearray().extend([256]), "byte must be in range(0, 256)") | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a caller-specific element error instead of unconditionally selecting the
byteswording here. When an extension passes[256]toPyByteArray_FromObject,crates/capi/src/bytearrayobject.rscallsbytes_from_object, so this change makes that bytearray API raiseValueError: bytes must be in range(0, 256); CPython'sPyByteArray_FromObjectraises the singularbyte must be in range(0, 256)for this input.Useful? React with 👍 / 👎.