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

miss-islington webhook-mailer at python.org
Thu Oct 6 08:44:11 EDT 2022


https://github.com/python/cpython/commit/4aa93afd6ef377c22cf9a8505a392f071ea4bc0e
commit: 4aa93afd6ef377c22cf9a8505a392f071ea4bc0e
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-10-06T05:44:05-07:00
summary:

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


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

Co-authored-by: Nikita Sobolev <mail at sobolevn.me>

files:
M Lib/test/test_long.py

diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index d092e0176c26..b6407b5a7c88 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(ZeroDivisionError, int.from_bytes, RaisingBytes())
+
     @support.cpython_only
     def test_from_bytes_small(self):
         # bpo-46361



More information about the Python-checkins mailing list