Skip to content

Commit fa7a709

Browse files
committed
pythongh-97556: Raise null bytes syntax error upon null in multiline string
1 parent 524a7f7 commit fa7a709

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎Lib/test/test_cmd_line_script.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,18 @@ def test_syntaxerror_null_bytes(self):
669669
],
670670
)
671671

672+
def test_syntaxerror_null_bytes_in_multiline_string(self):
673+
script = "\n'''\nmultilinestring\0\n'''"
674+
with os_helper.temp_dir() as script_dir:
675+
script_name = _make_test_script(script_dir, 'script', script)
676+
_, _, stderr = assert_python_failure(script_name)
677+
self.assertEqual(
678+
stderr.splitlines()[-2:],
679+
[ b" multilinestring",
680+
b'SyntaxError: source code cannot contain null bytes'
681+
]
682+
)
683+
672684
def test_consistent_sys_path_for_direct_execution(self):
673685
# This test case ensures that the following all give the same
674686
# sys.path configuration:

‎Parser/tokenizer.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2301,8 +2301,12 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
23012301
/* Get rest of string */
23022302
while (end_quote_size != quote_size) {
23032303
c = tok_nextc(tok);
2304-
if (tok->done == E_DECODE)
2304+
if (tok->done == E_ERROR) {
2305+
return MAKE_TOKEN(ERRORTOKEN);
2306+
}
2307+
if (tok->done == E_DECODE) {
23052308
break;
2309+
}
23062310
if (c == EOF || (quote_size == 1 && c == '\n')) {
23072311
assert(tok->multi_line_start != NULL);
23082312
// shift the tok_state's location into

0 commit comments

Comments
 (0)