[Python-checkins] [3.10] gh-94808: Cover `PyObject_PyBytes` case with custom `__bytes__` method (GH-96610) (#98121)

JelleZijlstra webhook-mailer at python.org
Sun Oct 9 10:02:45 EDT 2022


https://github.com/python/cpython/commit/ccb56804fd4a0b6d6d21325c2dd11ef16077eca6
commit: ccb56804fd4a0b6d6d21325c2dd11ef16077eca6
branch: 3.10
author: Nikita Sobolev <mail at sobolevn.me>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-10-09T07:02:39-07:00
summary:

[3.10] gh-94808: Cover `PyObject_PyBytes` case with custom `__bytes__` method (GH-96610) (#98121)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>.
Co-authored-by: Nikita Sobolev <mail at sobolevn.me>

(cherry picked from commit e39ae6bef2c357a88e232dcab2e4b4c0f367544b)

files:
M Lib/test/test_long.py

diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index c97842b5bfd2..f8a62077a4fb 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -1350,6 +1350,26 @@ 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
+
+        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
         # ob_digit[0] was being incorrectly accessed for instances of a



More information about the Python-checkins mailing list