Skip to content

Commit ef0df52

Browse files
authored
gh-97556: Raise null bytes syntax error upon null in multiline string (GH-104136)
1 parent 55d50d1 commit ef0df52

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Lib/test/test_cmd_line_script.py

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

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

Parser/tokenizer.c

Lines changed: 8 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
@@ -2554,6 +2558,9 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
25542558

25552559
while (end_quote_size != current_tok->f_string_quote_size) {
25562560
int c = tok_nextc(tok);
2561+
if (tok->done == E_ERROR) {
2562+
return MAKE_TOKEN(ERRORTOKEN);
2563+
}
25572564
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
25582565
if (tok->decoding_erred) {
25592566
return MAKE_TOKEN(ERRORTOKEN);

0 commit comments

Comments
 (0)