Skip to content

[3.10] gh-94808: Cover PyObject_PyBytes case with custom __bytes__ method (GH-96610) #98121

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

Merged
merged 2 commits into from
Oct 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,26 @@ def __init__(self, value):
self.assertEqual(i, 1)
self.assertEqual(getattr(i, 'foo', 'none'), 'bar')

class ValidBytes:
def __bytes__(self):
return b'\x01'
class InvalidBytes:
def __bytes__(self):
return 'abc'
class MissingBytes: ...
class RaisingBytes:
def __bytes__(self):
1 / 0

for byte_order in ('big', 'little'):
self.assertEqual(int.from_bytes(ValidBytes(), byte_order), 1)
self.assertRaises(
TypeError, int.from_bytes, InvalidBytes(), byte_order)
self.assertRaises(
TypeError, int.from_bytes, MissingBytes(), byte_order)
self.assertRaises(
ZeroDivisionError, int.from_bytes, RaisingBytes(), byte_order)

def test_access_to_nonexistent_digit_0(self):
# http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
# ob_digit[0] was being incorrectly accessed for instances of a
Expand Down