blob: d3e6d35943f64e21bbe7e6718e1697ef6d25d4c9 [file] [log] [blame]
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001import os
2import sys
3import unittest
Georg Brandl0c77a822008-06-10 16:37:50 +00004import ast
Benjamin Peterson9ed37432012-07-08 11:13:36 -07005import weakref
6
7from test import support
Tim Peters400cbc32006-02-28 18:44:41 +00008
9def to_tuple(t):
Guido van Rossum3172c5d2007-10-16 18:12:55 +000010 if t is None or isinstance(t, (str, int, complex)):
Tim Peters400cbc32006-02-28 18:44:41 +000011 return t
12 elif isinstance(t, list):
13 return [to_tuple(e) for e in t]
14 result = [t.__class__.__name__]
Martin v. Löwis49c5da12006-03-01 22:49:05 +000015 if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
16 result.append((t.lineno, t.col_offset))
Tim Peters400cbc32006-02-28 18:44:41 +000017 if t._fields is None:
18 return tuple(result)
19 for f in t._fields:
20 result.append(to_tuple(getattr(t, f)))
21 return tuple(result)
22
Neal Norwitzee9b10a2008-03-31 05:29:39 +000023
Tim Peters400cbc32006-02-28 18:44:41 +000024# These tests are compiled through "exec"
Ezio Melotti85a86292013-08-17 16:57:41 +030025# There should be at least one test per statement
Tim Peters400cbc32006-02-28 18:44:41 +000026exec_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050027 # None
28 "None",
Tim Peters400cbc32006-02-28 18:44:41 +000029 # FunctionDef
30 "def f(): pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050031 # FunctionDef with arg
32 "def f(a): pass",
33 # FunctionDef with arg and default value
34 "def f(a=0): pass",
35 # FunctionDef with varargs
36 "def f(*args): pass",
37 # FunctionDef with kwargs
38 "def f(**kwargs): pass",
39 # FunctionDef with all kind of args
Benjamin Petersone84fde92014-02-13 19:22:14 -050040 "def f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000041 # ClassDef
42 "class C:pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050043 # ClassDef, new style class
44 "class C(object): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000045 # Return
46 "def f():return 1",
47 # Delete
48 "del v",
49 # Assign
50 "v = 1",
51 # AugAssign
52 "v += 1",
Tim Peters400cbc32006-02-28 18:44:41 +000053 # For
54 "for v in v:pass",
55 # While
56 "while v:pass",
57 # If
58 "if v:pass",
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -050059 # With
60 "with x as y: pass",
61 "with x as y, z as q: pass",
Tim Peters400cbc32006-02-28 18:44:41 +000062 # Raise
Collin Winter828f04a2007-08-31 00:04:24 +000063 "raise Exception('string')",
Tim Peters400cbc32006-02-28 18:44:41 +000064 # TryExcept
65 "try:\n pass\nexcept Exception:\n pass",
66 # TryFinally
67 "try:\n pass\nfinally:\n pass",
68 # Assert
69 "assert v",
70 # Import
71 "import sys",
72 # ImportFrom
73 "from sys import v",
Tim Peters400cbc32006-02-28 18:44:41 +000074 # Global
75 "global v",
76 # Expr
77 "1",
78 # Pass,
79 "pass",
80 # Break
Yury Selivanovb3d53132015-09-01 16:10:49 -040081 "for v in v:break",
Tim Peters400cbc32006-02-28 18:44:41 +000082 # Continue
Yury Selivanovb3d53132015-09-01 16:10:49 -040083 "for v in v:continue",
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +000084 # for statements with naked tuples (see http://bugs.python.org/issue6704)
85 "for a,b in c: pass",
86 "[(a,b) for a,b in c]",
87 "((a,b) for a,b in c)",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050088 "((a,b) for (a,b) in c)",
89 # Multiline generator expression (test for .lineno & .col_offset)
90 """(
91 (
92 Aa
93 ,
94 Bb
95 )
96 for
97 Aa
98 ,
99 Bb in Cc
100 )""",
101 # dictcomp
102 "{a : b for w in x for m in p if g}",
103 # dictcomp with naked tuple
104 "{a : b for v,w in x}",
105 # setcomp
106 "{r for l in x if g}",
107 # setcomp with naked tuple
108 "{r for l,m in x}",
Yury Selivanov75445082015-05-11 22:57:16 -0400109 # AsyncFunctionDef
110 "async def f():\n await something()",
111 # AsyncFor
112 "async def f():\n async for e in i: 1\n else: 2",
113 # AsyncWith
114 "async def f():\n async with a as b: 1",
Yury Selivanovb3d53132015-09-01 16:10:49 -0400115 # PEP 448: Additional Unpacking Generalizations
116 "{**{1:2}, 2:3}",
117 "{*{1, 2}, 3}",
Tim Peters400cbc32006-02-28 18:44:41 +0000118]
119
120# These are compiled through "single"
121# because of overlap with "eval", it just tests what
122# can't be tested with "eval"
123single_tests = [
124 "1+2"
125]
126
127# These are compiled through "eval"
128# It should test all expressions
129eval_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500130 # None
131 "None",
Tim Peters400cbc32006-02-28 18:44:41 +0000132 # BoolOp
133 "a and b",
134 # BinOp
135 "a + b",
136 # UnaryOp
137 "not v",
138 # Lambda
139 "lambda:None",
140 # Dict
141 "{ 1:2 }",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500142 # Empty dict
143 "{}",
144 # Set
145 "{None,}",
146 # Multiline dict (test for .lineno & .col_offset)
147 """{
148 1
149 :
150 2
151 }""",
Tim Peters400cbc32006-02-28 18:44:41 +0000152 # ListComp
153 "[a for b in c if d]",
154 # GeneratorExp
155 "(a for b in c if d)",
156 # Yield - yield expressions can't work outside a function
157 #
158 # Compare
159 "1 < 2 < 3",
160 # Call
161 "f(1,2,c=3,*d,**e)",
Tim Peters400cbc32006-02-28 18:44:41 +0000162 # Num
Guido van Rossume2a383d2007-01-15 16:59:06 +0000163 "10",
Tim Peters400cbc32006-02-28 18:44:41 +0000164 # Str
165 "'string'",
166 # Attribute
167 "a.b",
168 # Subscript
169 "a[b:c]",
170 # Name
171 "v",
172 # List
173 "[1,2,3]",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500174 # Empty list
175 "[]",
Tim Peters400cbc32006-02-28 18:44:41 +0000176 # Tuple
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000177 "1,2,3",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500178 # Tuple
179 "(1,2,3)",
180 # Empty tuple
181 "()",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000182 # Combination
183 "a.b.c.d(a.b[1:2])",
184
Tim Peters400cbc32006-02-28 18:44:41 +0000185]
186
187# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
188# excepthandler, arguments, keywords, alias
189
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000190class AST_Tests(unittest.TestCase):
Tim Peters400cbc32006-02-28 18:44:41 +0000191
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500192 def _assertTrueorder(self, ast_node, parent_pos):
Georg Brandl0c77a822008-06-10 16:37:50 +0000193 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000194 return
Georg Brandl0c77a822008-06-10 16:37:50 +0000195 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000196 node_pos = (ast_node.lineno, ast_node.col_offset)
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500197 self.assertTrue(node_pos >= parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000198 parent_pos = (ast_node.lineno, ast_node.col_offset)
199 for name in ast_node._fields:
200 value = getattr(ast_node, name)
201 if isinstance(value, list):
202 for child in value:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500203 self._assertTrueorder(child, parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000204 elif value is not None:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500205 self._assertTrueorder(value, parent_pos)
Tim Peters5ddfe412006-03-01 23:02:57 +0000206
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500207 def test_AST_objects(self):
208 x = ast.AST()
209 self.assertEqual(x._fields, ())
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700210 x.foobar = 42
211 self.assertEqual(x.foobar, 42)
212 self.assertEqual(x.__dict__["foobar"], 42)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500213
214 with self.assertRaises(AttributeError):
215 x.vararg
216
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500217 with self.assertRaises(TypeError):
218 # "_ast.AST constructor takes 0 positional arguments"
219 ast.AST(2)
220
Benjamin Peterson9ed37432012-07-08 11:13:36 -0700221 def test_AST_garbage_collection(self):
222 class X:
223 pass
224 a = ast.AST()
225 a.x = X()
226 a.x.a = a
227 ref = weakref.ref(a.x)
228 del a
229 support.gc_collect()
230 self.assertIsNone(ref())
231
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000232 def test_snippets(self):
233 for input, output, kind in ((exec_tests, exec_results, "exec"),
234 (single_tests, single_results, "single"),
235 (eval_tests, eval_results, "eval")):
236 for i, o in zip(input, output):
Yury Selivanovb3d53132015-09-01 16:10:49 -0400237 with self.subTest(action="parsing", input=i):
238 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
239 self.assertEqual(to_tuple(ast_tree), o)
240 self._assertTrueorder(ast_tree, (0, 0))
241 with self.subTest(action="compiling", input=i):
242 compile(ast_tree, "?", kind)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000243
Benjamin Peterson78565b22009-06-28 19:19:51 +0000244 def test_slice(self):
245 slc = ast.parse("x[::]").body[0].value.slice
246 self.assertIsNone(slc.upper)
247 self.assertIsNone(slc.lower)
248 self.assertIsNone(slc.step)
249
250 def test_from_import(self):
251 im = ast.parse("from . import y").body[0]
252 self.assertIsNone(im.module)
253
Benjamin Petersona4e4e352012-03-22 08:19:04 -0400254 def test_non_interned_future_from_ast(self):
255 mod = ast.parse("from __future__ import division")
256 self.assertIsInstance(mod.body[0], ast.ImportFrom)
257 mod.body[0].module = " __future__ ".strip()
258 compile(mod, "<test>", "exec")
259
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000260 def test_base_classes(self):
261 self.assertTrue(issubclass(ast.For, ast.stmt))
262 self.assertTrue(issubclass(ast.Name, ast.expr))
263 self.assertTrue(issubclass(ast.stmt, ast.AST))
264 self.assertTrue(issubclass(ast.expr, ast.AST))
265 self.assertTrue(issubclass(ast.comprehension, ast.AST))
266 self.assertTrue(issubclass(ast.Gt, ast.AST))
267
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500268 def test_field_attr_existence(self):
269 for name, item in ast.__dict__.items():
270 if isinstance(item, type) and name != 'AST' and name[0].isupper():
271 x = item()
272 if isinstance(x, ast.AST):
273 self.assertEqual(type(x._fields), tuple)
274
275 def test_arguments(self):
276 x = ast.arguments()
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500277 self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs',
278 'kw_defaults', 'kwarg', 'defaults'))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500279
280 with self.assertRaises(AttributeError):
281 x.vararg
282
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700283 x = ast.arguments(*range(1, 7))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500284 self.assertEqual(x.vararg, 2)
285
286 def test_field_attr_writable(self):
287 x = ast.Num()
288 # We can assign to _fields
289 x._fields = 666
290 self.assertEqual(x._fields, 666)
291
292 def test_classattrs(self):
293 x = ast.Num()
294 self.assertEqual(x._fields, ('n',))
295
296 with self.assertRaises(AttributeError):
297 x.n
298
299 x = ast.Num(42)
300 self.assertEqual(x.n, 42)
301
302 with self.assertRaises(AttributeError):
303 x.lineno
304
305 with self.assertRaises(AttributeError):
306 x.foobar
307
308 x = ast.Num(lineno=2)
309 self.assertEqual(x.lineno, 2)
310
311 x = ast.Num(42, lineno=0)
312 self.assertEqual(x.lineno, 0)
313 self.assertEqual(x._fields, ('n',))
314 self.assertEqual(x.n, 42)
315
316 self.assertRaises(TypeError, ast.Num, 1, 2)
317 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
318
319 def test_module(self):
320 body = [ast.Num(42)]
321 x = ast.Module(body)
322 self.assertEqual(x.body, body)
323
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000324 def test_nodeclasses(self):
Florent Xicluna992d9e02011-11-11 19:35:42 +0100325 # Zero arguments constructor explicitly allowed
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500326 x = ast.BinOp()
327 self.assertEqual(x._fields, ('left', 'op', 'right'))
328
329 # Random attribute allowed too
330 x.foobarbaz = 5
331 self.assertEqual(x.foobarbaz, 5)
332
333 n1 = ast.Num(1)
334 n3 = ast.Num(3)
335 addop = ast.Add()
336 x = ast.BinOp(n1, addop, n3)
337 self.assertEqual(x.left, n1)
338 self.assertEqual(x.op, addop)
339 self.assertEqual(x.right, n3)
Benjamin Peterson68b543a2011-06-27 17:51:18 -0500340
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500341 x = ast.BinOp(1, 2, 3)
342 self.assertEqual(x.left, 1)
343 self.assertEqual(x.op, 2)
344 self.assertEqual(x.right, 3)
345
Georg Brandl0c77a822008-06-10 16:37:50 +0000346 x = ast.BinOp(1, 2, 3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000347 self.assertEqual(x.left, 1)
348 self.assertEqual(x.op, 2)
349 self.assertEqual(x.right, 3)
350 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000351
352 # node raises exception when not given enough arguments
Georg Brandl0c77a822008-06-10 16:37:50 +0000353 self.assertRaises(TypeError, ast.BinOp, 1, 2)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500354 # node raises exception when given too many arguments
355 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
356 # node raises exception when not given enough arguments
357 self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
358 # node raises exception when given too many arguments
359 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000360
361 # can set attributes through kwargs too
Georg Brandl0c77a822008-06-10 16:37:50 +0000362 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000363 self.assertEqual(x.left, 1)
364 self.assertEqual(x.op, 2)
365 self.assertEqual(x.right, 3)
366 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000367
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500368 # Random kwargs also allowed
369 x = ast.BinOp(1, 2, 3, foobarbaz=42)
370 self.assertEqual(x.foobarbaz, 42)
371
372 def test_no_fields(self):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000373 # this used to fail because Sub._fields was None
Georg Brandl0c77a822008-06-10 16:37:50 +0000374 x = ast.Sub()
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500375 self.assertEqual(x._fields, ())
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000376
377 def test_pickling(self):
378 import pickle
379 mods = [pickle]
380 try:
381 import cPickle
382 mods.append(cPickle)
383 except ImportError:
384 pass
385 protocols = [0, 1, 2]
386 for mod in mods:
387 for protocol in protocols:
388 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
389 ast2 = mod.loads(mod.dumps(ast, protocol))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000390 self.assertEqual(to_tuple(ast2), to_tuple(ast))
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000391
Benjamin Peterson5b066812010-11-20 01:38:49 +0000392 def test_invalid_sum(self):
393 pos = dict(lineno=2, col_offset=3)
394 m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
395 with self.assertRaises(TypeError) as cm:
396 compile(m, "<test>", "exec")
397 self.assertIn("but got <_ast.expr", str(cm.exception))
398
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500399 def test_invalid_identitifer(self):
400 m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
401 ast.fix_missing_locations(m)
402 with self.assertRaises(TypeError) as cm:
403 compile(m, "<test>", "exec")
404 self.assertIn("identifier must be of type str", str(cm.exception))
405
406 def test_invalid_string(self):
407 m = ast.Module([ast.Expr(ast.Str(42))])
408 ast.fix_missing_locations(m)
409 with self.assertRaises(TypeError) as cm:
410 compile(m, "<test>", "exec")
411 self.assertIn("string must be of type str", str(cm.exception))
412
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000413 def test_empty_yield_from(self):
414 # Issue 16546: yield from value is not optional.
415 empty_yield_from = ast.parse("def f():\n yield from g()")
416 empty_yield_from.body[0].body[0].value.value = None
417 with self.assertRaises(ValueError) as cm:
418 compile(empty_yield_from, "<test>", "exec")
419 self.assertIn("field value is required", str(cm.exception))
420
Georg Brandl0c77a822008-06-10 16:37:50 +0000421
422class ASTHelpers_Test(unittest.TestCase):
423
424 def test_parse(self):
425 a = ast.parse('foo(1 + 1)')
426 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
427 self.assertEqual(ast.dump(a), ast.dump(b))
428
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400429 def test_parse_in_error(self):
430 try:
431 1/0
432 except Exception:
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400433 with self.assertRaises(SyntaxError) as e:
434 ast.literal_eval(r"'\U'")
435 self.assertIsNotNone(e.exception.__context__)
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400436
Georg Brandl0c77a822008-06-10 16:37:50 +0000437 def test_dump(self):
438 node = ast.parse('spam(eggs, "and cheese")')
439 self.assertEqual(ast.dump(node),
440 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
441 "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400442 "keywords=[]))])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000443 )
444 self.assertEqual(ast.dump(node, annotate_fields=False),
445 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400446 "Str('and cheese')], []))])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000447 )
448 self.assertEqual(ast.dump(node, include_attributes=True),
449 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
450 "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
451 "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400452 "col_offset=11)], keywords=[], "
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500453 "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000454 )
455
456 def test_copy_location(self):
457 src = ast.parse('1 + 1', mode='eval')
458 src.body.right = ast.copy_location(ast.Num(2), src.body.right)
459 self.assertEqual(ast.dump(src, include_attributes=True),
460 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
461 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
462 'col_offset=0))'
463 )
464
465 def test_fix_missing_locations(self):
466 src = ast.parse('write("spam")')
467 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400468 [ast.Str('eggs')], [])))
Georg Brandl0c77a822008-06-10 16:37:50 +0000469 self.assertEqual(src, ast.fix_missing_locations(src))
470 self.assertEqual(ast.dump(src, include_attributes=True),
471 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
472 "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400473 "col_offset=6)], keywords=[], "
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500474 "lineno=1, col_offset=0), lineno=1, col_offset=0), "
Georg Brandl0c77a822008-06-10 16:37:50 +0000475 "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
476 "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400477 "keywords=[], lineno=1, "
Georg Brandl0c77a822008-06-10 16:37:50 +0000478 "col_offset=0), lineno=1, col_offset=0)])"
479 )
480
481 def test_increment_lineno(self):
482 src = ast.parse('1 + 1', mode='eval')
483 self.assertEqual(ast.increment_lineno(src, n=3), src)
484 self.assertEqual(ast.dump(src, include_attributes=True),
485 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
486 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
487 'col_offset=0))'
488 )
Georg Brandl619e7ba2011-01-09 07:38:51 +0000489 # issue10869: do not increment lineno of root twice
Georg Brandlefb69022011-01-09 07:50:48 +0000490 src = ast.parse('1 + 1', mode='eval')
Georg Brandl619e7ba2011-01-09 07:38:51 +0000491 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
492 self.assertEqual(ast.dump(src, include_attributes=True),
493 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
494 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
495 'col_offset=0))'
496 )
Georg Brandl0c77a822008-06-10 16:37:50 +0000497
498 def test_iter_fields(self):
499 node = ast.parse('foo()', mode='eval')
500 d = dict(ast.iter_fields(node.body))
501 self.assertEqual(d.pop('func').id, 'foo')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400502 self.assertEqual(d, {'keywords': [], 'args': []})
Georg Brandl0c77a822008-06-10 16:37:50 +0000503
504 def test_iter_child_nodes(self):
505 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
506 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
507 iterator = ast.iter_child_nodes(node.body)
508 self.assertEqual(next(iterator).id, 'spam')
509 self.assertEqual(next(iterator).n, 23)
510 self.assertEqual(next(iterator).n, 42)
511 self.assertEqual(ast.dump(next(iterator)),
512 "keyword(arg='eggs', value=Str(s='leek'))"
513 )
514
515 def test_get_docstring(self):
516 node = ast.parse('def foo():\n """line one\n line two"""')
517 self.assertEqual(ast.get_docstring(node.body[0]),
518 'line one\nline two')
519
Yury Selivanov2f07a662015-07-23 08:54:35 +0300520 node = ast.parse('async def foo():\n """spam\n ham"""')
521 self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
522
Georg Brandl0c77a822008-06-10 16:37:50 +0000523 def test_literal_eval(self):
524 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
525 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
526 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
Benjamin Peterson3e742892010-07-11 12:59:24 +0000527 self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
Benjamin Peterson5ef96e52010-07-11 23:06:06 +0000528 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
Georg Brandl0c77a822008-06-10 16:37:50 +0000529 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
Raymond Hettingerbc959732010-10-08 00:47:45 +0000530 self.assertEqual(ast.literal_eval('-6'), -6)
531 self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
532 self.assertEqual(ast.literal_eval('3.25'), 3.25)
Georg Brandl0c77a822008-06-10 16:37:50 +0000533
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000534 def test_literal_eval_issue4907(self):
535 self.assertEqual(ast.literal_eval('2j'), 2j)
536 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
537 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000538
Amaury Forgeot d'Arc58e87612011-11-22 21:51:55 +0100539 def test_bad_integer(self):
540 # issue13436: Bad error message with invalid numeric values
541 body = [ast.ImportFrom(module='time',
542 names=[ast.alias(name='sleep')],
543 level=None,
544 lineno=None, col_offset=None)]
545 mod = ast.Module(body)
546 with self.assertRaises(ValueError) as cm:
547 compile(mod, 'test', 'exec')
548 self.assertIn("invalid integer value: None", str(cm.exception))
549
Georg Brandl0c77a822008-06-10 16:37:50 +0000550
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500551class ASTValidatorTests(unittest.TestCase):
552
553 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
554 mod.lineno = mod.col_offset = 0
555 ast.fix_missing_locations(mod)
556 with self.assertRaises(exc) as cm:
557 compile(mod, "<test>", mode)
558 if msg is not None:
559 self.assertIn(msg, str(cm.exception))
560
561 def expr(self, node, msg=None, *, exc=ValueError):
562 mod = ast.Module([ast.Expr(node)])
563 self.mod(mod, msg, exc=exc)
564
565 def stmt(self, stmt, msg=None):
566 mod = ast.Module([stmt])
567 self.mod(mod, msg)
568
569 def test_module(self):
570 m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
571 self.mod(m, "must have Load context", "single")
572 m = ast.Expression(ast.Name("x", ast.Store()))
573 self.mod(m, "must have Load context", "eval")
574
575 def _check_arguments(self, fac, check):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700576 def arguments(args=None, vararg=None,
577 kwonlyargs=None, kwarg=None,
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500578 defaults=None, kw_defaults=None):
579 if args is None:
580 args = []
581 if kwonlyargs is None:
582 kwonlyargs = []
583 if defaults is None:
584 defaults = []
585 if kw_defaults is None:
586 kw_defaults = []
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700587 args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
588 kwarg, defaults)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500589 return fac(args)
590 args = [ast.arg("x", ast.Name("x", ast.Store()))]
591 check(arguments(args=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500592 check(arguments(kwonlyargs=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500593 check(arguments(defaults=[ast.Num(3)]),
594 "more positional defaults than args")
595 check(arguments(kw_defaults=[ast.Num(4)]),
596 "length of kwonlyargs is not the same as kw_defaults")
597 args = [ast.arg("x", ast.Name("x", ast.Load()))]
598 check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
599 "must have Load context")
600 args = [ast.arg("a", ast.Name("x", ast.Load())),
601 ast.arg("b", ast.Name("y", ast.Load()))]
602 check(arguments(kwonlyargs=args,
603 kw_defaults=[None, ast.Name("x", ast.Store())]),
604 "must have Load context")
605
606 def test_funcdef(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700607 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500608 f = ast.FunctionDef("x", a, [], [], None)
609 self.stmt(f, "empty body on FunctionDef")
610 f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
611 None)
612 self.stmt(f, "must have Load context")
613 f = ast.FunctionDef("x", a, [ast.Pass()], [],
614 ast.Name("x", ast.Store()))
615 self.stmt(f, "must have Load context")
616 def fac(args):
617 return ast.FunctionDef("x", args, [ast.Pass()], [], None)
618 self._check_arguments(fac, self.stmt)
619
620 def test_classdef(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400621 def cls(bases=None, keywords=None, body=None, decorator_list=None):
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500622 if bases is None:
623 bases = []
624 if keywords is None:
625 keywords = []
626 if body is None:
627 body = [ast.Pass()]
628 if decorator_list is None:
629 decorator_list = []
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400630 return ast.ClassDef("myclass", bases, keywords,
631 body, decorator_list)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500632 self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
633 "must have Load context")
634 self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
635 "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500636 self.stmt(cls(body=[]), "empty body on ClassDef")
637 self.stmt(cls(body=[None]), "None disallowed")
638 self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
639 "must have Load context")
640
641 def test_delete(self):
642 self.stmt(ast.Delete([]), "empty targets on Delete")
643 self.stmt(ast.Delete([None]), "None disallowed")
644 self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
645 "must have Del context")
646
647 def test_assign(self):
648 self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
649 self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
650 self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
651 "must have Store context")
652 self.stmt(ast.Assign([ast.Name("x", ast.Store())],
653 ast.Name("y", ast.Store())),
654 "must have Load context")
655
656 def test_augassign(self):
657 aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
658 ast.Name("y", ast.Load()))
659 self.stmt(aug, "must have Store context")
660 aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
661 ast.Name("y", ast.Store()))
662 self.stmt(aug, "must have Load context")
663
664 def test_for(self):
665 x = ast.Name("x", ast.Store())
666 y = ast.Name("y", ast.Load())
667 p = ast.Pass()
668 self.stmt(ast.For(x, y, [], []), "empty body on For")
669 self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
670 "must have Store context")
671 self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
672 "must have Load context")
673 e = ast.Expr(ast.Name("x", ast.Store()))
674 self.stmt(ast.For(x, y, [e], []), "must have Load context")
675 self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
676
677 def test_while(self):
678 self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
679 self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
680 "must have Load context")
681 self.stmt(ast.While(ast.Num(3), [ast.Pass()],
682 [ast.Expr(ast.Name("x", ast.Store()))]),
683 "must have Load context")
684
685 def test_if(self):
686 self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
687 i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
688 self.stmt(i, "must have Load context")
689 i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
690 self.stmt(i, "must have Load context")
691 i = ast.If(ast.Num(3), [ast.Pass()],
692 [ast.Expr(ast.Name("x", ast.Store()))])
693 self.stmt(i, "must have Load context")
694
695 def test_with(self):
696 p = ast.Pass()
697 self.stmt(ast.With([], [p]), "empty items on With")
698 i = ast.withitem(ast.Num(3), None)
699 self.stmt(ast.With([i], []), "empty body on With")
700 i = ast.withitem(ast.Name("x", ast.Store()), None)
701 self.stmt(ast.With([i], [p]), "must have Load context")
702 i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
703 self.stmt(ast.With([i], [p]), "must have Store context")
704
705 def test_raise(self):
706 r = ast.Raise(None, ast.Num(3))
707 self.stmt(r, "Raise with cause but no exception")
708 r = ast.Raise(ast.Name("x", ast.Store()), None)
709 self.stmt(r, "must have Load context")
710 r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
711 self.stmt(r, "must have Load context")
712
713 def test_try(self):
714 p = ast.Pass()
715 t = ast.Try([], [], [], [p])
716 self.stmt(t, "empty body on Try")
717 t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
718 self.stmt(t, "must have Load context")
719 t = ast.Try([p], [], [], [])
720 self.stmt(t, "Try has neither except handlers nor finalbody")
721 t = ast.Try([p], [], [p], [p])
722 self.stmt(t, "Try has orelse but no except handlers")
723 t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
724 self.stmt(t, "empty body on ExceptHandler")
725 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
726 self.stmt(ast.Try([p], e, [], []), "must have Load context")
727 e = [ast.ExceptHandler(None, "x", [p])]
728 t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
729 self.stmt(t, "must have Load context")
730 t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
731 self.stmt(t, "must have Load context")
732
733 def test_assert(self):
734 self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
735 "must have Load context")
736 assrt = ast.Assert(ast.Name("x", ast.Load()),
737 ast.Name("y", ast.Store()))
738 self.stmt(assrt, "must have Load context")
739
740 def test_import(self):
741 self.stmt(ast.Import([]), "empty names on Import")
742
743 def test_importfrom(self):
744 imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
745 self.stmt(imp, "level less than -1")
746 self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
747
748 def test_global(self):
749 self.stmt(ast.Global([]), "empty names on Global")
750
751 def test_nonlocal(self):
752 self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
753
754 def test_expr(self):
755 e = ast.Expr(ast.Name("x", ast.Store()))
756 self.stmt(e, "must have Load context")
757
758 def test_boolop(self):
759 b = ast.BoolOp(ast.And(), [])
760 self.expr(b, "less than 2 values")
761 b = ast.BoolOp(ast.And(), [ast.Num(3)])
762 self.expr(b, "less than 2 values")
763 b = ast.BoolOp(ast.And(), [ast.Num(4), None])
764 self.expr(b, "None disallowed")
765 b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
766 self.expr(b, "must have Load context")
767
768 def test_unaryop(self):
769 u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
770 self.expr(u, "must have Load context")
771
772 def test_lambda(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700773 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500774 self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
775 "must have Load context")
776 def fac(args):
777 return ast.Lambda(args, ast.Name("x", ast.Load()))
778 self._check_arguments(fac, self.expr)
779
780 def test_ifexp(self):
781 l = ast.Name("x", ast.Load())
782 s = ast.Name("y", ast.Store())
783 for args in (s, l, l), (l, s, l), (l, l, s):
Benjamin Peterson71ce8972011-08-09 16:17:12 -0500784 self.expr(ast.IfExp(*args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500785
786 def test_dict(self):
787 d = ast.Dict([], [ast.Name("x", ast.Load())])
788 self.expr(d, "same number of keys as values")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500789 d = ast.Dict([ast.Name("x", ast.Load())], [None])
790 self.expr(d, "None disallowed")
791
792 def test_set(self):
793 self.expr(ast.Set([None]), "None disallowed")
794 s = ast.Set([ast.Name("x", ast.Store())])
795 self.expr(s, "must have Load context")
796
797 def _check_comprehension(self, fac):
798 self.expr(fac([]), "comprehension with no generators")
799 g = ast.comprehension(ast.Name("x", ast.Load()),
800 ast.Name("x", ast.Load()), [])
801 self.expr(fac([g]), "must have Store context")
802 g = ast.comprehension(ast.Name("x", ast.Store()),
803 ast.Name("x", ast.Store()), [])
804 self.expr(fac([g]), "must have Load context")
805 x = ast.Name("x", ast.Store())
806 y = ast.Name("y", ast.Load())
807 g = ast.comprehension(x, y, [None])
808 self.expr(fac([g]), "None disallowed")
809 g = ast.comprehension(x, y, [ast.Name("x", ast.Store())])
810 self.expr(fac([g]), "must have Load context")
811
812 def _simple_comp(self, fac):
813 g = ast.comprehension(ast.Name("x", ast.Store()),
814 ast.Name("x", ast.Load()), [])
815 self.expr(fac(ast.Name("x", ast.Store()), [g]),
816 "must have Load context")
817 def wrap(gens):
818 return fac(ast.Name("x", ast.Store()), gens)
819 self._check_comprehension(wrap)
820
821 def test_listcomp(self):
822 self._simple_comp(ast.ListComp)
823
824 def test_setcomp(self):
825 self._simple_comp(ast.SetComp)
826
827 def test_generatorexp(self):
828 self._simple_comp(ast.GeneratorExp)
829
830 def test_dictcomp(self):
831 g = ast.comprehension(ast.Name("y", ast.Store()),
832 ast.Name("p", ast.Load()), [])
833 c = ast.DictComp(ast.Name("x", ast.Store()),
834 ast.Name("y", ast.Load()), [g])
835 self.expr(c, "must have Load context")
836 c = ast.DictComp(ast.Name("x", ast.Load()),
837 ast.Name("y", ast.Store()), [g])
838 self.expr(c, "must have Load context")
839 def factory(comps):
840 k = ast.Name("x", ast.Load())
841 v = ast.Name("y", ast.Load())
842 return ast.DictComp(k, v, comps)
843 self._check_comprehension(factory)
844
845 def test_yield(self):
Benjamin Peterson527c6222012-01-14 08:58:23 -0500846 self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
847 self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500848
849 def test_compare(self):
850 left = ast.Name("x", ast.Load())
851 comp = ast.Compare(left, [ast.In()], [])
852 self.expr(comp, "no comparators")
853 comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
854 self.expr(comp, "different number of comparators and operands")
855 comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
856 self.expr(comp, "non-numeric", exc=TypeError)
857 comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
858 self.expr(comp, "non-numeric", exc=TypeError)
859
860 def test_call(self):
861 func = ast.Name("x", ast.Load())
862 args = [ast.Name("y", ast.Load())]
863 keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400864 call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500865 self.expr(call, "must have Load context")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400866 call = ast.Call(func, [None], keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500867 self.expr(call, "None disallowed")
868 bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400869 call = ast.Call(func, args, bad_keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500870 self.expr(call, "must have Load context")
871
872 def test_num(self):
873 class subint(int):
874 pass
875 class subfloat(float):
876 pass
877 class subcomplex(complex):
878 pass
879 for obj in "0", "hello", subint(), subfloat(), subcomplex():
880 self.expr(ast.Num(obj), "non-numeric", exc=TypeError)
881
882 def test_attribute(self):
883 attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
884 self.expr(attr, "must have Load context")
885
886 def test_subscript(self):
887 sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
888 ast.Load())
889 self.expr(sub, "must have Load context")
890 x = ast.Name("x", ast.Load())
891 sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
892 ast.Load())
893 self.expr(sub, "must have Load context")
894 s = ast.Name("x", ast.Store())
895 for args in (s, None, None), (None, s, None), (None, None, s):
896 sl = ast.Slice(*args)
897 self.expr(ast.Subscript(x, sl, ast.Load()),
898 "must have Load context")
899 sl = ast.ExtSlice([])
900 self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
901 sl = ast.ExtSlice([ast.Index(s)])
902 self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
903
904 def test_starred(self):
905 left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
906 ast.Store())
907 assign = ast.Assign([left], ast.Num(4))
908 self.stmt(assign, "must have Store context")
909
910 def _sequence(self, fac):
911 self.expr(fac([None], ast.Load()), "None disallowed")
912 self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
913 "must have Load context")
914
915 def test_list(self):
916 self._sequence(ast.List)
917
918 def test_tuple(self):
919 self._sequence(ast.Tuple)
920
Benjamin Peterson442f2092012-12-06 17:41:04 -0500921 def test_nameconstant(self):
922 self.expr(ast.NameConstant(4), "singleton must be True, False, or None")
923
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500924 def test_stdlib_validates(self):
925 stdlib = os.path.dirname(ast.__file__)
926 tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
927 tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
928 for module in tests:
929 fn = os.path.join(stdlib, module)
930 with open(fn, "r", encoding="utf-8") as fp:
931 source = fp.read()
Victor Stinnerd502a072013-03-22 00:06:20 +0100932 mod = ast.parse(source, fn)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500933 compile(mod, fn, "exec")
934
935
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000936def main():
937 if __name__ != '__main__':
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000938 return
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000939 if sys.argv[1:] == ['-g']:
940 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
941 (eval_tests, "eval")):
942 print(kind+"_results = [")
943 for s in statements:
944 print(repr(to_tuple(compile(s, "?", kind, 0x400)))+",")
945 print("]")
946 print("main()")
947 raise SystemExit
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400948 unittest.main()
Tim Peters400cbc32006-02-28 18:44:41 +0000949
950#### EVERYTHING BELOW IS GENERATED #####
951exec_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -0500952('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700953('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]),
954('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]),
955('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
956('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
957('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500958('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [('arg', (1, 41), 'f', None)], [('Num', (1, 43), 42)], ('arg', (1, 49), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 58))], [], None)]),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400959('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]),
960('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700961('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000962('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
963('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000964('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000965('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
966('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
967('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -0500968('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
969('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',))), ('withitem', ('Name', (1, 13), 'z', ('Load',)), ('Name', (1, 18), 'q', ('Store',)))], [('Pass', (1, 21))])]),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400970('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]),
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500971('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
972('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000973('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
974('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
975('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000976('Module', [('Global', (1, 0), ['v'])]),
977('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
978('Module', [('Pass', (1, 0))]),
Yury Selivanovb3d53132015-09-01 16:10:49 -0400979('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
980('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +0000981('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
982('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
983('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500984('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [])]))]),
985('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [])]))]),
Benjamin Peterson58b53952015-09-25 22:44:43 -0700986('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), []), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))])]))]),
987('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [])]))]),
988('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))])]))]),
989('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [])]))]),
Yury Selivanov75445082015-05-11 22:57:16 -0400990('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
991('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 7), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]),
992('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 7), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]),
Benjamin Peterson58b53952015-09-25 22:44:43 -0700993('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Num', (1, 10), 2)], [('Dict', (1, 3), [('Num', (1, 4), 1)], [('Num', (1, 6), 2)]), ('Num', (1, 12), 3)]))]),
994('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Num', (1, 3), 1), ('Num', (1, 6), 2)]), ('Load',)), ('Num', (1, 10), 3)]))]),
Tim Peters400cbc32006-02-28 18:44:41 +0000995]
996single_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000997('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Tim Peters400cbc32006-02-28 18:44:41 +0000998]
999eval_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001000('Expression', ('NameConstant', (1, 0), None)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001001('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
1002('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
1003('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001004('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001005('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001006('Expression', ('Dict', (1, 0), [], [])),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001007('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
1008('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001009('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
1010('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
1011('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001012('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Num', (1, 8), 3)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])),
Neal Norwitzc1505362006-12-28 06:47:50 +00001013('Expression', ('Num', (1, 0), 10)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001014('Expression', ('Str', (1, 0), 'string')),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001015('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1016('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001017('Expression', ('Name', (1, 0), 'v', ('Load',))),
1018('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001019('Expression', ('List', (1, 0), [], ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001020('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001021('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1022('Expression', ('Tuple', (1, 0), [], ('Load',))),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001023('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [])),
Tim Peters400cbc32006-02-28 18:44:41 +00001024]
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001025main()