mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
Issue #12705: Raise SyntaxError when compiling multiple statements as single interactive statement
This commit is contained in:
parent
00c7f85298
commit
fa21bf015d
5 changed files with 52 additions and 0 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue