]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94808: Cover `PyObject_PyBytes` case with custom `__bytes__` method (#96610)
authorNikita Sobolev <mail@sobolevn.me>
Thu, 6 Oct 2022 12:16:16 +0000 (15:16 +0300)
committerGitHub <noreply@github.com>
Thu, 6 Oct 2022 12:16:16 +0000 (05:16 -0700)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Lib/test/test_long.py

index d092e0176c2616500d531989ac915bfda1a6f54f..b6407b5a7c881ed536c36dc6774d35f960a5bbfd 100644 (file)
@@ -1518,6 +1518,22 @@ class LongTest(unittest.TestCase):
         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