From 23419ec8470ff748bf868f2021ca0ce21b1acaf9 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 6 Oct 2022 15:16:16 +0300 Subject: [PATCH 1/2] [3.10] gh-94808: Cover `PyObject_PyBytes` case with custom `__bytes__` method (GH-96610) Co-authored-by: Jelle Zijlstra . (cherry picked from commit e39ae6bef2c357a88e232dcab2e4b4c0f367544b) Co-authored-by: Nikita Sobolev --- 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 c97842b5bfd233..1908ceb1e750da 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1350,6 +1350,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()) + 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 From 3fc65b4569f7777a73689a6cea56d551a0453f2e Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 9 Oct 2022 15:54:40 +0300 Subject: [PATCH 2/2] Use `byteorder` param --- Lib/test/test_long.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 1908ceb1e750da..f8a62077a4fb8f 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1361,10 +1361,14 @@ 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()) + 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