blob: 8c62408bd8a392d722d3b8278a42712c584e7c5c [file] [log] [blame]
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001import ast
2import dis
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003import os
4import sys
5import unittest
Benjamin Peterson9ed37432012-07-08 11:13:36 -07006import weakref
7
8from test import support
Tim Peters400cbc32006-02-28 18:44:41 +00009
10def to_tuple(t):
Guido van Rossum3172c5d2007-10-16 18:12:55 +000011 if t is None or isinstance(t, (str, int, complex)):
Tim Peters400cbc32006-02-28 18:44:41 +000012 return t
13 elif isinstance(t, list):
14 return [to_tuple(e) for e in t]
15 result = [t.__class__.__name__]
Martin v. Löwis49c5da12006-03-01 22:49:05 +000016 if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
17 result.append((t.lineno, t.col_offset))
Tim Peters400cbc32006-02-28 18:44:41 +000018 if t._fields is None:
19 return tuple(result)
20 for f in t._fields:
21 result.append(to_tuple(getattr(t, f)))
22 return tuple(result)
23
Neal Norwitzee9b10a2008-03-31 05:29:39 +000024
Tim Peters400cbc32006-02-28 18:44:41 +000025# These tests are compiled through "exec"
Ezio Melotti85a86292013-08-17 16:57:41 +030026# There should be at least one test per statement
Tim Peters400cbc32006-02-28 18:44:41 +000027exec_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050028 # None
29 "None",
Tim Peters400cbc32006-02-28 18:44:41 +000030 # FunctionDef
31 "def f(): pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050032 # FunctionDef with arg
33 "def f(a): pass",
34 # FunctionDef with arg and default value
35 "def f(a=0): pass",
36 # FunctionDef with varargs
37 "def f(*args): pass",
38 # FunctionDef with kwargs
39 "def f(**kwargs): pass",
40 # FunctionDef with all kind of args
Benjamin Petersone84fde92014-02-13 19:22:14 -050041 "def f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000042 # ClassDef
43 "class C:pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050044 # ClassDef, new style class
45 "class C(object): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000046 # Return
47 "def f():return 1",
48 # Delete
49 "del v",
50 # Assign
51 "v = 1",
52 # AugAssign
53 "v += 1",
Tim Peters400cbc32006-02-28 18:44:41 +000054 # For
55 "for v in v:pass",
56 # While
57 "while v:pass",
58 # If
59 "if v:pass",
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -050060 # With
61 "with x as y: pass",
62 "with x as y, z as q: pass",
Tim Peters400cbc32006-02-28 18:44:41 +000063 # Raise
Collin Winter828f04a2007-08-31 00:04:24 +000064 "raise Exception('string')",
Tim Peters400cbc32006-02-28 18:44:41 +000065 # TryExcept
66 "try:\n pass\nexcept Exception:\n pass",
67 # TryFinally
68 "try:\n pass\nfinally:\n pass",
69 # Assert
70 "assert v",
71 # Import
72 "import sys",
73 # ImportFrom
74 "from sys import v",
Tim Peters400cbc32006-02-28 18:44:41 +000075 # Global
76 "global v",
77 # Expr
78 "1",
79 # Pass,
80 "pass",
81 # Break
Yury Selivanovb3d53132015-09-01 16:10:49 -040082 "for v in v:break",
Tim Peters400cbc32006-02-28 18:44:41 +000083 # Continue
Yury Selivanovb3d53132015-09-01 16:10:49 -040084 "for v in v:continue",
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +000085 # for statements with naked tuples (see http://bugs.python.org/issue6704)
86 "for a,b in c: pass",
87 "[(a,b) for a,b in c]",
88 "((a,b) for a,b in c)",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050089 "((a,b) for (a,b) in c)",
90 # Multiline generator expression (test for .lineno & .col_offset)
91 """(
92 (
93 Aa
94 ,
95 Bb
96 )
97 for
98 Aa
99 ,
100 Bb in Cc
101 )""",
102 # dictcomp
103 "{a : b for w in x for m in p if g}",
104 # dictcomp with naked tuple
105 "{a : b for v,w in x}",
106 # setcomp
107 "{r for l in x if g}",
108 # setcomp with naked tuple
109 "{r for l,m in x}",
Yury Selivanov75445082015-05-11 22:57:16 -0400110 # AsyncFunctionDef
111 "async def f():\n await something()",
112 # AsyncFor
113 "async def f():\n async for e in i: 1\n else: 2",
114 # AsyncWith
115 "async def f():\n async with a as b: 1",
Yury Selivanovb3d53132015-09-01 16:10:49 -0400116 # PEP 448: Additional Unpacking Generalizations
117 "{**{1:2}, 2:3}",
118 "{*{1, 2}, 3}",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700119 # Asynchronous comprehensions
120 "async def f():\n [i async for b in c]",
Tim Peters400cbc32006-02-28 18:44:41 +0000121]
122
123# These are compiled through "single"
124# because of overlap with "eval", it just tests what
125# can't be tested with "eval"
126single_tests = [
127 "1+2"
128]
129
130# These are compiled through "eval"
131# It should test all expressions
132eval_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500133 # None
134 "None",
Tim Peters400cbc32006-02-28 18:44:41 +0000135 # BoolOp
136 "a and b",
137 # BinOp
138 "a + b",
139 # UnaryOp
140 "not v",
141 # Lambda
142 "lambda:None",
143 # Dict
144 "{ 1:2 }",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500145 # Empty dict
146 "{}",
147 # Set
148 "{None,}",
149 # Multiline dict (test for .lineno & .col_offset)
150 """{
151 1
152 :
153 2
154 }""",
Tim Peters400cbc32006-02-28 18:44:41 +0000155 # ListComp
156 "[a for b in c if d]",
157 # GeneratorExp
158 "(a for b in c if d)",
159 # Yield - yield expressions can't work outside a function
160 #
161 # Compare
162 "1 < 2 < 3",
163 # Call
164 "f(1,2,c=3,*d,**e)",
Tim Peters400cbc32006-02-28 18:44:41 +0000165 # Num
Guido van Rossume2a383d2007-01-15 16:59:06 +0000166 "10",
Tim Peters400cbc32006-02-28 18:44:41 +0000167 # Str
168 "'string'",
169 # Attribute
170 "a.b",
171 # Subscript
172 "a[b:c]",
173 # Name
174 "v",
175 # List
176 "[1,2,3]",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500177 # Empty list
178 "[]",
Tim Peters400cbc32006-02-28 18:44:41 +0000179 # Tuple
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000180 "1,2,3",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500181 # Tuple
182 "(1,2,3)",
183 # Empty tuple
184 "()",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000185 # Combination
186 "a.b.c.d(a.b[1:2])",
187
Tim Peters400cbc32006-02-28 18:44:41 +0000188]
189
190# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
191# excepthandler, arguments, keywords, alias
192
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000193class AST_Tests(unittest.TestCase):
Tim Peters400cbc32006-02-28 18:44:41 +0000194
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500195 def _assertTrueorder(self, ast_node, parent_pos):
Georg Brandl0c77a822008-06-10 16:37:50 +0000196 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000197 return
Georg Brandl0c77a822008-06-10 16:37:50 +0000198 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000199 node_pos = (ast_node.lineno, ast_node.col_offset)
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500200 self.assertTrue(node_pos >= parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000201 parent_pos = (ast_node.lineno, ast_node.col_offset)
202 for name in ast_node._fields:
203 value = getattr(ast_node, name)
204 if isinstance(value, list):
205 for child in value:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500206 self._assertTrueorder(child, parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000207 elif value is not None:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500208 self._assertTrueorder(value, parent_pos)
Tim Peters5ddfe412006-03-01 23:02:57 +0000209
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500210 def test_AST_objects(self):
211 x = ast.AST()
212 self.assertEqual(x._fields, ())
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700213 x.foobar = 42
214 self.assertEqual(x.foobar, 42)
215 self.assertEqual(x.__dict__["foobar"], 42)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500216
217 with self.assertRaises(AttributeError):
218 x.vararg
219
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500220 with self.assertRaises(TypeError):
221 # "_ast.AST constructor takes 0 positional arguments"
222 ast.AST(2)
223
Benjamin Peterson9ed37432012-07-08 11:13:36 -0700224 def test_AST_garbage_collection(self):
225 class X:
226 pass
227 a = ast.AST()
228 a.x = X()
229 a.x.a = a
230 ref = weakref.ref(a.x)
231 del a
232 support.gc_collect()
233 self.assertIsNone(ref())
234
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000235 def test_snippets(self):
236 for input, output, kind in ((exec_tests, exec_results, "exec"),
237 (single_tests, single_results, "single"),
238 (eval_tests, eval_results, "eval")):
239 for i, o in zip(input, output):
Yury Selivanovb3d53132015-09-01 16:10:49 -0400240 with self.subTest(action="parsing", input=i):
241 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
242 self.assertEqual(to_tuple(ast_tree), o)
243 self._assertTrueorder(ast_tree, (0, 0))
Victor Stinner15a30952016-02-08 22:45:06 +0100244 with self.subTest(action="compiling", input=i, kind=kind):
245 compile(ast_tree, "?", kind)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000246
Benjamin Peterson78565b22009-06-28 19:19:51 +0000247 def test_slice(self):
248 slc = ast.parse("x[::]").body[0].value.slice
249 self.assertIsNone(slc.upper)
250 self.assertIsNone(slc.lower)
251 self.assertIsNone(slc.step)
252
253 def test_from_import(self):
254 im = ast.parse("from . import y").body[0]
255 self.assertIsNone(im.module)
256
Benjamin Petersona4e4e352012-03-22 08:19:04 -0400257 def test_non_interned_future_from_ast(self):
258 mod = ast.parse("from __future__ import division")
259 self.assertIsInstance(mod.body[0], ast.ImportFrom)
260 mod.body[0].module = " __future__ ".strip()
261 compile(mod, "<test>", "exec")
262
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000263 def test_base_classes(self):
264 self.assertTrue(issubclass(ast.For, ast.stmt))
265 self.assertTrue(issubclass(ast.Name, ast.expr))
266 self.assertTrue(issubclass(ast.stmt, ast.AST))
267 self.assertTrue(issubclass(ast.expr, ast.AST))
268 self.assertTrue(issubclass(ast.comprehension, ast.AST))
269 self.assertTrue(issubclass(ast.Gt, ast.AST))
270
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500271 def test_field_attr_existence(self):
272 for name, item in ast.__dict__.items():
273 if isinstance(item, type) and name != 'AST' and name[0].isupper():
274 x = item()
275 if isinstance(x, ast.AST):
276 self.assertEqual(type(x._fields), tuple)
277
278 def test_arguments(self):
279 x = ast.arguments()
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500280 self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs',
281 'kw_defaults', 'kwarg', 'defaults'))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500282
283 with self.assertRaises(AttributeError):
284 x.vararg
285
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700286 x = ast.arguments(*range(1, 7))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500287 self.assertEqual(x.vararg, 2)
288
289 def test_field_attr_writable(self):
290 x = ast.Num()
291 # We can assign to _fields
292 x._fields = 666
293 self.assertEqual(x._fields, 666)
294
295 def test_classattrs(self):
296 x = ast.Num()
297 self.assertEqual(x._fields, ('n',))
298
299 with self.assertRaises(AttributeError):
300 x.n
301
302 x = ast.Num(42)
303 self.assertEqual(x.n, 42)
304
305 with self.assertRaises(AttributeError):
306 x.lineno
307
308 with self.assertRaises(AttributeError):
309 x.foobar
310
311 x = ast.Num(lineno=2)
312 self.assertEqual(x.lineno, 2)
313
314 x = ast.Num(42, lineno=0)
315 self.assertEqual(x.lineno, 0)
316 self.assertEqual(x._fields, ('n',))
317 self.assertEqual(x.n, 42)
318
319 self.assertRaises(TypeError, ast.Num, 1, 2)
320 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
321
322 def test_module(self):
323 body = [ast.Num(42)]
324 x = ast.Module(body)
325 self.assertEqual(x.body, body)
326
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000327 def test_nodeclasses(self):
Florent Xicluna992d9e02011-11-11 19:35:42 +0100328 # Zero arguments constructor explicitly allowed
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500329 x = ast.BinOp()
330 self.assertEqual(x._fields, ('left', 'op', 'right'))
331
332 # Random attribute allowed too
333 x.foobarbaz = 5
334 self.assertEqual(x.foobarbaz, 5)
335
336 n1 = ast.Num(1)
337 n3 = ast.Num(3)
338 addop = ast.Add()
339 x = ast.BinOp(n1, addop, n3)
340 self.assertEqual(x.left, n1)
341 self.assertEqual(x.op, addop)
342 self.assertEqual(x.right, n3)
Benjamin Peterson68b543a2011-06-27 17:51:18 -0500343
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500344 x = ast.BinOp(1, 2, 3)
345 self.assertEqual(x.left, 1)
346 self.assertEqual(x.op, 2)
347 self.assertEqual(x.right, 3)
348
Georg Brandl0c77a822008-06-10 16:37:50 +0000349 x = ast.BinOp(1, 2, 3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000350 self.assertEqual(x.left, 1)
351 self.assertEqual(x.op, 2)
352 self.assertEqual(x.right, 3)
353 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000354
355 # node raises exception when not given enough arguments
Georg Brandl0c77a822008-06-10 16:37:50 +0000356 self.assertRaises(TypeError, ast.BinOp, 1, 2)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500357 # node raises exception when given too many arguments
358 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
359 # node raises exception when not given enough arguments
360 self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
361 # node raises exception when given too many arguments
362 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000363
364 # can set attributes through kwargs too
Georg Brandl0c77a822008-06-10 16:37:50 +0000365 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000366 self.assertEqual(x.left, 1)
367 self.assertEqual(x.op, 2)
368 self.assertEqual(x.right, 3)
369 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000370
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500371 # Random kwargs also allowed
372 x = ast.BinOp(1, 2, 3, foobarbaz=42)
373 self.assertEqual(x.foobarbaz, 42)
374
375 def test_no_fields(self):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000376 # this used to fail because Sub._fields was None
Georg Brandl0c77a822008-06-10 16:37:50 +0000377 x = ast.Sub()
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500378 self.assertEqual(x._fields, ())
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000379
380 def test_pickling(self):
381 import pickle
382 mods = [pickle]
383 try:
384 import cPickle
385 mods.append(cPickle)
386 except ImportError:
387 pass
388 protocols = [0, 1, 2]
389 for mod in mods:
390 for protocol in protocols:
391 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
392 ast2 = mod.loads(mod.dumps(ast, protocol))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000393 self.assertEqual(to_tuple(ast2), to_tuple(ast))
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000394
Benjamin Peterson5b066812010-11-20 01:38:49 +0000395 def test_invalid_sum(self):
396 pos = dict(lineno=2, col_offset=3)
397 m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
398 with self.assertRaises(TypeError) as cm:
399 compile(m, "<test>", "exec")
400 self.assertIn("but got <_ast.expr", str(cm.exception))
401
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500402 def test_invalid_identitifer(self):
403 m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
404 ast.fix_missing_locations(m)
405 with self.assertRaises(TypeError) as cm:
406 compile(m, "<test>", "exec")
407 self.assertIn("identifier must be of type str", str(cm.exception))
408
409 def test_invalid_string(self):
410 m = ast.Module([ast.Expr(ast.Str(42))])
411 ast.fix_missing_locations(m)
412 with self.assertRaises(TypeError) as cm:
413 compile(m, "<test>", "exec")
414 self.assertIn("string must be of type str", str(cm.exception))
415
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000416 def test_empty_yield_from(self):
417 # Issue 16546: yield from value is not optional.
418 empty_yield_from = ast.parse("def f():\n yield from g()")
419 empty_yield_from.body[0].body[0].value.value = None
420 with self.assertRaises(ValueError) as cm:
421 compile(empty_yield_from, "<test>", "exec")
422 self.assertIn("field value is required", str(cm.exception))
423
Georg Brandl0c77a822008-06-10 16:37:50 +0000424
425class ASTHelpers_Test(unittest.TestCase):
426
427 def test_parse(self):
428 a = ast.parse('foo(1 + 1)')
429 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
430 self.assertEqual(ast.dump(a), ast.dump(b))
431
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400432 def test_parse_in_error(self):
433 try:
434 1/0
435 except Exception:
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400436 with self.assertRaises(SyntaxError) as e:
437 ast.literal_eval(r"'\U'")
438 self.assertIsNotNone(e.exception.__context__)
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400439
Georg Brandl0c77a822008-06-10 16:37:50 +0000440 def test_dump(self):
441 node = ast.parse('spam(eggs, "and cheese")')
442 self.assertEqual(ast.dump(node),
443 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
444 "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400445 "keywords=[]))])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000446 )
447 self.assertEqual(ast.dump(node, annotate_fields=False),
448 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400449 "Str('and cheese')], []))])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000450 )
451 self.assertEqual(ast.dump(node, include_attributes=True),
452 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
453 "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
454 "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400455 "col_offset=11)], keywords=[], "
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500456 "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000457 )
458
459 def test_copy_location(self):
460 src = ast.parse('1 + 1', mode='eval')
461 src.body.right = ast.copy_location(ast.Num(2), src.body.right)
462 self.assertEqual(ast.dump(src, include_attributes=True),
463 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
464 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
465 'col_offset=0))'
466 )
467
468 def test_fix_missing_locations(self):
469 src = ast.parse('write("spam")')
470 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400471 [ast.Str('eggs')], [])))
Georg Brandl0c77a822008-06-10 16:37:50 +0000472 self.assertEqual(src, ast.fix_missing_locations(src))
473 self.assertEqual(ast.dump(src, include_attributes=True),
474 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
475 "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400476 "col_offset=6)], keywords=[], "
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500477 "lineno=1, col_offset=0), lineno=1, col_offset=0), "
Georg Brandl0c77a822008-06-10 16:37:50 +0000478 "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
479 "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400480 "keywords=[], lineno=1, "
Georg Brandl0c77a822008-06-10 16:37:50 +0000481 "col_offset=0), lineno=1, col_offset=0)])"
482 )
483
484 def test_increment_lineno(self):
485 src = ast.parse('1 + 1', mode='eval')
486 self.assertEqual(ast.increment_lineno(src, n=3), src)
487 self.assertEqual(ast.dump(src, include_attributes=True),
488 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
489 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
490 'col_offset=0))'
491 )
Georg Brandl619e7ba2011-01-09 07:38:51 +0000492 # issue10869: do not increment lineno of root twice
Georg Brandlefb69022011-01-09 07:50:48 +0000493 src = ast.parse('1 + 1', mode='eval')
Georg Brandl619e7ba2011-01-09 07:38:51 +0000494 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
495 self.assertEqual(ast.dump(src, include_attributes=True),
496 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
497 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
498 'col_offset=0))'
499 )
Georg Brandl0c77a822008-06-10 16:37:50 +0000500
501 def test_iter_fields(self):
502 node = ast.parse('foo()', mode='eval')
503 d = dict(ast.iter_fields(node.body))
504 self.assertEqual(d.pop('func').id, 'foo')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400505 self.assertEqual(d, {'keywords': [], 'args': []})
Georg Brandl0c77a822008-06-10 16:37:50 +0000506
507 def test_iter_child_nodes(self):
508 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
509 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
510 iterator = ast.iter_child_nodes(node.body)
511 self.assertEqual(next(iterator).id, 'spam')
512 self.assertEqual(next(iterator).n, 23)
513 self.assertEqual(next(iterator).n, 42)
514 self.assertEqual(ast.dump(next(iterator)),
515 "keyword(arg='eggs', value=Str(s='leek'))"
516 )
517
518 def test_get_docstring(self):
519 node = ast.parse('def foo():\n """line one\n line two"""')
520 self.assertEqual(ast.get_docstring(node.body[0]),
521 'line one\nline two')
522
Yury Selivanov2f07a662015-07-23 08:54:35 +0300523 node = ast.parse('async def foo():\n """spam\n ham"""')
524 self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
525
Georg Brandl0c77a822008-06-10 16:37:50 +0000526 def test_literal_eval(self):
527 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
528 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
529 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
Benjamin Peterson3e742892010-07-11 12:59:24 +0000530 self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
Benjamin Peterson5ef96e52010-07-11 23:06:06 +0000531 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
Georg Brandl0c77a822008-06-10 16:37:50 +0000532 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
Raymond Hettingerbc959732010-10-08 00:47:45 +0000533 self.assertEqual(ast.literal_eval('-6'), -6)
534 self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
535 self.assertEqual(ast.literal_eval('3.25'), 3.25)
Georg Brandl0c77a822008-06-10 16:37:50 +0000536
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000537 def test_literal_eval_issue4907(self):
538 self.assertEqual(ast.literal_eval('2j'), 2j)
539 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
540 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000541
Amaury Forgeot d'Arc58e87612011-11-22 21:51:55 +0100542 def test_bad_integer(self):
543 # issue13436: Bad error message with invalid numeric values
544 body = [ast.ImportFrom(module='time',
545 names=[ast.alias(name='sleep')],
546 level=None,
547 lineno=None, col_offset=None)]
548 mod = ast.Module(body)
549 with self.assertRaises(ValueError) as cm:
550 compile(mod, 'test', 'exec')
551 self.assertIn("invalid integer value: None", str(cm.exception))
552
Berker Peksag0a5bd512016-04-29 19:50:02 +0300553 def test_level_as_none(self):
554 body = [ast.ImportFrom(module='time',
555 names=[ast.alias(name='sleep')],
556 level=None,
557 lineno=0, col_offset=0)]
558 mod = ast.Module(body)
559 code = compile(mod, 'test', 'exec')
560 ns = {}
561 exec(code, ns)
562 self.assertIn('sleep', ns)
563
Georg Brandl0c77a822008-06-10 16:37:50 +0000564
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500565class ASTValidatorTests(unittest.TestCase):
566
567 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
568 mod.lineno = mod.col_offset = 0
569 ast.fix_missing_locations(mod)
570 with self.assertRaises(exc) as cm:
571 compile(mod, "<test>", mode)
572 if msg is not None:
573 self.assertIn(msg, str(cm.exception))
574
575 def expr(self, node, msg=None, *, exc=ValueError):
576 mod = ast.Module([ast.Expr(node)])
577 self.mod(mod, msg, exc=exc)
578
579 def stmt(self, stmt, msg=None):
580 mod = ast.Module([stmt])
581 self.mod(mod, msg)
582
583 def test_module(self):
584 m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
585 self.mod(m, "must have Load context", "single")
586 m = ast.Expression(ast.Name("x", ast.Store()))
587 self.mod(m, "must have Load context", "eval")
588
589 def _check_arguments(self, fac, check):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700590 def arguments(args=None, vararg=None,
591 kwonlyargs=None, kwarg=None,
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500592 defaults=None, kw_defaults=None):
593 if args is None:
594 args = []
595 if kwonlyargs is None:
596 kwonlyargs = []
597 if defaults is None:
598 defaults = []
599 if kw_defaults is None:
600 kw_defaults = []
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700601 args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
602 kwarg, defaults)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500603 return fac(args)
604 args = [ast.arg("x", ast.Name("x", ast.Store()))]
605 check(arguments(args=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500606 check(arguments(kwonlyargs=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500607 check(arguments(defaults=[ast.Num(3)]),
608 "more positional defaults than args")
609 check(arguments(kw_defaults=[ast.Num(4)]),
610 "length of kwonlyargs is not the same as kw_defaults")
611 args = [ast.arg("x", ast.Name("x", ast.Load()))]
612 check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
613 "must have Load context")
614 args = [ast.arg("a", ast.Name("x", ast.Load())),
615 ast.arg("b", ast.Name("y", ast.Load()))]
616 check(arguments(kwonlyargs=args,
617 kw_defaults=[None, ast.Name("x", ast.Store())]),
618 "must have Load context")
619
620 def test_funcdef(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700621 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500622 f = ast.FunctionDef("x", a, [], [], None)
623 self.stmt(f, "empty body on FunctionDef")
624 f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
625 None)
626 self.stmt(f, "must have Load context")
627 f = ast.FunctionDef("x", a, [ast.Pass()], [],
628 ast.Name("x", ast.Store()))
629 self.stmt(f, "must have Load context")
630 def fac(args):
631 return ast.FunctionDef("x", args, [ast.Pass()], [], None)
632 self._check_arguments(fac, self.stmt)
633
634 def test_classdef(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400635 def cls(bases=None, keywords=None, body=None, decorator_list=None):
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500636 if bases is None:
637 bases = []
638 if keywords is None:
639 keywords = []
640 if body is None:
641 body = [ast.Pass()]
642 if decorator_list is None:
643 decorator_list = []
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400644 return ast.ClassDef("myclass", bases, keywords,
645 body, decorator_list)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500646 self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
647 "must have Load context")
648 self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
649 "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500650 self.stmt(cls(body=[]), "empty body on ClassDef")
651 self.stmt(cls(body=[None]), "None disallowed")
652 self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
653 "must have Load context")
654
655 def test_delete(self):
656 self.stmt(ast.Delete([]), "empty targets on Delete")
657 self.stmt(ast.Delete([None]), "None disallowed")
658 self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
659 "must have Del context")
660
661 def test_assign(self):
662 self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
663 self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
664 self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
665 "must have Store context")
666 self.stmt(ast.Assign([ast.Name("x", ast.Store())],
667 ast.Name("y", ast.Store())),
668 "must have Load context")
669
670 def test_augassign(self):
671 aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
672 ast.Name("y", ast.Load()))
673 self.stmt(aug, "must have Store context")
674 aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
675 ast.Name("y", ast.Store()))
676 self.stmt(aug, "must have Load context")
677
678 def test_for(self):
679 x = ast.Name("x", ast.Store())
680 y = ast.Name("y", ast.Load())
681 p = ast.Pass()
682 self.stmt(ast.For(x, y, [], []), "empty body on For")
683 self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
684 "must have Store context")
685 self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
686 "must have Load context")
687 e = ast.Expr(ast.Name("x", ast.Store()))
688 self.stmt(ast.For(x, y, [e], []), "must have Load context")
689 self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
690
691 def test_while(self):
692 self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
693 self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
694 "must have Load context")
695 self.stmt(ast.While(ast.Num(3), [ast.Pass()],
696 [ast.Expr(ast.Name("x", ast.Store()))]),
697 "must have Load context")
698
699 def test_if(self):
700 self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
701 i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
702 self.stmt(i, "must have Load context")
703 i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
704 self.stmt(i, "must have Load context")
705 i = ast.If(ast.Num(3), [ast.Pass()],
706 [ast.Expr(ast.Name("x", ast.Store()))])
707 self.stmt(i, "must have Load context")
708
709 def test_with(self):
710 p = ast.Pass()
711 self.stmt(ast.With([], [p]), "empty items on With")
712 i = ast.withitem(ast.Num(3), None)
713 self.stmt(ast.With([i], []), "empty body on With")
714 i = ast.withitem(ast.Name("x", ast.Store()), None)
715 self.stmt(ast.With([i], [p]), "must have Load context")
716 i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
717 self.stmt(ast.With([i], [p]), "must have Store context")
718
719 def test_raise(self):
720 r = ast.Raise(None, ast.Num(3))
721 self.stmt(r, "Raise with cause but no exception")
722 r = ast.Raise(ast.Name("x", ast.Store()), None)
723 self.stmt(r, "must have Load context")
724 r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
725 self.stmt(r, "must have Load context")
726
727 def test_try(self):
728 p = ast.Pass()
729 t = ast.Try([], [], [], [p])
730 self.stmt(t, "empty body on Try")
731 t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
732 self.stmt(t, "must have Load context")
733 t = ast.Try([p], [], [], [])
734 self.stmt(t, "Try has neither except handlers nor finalbody")
735 t = ast.Try([p], [], [p], [p])
736 self.stmt(t, "Try has orelse but no except handlers")
737 t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
738 self.stmt(t, "empty body on ExceptHandler")
739 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
740 self.stmt(ast.Try([p], e, [], []), "must have Load context")
741 e = [ast.ExceptHandler(None, "x", [p])]
742 t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
743 self.stmt(t, "must have Load context")
744 t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
745 self.stmt(t, "must have Load context")
746
747 def test_assert(self):
748 self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
749 "must have Load context")
750 assrt = ast.Assert(ast.Name("x", ast.Load()),
751 ast.Name("y", ast.Store()))
752 self.stmt(assrt, "must have Load context")
753
754 def test_import(self):
755 self.stmt(ast.Import([]), "empty names on Import")
756
757 def test_importfrom(self):
758 imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
Serhiy Storchaka7de28402016-06-27 23:40:43 +0300759 self.stmt(imp, "Negative ImportFrom level")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500760 self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
761
762 def test_global(self):
763 self.stmt(ast.Global([]), "empty names on Global")
764
765 def test_nonlocal(self):
766 self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
767
768 def test_expr(self):
769 e = ast.Expr(ast.Name("x", ast.Store()))
770 self.stmt(e, "must have Load context")
771
772 def test_boolop(self):
773 b = ast.BoolOp(ast.And(), [])
774 self.expr(b, "less than 2 values")
775 b = ast.BoolOp(ast.And(), [ast.Num(3)])
776 self.expr(b, "less than 2 values")
777 b = ast.BoolOp(ast.And(), [ast.Num(4), None])
778 self.expr(b, "None disallowed")
779 b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
780 self.expr(b, "must have Load context")
781
782 def test_unaryop(self):
783 u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
784 self.expr(u, "must have Load context")
785
786 def test_lambda(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700787 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500788 self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
789 "must have Load context")
790 def fac(args):
791 return ast.Lambda(args, ast.Name("x", ast.Load()))
792 self._check_arguments(fac, self.expr)
793
794 def test_ifexp(self):
795 l = ast.Name("x", ast.Load())
796 s = ast.Name("y", ast.Store())
797 for args in (s, l, l), (l, s, l), (l, l, s):
Benjamin Peterson71ce8972011-08-09 16:17:12 -0500798 self.expr(ast.IfExp(*args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500799
800 def test_dict(self):
801 d = ast.Dict([], [ast.Name("x", ast.Load())])
802 self.expr(d, "same number of keys as values")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500803 d = ast.Dict([ast.Name("x", ast.Load())], [None])
804 self.expr(d, "None disallowed")
805
806 def test_set(self):
807 self.expr(ast.Set([None]), "None disallowed")
808 s = ast.Set([ast.Name("x", ast.Store())])
809 self.expr(s, "must have Load context")
810
811 def _check_comprehension(self, fac):
812 self.expr(fac([]), "comprehension with no generators")
813 g = ast.comprehension(ast.Name("x", ast.Load()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700814 ast.Name("x", ast.Load()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500815 self.expr(fac([g]), "must have Store context")
816 g = ast.comprehension(ast.Name("x", ast.Store()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700817 ast.Name("x", ast.Store()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500818 self.expr(fac([g]), "must have Load context")
819 x = ast.Name("x", ast.Store())
820 y = ast.Name("y", ast.Load())
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700821 g = ast.comprehension(x, y, [None], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500822 self.expr(fac([g]), "None disallowed")
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700823 g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500824 self.expr(fac([g]), "must have Load context")
825
826 def _simple_comp(self, fac):
827 g = ast.comprehension(ast.Name("x", ast.Store()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700828 ast.Name("x", ast.Load()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500829 self.expr(fac(ast.Name("x", ast.Store()), [g]),
830 "must have Load context")
831 def wrap(gens):
832 return fac(ast.Name("x", ast.Store()), gens)
833 self._check_comprehension(wrap)
834
835 def test_listcomp(self):
836 self._simple_comp(ast.ListComp)
837
838 def test_setcomp(self):
839 self._simple_comp(ast.SetComp)
840
841 def test_generatorexp(self):
842 self._simple_comp(ast.GeneratorExp)
843
844 def test_dictcomp(self):
845 g = ast.comprehension(ast.Name("y", ast.Store()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700846 ast.Name("p", ast.Load()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500847 c = ast.DictComp(ast.Name("x", ast.Store()),
848 ast.Name("y", ast.Load()), [g])
849 self.expr(c, "must have Load context")
850 c = ast.DictComp(ast.Name("x", ast.Load()),
851 ast.Name("y", ast.Store()), [g])
852 self.expr(c, "must have Load context")
853 def factory(comps):
854 k = ast.Name("x", ast.Load())
855 v = ast.Name("y", ast.Load())
856 return ast.DictComp(k, v, comps)
857 self._check_comprehension(factory)
858
859 def test_yield(self):
Benjamin Peterson527c6222012-01-14 08:58:23 -0500860 self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
861 self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500862
863 def test_compare(self):
864 left = ast.Name("x", ast.Load())
865 comp = ast.Compare(left, [ast.In()], [])
866 self.expr(comp, "no comparators")
867 comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
868 self.expr(comp, "different number of comparators and operands")
869 comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
870 self.expr(comp, "non-numeric", exc=TypeError)
871 comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
872 self.expr(comp, "non-numeric", exc=TypeError)
873
874 def test_call(self):
875 func = ast.Name("x", ast.Load())
876 args = [ast.Name("y", ast.Load())]
877 keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400878 call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500879 self.expr(call, "must have Load context")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400880 call = ast.Call(func, [None], keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500881 self.expr(call, "None disallowed")
882 bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400883 call = ast.Call(func, args, bad_keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500884 self.expr(call, "must have Load context")
885
886 def test_num(self):
887 class subint(int):
888 pass
889 class subfloat(float):
890 pass
891 class subcomplex(complex):
892 pass
893 for obj in "0", "hello", subint(), subfloat(), subcomplex():
894 self.expr(ast.Num(obj), "non-numeric", exc=TypeError)
895
896 def test_attribute(self):
897 attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
898 self.expr(attr, "must have Load context")
899
900 def test_subscript(self):
901 sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
902 ast.Load())
903 self.expr(sub, "must have Load context")
904 x = ast.Name("x", ast.Load())
905 sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
906 ast.Load())
907 self.expr(sub, "must have Load context")
908 s = ast.Name("x", ast.Store())
909 for args in (s, None, None), (None, s, None), (None, None, s):
910 sl = ast.Slice(*args)
911 self.expr(ast.Subscript(x, sl, ast.Load()),
912 "must have Load context")
913 sl = ast.ExtSlice([])
914 self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
915 sl = ast.ExtSlice([ast.Index(s)])
916 self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
917
918 def test_starred(self):
919 left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
920 ast.Store())
921 assign = ast.Assign([left], ast.Num(4))
922 self.stmt(assign, "must have Store context")
923
924 def _sequence(self, fac):
925 self.expr(fac([None], ast.Load()), "None disallowed")
926 self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
927 "must have Load context")
928
929 def test_list(self):
930 self._sequence(ast.List)
931
932 def test_tuple(self):
933 self._sequence(ast.Tuple)
934
Benjamin Peterson442f2092012-12-06 17:41:04 -0500935 def test_nameconstant(self):
936 self.expr(ast.NameConstant(4), "singleton must be True, False, or None")
937
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500938 def test_stdlib_validates(self):
939 stdlib = os.path.dirname(ast.__file__)
940 tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
941 tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
942 for module in tests:
943 fn = os.path.join(stdlib, module)
944 with open(fn, "r", encoding="utf-8") as fp:
945 source = fp.read()
Victor Stinnerd502a072013-03-22 00:06:20 +0100946 mod = ast.parse(source, fn)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500947 compile(mod, fn, "exec")
948
949
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100950class ConstantTests(unittest.TestCase):
951 """Tests on the ast.Constant node type."""
952
953 def compile_constant(self, value):
954 tree = ast.parse("x = 123")
955
956 node = tree.body[0].value
957 new_node = ast.Constant(value=value)
958 ast.copy_location(new_node, node)
959 tree.body[0].value = new_node
960
961 code = compile(tree, "<string>", "exec")
962
963 ns = {}
964 exec(code, ns)
965 return ns['x']
966
Victor Stinnerbe59d142016-01-27 00:39:12 +0100967 def test_validation(self):
968 with self.assertRaises(TypeError) as cm:
969 self.compile_constant([1, 2, 3])
970 self.assertEqual(str(cm.exception),
971 "got an invalid type in Constant: list")
972
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100973 def test_singletons(self):
974 for const in (None, False, True, Ellipsis, b'', frozenset()):
975 with self.subTest(const=const):
976 value = self.compile_constant(const)
977 self.assertIs(value, const)
978
979 def test_values(self):
980 nested_tuple = (1,)
981 nested_frozenset = frozenset({1})
982 for level in range(3):
983 nested_tuple = (nested_tuple, 2)
984 nested_frozenset = frozenset({nested_frozenset, 2})
985 values = (123, 123.0, 123j,
986 "unicode", b'bytes',
987 tuple("tuple"), frozenset("frozenset"),
988 nested_tuple, nested_frozenset)
989 for value in values:
990 with self.subTest(value=value):
991 result = self.compile_constant(value)
992 self.assertEqual(result, value)
993
994 def test_assign_to_constant(self):
995 tree = ast.parse("x = 1")
996
997 target = tree.body[0].targets[0]
998 new_target = ast.Constant(value=1)
999 ast.copy_location(new_target, target)
1000 tree.body[0].targets[0] = new_target
1001
1002 with self.assertRaises(ValueError) as cm:
1003 compile(tree, "string", "exec")
1004 self.assertEqual(str(cm.exception),
1005 "expression which can't be assigned "
1006 "to in Store context")
1007
1008 def test_get_docstring(self):
1009 tree = ast.parse("'docstring'\nx = 1")
1010 self.assertEqual(ast.get_docstring(tree), 'docstring')
1011
1012 tree.body[0].value = ast.Constant(value='constant docstring')
1013 self.assertEqual(ast.get_docstring(tree), 'constant docstring')
1014
1015 def get_load_const(self, tree):
1016 # Compile to bytecode, disassemble and get parameter of LOAD_CONST
1017 # instructions
1018 co = compile(tree, '<string>', 'exec')
1019 consts = []
1020 for instr in dis.get_instructions(co):
1021 if instr.opname == 'LOAD_CONST':
1022 consts.append(instr.argval)
1023 return consts
1024
1025 @support.cpython_only
1026 def test_load_const(self):
1027 consts = [None,
1028 True, False,
1029 124,
1030 2.0,
1031 3j,
1032 "unicode",
1033 b'bytes',
1034 (1, 2, 3)]
1035
Victor Stinnera2724092016-02-08 18:17:58 +01001036 code = '\n'.join(['x={!r}'.format(const) for const in consts])
1037 code += '\nx = ...'
1038 consts.extend((Ellipsis, None))
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001039
1040 tree = ast.parse(code)
Victor Stinnera2724092016-02-08 18:17:58 +01001041 self.assertEqual(self.get_load_const(tree),
1042 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001043
1044 # Replace expression nodes with constants
Victor Stinnera2724092016-02-08 18:17:58 +01001045 for assign, const in zip(tree.body, consts):
1046 assert isinstance(assign, ast.Assign), ast.dump(assign)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001047 new_node = ast.Constant(value=const)
Victor Stinnera2724092016-02-08 18:17:58 +01001048 ast.copy_location(new_node, assign.value)
1049 assign.value = new_node
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001050
Victor Stinnera2724092016-02-08 18:17:58 +01001051 self.assertEqual(self.get_load_const(tree),
1052 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001053
1054 def test_literal_eval(self):
1055 tree = ast.parse("1 + 2")
1056 binop = tree.body[0].value
1057
1058 new_left = ast.Constant(value=10)
1059 ast.copy_location(new_left, binop.left)
1060 binop.left = new_left
1061
1062 new_right = ast.Constant(value=20)
1063 ast.copy_location(new_right, binop.right)
1064 binop.right = new_right
1065
1066 self.assertEqual(ast.literal_eval(binop), 30)
1067
1068
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001069def main():
1070 if __name__ != '__main__':
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001071 return
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001072 if sys.argv[1:] == ['-g']:
1073 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
1074 (eval_tests, "eval")):
1075 print(kind+"_results = [")
Victor Stinnerf0891962016-02-08 17:15:21 +01001076 for statement in statements:
1077 tree = ast.parse(statement, "?", kind)
1078 print("%r," % (to_tuple(tree),))
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001079 print("]")
1080 print("main()")
1081 raise SystemExit
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04001082 unittest.main()
Tim Peters400cbc32006-02-28 18:44:41 +00001083
1084#### EVERYTHING BELOW IS GENERATED #####
1085exec_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001086('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001087('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]),
1088('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]),
1089('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
1090('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
1091('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001092('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 -04001093('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]),
1094('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001095('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 +00001096('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
1097('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001098('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001099('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
1100('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
1101('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -05001102('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
1103('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 -04001104('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]),
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001105('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
1106('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001107('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
1108('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
1109('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001110('Module', [('Global', (1, 0), ['v'])]),
1111('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
1112('Module', [('Pass', (1, 0))]),
Yury Selivanovb3d53132015-09-01 16:10:49 -04001113('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
1114('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001115('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))], [])]),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001116('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',)), [], 0)]))]),
1117('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',)), [], 0)]))]),
1118('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',)), [], 0)]))]),
1119('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',)), [], 0)]))]),
1120('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',)), [], 0), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))], 0)]))]),
1121('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',)), [], 0)]))]),
1122('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',))], 0)]))]),
1123('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',)), [], 0)]))]),
Yury Selivanov75445082015-05-11 22:57:16 -04001124('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
1125('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)]),
1126('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 -07001127('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)]))]),
1128('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)]))]),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001129('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]),
Tim Peters400cbc32006-02-28 18:44:41 +00001130]
1131single_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001132('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Tim Peters400cbc32006-02-28 18:44:41 +00001133]
1134eval_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001135('Expression', ('NameConstant', (1, 0), None)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001136('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
1137('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
1138('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001139('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001140('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001141('Expression', ('Dict', (1, 0), [], [])),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001142('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
1143('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001144('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',))], 0)])),
1145('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',))], 0)])),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001146('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 -04001147('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 +00001148('Expression', ('Num', (1, 0), 10)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001149('Expression', ('Str', (1, 0), 'string')),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001150('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1151('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 +00001152('Expression', ('Name', (1, 0), 'v', ('Load',))),
1153('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001154('Expression', ('List', (1, 0), [], ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001155('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001156('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1157('Expression', ('Tuple', (1, 0), [], ('Load',))),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001158('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 +00001159]
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001160main()