Skip to content

Commit c0b64eb

Browse files
committed
[3.11] Raise null bytes syntax error upon null in multiline string
1 parent e6d28ba commit c0b64eb

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,8 +1998,12 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
19981998
/* Get rest of string */
19991999
while (end_quote_size != quote_size) {
20002000
c = tok_nextc(tok);
2001-
if (tok->done == E_DECODE)
2001+
if (tok->done == E_ERROR) {
2002+
return ERRORTOKEN;
2003+
}
2004+
if (tok->done == E_DECODE) {
20022005
break;
2006+
}
20032007
if (c == EOF || (quote_size == 1 && c == '\n')) {
20042008
assert(tok->multi_line_start != NULL);
20052009
// shift the tok_state's location into

0 commit comments

Comments
 (0)