From 712aad4f2db68e8f2de833662b6534c2b139a32d Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 13 Jan 2021 19:39:47 +0000 Subject: [PATCH 1/2] bpo-28146: Fix a confusing error message in str.format() --- Lib/test/test_unicode.py | 7 +++++-- .../2021-01-13-19-34-41.bpo-28146.AZBBkH.rst | 1 + Python/formatter_unicode.c | 10 ++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 4f5636e1426f4d..af30478d3e510c 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -1225,8 +1225,11 @@ def __repr__(self): 0, 1, 2, 3, 4, 5, 6, 7) # string format spec errors - self.assertRaises(ValueError, "{0:-s}".format, '') - self.assertRaises(ValueError, format, "", "-") + sign_msg = "Sign not allowed in string format specifier" + self.assertRaisesRegex(ValueError, sign_msg, "{0:-s}".format, '') + self.assertRaisesRegex(ValueError, sign_msg, format, "", "-") + space_msg = "Space not allowed in string format specifier" + self.assertRaisesRegex(ValueError, space_msg, "{: }".format, '') self.assertRaises(ValueError, "{0:=s}".format, '') # Alternate formatting is not supported diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst new file mode 100644 index 00000000000000..853205995f9540 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst @@ -0,0 +1 @@ +Fixed a confusing error message in :func:`str.format`. diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index ed95f267d476c7..e7ec4dd5cb4d2e 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -773,8 +773,14 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format, /* sign is not allowed on strings */ if (format->sign != '\0') { - PyErr_SetString(PyExc_ValueError, - "Sign not allowed in string format specifier"); + if (format->sign == ' ') { + PyErr_SetString(PyExc_ValueError, + "Space not allowed in string format specifier"); + } + else { + PyErr_SetString(PyExc_ValueError, + "Sign not allowed in string format specifier"); + } goto done; } From 7d1e537ddac3cd8eedb39501d3a3143a253bea78 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 13 May 2021 22:36:01 +0200 Subject: [PATCH 2/2] Fix tense in NEWS --- .../Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst index 853205995f9540..e6198819389532 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst @@ -1 +1 @@ -Fixed a confusing error message in :func:`str.format`. +Fix a confusing error message in :func:`str.format`.