22 Test cases for codeop.py
33 Nick Mathewson
44"""
5- import sys
65import unittest
76import warnings
8- from test import support
97from test .support import warnings_helper
8+ from textwrap import dedent
109
1110from codeop import compile_command , PyCF_DONT_IMPLY_DEDENT
12- import io
13-
14- if support .is_jython :
15-
16- def unify_callables (d ):
17- for n , v in d .items ():
18- if hasattr (v , '__call__' ):
19- d [n ] = True
20- return d
21-
2211
2312class CodeopTests (unittest .TestCase ):
2413
2514 def assertValid (self , str , symbol = 'single' ):
2615 '''succeed iff str is a valid piece of code'''
27- if support .is_jython :
28- code = compile_command (str , "<input>" , symbol )
29- self .assertTrue (code )
30- if symbol == "single" :
31- d , r = {}, {}
32- saved_stdout = sys .stdout
33- sys .stdout = io .StringIO ()
34- try :
35- exec (code , d )
36- exec (compile (str , "<input>" , "single" ), r )
37- finally :
38- sys .stdout = saved_stdout
39- elif symbol == 'eval' :
40- ctx = {'a' : 2 }
41- d = {'value' : eval (code , ctx )}
42- r = {'value' : eval (str , ctx )}
43- self .assertEqual (unify_callables (r ), unify_callables (d ))
44- else :
45- expected = compile (str , "<input>" , symbol , PyCF_DONT_IMPLY_DEDENT )
46- self .assertEqual (compile_command (str , "<input>" , symbol ), expected )
16+ expected = compile (str , "<input>" , symbol , PyCF_DONT_IMPLY_DEDENT )
17+ self .assertEqual (compile_command (str , "<input>" , symbol ), expected )
4718
4819 def assertIncomplete (self , str , symbol = 'single' ):
4920 '''succeed iff str is the start of a valid piece of code'''
@@ -52,30 +23,25 @@ def assertIncomplete(self, str, symbol='single'):
5223 def assertInvalid (self , str , symbol = 'single' , is_syntax = 1 ):
5324 '''succeed iff str is the start of an invalid piece of code'''
5425 try :
55- compile_command (str , symbol = symbol )
26+ compile_command (str ,symbol = symbol )
5627 self .fail ("No exception raised for invalid code" )
5728 except SyntaxError :
5829 self .assertTrue (is_syntax )
5930 except OverflowError :
6031 self .assertTrue (not is_syntax )
6132
6233 # TODO: RUSTPYTHON
63-
6434 @unittest .expectedFailure
6535 def test_valid (self ):
6636 av = self .assertValid
6737
6838 # special case
69- if not support .is_jython :
70- self .assertEqual (compile_command ("" ),
71- compile ("pass" , "<input>" , 'single' ,
72- PyCF_DONT_IMPLY_DEDENT ))
73- self .assertEqual (compile_command ("\n " ),
74- compile ("pass" , "<input>" , 'single' ,
75- PyCF_DONT_IMPLY_DEDENT ))
76- else :
77- av ("" )
78- av ("\n " )
39+ self .assertEqual (compile_command ("" ),
40+ compile ("pass" , "<input>" , 'single' ,
41+ PyCF_DONT_IMPLY_DEDENT ))
42+ self .assertEqual (compile_command ("\n " ),
43+ compile ("pass" , "<input>" , 'single' ,
44+ PyCF_DONT_IMPLY_DEDENT ))
7945
8046 av ("a = 1" )
8147 av ("\n a = 1" )
@@ -104,25 +70,25 @@ def test_valid(self):
10470 av ("a=3\n \n " )
10571 av ("a = 9+ \\ \n 3" )
10672
107- av ("3**3" , "eval" )
108- av ("(lambda z: \n z**3)" , "eval" )
73+ av ("3**3" ,"eval" )
74+ av ("(lambda z: \n z**3)" ,"eval" )
10975
110- av ("9+ \\ \n 3" , "eval" )
111- av ("9+ \\ \n 3\n " , "eval" )
76+ av ("9+ \\ \n 3" ,"eval" )
77+ av ("9+ \\ \n 3\n " ,"eval" )
11278
113- av ("\n \n a**3" , "eval" )
114- av ("\n \n a**3" , "eval" )
115- av ("#a\n #b\n a**3" , "eval" )
79+ av ("\n \n a**3" ,"eval" )
80+ av ("\n \n a**3" ,"eval" )
81+ av ("#a\n #b\n a**3" ,"eval" )
11682
11783 av ("\n \n a = 1\n \n " )
11884 av ("\n \n if 1: a=1\n \n " )
11985
12086 av ("if 1:\n pass\n if 1:\n pass\n else:\n pass\n " )
12187 av ("#a\n \n \n a=3\n \n " )
12288
123- av ("\n \n a**3" , "eval" )
124- av ("\n \n a**3" , "eval" )
125- av ("#a\n #b\n a**3" , "eval" )
89+ av ("\n \n a**3" ,"eval" )
90+ av ("\n \n a**3" ,"eval" )
91+ av ("#a\n #b\n a**3" ,"eval" )
12692
12793 av ("def f():\n try: pass\n finally: [x for x in (1,2)]\n " )
12894 av ("def f():\n pass\n #foo\n " )
@@ -141,6 +107,10 @@ def test_incomplete(self):
141107 ai ("a = {" )
142108 ai ("b + {" )
143109
110+ ai ("print([1,\n 2," )
111+ ai ("print({1:1,\n 2:3," )
112+ ai ("print((1,\n 2," )
113+
144114 ai ("if 9==3:\n pass\n else:" )
145115 ai ("if 9==3:\n pass\n else:\n " )
146116 ai ("if 9==3:\n pass\n else:\n pass" )
@@ -163,13 +133,12 @@ def test_incomplete(self):
163133 ai ("a = 'a\\ " )
164134 ai ("a = '''xy" )
165135
166- ai ("" , "eval" )
167- ai ("\n " , "eval" )
168- ai ("(" , "eval" )
169- ai ("(\n \n \n " , "eval" )
170- ai ("(9+" , "eval" )
171- ai ("9+ \\ " , "eval" )
172- ai ("lambda z: \\ " , "eval" )
136+ ai ("" ,"eval" )
137+ ai ("\n " ,"eval" )
138+ ai ("(" ,"eval" )
139+ ai ("(9+" ,"eval" )
140+ ai ("9+ \\ " ,"eval" )
141+ ai ("lambda z: \\ " ,"eval" )
173142
174143 ai ("if True:\n if True:\n if True: \n " )
175144
@@ -277,14 +246,13 @@ def test_invalid(self):
277246 ai ("a = 'a\\ " )
278247 ai ("a = 'a\\ \n " )
279248
280- ai ("a = 1" , "eval" )
281- ai ("a = (" , "eval" )
282- ai ("]" , "eval" )
283- ai ("())" , "eval" )
284- ai ("[}" , "eval" )
285- ai ("9+" , "eval" )
286- ai ("lambda z:" , "eval" )
287- ai ("a b" , "eval" )
249+ ai ("a = 1" ,"eval" )
250+ ai ("]" ,"eval" )
251+ ai ("())" ,"eval" )
252+ ai ("[}" ,"eval" )
253+ ai ("9+" ,"eval" )
254+ ai ("lambda z:" ,"eval" )
255+ ai ("a b" ,"eval" )
288256
289257 ai ("return 2.3" )
290258 ai ("if (a == 1 and b = 2): pass" )
@@ -314,11 +282,11 @@ def test_filename(self):
314282 # TODO: RUSTPYTHON
315283 @unittest .expectedFailure
316284 def test_warning (self ):
317- # Teswarnings_helper.check_warningsonly returned once.
285+ # Test that the warning is only returned once.
318286 with warnings_helper .check_warnings (
319- (".*literal" , SyntaxWarning ),
320- (".* invalid" , DeprecationWarning ),
321- ) as w :
287+ ('"is" with \' str \' literal' , SyntaxWarning ),
288+ ("invalid escape sequence " , SyntaxWarning ),
289+ ) as w :
322290 compile_command (r"'\e' is 0" )
323291 self .assertEqual (len (w .warnings ), 2 )
324292
@@ -327,6 +295,42 @@ def test_warning(self):
327295 warnings .simplefilter ('error' , SyntaxWarning )
328296 compile_command ('1 is 1' , symbol = 'exec' )
329297
298+ # Check SyntaxWarning treated as an SyntaxError
299+ with warnings .catch_warnings (), self .assertRaises (SyntaxError ):
300+ warnings .simplefilter ('error' , SyntaxWarning )
301+ compile_command (r"'\e'" , symbol = 'exec' )
302+
303+ # TODO: RUSTPYTHON
304+ #def test_incomplete_warning(self):
305+ # with warnings.catch_warnings(record=True) as w:
306+ # warnings.simplefilter('always')
307+ # self.assertIncomplete("'\\e' + (")
308+ # self.assertEqual(w, [])
309+
310+ # TODO: RUSTPYTHON
311+ @unittest .expectedFailure
312+ def test_invalid_warning (self ):
313+ with warnings .catch_warnings (record = True ) as w :
314+ warnings .simplefilter ('always' )
315+ self .assertInvalid ("'\\ e' 1" )
316+ self .assertEqual (len (w ), 1 )
317+ self .assertEqual (w [0 ].category , SyntaxWarning )
318+ self .assertRegex (str (w [0 ].message ), 'invalid escape sequence' )
319+ self .assertEqual (w [0 ].filename , '<input>' )
320+
321+ def assertSyntaxErrorMatches (self , code , message ):
322+ with self .subTest (code ):
323+ with self .assertRaisesRegex (SyntaxError , message ):
324+ compile_command (code , symbol = 'exec' )
325+
326+ def test_syntax_errors (self ):
327+ self .assertSyntaxErrorMatches (
328+ dedent ("""\
329+ def foo(x,x):
330+ pass
331+ """ ), "duplicate argument 'x' in function definition" )
332+
333+
330334
331335if __name__ == "__main__" :
332336 unittest .main ()
0 commit comments