Skip to content

Commit 7195d7f

Browse files
miss-islingtonskirpichevserhiy-storchaka
authored
[3.13] gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-138739) (#138783)
gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-138739) ```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 ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 72b28ca commit 7195d7f

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Lib/test/test_long.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,17 +1322,22 @@ def equivalent_python(n, length, byteorder, signed=False):
13221322
check(tests4, 'little', signed=False)
13231323

13241324
self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=False)
1325-
self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=True)
13261325
self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=False)
1327-
self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=True)
1326+
self.assertRaises(OverflowError, (128).to_bytes, 1, 'big', signed=True)
1327+
self.assertRaises(OverflowError, (128).to_bytes, 1, 'little', signed=True)
1328+
self.assertRaises(OverflowError, (-129).to_bytes, 1, 'big', signed=True)
1329+
self.assertRaises(OverflowError, (-129).to_bytes, 1, 'little', signed=True)
13281330
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False)
13291331
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False)
13301332
self.assertEqual((0).to_bytes(0, 'big'), b'')
1333+
self.assertEqual((0).to_bytes(0, 'big', signed=True), b'')
13311334
self.assertEqual((1).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x01')
13321335
self.assertEqual((0).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x00')
13331336
self.assertEqual((-1).to_bytes(5, 'big', signed=True),
13341337
b'\xff\xff\xff\xff\xff')
13351338
self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')
1339+
self.assertRaises(OverflowError, (-1).to_bytes, 0, 'big', signed=True)
1340+
self.assertRaises(OverflowError, (-1).to_bytes, 0, 'little', signed=True)
13361341

13371342
# gh-98783
13381343
class SubStr(str):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise :exc:`OverflowError` for ``(-1).to_bytes()`` for signed conversions
2+
when bytes count is zero. Patch by Sergey B Kirpichev.

Objects/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ _PyLong_AsByteArray(PyLongObject* v,
10411041
*p = (unsigned char)(accum & 0xff);
10421042
p += pincr;
10431043
}
1044-
else if (j == n && n > 0 && is_signed) {
1044+
else if (j == n && is_signed) {
10451045
/* The main loop filled the byte array exactly, so the code
10461046
just above didn't get to ensure there's a sign bit, and the
10471047
loop below wouldn't add one either. Make sure a sign bit

0 commit comments

Comments
 (0)