summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_ast.py4
-rw-r--r--Lib/test/test_builtin.py3
-rw-r--r--Lib/test/test_cmd_line_script.py12
-rw-r--r--Lib/test/test_compile.py8
4 files changed, 21 insertions, 6 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index c97d161..9a7df28 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -844,6 +844,10 @@ class AST_Tests(unittest.TestCase):
check_limit("a", "[0]")
check_limit("a", "*a")
+ def test_null_bytes(self):
+ with self.assertRaises(SyntaxError,
+ msg="source code string cannot contain null bytes"):
+ ast.parse("a\0b")
class ASTHelpers_Test(unittest.TestCase):
maxDiff = None
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 8c9c1e5..31e5063 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -334,11 +334,10 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, compile)
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff)
- self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
mode='eval', source='0', filename='tmp')
compile('print("\xe5")\n', '', 'exec')
- self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
+ self.assertRaises(SyntaxError, compile, chr(0), 'f', 'exec')
self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
# test the optimize argument
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 9e98edf..1ee3acd 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -657,6 +657,18 @@ 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_consistent_sys_path_for_direct_execution(self):
# This test case ensures that the following all give the same
# sys.path configuration:
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 4b35cce..7c55c71 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -544,7 +544,7 @@ if 1:
with open(fn, "wb") as fp:
fp.write(src)
res = script_helper.run_python_until_end(fn)[0]
- self.assertIn(b"Non-UTF-8", res.err)
+ self.assertIn(b"source code cannot contain null bytes", res.err)
def test_yet_more_evil_still_undecodable(self):
# Issue #25388
@@ -554,7 +554,7 @@ if 1:
with open(fn, "wb") as fp:
fp.write(src)
res = script_helper.run_python_until_end(fn)[0]
- self.assertIn(b"Non-UTF-8", res.err)
+ self.assertIn(b"source code cannot contain null bytes", res.err)
@support.cpython_only
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
@@ -591,9 +591,9 @@ if 1:
def test_null_terminated(self):
# The source code is null-terminated internally, but bytes-like
# objects are accepted, which could be not terminated.
- with self.assertRaisesRegex(ValueError, "cannot contain null"):
+ with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
compile("123\x00", "<dummy>", "eval")
- with self.assertRaisesRegex(ValueError, "cannot contain null"):
+ with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
compile(memoryview(b"123\x00"), "<dummy>", "eval")
code = compile(memoryview(b"123\x00")[1:-1], "<dummy>", "eval")
self.assertEqual(eval(code), 23)