Skip to content

Commit

Permalink
bpo-28146: Fix a confusing error message in str.format() (GH-24213)
Browse files Browse the repository at this point in the history
Automerge-Triggered-By: GH:pitrou
  • Loading branch information
iritkatriel committed May 13, 2021
1 parent ae3c66a commit 4aeee0b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Lib/test/test_unicode.py
Expand Up @@ -1231,8 +1231,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
Expand Down
@@ -0,0 +1 @@
Fix a confusing error message in :func:`str.format`.
10 changes: 8 additions & 2 deletions Python/formatter_unicode.c
Expand Up @@ -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;
}

Expand Down

0 comments on commit 4aeee0b

Please sign in to comment.