Skip to content

Commit 011179a

Browse files
gh-71810: Fix corner case (length==0) for int.to_bytes() (#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 ``` Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent a1707e4 commit 011179a

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
@@ -1374,17 +1374,22 @@ def equivalent_python(n, length, byteorder, signed=False):
13741374
check(tests4, 'little', signed=False)
13751375

13761376
self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=False)
1377-
self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=True)
13781377
self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=False)
1379-
self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=True)
1378+
self.assertRaises(OverflowError, (128).to_bytes, 1, 'big', signed=True)
1379+
self.assertRaises(OverflowError, (128).to_bytes, 1, 'little', signed=True)
1380+
self.assertRaises(OverflowError, (-129).to_bytes, 1, 'big', signed=True)
1381+
self.assertRaises(OverflowError, (-129).to_bytes, 1, 'little', signed=True)
13801382
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False)
13811383
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False)
13821384
self.assertEqual((0).to_bytes(0, 'big'), b'')
1385+
self.assertEqual((0).to_bytes(0, 'big', signed=True), b'')
13831386
self.assertEqual((1).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x01')
13841387
self.assertEqual((0).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x00')
13851388
self.assertEqual((-1).to_bytes(5, 'big', signed=True),
13861389
b'\xff\xff\xff\xff\xff')
13871390
self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')
1391+
self.assertRaises(OverflowError, (-1).to_bytes, 0, 'big', signed=True)
1392+
self.assertRaises(OverflowError, (-1).to_bytes, 0, 'little', signed=True)
13881393

13891394
# gh-98783
13901395
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
@@ -1209,7 +1209,7 @@ _PyLong_AsByteArray(PyLongObject* v,
12091209
*p = (unsigned char)(accum & 0xff);
12101210
p += pincr;
12111211
}
1212-
else if (j == n && n > 0 && is_signed) {
1212+
else if (j == n && is_signed) {
12131213
/* The main loop filled the byte array exactly, so the code
12141214
just above didn't get to ensure there's a sign bit, and the
12151215
loop below wouldn't add one either. Make sure a sign bit

0 commit comments

Comments
 (0)