Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a confusing error message in :func:`str.format`.
10 changes: 8 additions & 2 deletions Python/formatter_unicode.c
Original file line number Diff line number Diff line change
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