https://github.com/python/cpython/commit/05557788f3c284ede73e6f94810ec796bb9d3721
commit: 05557788f3c284ede73e6f94810ec796bb9d3721
branch: main
author: Ageev Maxim <maksim170...@gmail.com>
committer: picnixz <10796600+picn...@users.noreply.github.com>
date: 2025-04-05T10:30:16+02:00
summary:

gh-131015: Add test for bytes formatting errors (#131881)

Co-authored-by: Bénédikt Tran <10796600+picn...@users.noreply.github.com>

files:
M Lib/test/test_bytes.py

diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 44486760c08349..82d9916e38d341 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -772,8 +772,35 @@ def check(fmt, vals, result):
         check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103   abc')
         check(b'%c', b'a', b'a')
 
-        self.assertRaisesRegex(TypeError, '%i format: a real number is 
required, not complex', operator.mod, '%i', 2j)
-        self.assertRaisesRegex(TypeError, '%d format: a real number is 
required, not complex', operator.mod, '%d', 2j)
+        class PseudoFloat:
+            def __init__(self, value):
+                self.value = float(value)
+            def __int__(self):
+                return int(self.value)
+
+        pi = PseudoFloat(3.1415)
+
+        exceptions_params = [
+            ('%x format: an integer is required, not float', b'%x', 3.14),
+            ('%X format: an integer is required, not float', b'%X', 2.11),
+            ('%o format: an integer is required, not float', b'%o', 1.79),
+            ('%x format: an integer is required, not PseudoFloat', b'%x', pi),
+            ('%x format: an integer is required, not complex', b'%x', 3j),
+            ('%X format: an integer is required, not complex', b'%X', 2j),
+            ('%o format: an integer is required, not complex', b'%o', 1j),
+            ('%u format: a real number is required, not complex', b'%u', 3j),
+            ('%i format: a real number is required, not complex', b'%i', 2j),
+            ('%d format: a real number is required, not complex', b'%d', 2j),
+            (
+                r'%c requires an integer in range\(256\)'
+                r' or a single byte, not .*\.PseudoFloat',
+                b'%c', pi
+            ),
+        ]
+
+        for msg, format_bytes, value in exceptions_params:
+            with self.assertRaisesRegex(TypeError, msg):
+                operator.mod(format_bytes, value)
 
     def test_imod(self):
         b = self.type2test(b'hello, %b!')

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to