Skip to content

Commit bb0dab5

Browse files
gh-96611: Fix error message for invalid UTF-8 in mid-multiline string (GH-96623)
(cherry picked from commit 05692c6) Co-authored-by: Michael Droettboom <[email protected]>
1 parent a389fdb commit bb0dab5

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/test/test_source_encoding.py

+12
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ def test_error_from_string(self):
148148
self.assertTrue(c.exception.args[0].startswith(expected),
149149
msg=c.exception.args[0])
150150

151+
def test_file_parse_error_multiline(self):
152+
# gh96611:
153+
with open(TESTFN, "wb") as fd:
154+
fd.write(b'print("""\n\xb1""")\n')
155+
156+
try:
157+
retcode, stdout, stderr = script_helper.assert_python_failure(TESTFN)
158+
159+
self.assertGreater(retcode, 0)
160+
self.assertIn(b"Non-UTF-8 code starting with '\\xb1'", stderr)
161+
finally:
162+
os.unlink(TESTFN)
151163

152164
class AbstractSourceEncodingTest:
153165

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When loading a file with invalid UTF-8 inside a multi-line string, a correct
2+
SyntaxError is emitted.

Parser/tokenizer.c

+2
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,8 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
19451945
/* Get rest of string */
19461946
while (end_quote_size != quote_size) {
19471947
c = tok_nextc(tok);
1948+
if (tok->done == E_DECODE)
1949+
break;
19481950
if (c == EOF || (quote_size == 1 && c == '\n')) {
19491951
assert(tok->multi_line_start != NULL);
19501952
// shift the tok_state's location into

0 commit comments

Comments
 (0)