Skip to content

Commit 1568968

Browse files
committed
[3.10] gh-94808: Cover PyObject_PyBytes case with custom __bytes__ method (GH-96610)
Co-authored-by: Jelle Zijlstra <[email protected]>. (cherry picked from commit e39ae6b) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 4d7d91f commit 1568968

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Lib/test/test_long.py

+23
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,29 @@ def __init__(self, value):
13501350
self.assertEqual(i, 1)
13511351
self.assertEqual(getattr(i, 'foo', 'none'), 'bar')
13521352

1353+
class ValidBytes:
1354+
def __bytes__(self):
1355+
return b'\x01'
1356+
class InvalidBytes:
1357+
def __bytes__(self):
1358+
return 'abc'
1359+
class MissingBytes: ...
1360+
class RaisingBytes:
1361+
def __bytes__(self):
1362+
1 / 0
1363+
1364+
self.assertEqual(int.from_bytes(ValidBytes()), 1)
1365+
self.assertRaises(TypeError, int.from_bytes, InvalidBytes())
1366+
self.assertRaises(TypeError, int.from_bytes, MissingBytes())
1367+
self.assertRaises(ZeroDivisionError, int.from_bytes, RaisingBytes())
1368+
1369+
@support.cpython_only
1370+
def test_from_bytes_small(self):
1371+
# bpo-46361
1372+
for i in range(-5, 257):
1373+
b = i.to_bytes(2, signed=True)
1374+
self.assertIs(int.from_bytes(b, signed=True), i)
1375+
13531376
def test_access_to_nonexistent_digit_0(self):
13541377
# http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
13551378
# ob_digit[0] was being incorrectly accessed for instances of a

0 commit comments

Comments
 (0)