summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2022-10-06 12:16:16 (GMT)
committerGitHub <noreply@github.com>2022-10-06 12:16:16 (GMT)
commite39ae6bef2c357a88e232dcab2e4b4c0f367544b (patch)
treecfff7ef74a813a69f66b59fbed4728ca2473af55
parent0d68879104dfb392d31e52e25dcb0661801a0249 (diff)
downloadcpython-e39ae6bef2c357a88e232dcab2e4b4c0f367544b.zip
cpython-e39ae6bef2c357a88e232dcab2e4b4c0f367544b.tar.gz
cpython-e39ae6bef2c357a88e232dcab2e4b4c0f367544b.tar.bz2
gh-94808: Cover `PyObject_PyBytes` case with custom `__bytes__` method (#96610)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
-rw-r--r--Lib/test/test_long.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index d092e01..b6407b5 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -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