gh-94808: Cover PyObject_PyBytes case with custom __bytes__ method (GH-96610)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
(cherry picked from commit e39ae6bef2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2022-10-06 05:44:05 -07:00 committed by GitHub
parent 1144cf64d8
commit 4aa93afd6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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(ZeroDivisionError, int.from_bytes, RaisingBytes())
@support.cpython_only
def test_from_bytes_small(self):
# bpo-46361