bpo-28146: Fix a confusing error message in str.format() (GH-24213)

Automerge-Triggered-By: GH:pitrou
This commit is contained in:
Irit Katriel 2021-05-13 21:55:55 +01:00 committed by GitHub
parent ae3c66acb8
commit 4aeee0b47b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View File

@ -1231,8 +1231,11 @@ class UnicodeTest(string_tests.CommonTest,
0, 1, 2, 3, 4, 5, 6, 7) 0, 1, 2, 3, 4, 5, 6, 7)
# string format spec errors # string format spec errors
self.assertRaises(ValueError, "{0:-s}".format, '') sign_msg = "Sign not allowed in string format specifier"
self.assertRaises(ValueError, format, "", "-") 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, '') self.assertRaises(ValueError, "{0:=s}".format, '')
# Alternate formatting is not supported # Alternate formatting is not supported

View File

@ -0,0 +1 @@
Fix a confusing error message in :func:`str.format`.

View File

@ -773,8 +773,14 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format,
/* sign is not allowed on strings */ /* sign is not allowed on strings */
if (format->sign != '\0') { if (format->sign != '\0') {
if (format->sign == ' ') {
PyErr_SetString(PyExc_ValueError,
"Space not allowed in string format specifier");
}
else {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Sign not allowed in string format specifier"); "Sign not allowed in string format specifier");
}
goto done; goto done;
} }