-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
gh-71810: fix corner case (length==0) for int.to_bytes() #138739
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
base: main
Are you sure you want to change the base?
Conversation
```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ```
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.
LGTM
This comment was marked as resolved.
This comment was marked as resolved.
@serhiy-storchaka: I would suggest to backport this change to Python 3.13 and 3.14. What do you think? |
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.
It seems there are no enough tests for signed=True
. Please add also tests for (128).to_bytes(0, 'big', signed=True)
and (-129).to_bytes(0, 'big', signed=True)
.
Disclaimer: I was thinking about |
Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2. |
Done. There are such tests, but rather for "automatic" value for length parameter (determined by result): Line 1262 in 1ce0553
It seems to be a big refactoring of tests. Maybe it does make sense as a separate pr? |
self.assertRaises(OverflowError, (128).to_bytes, 0, 'big', signed=True) | ||
self.assertRaises(OverflowError, (128).to_bytes, 0, 'little', signed=True) | ||
self.assertRaises(OverflowError, (-129).to_bytes, 0, 'big', signed=True) | ||
self.assertRaises(OverflowError, (-129).to_bytes, 0, 'little', signed=True) |
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.
Sorry, use n=1 here.
@@ -1377,6 +1377,10 @@ def equivalent_python(n, length, byteorder, signed=False): | |||
self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=True) |
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.
And this test is redundant. We need to test for 128.
Uh oh!
There was an error while loading. Please reload this page.