Skip to content

Commit 81f4e11

Browse files
authored
bpo-45811: Improve error message when source code contains invisible control characters (GH-29654)
1 parent 7a1d932 commit 81f4e11

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_syntax.py

+3
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,9 @@ def test_error_parenthesis(self):
15661566
for paren in ")]}":
15671567
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
15681568

1569+
def test_invisible_characters(self):
1570+
self._check_error('print\x17("Hello")', "invalid non-printable character")
1571+
15691572
def test_match_call_does_not_raise_syntax_error(self):
15701573
code = """
15711574
def match(x):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the tokenizer errors when encountering invisible control characters
2+
in the parser. Patch by Pablo Galindo

Parser/tokenizer.c

+6
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,12 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
20452045
break;
20462046
}
20472047

2048+
if (!Py_UNICODE_ISPRINTABLE(c)) {
2049+
char hex[9];
2050+
(void)PyOS_snprintf(hex, sizeof(hex), "%04X", c);
2051+
return syntaxerror(tok, "invalid non-printable character U+%s", hex);
2052+
}
2053+
20482054
/* Punctuation character */
20492055
*p_start = tok->start;
20502056
*p_end = tok->cur;

0 commit comments

Comments
 (0)