]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130928: Fix error message during bytes formatting for the `'i'` flag (#130967)
authorAgeev Maxim <maksim170901@gmail.com>
Mon, 24 Mar 2025 19:07:03 +0000 (22:07 +0300)
committerGitHub <noreply@github.com>
Mon, 24 Mar 2025 19:07:03 +0000 (22:07 +0300)
Lib/test/test_bytes.py
Lib/test/test_format.py
Misc/NEWS.d/next/Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst [new file with mode: 0644]
Objects/bytesobject.c

index 115899d9f3d2b83177dd91a66e3f8cba213c7bea..44486760c0834962f015a8d1c17434496e66d4d4 100644 (file)
@@ -5,6 +5,7 @@ the latter should be modernized).
 """
 
 import array
+import operator
 import os
 import re
 import sys
@@ -771,6 +772,9 @@ class BaseBytesTest:
         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)
+
     def test_imod(self):
         b = self.type2test(b'hello, %b!')
         orig = b
index 3916bc3d4cd54cf9707d46ee9bc755d3f6ad61cd..c7cc32e09490b2804434a7914d40c61775a2e0ea 100644 (file)
@@ -283,6 +283,10 @@ class FormatTest(unittest.TestCase):
                         "%x format: an integer is required, not str")
         test_exc_common('%x', 3.14, TypeError,
                         "%x format: an integer is required, not float")
+        test_exc_common('%i', '1', TypeError,
+                        "%i format: a real number is required, not str")
+        test_exc_common('%i', b'1', TypeError,
+                        "%i format: a real number is required, not bytes")
 
     def test_str_format(self):
         testformat("%r", "\u0378", "'\\u0378'")  # non printable
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst
new file mode 100644 (file)
index 0000000..f9f144a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix error message when formatting bytes using the ``'i'`` flag.\r
+Patch by Maxim Ageev.
index 31ba89ffb18379179e7fb980c49c16861cd52aef..fc407ec6bf99d67a9376ee3e9a9b9fbfe52e050c 100644 (file)
@@ -469,8 +469,6 @@ static PyObject *
 formatlong(PyObject *v, int flags, int prec, int type)
 {
     PyObject *result, *iobj;
-    if (type == 'i')
-        type = 'd';
     if (PyLong_Check(v))
         return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type);
     if (PyNumber_Check(v)) {