diff options
author | Meador Inge <meadori@gmail.com> | 2012-01-19 07:08:41 (GMT) |
---|---|---|
committer | Meador Inge <meadori@gmail.com> | 2012-01-19 07:08:41 (GMT) |
commit | fa21bf015d4e9cb0c010fde28db0c344174634d1 (patch) | |
tree | 449cbadc11a8420aa3cb8db9678dded1d9176599 /Lib/test/test_compile.py | |
parent | 00c7f85298b9803371b4a0019ce8732ed8a2dd3b (diff) | |
download | cpython-fa21bf015d4e9cb0c010fde28db0c344174634d1.zip cpython-fa21bf015d4e9cb0c010fde28db0c344174634d1.tar.gz cpython-fa21bf015d4e9cb0c010fde28db0c344174634d1.tar.bz2 |
Issue #12705: Raise SyntaxError when compiling multiple statements as single interactive statement
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r-- | Lib/test/test_compile.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index c5f9189..7089872 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -6,6 +6,12 @@ from test import support class TestSpecifics(unittest.TestCase): + def compile_single(self, source): + compile(source, "<single>", "single") + + def assertInvalidSingle(self, source): + self.assertRaises(SyntaxError, self.compile_single, source) + def test_no_ending_newline(self): compile("hi", "<test>", "exec") compile("hi\r", "<test>", "exec") @@ -442,6 +448,28 @@ if 1: if isinstance(obj, types.CodeType): self.assertIs(obj.co_filename, c.co_filename) + def test_single_statement(self): + self.compile_single("1 + 2") + self.compile_single("\n1 + 2") + self.compile_single("1 + 2\n") + self.compile_single("1 + 2\n\n") + self.compile_single("1 + 2\t\t\n") + self.compile_single("1 + 2\t\t\n ") + self.compile_single("1 + 2 # one plus two") + self.compile_single("1; 2") + self.compile_single("import sys; sys") + self.compile_single("def f():\n pass") + self.compile_single("while False:\n pass") + self.compile_single("if x:\n f(x)") + self.compile_single("if x:\n f(x)\nelse:\n g(x)") + self.compile_single("class T:\n pass") + + def test_bad_single_statement(self): + self.assertInvalidSingle('1\n2') + self.assertInvalidSingle('def f(): pass') + self.assertInvalidSingle('a = 13\nb = 187') + self.assertInvalidSingle('del x\ndel y') + self.assertInvalidSingle('f()\ng()') def test_main(): support.run_unittest(TestSpecifics) |