blob: 7d1649c1155c78f12080b2ef4e7872fb33785368 [file] [log] [blame]
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001import sys, unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Georg Brandl0c77a822008-06-10 16:37:50 +00003import ast
Tim Peters400cbc32006-02-28 18:44:41 +00004
5def to_tuple(t):
Guido van Rossum3172c5d2007-10-16 18:12:55 +00006 if t is None or isinstance(t, (str, int, complex)):
Tim Peters400cbc32006-02-28 18:44:41 +00007 return t
8 elif isinstance(t, list):
9 return [to_tuple(e) for e in t]
10 result = [t.__class__.__name__]
Martin v. Löwis49c5da12006-03-01 22:49:05 +000011 if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
12 result.append((t.lineno, t.col_offset))
Tim Peters400cbc32006-02-28 18:44:41 +000013 if t._fields is None:
14 return tuple(result)
15 for f in t._fields:
16 result.append(to_tuple(getattr(t, f)))
17 return tuple(result)
18
Neal Norwitzee9b10a2008-03-31 05:29:39 +000019
Tim Peters400cbc32006-02-28 18:44:41 +000020# These tests are compiled through "exec"
21# There should be atleast one test per statement
22exec_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050023 # None
24 "None",
Tim Peters400cbc32006-02-28 18:44:41 +000025 # FunctionDef
26 "def f(): pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050027 # FunctionDef with arg
28 "def f(a): pass",
29 # FunctionDef with arg and default value
30 "def f(a=0): pass",
31 # FunctionDef with varargs
32 "def f(*args): pass",
33 # FunctionDef with kwargs
34 "def f(**kwargs): pass",
35 # FunctionDef with all kind of args
36 "def f(a, b=1, c=None, d=[], e={}, *args, **kwargs): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000037 # ClassDef
38 "class C:pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050039 # ClassDef, new style class
40 "class C(object): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000041 # Return
42 "def f():return 1",
43 # Delete
44 "del v",
45 # Assign
46 "v = 1",
47 # AugAssign
48 "v += 1",
Tim Peters400cbc32006-02-28 18:44:41 +000049 # For
50 "for v in v:pass",
51 # While
52 "while v:pass",
53 # If
54 "if v:pass",
55 # Raise
Collin Winter828f04a2007-08-31 00:04:24 +000056 "raise Exception('string')",
Tim Peters400cbc32006-02-28 18:44:41 +000057 # TryExcept
58 "try:\n pass\nexcept Exception:\n pass",
59 # TryFinally
60 "try:\n pass\nfinally:\n pass",
61 # Assert
62 "assert v",
63 # Import
64 "import sys",
65 # ImportFrom
66 "from sys import v",
Tim Peters400cbc32006-02-28 18:44:41 +000067 # Global
68 "global v",
69 # Expr
70 "1",
71 # Pass,
72 "pass",
73 # Break
74 "break",
75 # Continue
76 "continue",
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +000077 # for statements with naked tuples (see http://bugs.python.org/issue6704)
78 "for a,b in c: pass",
79 "[(a,b) for a,b in c]",
80 "((a,b) for a,b in c)",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050081 "((a,b) for (a,b) in c)",
82 # Multiline generator expression (test for .lineno & .col_offset)
83 """(
84 (
85 Aa
86 ,
87 Bb
88 )
89 for
90 Aa
91 ,
92 Bb in Cc
93 )""",
94 # dictcomp
95 "{a : b for w in x for m in p if g}",
96 # dictcomp with naked tuple
97 "{a : b for v,w in x}",
98 # setcomp
99 "{r for l in x if g}",
100 # setcomp with naked tuple
101 "{r for l,m in x}",
Tim Peters400cbc32006-02-28 18:44:41 +0000102]
103
104# These are compiled through "single"
105# because of overlap with "eval", it just tests what
106# can't be tested with "eval"
107single_tests = [
108 "1+2"
109]
110
111# These are compiled through "eval"
112# It should test all expressions
113eval_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500114 # None
115 "None",
Tim Peters400cbc32006-02-28 18:44:41 +0000116 # BoolOp
117 "a and b",
118 # BinOp
119 "a + b",
120 # UnaryOp
121 "not v",
122 # Lambda
123 "lambda:None",
124 # Dict
125 "{ 1:2 }",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500126 # Empty dict
127 "{}",
128 # Set
129 "{None,}",
130 # Multiline dict (test for .lineno & .col_offset)
131 """{
132 1
133 :
134 2
135 }""",
Tim Peters400cbc32006-02-28 18:44:41 +0000136 # ListComp
137 "[a for b in c if d]",
138 # GeneratorExp
139 "(a for b in c if d)",
140 # Yield - yield expressions can't work outside a function
141 #
142 # Compare
143 "1 < 2 < 3",
144 # Call
145 "f(1,2,c=3,*d,**e)",
Tim Peters400cbc32006-02-28 18:44:41 +0000146 # Num
Guido van Rossume2a383d2007-01-15 16:59:06 +0000147 "10",
Tim Peters400cbc32006-02-28 18:44:41 +0000148 # Str
149 "'string'",
150 # Attribute
151 "a.b",
152 # Subscript
153 "a[b:c]",
154 # Name
155 "v",
156 # List
157 "[1,2,3]",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500158 # Empty list
159 "[]",
Tim Peters400cbc32006-02-28 18:44:41 +0000160 # Tuple
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000161 "1,2,3",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500162 # Tuple
163 "(1,2,3)",
164 # Empty tuple
165 "()",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000166 # Combination
167 "a.b.c.d(a.b[1:2])",
168
Tim Peters400cbc32006-02-28 18:44:41 +0000169]
170
171# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
172# excepthandler, arguments, keywords, alias
173
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000174class AST_Tests(unittest.TestCase):
Tim Peters400cbc32006-02-28 18:44:41 +0000175
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000176 def _assertTrueorder(self, ast_node, parent_pos):
Georg Brandl0c77a822008-06-10 16:37:50 +0000177 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000178 return
Georg Brandl0c77a822008-06-10 16:37:50 +0000179 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000180 node_pos = (ast_node.lineno, ast_node.col_offset)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000181 self.assertTrue(node_pos >= parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000182 parent_pos = (ast_node.lineno, ast_node.col_offset)
183 for name in ast_node._fields:
184 value = getattr(ast_node, name)
185 if isinstance(value, list):
186 for child in value:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000187 self._assertTrueorder(child, parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000188 elif value is not None:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000189 self._assertTrueorder(value, parent_pos)
Tim Peters5ddfe412006-03-01 23:02:57 +0000190
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500191 def test_AST_objects(self):
192 x = ast.AST()
193 self.assertEqual(x._fields, ())
194
195 with self.assertRaises(AttributeError):
196 x.vararg
197
198 with self.assertRaises(AttributeError):
199 x.foobar = 21
200
201 with self.assertRaises(AttributeError):
202 ast.AST(lineno=2)
203
204 with self.assertRaises(TypeError):
205 # "_ast.AST constructor takes 0 positional arguments"
206 ast.AST(2)
207
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000208 def test_snippets(self):
209 for input, output, kind in ((exec_tests, exec_results, "exec"),
210 (single_tests, single_results, "single"),
211 (eval_tests, eval_results, "eval")):
212 for i, o in zip(input, output):
Georg Brandl0c77a822008-06-10 16:37:50 +0000213 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000214 self.assertEqual(to_tuple(ast_tree), o)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000215 self._assertTrueorder(ast_tree, (0, 0))
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000216
Benjamin Peterson78565b22009-06-28 19:19:51 +0000217 def test_slice(self):
218 slc = ast.parse("x[::]").body[0].value.slice
219 self.assertIsNone(slc.upper)
220 self.assertIsNone(slc.lower)
221 self.assertIsNone(slc.step)
222
223 def test_from_import(self):
224 im = ast.parse("from . import y").body[0]
225 self.assertIsNone(im.module)
226
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000227 def test_base_classes(self):
228 self.assertTrue(issubclass(ast.For, ast.stmt))
229 self.assertTrue(issubclass(ast.Name, ast.expr))
230 self.assertTrue(issubclass(ast.stmt, ast.AST))
231 self.assertTrue(issubclass(ast.expr, ast.AST))
232 self.assertTrue(issubclass(ast.comprehension, ast.AST))
233 self.assertTrue(issubclass(ast.Gt, ast.AST))
234
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500235 def test_field_attr_existence(self):
236 for name, item in ast.__dict__.items():
237 if isinstance(item, type) and name != 'AST' and name[0].isupper():
238 x = item()
239 if isinstance(x, ast.AST):
240 self.assertEqual(type(x._fields), tuple)
241
242 def test_arguments(self):
243 x = ast.arguments()
244 self.assertEqual(x._fields, ('args', 'vararg', 'varargannotation',
245 'kwonlyargs', 'kwarg', 'kwargannotation',
246 'defaults', 'kw_defaults'))
247
248 with self.assertRaises(AttributeError):
249 x.vararg
250
251 x = ast.arguments(*range(1, 9))
252 self.assertEqual(x.vararg, 2)
253
254 def test_field_attr_writable(self):
255 x = ast.Num()
256 # We can assign to _fields
257 x._fields = 666
258 self.assertEqual(x._fields, 666)
259
260 def test_classattrs(self):
261 x = ast.Num()
262 self.assertEqual(x._fields, ('n',))
263
264 with self.assertRaises(AttributeError):
265 x.n
266
267 x = ast.Num(42)
268 self.assertEqual(x.n, 42)
269
270 with self.assertRaises(AttributeError):
271 x.lineno
272
273 with self.assertRaises(AttributeError):
274 x.foobar
275
276 x = ast.Num(lineno=2)
277 self.assertEqual(x.lineno, 2)
278
279 x = ast.Num(42, lineno=0)
280 self.assertEqual(x.lineno, 0)
281 self.assertEqual(x._fields, ('n',))
282 self.assertEqual(x.n, 42)
283
284 self.assertRaises(TypeError, ast.Num, 1, 2)
285 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
286
287 def test_module(self):
288 body = [ast.Num(42)]
289 x = ast.Module(body)
290 self.assertEqual(x.body, body)
291
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000292 def test_nodeclasses(self):
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500293 # Zero arguments constructor explicitely allowed
294 x = ast.BinOp()
295 self.assertEqual(x._fields, ('left', 'op', 'right'))
296
297 # Random attribute allowed too
298 x.foobarbaz = 5
299 self.assertEqual(x.foobarbaz, 5)
300
301 n1 = ast.Num(1)
302 n3 = ast.Num(3)
303 addop = ast.Add()
304 x = ast.BinOp(n1, addop, n3)
305 self.assertEqual(x.left, n1)
306 self.assertEqual(x.op, addop)
307 self.assertEqual(x.right, n3)
Benjamin Peterson68b543a2011-06-27 17:51:18 -0500308
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500309 x = ast.BinOp(1, 2, 3)
310 self.assertEqual(x.left, 1)
311 self.assertEqual(x.op, 2)
312 self.assertEqual(x.right, 3)
313
Georg Brandl0c77a822008-06-10 16:37:50 +0000314 x = ast.BinOp(1, 2, 3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000315 self.assertEqual(x.left, 1)
316 self.assertEqual(x.op, 2)
317 self.assertEqual(x.right, 3)
318 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000319
320 # node raises exception when not given enough arguments
Georg Brandl0c77a822008-06-10 16:37:50 +0000321 self.assertRaises(TypeError, ast.BinOp, 1, 2)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500322 # node raises exception when given too many arguments
323 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
324 # node raises exception when not given enough arguments
325 self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
326 # node raises exception when given too many arguments
327 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000328
329 # can set attributes through kwargs too
Georg Brandl0c77a822008-06-10 16:37:50 +0000330 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000331 self.assertEqual(x.left, 1)
332 self.assertEqual(x.op, 2)
333 self.assertEqual(x.right, 3)
334 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000335
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500336 # Random kwargs also allowed
337 x = ast.BinOp(1, 2, 3, foobarbaz=42)
338 self.assertEqual(x.foobarbaz, 42)
339
340 def test_no_fields(self):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000341 # this used to fail because Sub._fields was None
Georg Brandl0c77a822008-06-10 16:37:50 +0000342 x = ast.Sub()
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500343 self.assertEqual(x._fields, ())
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000344
345 def test_pickling(self):
346 import pickle
347 mods = [pickle]
348 try:
349 import cPickle
350 mods.append(cPickle)
351 except ImportError:
352 pass
353 protocols = [0, 1, 2]
354 for mod in mods:
355 for protocol in protocols:
356 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
357 ast2 = mod.loads(mod.dumps(ast, protocol))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000358 self.assertEqual(to_tuple(ast2), to_tuple(ast))
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000359
Benjamin Peterson5b066812010-11-20 01:38:49 +0000360 def test_invalid_sum(self):
361 pos = dict(lineno=2, col_offset=3)
362 m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
363 with self.assertRaises(TypeError) as cm:
364 compile(m, "<test>", "exec")
365 self.assertIn("but got <_ast.expr", str(cm.exception))
366
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500367 def test_invalid_identitifer(self):
368 m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
369 ast.fix_missing_locations(m)
370 with self.assertRaises(TypeError) as cm:
371 compile(m, "<test>", "exec")
372 self.assertIn("identifier must be of type str", str(cm.exception))
373
374 def test_invalid_string(self):
375 m = ast.Module([ast.Expr(ast.Str(42))])
376 ast.fix_missing_locations(m)
377 with self.assertRaises(TypeError) as cm:
378 compile(m, "<test>", "exec")
379 self.assertIn("string must be of type str", str(cm.exception))
380
Georg Brandl0c77a822008-06-10 16:37:50 +0000381
382class ASTHelpers_Test(unittest.TestCase):
383
384 def test_parse(self):
385 a = ast.parse('foo(1 + 1)')
386 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
387 self.assertEqual(ast.dump(a), ast.dump(b))
388
389 def test_dump(self):
390 node = ast.parse('spam(eggs, "and cheese")')
391 self.assertEqual(ast.dump(node),
392 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
393 "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
394 "keywords=[], starargs=None, kwargs=None))])"
395 )
396 self.assertEqual(ast.dump(node, annotate_fields=False),
397 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
398 "Str('and cheese')], [], None, None))])"
399 )
400 self.assertEqual(ast.dump(node, include_attributes=True),
401 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
402 "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
403 "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
404 "col_offset=11)], keywords=[], starargs=None, kwargs=None, "
405 "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
406 )
407
408 def test_copy_location(self):
409 src = ast.parse('1 + 1', mode='eval')
410 src.body.right = ast.copy_location(ast.Num(2), src.body.right)
411 self.assertEqual(ast.dump(src, include_attributes=True),
412 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
413 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
414 'col_offset=0))'
415 )
416
417 def test_fix_missing_locations(self):
418 src = ast.parse('write("spam")')
419 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
420 [ast.Str('eggs')], [], None, None)))
421 self.assertEqual(src, ast.fix_missing_locations(src))
422 self.assertEqual(ast.dump(src, include_attributes=True),
423 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
424 "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
425 "col_offset=6)], keywords=[], starargs=None, kwargs=None, "
426 "lineno=1, col_offset=0), lineno=1, col_offset=0), "
427 "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
428 "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
429 "keywords=[], starargs=None, kwargs=None, lineno=1, "
430 "col_offset=0), lineno=1, col_offset=0)])"
431 )
432
433 def test_increment_lineno(self):
434 src = ast.parse('1 + 1', mode='eval')
435 self.assertEqual(ast.increment_lineno(src, n=3), src)
436 self.assertEqual(ast.dump(src, include_attributes=True),
437 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
438 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
439 'col_offset=0))'
440 )
Georg Brandl619e7ba2011-01-09 07:38:51 +0000441 # issue10869: do not increment lineno of root twice
Georg Brandlefb69022011-01-09 07:50:48 +0000442 src = ast.parse('1 + 1', mode='eval')
Georg Brandl619e7ba2011-01-09 07:38:51 +0000443 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
444 self.assertEqual(ast.dump(src, include_attributes=True),
445 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
446 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
447 'col_offset=0))'
448 )
Georg Brandl0c77a822008-06-10 16:37:50 +0000449
450 def test_iter_fields(self):
451 node = ast.parse('foo()', mode='eval')
452 d = dict(ast.iter_fields(node.body))
453 self.assertEqual(d.pop('func').id, 'foo')
454 self.assertEqual(d, {'keywords': [], 'kwargs': None,
455 'args': [], 'starargs': None})
456
457 def test_iter_child_nodes(self):
458 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
459 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
460 iterator = ast.iter_child_nodes(node.body)
461 self.assertEqual(next(iterator).id, 'spam')
462 self.assertEqual(next(iterator).n, 23)
463 self.assertEqual(next(iterator).n, 42)
464 self.assertEqual(ast.dump(next(iterator)),
465 "keyword(arg='eggs', value=Str(s='leek'))"
466 )
467
468 def test_get_docstring(self):
469 node = ast.parse('def foo():\n """line one\n line two"""')
470 self.assertEqual(ast.get_docstring(node.body[0]),
471 'line one\nline two')
472
473 def test_literal_eval(self):
474 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
475 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
476 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
Benjamin Peterson3e742892010-07-11 12:59:24 +0000477 self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
Benjamin Peterson5ef96e52010-07-11 23:06:06 +0000478 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
Georg Brandl0c77a822008-06-10 16:37:50 +0000479 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
Raymond Hettingerbc959732010-10-08 00:47:45 +0000480 self.assertEqual(ast.literal_eval('-6'), -6)
481 self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
482 self.assertEqual(ast.literal_eval('3.25'), 3.25)
Georg Brandl0c77a822008-06-10 16:37:50 +0000483
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000484 def test_literal_eval_issue4907(self):
485 self.assertEqual(ast.literal_eval('2j'), 2j)
486 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
487 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000488
Georg Brandl0c77a822008-06-10 16:37:50 +0000489
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000490def test_main():
Georg Brandl0c77a822008-06-10 16:37:50 +0000491 support.run_unittest(AST_Tests, ASTHelpers_Test)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000492
493def main():
494 if __name__ != '__main__':
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000495 return
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000496 if sys.argv[1:] == ['-g']:
497 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
498 (eval_tests, "eval")):
499 print(kind+"_results = [")
500 for s in statements:
501 print(repr(to_tuple(compile(s, "?", kind, 0x400)))+",")
502 print("]")
503 print("main()")
504 raise SystemExit
505 test_main()
Tim Peters400cbc32006-02-28 18:44:41 +0000506
507#### EVERYTHING BELOW IS GENERATED #####
508exec_results = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500509('Module', [('Expr', (1, 0), ('Name', (1, 0), 'None', ('Load',)))]),
Neal Norwitzc1505362006-12-28 06:47:50 +0000510('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Pass', (1, 9))], [], None)]),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500511('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', 'a', None)], None, None, [], None, None, [], []), [('Pass', (1, 10))], [], None)]),
512('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', 'a', None)], None, None, [], None, None, [('Num', (1, 8), 0)], []), [('Pass', (1, 12))], [], None)]),
513('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', None, [], None, None, [], []), [('Pass', (1, 14))], [], None)]),
514('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], 'kwargs', None, [], []), [('Pass', (1, 17))], [], None)]),
515('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', 'a', None), ('arg', 'b', None), ('arg', 'c', None), ('arg', 'd', None), ('arg', 'e', None)], 'args', None, [], 'kwargs', None, [('Num', (1, 11), 1), ('Name', (1, 16), 'None', ('Load',)), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])], []), [('Pass', (1, 52))], [], None)]),
Neal Norwitzad74aa82008-03-31 05:14:30 +0000516('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500517('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], None, None, [('Pass', (1, 17))], [])]),
Neal Norwitzc1505362006-12-28 06:47:50 +0000518('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000519('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
520('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000522('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
523('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
524('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
Collin Winter828f04a2007-08-31 00:04:24 +0000525('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
Neal Norwitzad74aa82008-03-31 05:14:30 +0000526('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000527('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
528('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
529('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
530('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000531('Module', [('Global', (1, 0), ['v'])]),
532('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
533('Module', [('Pass', (1, 0))]),
534('Module', [('Break', (1, 0))]),
535('Module', [('Continue', (1, 0))]),
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +0000536('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))], [])]),
537('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',)), [])]))]),
538('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 -0500539('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',)), [])]))]),
540('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',)), [])]))]),
541('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('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',))])]))]),
542('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('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',)), [])]))]),
543('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))])]))]),
544('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('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',)), [])]))]),
Tim Peters400cbc32006-02-28 18:44:41 +0000545]
546single_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000547('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Tim Peters400cbc32006-02-28 18:44:41 +0000548]
549eval_results = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500550('Expression', ('Name', (1, 0), 'None', ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000551('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
552('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
553('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
Neal Norwitzc1505362006-12-28 06:47:50 +0000554('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, [], None, None, [], []), ('Name', (1, 7), 'None', ('Load',)))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000555('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500556('Expression', ('Dict', (1, 0), [], [])),
557('Expression', ('Set', (1, 0), [('Name', (1, 1), 'None', ('Load',))])),
558('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000559('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',))])])),
560('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',))])])),
561('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
562('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
Neal Norwitzc1505362006-12-28 06:47:50 +0000563('Expression', ('Num', (1, 0), 10)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000564('Expression', ('Str', (1, 0), 'string')),
565('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
566('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
567('Expression', ('Name', (1, 0), 'v', ('Load',))),
568('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500569('Expression', ('List', (1, 0), [], ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000570('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500571('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
572('Expression', ('Tuple', (1, 0), [], ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000573('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',))], [], None, None)),
Tim Peters400cbc32006-02-28 18:44:41 +0000574]
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000575main()