Skip to content
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

gh-94808: Cover PyObject_PyBytes case with custom __bytes__ method #96610

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,22 @@ 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

self.assertEqual(int.from_bytes(ValidBytes()), 1)
self.assertRaises(TypeError, int.from_bytes, InvalidBytes)
self.assertRaises(TypeError, int.from_bytes, MissingBytes)
self.assertRaises(TypeError, int.from_bytes, RaisingBytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't raise TypeError:

>>> class RaisingBytes:
...             def __bytes__(self):
...                 1 / 0
... 
>>> int.from_bytes(RaisingBytes())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __bytes__
ZeroDivisionError: division by zero

And yet CI is green. Am I missing something? I'm compiling your PR locally now to see what's going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it's because you don't instantiate the class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, Good catch! Even I missed this.

sobolevn marked this conversation as resolved.
Show resolved Hide resolved

@support.cpython_only
def test_from_bytes_small(self):
# bpo-46361
Expand Down