Skip to content

Commit 4aeee0b

Browse files
authored
bpo-28146: Fix a confusing error message in str.format() (pythonGH-24213)
Automerge-Triggered-By: GH:pitrou
1 parent ae3c66a commit 4aeee0b

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Lib/test/test_unicode.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1231,8 +1231,11 @@ def __repr__(self):
12311231
0, 1, 2, 3, 4, 5, 6, 7)
12321232

12331233
# string format spec errors
1234-
self.assertRaises(ValueError, "{0:-s}".format, '')
1235-
self.assertRaises(ValueError, format, "", "-")
1234+
sign_msg = "Sign not allowed in string format specifier"
1235+
self.assertRaisesRegex(ValueError, sign_msg, "{0:-s}".format, '')
1236+
self.assertRaisesRegex(ValueError, sign_msg, format, "", "-")
1237+
space_msg = "Space not allowed in string format specifier"
1238+
self.assertRaisesRegex(ValueError, space_msg, "{: }".format, '')
12361239
self.assertRaises(ValueError, "{0:=s}".format, '')
12371240

12381241
# Alternate formatting is not supported
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a confusing error message in :func:`str.format`.

Python/formatter_unicode.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,14 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format,
773773

774774
/* sign is not allowed on strings */
775775
if (format->sign != '\0') {
776-
PyErr_SetString(PyExc_ValueError,
777-
"Sign not allowed in string format specifier");
776+
if (format->sign == ' ') {
777+
PyErr_SetString(PyExc_ValueError,
778+
"Space not allowed in string format specifier");
779+
}
780+
else {
781+
PyErr_SetString(PyExc_ValueError,
782+
"Sign not allowed in string format specifier");
783+
}
778784
goto done;
779785
}
780786

0 commit comments

Comments
 (0)