From 59f8c409050497aa3ac2193ac1110d852a4d11b7 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 6 Sep 2022 11:41:28 +0300 Subject: [PATCH 1/2] gh-94808: Cover `PyObject_PyBytes` case with custom `__bytes__` method --- Lib/test/test_long.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index d092e0176c2616..03852ff8fb63d7 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -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) + @support.cpython_only def test_from_bytes_small(self): # bpo-46361 From 27d5e5cf684c5407eb58c6172ebcb015add91751 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 6 Oct 2022 14:31:42 +0300 Subject: [PATCH 2/2] Update Lib/test/test_long.py Co-authored-by: Jelle Zijlstra --- Lib/test/test_long.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 03852ff8fb63d7..b6407b5a7c881e 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1530,9 +1530,9 @@ 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) + 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):