Skip to content

Commit 7c3692f

Browse files
authored
gh-130928: Fix error message during bytes formatting for the 'i' flag (#130967)
1 parent 929afd1 commit 7c3692f

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

Lib/test/test_bytes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import array
8+
import operator
89
import os
910
import re
1011
import sys
@@ -771,6 +772,9 @@ def check(fmt, vals, result):
771772
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
772773
check(b'%c', b'a', b'a')
773774

775+
self.assertRaisesRegex(TypeError, '%i format: a real number is required, not complex', operator.mod, '%i', 2j)
776+
self.assertRaisesRegex(TypeError, '%d format: a real number is required, not complex', operator.mod, '%d', 2j)
777+
774778
def test_imod(self):
775779
b = self.type2test(b'hello, %b!')
776780
orig = b

Lib/test/test_format.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ def test_common_format(self):
283283
"%x format: an integer is required, not str")
284284
test_exc_common('%x', 3.14, TypeError,
285285
"%x format: an integer is required, not float")
286+
test_exc_common('%i', '1', TypeError,
287+
"%i format: a real number is required, not str")
288+
test_exc_common('%i', b'1', TypeError,
289+
"%i format: a real number is required, not bytes")
286290

287291
def test_str_format(self):
288292
testformat("%r", "\u0378", "'\\u0378'") # non printable
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix error message when formatting bytes using the ``'i'`` flag.
2+
Patch by Maxim Ageev.

Objects/bytesobject.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,6 @@ static PyObject *
469469
formatlong(PyObject *v, int flags, int prec, int type)
470470
{
471471
PyObject *result, *iobj;
472-
if (type == 'i')
473-
type = 'd';
474472
if (PyLong_Check(v))
475473
return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type);
476474
if (PyNumber_Check(v)) {

0 commit comments

Comments
 (0)