Skip to content
6 changes: 4 additions & 2 deletions Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ def test_common_format(self):
"unsupported format %z at position 4")
test_exc_common("abc %Id", 1, ValueError,
"unsupported format %I at position 4")
test_exc_common("abc %'d", 1, ValueError,
"stray % at position 4 or unexpected format character ''' at position 5")
test_exc_common("abc %1 d", 1, ValueError,
"stray % at position 4 or unexpected format character ' ' at position 6")
test_exc_common('abc % (x)r', {}, ValueError,
Expand Down Expand Up @@ -363,6 +361,8 @@ def test_str_format(self):
print('Testing exceptions')
test_exc('abc %b', 1, ValueError,
"unsupported format %b at position 4")
test_exc("abc %'d", 1, ValueError,
"stray % at position 4 or unexpected format character ''' (U+0027) at position 5")
test_exc("abc %\nd", 1, ValueError,
"stray % at position 4 or unexpected format character U+000A at position 5")
test_exc("abc %\x1fd", 1, ValueError,
Expand Down Expand Up @@ -465,6 +465,8 @@ def __bytes__(self):
print('Testing exceptions')
test_exc(b"abc %\nd", 1, ValueError,
"stray % at position 4 or unexpected format character with code 0x0a at position 5")
test_exc(b"abc %'d", 1, ValueError,
"stray % at position 4 or unexpected format character with code 0x27 at position 5")
test_exc(b"abc %\x1fd", 1, ValueError,
"stray % at position 4 or unexpected format character with code 0x1f at position 5")
test_exc(b"abc %\x7fd", 1, ValueError,
Expand Down
2 changes: 1 addition & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
"unsupported format %%%c at position %zd",
c, (Py_ssize_t)(fmtstart - format - 1));
}
else if (c >= 32 && c < 127) {
else if (c >= 32 && c < 127 && c != '\'') {
PyErr_Format(PyExc_ValueError,
"stray %% at position %zd or unexpected "
"format character '%c' "
Expand Down
2 changes: 1 addition & 1 deletion Objects/unicode_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ unicode_format_arg_format(struct unicode_formatter_t *ctx,
"unsupported format %%%c at position %zd",
(int)arg->ch, arg->fmtstart);
}
else if (arg->ch >= 32 && arg->ch < 127) {
else if (arg->ch >= 32 && arg->ch < 127 && arg->ch != '\'') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ignore also the quote character in Py_UNICODE_ISPRINTABLE() test below?

PyErr_Format(PyExc_ValueError,
"stray %% at position %zd or unexpected "
"format character '%c' at position %zd",
Expand Down
Loading