diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index f6ffe83c5d69e8..42e7427725d2d7 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 @@ -751,6 +752,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/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 b3d1c425ad18b7..fb735adf869534 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -468,8 +468,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)) {