Skip to content

Commit 2c14b2b

Browse files
committed
gh-162599: Warn about invalid escapes of non-ASCII characters
1 parent 5dbe2ac commit 2c14b2b

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎Lib/test/test_string_literals.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def test_eval_str_invalid_escape(self):
130130
continue
131131
with self.assertWarns(SyntaxWarning):
132132
self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
133+
for c in '\xe9\u20ac\U0001f600':
134+
for src in (r"'\%s'", r"f'\%s'", r"'\%s\z'"):
135+
with self.assertWarnsRegex(SyntaxWarning, r'"\\%s" is an invalid' % c):
136+
self.assertEqual(eval(src % c)[:2], '\\' + c)
133137

134138
with warnings.catch_warnings(record=True) as w:
135139
warnings.simplefilter('always', category=SyntaxWarning)

‎Lib/test/test_utf8source.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_pep3120(self):
88
b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
99
)
1010
self.assertEqual(
11-
"\П".encode("utf-8"),
11+
"\\П".encode("utf-8"),
1212
b'\\\xd0\x9f'
1313
)
1414

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A backslash followed by a non-ASCII character in a string literal now emits
2+
the same :exc:`SyntaxWarning` as other invalid escape sequences.

‎Parser/string_parser.c‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
//// STRING HANDLING FUNCTIONS ////
1111

1212
static int
13-
warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_invalid_escape, Token *t)
13+
warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_invalid_escape,
14+
Py_UCS4 c, Token *t)
1415
{
1516
if (p->call_invalid_rules) {
1617
// Do not report warnings if we are in the second pass of the parser
1718
// to avoid showing the warning twice.
1819
return 0;
1920
}
20-
unsigned char c = (unsigned char)*first_invalid_escape;
2121
if ((t->type == FSTRING_MIDDLE || t->type == FSTRING_END || t->type == TSTRING_MIDDLE || t->type == TSTRING_END)
2222
&& (c == '{' || c == '}')) {
2323
// in this case the tokenizer has already emitted a warning,
@@ -37,7 +37,7 @@ warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_in
3737
"\"\\%c\" is an invalid escape sequence. "
3838
"Such sequences will not work in the future. "
3939
"Did you mean \"\\\\%c\"? A raw string is also an option.",
40-
c, c);
40+
(int)c, (int)c);
4141
if (msg == NULL) {
4242
return -1;
4343
}
@@ -108,7 +108,7 @@ warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_in
108108
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, lineno, col_offset-1, lineno, col_offset+1,
109109
"\"\\%c\" is an invalid escape sequence. "
110110
"Did you mean \"\\\\%c\"? A raw string is also an option.",
111-
c, c);
111+
(int)c, (int)c);
112112
}
113113
}
114114
Py_DECREF(msg);
@@ -151,10 +151,19 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
151151
}
152152
p = buf;
153153
end = s + len;
154+
// A backslash before a non-ASCII character becomes "\u005c" below, which
155+
// the decoder does not report as an invalid escape, so remember it here.
156+
const char *nonascii_escape_ptr = NULL;
157+
Py_UCS4 nonascii_escape_char = 0;
154158
while (s < end) {
159+
int escaped_nonascii = 0;
155160
if (*s == '\\') {
156161
*p++ = *s++;
157162
if (s >= end || *s & 0x80) {
163+
if (s < end && nonascii_escape_ptr == NULL) {
164+
nonascii_escape_ptr = p;
165+
escaped_nonascii = 1;
166+
}
158167
memcpy(p, "u005c", 5);
159168
p += 5;
160169
if (s >= end) {
@@ -176,6 +185,9 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
176185
kind = PyUnicode_KIND(w);
177186
data = PyUnicode_DATA(w);
178187
w_len = PyUnicode_GET_LENGTH(w);
188+
if (escaped_nonascii) {
189+
nonascii_escape_char = PyUnicode_READ(kind, data, 0);
190+
}
179191
for (i = 0; i < w_len; i++) {
180192
// sprintf() writes a null byte: the buffer is large enough
181193
// for that thanks to the overallocation.
@@ -202,10 +214,21 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
202214
&first_invalid_escape_char,
203215
&first_invalid_escape_ptr);
204216

217+
Py_UCS4 c = 0;
218+
if (first_invalid_escape_ptr != NULL) {
219+
c = (unsigned char)*first_invalid_escape_ptr;
220+
}
221+
if (nonascii_escape_ptr != NULL &&
222+
(first_invalid_escape_ptr == NULL ||
223+
nonascii_escape_ptr < first_invalid_escape_ptr)) {
224+
first_invalid_escape_ptr = nonascii_escape_ptr;
225+
c = nonascii_escape_char;
226+
}
227+
205228
// HACK: later we can simply pass the line no, since we don't preserve the tokens
206229
// when we are decoding the string but we preserve the line numbers.
207230
if (v != NULL && first_invalid_escape_ptr != NULL && t != NULL && !incomplete) {
208-
if (warn_invalid_escape_sequence(parser, buf, first_invalid_escape_ptr, t) < 0) {
231+
if (warn_invalid_escape_sequence(parser, buf, first_invalid_escape_ptr, c, t) < 0) {
209232
/* We have not deallocated the buffer before because
210233
* first_invalid_escape_ptr points inside buf. */
211234
PyMem_Free(buf);
@@ -230,7 +253,8 @@ decode_bytes_with_escapes(Parser *p, const char *s, Py_ssize_t len, Token *t)
230253
}
231254

232255
if (first_invalid_escape_ptr != NULL) {
233-
if (warn_invalid_escape_sequence(p, s, first_invalid_escape_ptr, t) < 0) {
256+
if (warn_invalid_escape_sequence(p, s, first_invalid_escape_ptr,
257+
(unsigned char)*first_invalid_escape_ptr, t) < 0) {
234258
Py_DECREF(result);
235259
return NULL;
236260
}

0 commit comments

Comments
 (0)