]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-28146: Fix a confusing error message in str.format() (GH-24213)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 13 May 2021 21:35:30 +0000 (14:35 -0700)
committerGitHub <noreply@github.com>
Thu, 13 May 2021 21:35:30 +0000 (14:35 -0700)
Automerge-Triggered-By: GH:pitrou
(cherry picked from commit 4aeee0b47b3a2b604bbac37040320ffc88c291f2)

Co-authored-by: Irit Katriel <iritkatriel@yahoo.com>
Lib/test/test_unicode.py
Misc/NEWS.d/next/Core and Builtins/2021-01-13-19-34-41.bpo-28146.AZBBkH.rst [new file with mode: 0644]
Python/formatter_unicode.c

index 23508c56e7ffef2715c0dfac17bb9ebc8a398b6d..787f841573876d436e16952f548f2dbae57eadd8 100644 (file)
@@ -1217,8 +1217,11 @@ class UnicodeTest(string_tests.CommonTest,
                           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 (file)
index 0000000..e619881
--- /dev/null
@@ -0,0 +1 @@
+Fix a confusing error message in :func:`str.format`.
index ed95f267d476c7bab41e74d81a34afb5fc96b395..e7ec4dd5cb4d2e99d1e1b45b3b6604b02e962e9f 100644 (file)
@@ -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;
     }