Skip to content

Commit e39ae6b

Browse files
gh-94808: Cover PyObject_PyBytes case with custom __bytes__ method (#96610)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 0d68879 commit e39ae6b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lib/test/test_long.py

+16
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,22 @@ def __init__(self, value):
15181518
self.assertEqual(i, 1)
15191519
self.assertEqual(getattr(i, 'foo', 'none'), 'bar')
15201520

1521+
class ValidBytes:
1522+
def __bytes__(self):
1523+
return b'\x01'
1524+
class InvalidBytes:
1525+
def __bytes__(self):
1526+
return 'abc'
1527+
class MissingBytes: ...
1528+
class RaisingBytes:
1529+
def __bytes__(self):
1530+
1 / 0
1531+
1532+
self.assertEqual(int.from_bytes(ValidBytes()), 1)
1533+
self.assertRaises(TypeError, int.from_bytes, InvalidBytes())
1534+
self.assertRaises(TypeError, int.from_bytes, MissingBytes())
1535+
self.assertRaises(ZeroDivisionError, int.from_bytes, RaisingBytes())
1536+
15211537
@support.cpython_only
15221538
def test_from_bytes_small(self):
15231539
# bpo-46361

0 commit comments

Comments
 (0)