https://github.com/python/cpython/commit/7c3692fe275088e986f92cec34dcccb823b31fa2
commit: 7c3692fe275088e986f92cec34dcccb823b31fa2
branch: main
author: Ageev Maxim <[email protected]>
committer: sobolevn <[email protected]>
date: 2025-03-24T22:07:03+03:00
summary:
gh-130928: Fix error message during bytes formatting for the `'i'` flag
(#130967)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst
M Lib/test/test_bytes.py
M Lib/test/test_format.py
M Objects/bytesobject.c
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 115899d9f3d2b8..44486760c08349 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -5,6 +5,7 @@
"""
import array
+import operator
import os
import re
import sys
@@ -771,6 +772,9 @@ 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)
+
def test_imod(self):
b = self.type2test(b'hello, %b!')
orig = b
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 3916bc3d4cd54c..c7cc32e09490b2 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -283,6 +283,10 @@ def test_common_format(self):
"%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
index 00000000000000..f9f144a80aa6c6
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst
@@ -0,0 +1,2 @@
+Fix error message when formatting bytes using the ``'i'`` flag.
+Patch by Maxim Ageev.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 31ba89ffb18379..fc407ec6bf99d6 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -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)) {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]