Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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 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
Next Next commit
gh-94808: Cover PyObject_PyBytes case with custom __bytes__ method
  • Loading branch information
sobolevn committed Sep 6, 2022
commit 59f8c409050497aa3ac2193ac1110d852a4d11b7
16 changes: 16 additions & 0 deletions 16 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.