Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-96611: Fix error message for invalid UTF-8 in mid-multiline string #96623

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_source_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ def test_error_from_string(self):
self.assertTrue(c.exception.args[0].startswith(expected),
msg=c.exception.args[0])

def test_file_parse_error_multiline(self):
# gh96611:
with open(TESTFN, "wb") as fd:
fd.write(b'print("""\n\xb1""")\n')

try:
retcode, stdout, stderr = script_helper.assert_python_failure(TESTFN)

self.assertGreater(retcode, 0)
self.assertIn(b"Non-UTF-8 code starting with '\\xb1'", stderr)
finally:
os.unlink(TESTFN)

class AbstractSourceEncodingTest:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When loading a file with invalid UTF-8 inside a multi-line string, a correct
SyntaxError is emitted.
2 changes: 2 additions & 0 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,8 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
/* Get rest of string */
while (end_quote_size != quote_size) {
c = tok_nextc(tok);
if (tok->done == E_DECODE)
break;
if (c == EOF || (quote_size == 1 && c == '\n')) {
assert(tok->multi_line_start != NULL);
// shift the tok_state's location into
Expand Down