From 7c3692fe275088e986f92cec34dcccb823b31fa2 Mon Sep 17 00:00:00 2001 From: Ageev Maxim Date: Mon, 24 Mar 2025 22:07:03 +0300 Subject: gh-130928: Fix error message during bytes formatting for the `'i'` flag (#130967) --- Lib/test/test_bytes.py | 4 ++++ Lib/test/test_format.py | 4 ++++ .../Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst | 2 ++ Objects/bytesobject.c | 2 -- 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-03-09-09-03-24.gh-issue-130928.gP1yKv.rst diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 115899d..4448676 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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 diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py index 3916bc3..c7cc32e 100644 --- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -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 index 0000000..f9f144a --- /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 31ba89f..fc407ec 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)) { -- cgit v0.12