Skip to content

Commit c52feb2

Browse files
Improve error message for '.
1 parent 8d9d009 commit c52feb2

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

Lib/test/test_format.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ def test_common_format(self):
278278
"unsupported format %z at position 4")
279279
test_exc_common("abc %Id", 1, ValueError,
280280
"unsupported format %I at position 4")
281-
test_exc_common("abc %'d", 1, ValueError,
282-
"stray % at position 4 or unexpected format character ''' at position 5")
283281
test_exc_common("abc %1 d", 1, ValueError,
284282
"stray % at position 4 or unexpected format character ' ' at position 6")
285283
test_exc_common('abc % (x)r', {}, ValueError,
@@ -363,6 +361,8 @@ def test_str_format(self):
363361
print('Testing exceptions')
364362
test_exc('abc %b', 1, ValueError,
365363
"unsupported format %b at position 4")
364+
test_exc("abc %'d", 1, ValueError,
365+
"stray % at position 4 or unexpected format character ''' (U+0027) at position 5")
366366
test_exc("abc %\nd", 1, ValueError,
367367
"stray % at position 4 or unexpected format character U+000A at position 5")
368368
test_exc("abc %\x1fd", 1, ValueError,
@@ -465,6 +465,8 @@ def __bytes__(self):
465465
print('Testing exceptions')
466466
test_exc(b"abc %\nd", 1, ValueError,
467467
"stray % at position 4 or unexpected format character with code 0x0a at position 5")
468+
test_exc(b"abc %'d", 1, ValueError,
469+
"stray % at position 4 or unexpected format character with code 0x27 at position 5")
468470
test_exc(b"abc %\x1fd", 1, ValueError,
469471
"stray % at position 4 or unexpected format character with code 0x1f at position 5")
470472
test_exc(b"abc %\x7fd", 1, ValueError,

Objects/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10121012
"unsupported format %%%c at position %zd",
10131013
c, (Py_ssize_t)(fmtstart - format - 1));
10141014
}
1015-
else if (c >= 32 && c < 127) {
1015+
else if (c >= 32 && c < 127 && c != '\'') {
10161016
PyErr_Format(PyExc_ValueError,
10171017
"stray %% at position %zd or unexpected "
10181018
"format character '%c' "

Objects/unicode_format.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ unicode_format_arg_format(struct unicode_formatter_t *ctx,
768768
"unsupported format %%%c at position %zd",
769769
(int)arg->ch, arg->fmtstart);
770770
}
771-
else if (arg->ch >= 32 && arg->ch < 127) {
771+
else if (arg->ch >= 32 && arg->ch < 127 && arg->ch != '\'') {
772772
PyErr_Format(PyExc_ValueError,
773773
"stray %% at position %zd or unexpected "
774774
"format character '%c' at position %zd",

0 commit comments

Comments
 (0)