2001-07-30 09:30:08 -03:00
|
|
|
"""
|
|
|
|
Test cases for codeop.py
|
|
|
|
Nick Mathewson
|
|
|
|
"""
|
|
|
|
import unittest
|
2020-08-12 09:53:28 -03:00
|
|
|
import warnings
|
2020-07-09 10:25:10 -03:00
|
|
|
from test.support import warnings_helper
|
2023-10-30 16:24:21 -03:00
|
|
|
from textwrap import dedent
|
2001-07-30 09:30:08 -03:00
|
|
|
|
2003-02-13 18:07:59 -04:00
|
|
|
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
|
2003-05-16 23:39:52 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
class CodeopTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def assertValid(self, str, symbol='single'):
|
|
|
|
'''succeed iff str is a valid piece of code'''
|
2022-12-23 16:17:24 -04:00
|
|
|
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
|
|
|
|
self.assertEqual(compile_command(str, "<input>", symbol), expected)
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
def assertIncomplete(self, str, symbol='single'):
|
|
|
|
'''succeed iff str is the start of a valid piece of code'''
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(compile_command(str, symbol=symbol), None)
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
def assertInvalid(self, str, symbol='single', is_syntax=1):
|
|
|
|
'''succeed iff str is the start of an invalid piece of code'''
|
|
|
|
try:
|
|
|
|
compile_command(str,symbol=symbol)
|
2012-12-18 15:14:22 -04:00
|
|
|
self.fail("No exception raised for invalid code")
|
2001-07-30 09:30:08 -03:00
|
|
|
except SyntaxError:
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(is_syntax)
|
2001-07-30 09:30:08 -03:00
|
|
|
except OverflowError:
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(not is_syntax)
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
av = self.assertValid
|
2003-05-16 23:39:52 -03:00
|
|
|
|
|
|
|
# special case
|
2022-12-23 16:17:24 -04:00
|
|
|
self.assertEqual(compile_command(""),
|
|
|
|
compile("pass", "<input>", 'single',
|
|
|
|
PyCF_DONT_IMPLY_DEDENT))
|
|
|
|
self.assertEqual(compile_command("\n"),
|
|
|
|
compile("pass", "<input>", 'single',
|
|
|
|
PyCF_DONT_IMPLY_DEDENT))
|
2003-06-15 20:26:30 -03:00
|
|
|
|
2003-05-16 23:39:52 -03:00
|
|
|
av("a = 1")
|
|
|
|
av("\na = 1")
|
2001-07-30 09:30:08 -03:00
|
|
|
av("a = 1\n")
|
2003-05-16 23:39:52 -03:00
|
|
|
av("a = 1\n\n")
|
|
|
|
av("\n\na = 1\n\n")
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
av("def x():\n pass\n")
|
2003-05-16 23:39:52 -03:00
|
|
|
av("if 1:\n pass\n")
|
|
|
|
|
|
|
|
av("\n\nif 1: pass\n")
|
|
|
|
av("\n\nif 1: pass\n\n")
|
|
|
|
|
|
|
|
av("def x():\n\n pass\n")
|
|
|
|
av("def x():\n pass\n \n")
|
|
|
|
av("def x():\n pass\n \n")
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
av("pass\n")
|
|
|
|
av("3**3\n")
|
2003-05-16 23:39:52 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
av("if 9==3:\n pass\nelse:\n pass\n")
|
2003-05-16 23:39:52 -03:00
|
|
|
av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
av("#a\n#b\na = 3\n")
|
|
|
|
av("#a\n\n \na=3\n")
|
|
|
|
av("a=3\n\n")
|
2003-05-16 23:39:52 -03:00
|
|
|
av("a = 9+ \\\n3")
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
av("3**3","eval")
|
|
|
|
av("(lambda z: \n z**3)","eval")
|
2003-05-16 23:39:52 -03:00
|
|
|
|
|
|
|
av("9+ \\\n3","eval")
|
|
|
|
av("9+ \\\n3\n","eval")
|
|
|
|
|
|
|
|
av("\n\na**3","eval")
|
|
|
|
av("\n \na**3","eval")
|
2001-07-30 09:30:08 -03:00
|
|
|
av("#a\n#b\na**3","eval")
|
2001-08-09 18:40:30 -03:00
|
|
|
|
2009-08-02 19:19:05 -03:00
|
|
|
av("\n\na = 1\n\n")
|
|
|
|
av("\n\nif 1: a=1\n\n")
|
|
|
|
|
|
|
|
av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
|
|
|
|
av("#a\n\n \na=3\n\n")
|
|
|
|
|
|
|
|
av("\n\na**3","eval")
|
|
|
|
av("\n \na**3","eval")
|
|
|
|
av("#a\n#b\na**3","eval")
|
|
|
|
|
|
|
|
av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
|
|
|
|
av("def f():\n pass\n#foo\n")
|
|
|
|
av("@a.b.c\ndef f():\n pass\n")
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
def test_incomplete(self):
|
|
|
|
ai = self.assertIncomplete
|
2003-05-16 23:39:52 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("(a **")
|
|
|
|
ai("(a,b,")
|
|
|
|
ai("(a,b,(")
|
|
|
|
ai("(a,b,(")
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("a = (")
|
|
|
|
ai("a = {")
|
|
|
|
ai("b + {")
|
|
|
|
|
2021-02-09 16:07:38 -04:00
|
|
|
ai("print([1,\n2,")
|
|
|
|
ai("print({1:1,\n2:3,")
|
|
|
|
ai("print((1,\n2,")
|
|
|
|
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("if 9==3:\n pass\nelse:")
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("if 9==3:\n pass\nelse:\n")
|
|
|
|
ai("if 9==3:\n pass\nelse:\n pass")
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("if 1:")
|
|
|
|
ai("if 1:\n")
|
|
|
|
ai("if 1:\n pass\n if 1:\n pass\n else:")
|
2003-06-15 20:26:30 -03:00
|
|
|
ai("if 1:\n pass\n if 1:\n pass\n else:\n")
|
|
|
|
ai("if 1:\n pass\n if 1:\n pass\n else:\n pass")
|
|
|
|
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("def x():")
|
|
|
|
ai("def x():\n")
|
|
|
|
ai("def x():\n\n")
|
|
|
|
|
|
|
|
ai("def x():\n pass")
|
|
|
|
ai("def x():\n pass\n ")
|
|
|
|
ai("def x():\n pass\n ")
|
|
|
|
ai("\n\ndef x():\n pass")
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("a = 9+ \\")
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("a = 'a\\")
|
|
|
|
ai("a = '''xy")
|
2001-08-09 18:40:30 -03:00
|
|
|
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("","eval")
|
|
|
|
ai("\n","eval")
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("(","eval")
|
|
|
|
ai("(9+","eval")
|
|
|
|
ai("9+ \\","eval")
|
|
|
|
ai("lambda z: \\","eval")
|
|
|
|
|
2009-08-02 19:19:05 -03:00
|
|
|
ai("if True:\n if True:\n if True: \n")
|
|
|
|
|
|
|
|
ai("@a(")
|
|
|
|
ai("@a(b")
|
|
|
|
ai("@a(b,")
|
|
|
|
ai("@a(b,c")
|
|
|
|
ai("@a(b,c,")
|
|
|
|
|
|
|
|
ai("from a import (")
|
|
|
|
ai("from a import (b")
|
|
|
|
ai("from a import (b,")
|
|
|
|
ai("from a import (b,c")
|
|
|
|
ai("from a import (b,c,")
|
|
|
|
|
2021-06-23 06:01:06 -03:00
|
|
|
ai("[")
|
|
|
|
ai("[a")
|
|
|
|
ai("[a,")
|
|
|
|
ai("[a,b")
|
|
|
|
ai("[a,b,")
|
|
|
|
|
|
|
|
ai("{")
|
|
|
|
ai("{a")
|
|
|
|
ai("{a:")
|
|
|
|
ai("{a:b")
|
|
|
|
ai("{a:b,")
|
|
|
|
ai("{a:b,c")
|
|
|
|
ai("{a:b,c:")
|
|
|
|
ai("{a:b,c:d")
|
|
|
|
ai("{a:b,c:d,")
|
2009-08-02 19:19:05 -03:00
|
|
|
|
|
|
|
ai("a(")
|
|
|
|
ai("a(b")
|
|
|
|
ai("a(b,")
|
|
|
|
ai("a(b,c")
|
|
|
|
ai("a(b,c,")
|
|
|
|
|
|
|
|
ai("a[")
|
|
|
|
ai("a[b")
|
|
|
|
ai("a[b,")
|
|
|
|
ai("a[b:")
|
|
|
|
ai("a[b:c")
|
|
|
|
ai("a[b:c:")
|
|
|
|
ai("a[b:c:d")
|
|
|
|
|
|
|
|
ai("def a(")
|
|
|
|
ai("def a(b")
|
|
|
|
ai("def a(b,")
|
|
|
|
ai("def a(b,c")
|
|
|
|
ai("def a(b,c,")
|
|
|
|
|
|
|
|
ai("(")
|
|
|
|
ai("(a")
|
|
|
|
ai("(a,")
|
|
|
|
ai("(a,b")
|
|
|
|
ai("(a,b,")
|
|
|
|
|
|
|
|
ai("if a:\n pass\nelif b:")
|
|
|
|
ai("if a:\n pass\nelif b:\n pass\nelse:")
|
|
|
|
|
|
|
|
ai("while a:")
|
|
|
|
ai("while a:\n pass\nelse:")
|
|
|
|
|
|
|
|
ai("for a in b:")
|
|
|
|
ai("for a in b:\n pass\nelse:")
|
|
|
|
|
|
|
|
ai("try:")
|
|
|
|
ai("try:\n pass\nexcept:")
|
|
|
|
ai("try:\n pass\nfinally:")
|
|
|
|
ai("try:\n pass\nexcept:\n pass\nfinally:")
|
|
|
|
|
|
|
|
ai("with a:")
|
|
|
|
ai("with a as b:")
|
|
|
|
|
|
|
|
ai("class a:")
|
|
|
|
ai("class a(")
|
|
|
|
ai("class a(b")
|
|
|
|
ai("class a(b,")
|
|
|
|
ai("class a():")
|
|
|
|
|
|
|
|
ai("[x for")
|
|
|
|
ai("[x for x in")
|
|
|
|
ai("[x for x in (")
|
|
|
|
|
|
|
|
ai("(x for")
|
|
|
|
ai("(x for x in")
|
|
|
|
ai("(x for x in (")
|
|
|
|
|
2024-01-05 08:16:46 -04:00
|
|
|
ai('a = f"""')
|
|
|
|
ai('a = \\')
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
def test_invalid(self):
|
|
|
|
ai = self.assertInvalid
|
|
|
|
ai("a b")
|
2003-05-16 23:39:52 -03:00
|
|
|
|
|
|
|
ai("a @")
|
|
|
|
ai("a b @")
|
|
|
|
ai("a ** @")
|
2003-06-15 20:26:30 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("a = ")
|
|
|
|
ai("a = 9 +")
|
|
|
|
|
2003-05-16 23:39:52 -03:00
|
|
|
ai("def x():\n\npass\n")
|
|
|
|
|
|
|
|
ai("\n\n if 1: pass\n\npass")
|
|
|
|
|
|
|
|
ai("a = 9+ \\\n")
|
|
|
|
ai("a = 'a\\ ")
|
|
|
|
ai("a = 'a\\\n")
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("a = 1","eval")
|
|
|
|
ai("]","eval")
|
|
|
|
ai("())","eval")
|
|
|
|
ai("[}","eval")
|
|
|
|
ai("9+","eval")
|
|
|
|
ai("lambda z:","eval")
|
|
|
|
ai("a b","eval")
|
|
|
|
|
2009-08-02 19:19:05 -03:00
|
|
|
ai("return 2.3")
|
|
|
|
ai("if (a == 1 and b = 2): pass")
|
|
|
|
|
|
|
|
ai("del 1")
|
|
|
|
ai("del (1,)")
|
|
|
|
ai("del [1]")
|
|
|
|
ai("del '1'")
|
|
|
|
|
|
|
|
ai("[i for i in range(10)] = (1, 2, 3)")
|
|
|
|
|
2020-05-10 21:41:26 -03:00
|
|
|
def test_invalid_exec(self):
|
|
|
|
ai = self.assertInvalid
|
|
|
|
ai("raise = 4", symbol="exec")
|
|
|
|
ai('def a-b', symbol='exec')
|
|
|
|
ai('await?', symbol='exec')
|
|
|
|
ai('=!=', symbol='exec')
|
|
|
|
ai('a await raise b', symbol='exec')
|
|
|
|
ai('a await raise b?+1', symbol='exec')
|
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
def test_filename(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(compile_command("a = 1\n", "abc").co_filename,
|
|
|
|
compile("a = 1\n", "abc", 'single').co_filename)
|
|
|
|
self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename,
|
|
|
|
compile("a = 1\n", "def", 'single').co_filename)
|
2001-07-30 09:30:08 -03:00
|
|
|
|
2020-06-04 20:40:24 -03:00
|
|
|
def test_warning(self):
|
|
|
|
# Test that the warning is only returned once.
|
2020-08-13 14:18:49 -03:00
|
|
|
with warnings_helper.check_warnings(
|
2023-04-24 18:42:57 -03:00
|
|
|
('"is" with \'str\' literal', SyntaxWarning),
|
2022-11-03 13:53:25 -03:00
|
|
|
("invalid escape sequence", SyntaxWarning),
|
2020-08-13 14:18:49 -03:00
|
|
|
) as w:
|
|
|
|
compile_command(r"'\e' is 0")
|
|
|
|
self.assertEqual(len(w.warnings), 2)
|
2001-09-20 18:33:42 -03:00
|
|
|
|
2020-08-12 09:53:28 -03:00
|
|
|
# bpo-41520: check SyntaxWarning treated as an SyntaxError
|
2020-08-13 14:18:49 -03:00
|
|
|
with warnings.catch_warnings(), self.assertRaises(SyntaxError):
|
2020-08-12 09:53:28 -03:00
|
|
|
warnings.simplefilter('error', SyntaxWarning)
|
2020-08-13 14:18:49 -03:00
|
|
|
compile_command('1 is 1', symbol='exec')
|
2020-08-12 09:53:28 -03:00
|
|
|
|
2022-11-03 13:53:25 -03:00
|
|
|
# Check SyntaxWarning treated as an SyntaxError
|
2022-09-16 11:37:30 -03:00
|
|
|
with warnings.catch_warnings(), self.assertRaises(SyntaxError):
|
2022-11-03 13:53:25 -03:00
|
|
|
warnings.simplefilter('error', SyntaxWarning)
|
2022-09-16 11:37:30 -03:00
|
|
|
compile_command(r"'\e'", symbol='exec')
|
|
|
|
|
|
|
|
def test_incomplete_warning(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
self.assertIncomplete("'\\e' + (")
|
|
|
|
self.assertEqual(w, [])
|
|
|
|
|
|
|
|
def test_invalid_warning(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
self.assertInvalid("'\\e' 1")
|
|
|
|
self.assertEqual(len(w), 1)
|
2022-11-03 13:53:25 -03:00
|
|
|
self.assertEqual(w[0].category, SyntaxWarning)
|
2022-09-16 11:37:30 -03:00
|
|
|
self.assertRegex(str(w[0].message), 'invalid escape sequence')
|
|
|
|
self.assertEqual(w[0].filename, '<input>')
|
|
|
|
|
2023-10-30 16:24:21 -03:00
|
|
|
def assertSyntaxErrorMatches(self, code, message):
|
|
|
|
with self.subTest(code):
|
|
|
|
with self.assertRaisesRegex(SyntaxError, message):
|
|
|
|
compile_command(code, symbol='exec')
|
|
|
|
|
|
|
|
def test_syntax_errors(self):
|
|
|
|
self.assertSyntaxErrorMatches(
|
|
|
|
dedent("""\
|
|
|
|
def foo(x,x):
|
|
|
|
pass
|
|
|
|
"""), "duplicate argument 'x' in function definition")
|
|
|
|
|
|
|
|
|
2020-08-12 09:53:28 -03:00
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
if __name__ == "__main__":
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|