mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
[3.11] gh-96670: Raise SyntaxError when parsing NULL bytes (GH-97594) (#104195)
This commit is contained in:
parent
c5dafeaa6d
commit
a09d3901a5
9 changed files with 77 additions and 22 deletions
|
@ -657,6 +657,31 @@ class CmdLineTest(unittest.TestCase):
|
|||
],
|
||||
)
|
||||
|
||||
def test_syntaxerror_null_bytes(self):
|
||||
script = "x = '\0' nothing to see here\n';import os;os.system('echo pwnd')\n"
|
||||
with os_helper.temp_dir() as script_dir:
|
||||
script_name = _make_test_script(script_dir, 'script', script)
|
||||
exitcode, stdout, stderr = assert_python_failure(script_name)
|
||||
self.assertEqual(
|
||||
stderr.splitlines()[-2:],
|
||||
[ b" x = '",
|
||||
b'SyntaxError: source code cannot contain null bytes'
|
||||
],
|
||||
)
|
||||
|
||||
def test_syntaxerror_null_bytes_in_multiline_string(self):
|
||||
scripts = ["\n'''\nmultilinestring\0\n'''", "\nf'''\nmultilinestring\0\n'''"] # Both normal and f-strings
|
||||
with os_helper.temp_dir() as script_dir:
|
||||
for script in scripts:
|
||||
script_name = _make_test_script(script_dir, 'script', script)
|
||||
_, _, stderr = assert_python_failure(script_name)
|
||||
self.assertEqual(
|
||||
stderr.splitlines()[-2:],
|
||||
[ b" multilinestring",
|
||||
b'SyntaxError: source code cannot contain null bytes'
|
||||
]
|
||||
)
|
||||
|
||||
def test_consistent_sys_path_for_direct_execution(self):
|
||||
# This test case ensures that the following all give the same
|
||||
# sys.path configuration:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue