diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-11-07 02:56:11 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-11-07 02:56:11 (GMT) |
commit | 61d6e4ae9db80e3f87104a03499ff89d3c275b22 (patch) | |
tree | 43160e9ff162635bb549ad31b7be875935cf0b86 /Lib/test/test_compile.py | |
parent | 9b566c324d2eb3ddbf00d5d7a78bea63cde4d15f (diff) | |
parent | eeb896c4116dd763efea45cb3c1b53257128f4e4 (diff) | |
download | cpython-61d6e4ae9db80e3f87104a03499ff89d3c275b22.zip cpython-61d6e4ae9db80e3f87104a03499ff89d3c275b22.tar.gz cpython-61d6e4ae9db80e3f87104a03499ff89d3c275b22.tar.bz2 |
Issue #24802: Merge null termination fixes from 3.4 into 3.5
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r-- | Lib/test/test_compile.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index db821be..13bb3a2 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -542,6 +542,27 @@ if 1: check_limit("a", "[0]") check_limit("a", "*a") + def test_null_terminated(self): + # The source code is null-terminated internally, but bytes-like + # objects are accepted, which could be not terminated. + # Exception changed from TypeError to ValueError in 3.5 + with self.assertRaisesRegex(Exception, "cannot contain null"): + compile("123\x00", "<dummy>", "eval") + with self.assertRaisesRegex(Exception, "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) + code = compile(memoryview(b"1234")[1:-1], "<dummy>", "eval") + self.assertEqual(eval(code), 23) + code = compile(memoryview(b"$23$")[1:-1], "<dummy>", "eval") + self.assertEqual(eval(code), 23) + + # Also test when eval() and exec() do the compilation step + self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23) + namespace = dict() + exec(memoryview(b"ax = 123")[1:-1], namespace) + self.assertEqual(namespace['x'], 12) + class TestStackSize(unittest.TestCase): # These tests check that the computed stack size for a code object |