gh-130928: Fix error message during bytes formatting for the 'i' flag (#130967)

This commit is contained in:
Ageev Maxim 2025-03-24 22:07:03 +03:00 committed by GitHub
parent 929afd1d6e
commit 7c3692fe27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 10 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,2 @@
Fix error message when formatting bytes using the ``'i'`` flag.
Patch by Maxim Ageev.

View 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)) {