blob: a025c20006c6265188888b481bacb27eac2e1ced [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}",
Tim Peters400cbc32006-02-28 18:44:41 +0000119]
120
121# These are compiled through "single"
122# because of overlap with "eval", it just tests what
123# can't be tested with "eval"
124single_tests = [
125 "1+2"
126]
127
128# These are compiled through "eval"
129# It should test all expressions
130eval_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500131 # None
132 "None",
Tim Peters400cbc32006-02-28 18:44:41 +0000133 # BoolOp
134 "a and b",
135 # BinOp
136 "a + b",
137 # UnaryOp
138 "not v",
139 # Lambda
140 "lambda:None",
141 # Dict
142 "{ 1:2 }",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500143 # Empty dict
144 "{}",
145 # Set
146 "{None,}",
147 # Multiline dict (test for .lineno & .col_offset)
148 """{
149 1
150 :
151 2
152 }""",
Tim Peters400cbc32006-02-28 18:44:41 +0000153 # ListComp
154 "[a for b in c if d]",
155 # GeneratorExp
156 "(a for b in c if d)",
157 # Yield - yield expressions can't work outside a function
158 #
159 # Compare
160 "1 < 2 < 3",
161 # Call
162 "f(1,2,c=3,*d,**e)",
Tim Peters400cbc32006-02-28 18:44:41 +0000163 # Num
Guido van Rossume2a383d2007-01-15 16:59:06 +0000164 "10",
Tim Peters400cbc32006-02-28 18:44:41 +0000165 # Str
166 "'string'",
167 # Attribute
168 "a.b",
169 # Subscript
170 "a[b:c]",
171 # Name
172 "v",
173 # List
174 "[1,2,3]",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500175 # Empty list
176 "[]",
Tim Peters400cbc32006-02-28 18:44:41 +0000177 # Tuple
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000178 "1,2,3",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500179 # Tuple
180 "(1,2,3)",
181 # Empty tuple
182 "()",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000183 # Combination
184 "a.b.c.d(a.b[1:2])",
185
Tim Peters400cbc32006-02-28 18:44:41 +0000186]
187
188# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
189# excepthandler, arguments, keywords, alias
190
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000191class AST_Tests(unittest.TestCase):
Tim Peters400cbc32006-02-28 18:44:41 +0000192
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500193 def _assertTrueorder(self, ast_node, parent_pos):
Georg Brandl0c77a822008-06-10 16:37:50 +0000194 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000195 return
Georg Brandl0c77a822008-06-10 16:37:50 +0000196 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000197 node_pos = (ast_node.lineno, ast_node.col_offset)
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500198 self.assertTrue(node_pos >= parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000199 parent_pos = (ast_node.lineno, ast_node.col_offset)
200 for name in ast_node._fields:
201 value = getattr(ast_node, name)
202 if isinstance(value, list):
203 for child in value:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500204 self._assertTrueorder(child, parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000205 elif value is not None:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500206 self._assertTrueorder(value, parent_pos)
Tim Peters5ddfe412006-03-01 23:02:57 +0000207
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500208 def test_AST_objects(self):
209 x = ast.AST()
210 self.assertEqual(x._fields, ())
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700211 x.foobar = 42
212 self.assertEqual(x.foobar, 42)
213 self.assertEqual(x.__dict__["foobar"], 42)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500214
215 with self.assertRaises(AttributeError):
216 x.vararg
217
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500218 with self.assertRaises(TypeError):
219 # "_ast.AST constructor takes 0 positional arguments"
220 ast.AST(2)
221
Benjamin Peterson9ed37432012-07-08 11:13:36 -0700222 def test_AST_garbage_collection(self):
223 class X:
224 pass
225 a = ast.AST()
226 a.x = X()
227 a.x.a = a
228 ref = weakref.ref(a.x)
229 del a
230 support.gc_collect()
231 self.assertIsNone(ref())
232
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000233 def test_snippets(self):
234 for input, output, kind in ((exec_tests, exec_results, "exec"),
235 (single_tests, single_results, "single"),
236 (eval_tests, eval_results, "eval")):
237 for i, o in zip(input, output):
Yury Selivanovb3d53132015-09-01 16:10:49 -0400238 with self.subTest(action="parsing", input=i):
239 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
240 self.assertEqual(to_tuple(ast_tree), o)
241 self._assertTrueorder(ast_tree, (0, 0))
Victor Stinner15a30952016-02-08 22:45:06 +0100242 with self.subTest(action="compiling", input=i, kind=kind):
243 compile(ast_tree, "?", kind)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000244
Benjamin Peterson78565b22009-06-28 19:19:51 +0000245 def test_slice(self):
246 slc = ast.parse("x[::]").body[0].value.slice
247 self.assertIsNone(slc.upper)
248 self.assertIsNone(slc.lower)
249 self.assertIsNone(slc.step)
250
251 def test_from_import(self):
252 im = ast.parse("from . import y").body[0]
253 self.assertIsNone(im.module)
254
Benjamin Petersona4e4e352012-03-22 08:19:04 -0400255 def test_non_interned_future_from_ast(self):
256 mod = ast.parse("from __future__ import division")
257 self.assertIsInstance(mod.body[0], ast.ImportFrom)
258 mod.body[0].module = " __future__ ".strip()
259 compile(mod, "<test>", "exec")
260
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000261 def test_base_classes(self):
262 self.assertTrue(issubclass(ast.For, ast.stmt))
263 self.assertTrue(issubclass(ast.Name, ast.expr))
264 self.assertTrue(issubclass(ast.stmt, ast.AST))
265 self.assertTrue(issubclass(ast.expr, ast.AST))
266 self.assertTrue(issubclass(ast.comprehension, ast.AST))
267 self.assertTrue(issubclass(ast.Gt, ast.AST))
268
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500269 def test_field_attr_existence(self):
270 for name, item in ast.__dict__.items():
271 if isinstance(item, type) and name != 'AST' and name[0].isupper():
272 x = item()
273 if isinstance(x, ast.AST):
274 self.assertEqual(type(x._fields), tuple)
275
276 def test_arguments(self):
277 x = ast.arguments()
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500278 self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs',
279 'kw_defaults', 'kwarg', 'defaults'))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500280
281 with self.assertRaises(AttributeError):
282 x.vararg
283
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700284 x = ast.arguments(*range(1, 7))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500285 self.assertEqual(x.vararg, 2)
286
287 def test_field_attr_writable(self):
288 x = ast.Num()
289 # We can assign to _fields
290 x._fields = 666
291 self.assertEqual(x._fields, 666)
292
293 def test_classattrs(self):
294 x = ast.Num()
295 self.assertEqual(x._fields, ('n',))
296
297 with self.assertRaises(AttributeError):
298 x.n
299
300 x = ast.Num(42)
301 self.assertEqual(x.n, 42)
302
303 with self.assertRaises(AttributeError):
304 x.lineno
305
306 with self.assertRaises(AttributeError):
307 x.foobar
308
309 x = ast.Num(lineno=2)
310 self.assertEqual(x.lineno, 2)
311
312 x = ast.Num(42, lineno=0)
313 self.assertEqual(x.lineno, 0)
314 self.assertEqual(x._fields, ('n',))
315 self.assertEqual(x.n, 42)
316
317 self.assertRaises(TypeError, ast.Num, 1, 2)
318 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
319
320 def test_module(self):
321 body = [ast.Num(42)]
322 x = ast.Module(body)
323 self.assertEqual(x.body, body)
324
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000325 def test_nodeclasses(self):
Florent Xicluna992d9e02011-11-11 19:35:42 +0100326 # Zero arguments constructor explicitly allowed
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500327 x = ast.BinOp()
328 self.assertEqual(x._fields, ('left', 'op', 'right'))
329
330 # Random attribute allowed too
331 x.foobarbaz = 5
332 self.assertEqual(x.foobarbaz, 5)
333
334 n1 = ast.Num(1)
335 n3 = ast.Num(3)
336 addop = ast.Add()
337 x = ast.BinOp(n1, addop, n3)
338 self.assertEqual(x.left, n1)
339 self.assertEqual(x.op, addop)
340 self.assertEqual(x.right, n3)
Benjamin Peterson68b543a2011-06-27 17:51:18 -0500341
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500342 x = ast.BinOp(1, 2, 3)
343 self.assertEqual(x.left, 1)
344 self.assertEqual(x.op, 2)
345 self.assertEqual(x.right, 3)
346
Georg Brandl0c77a822008-06-10 16:37:50 +0000347 x = ast.BinOp(1, 2, 3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000348 self.assertEqual(x.left, 1)
349 self.assertEqual(x.op, 2)
350 self.assertEqual(x.right, 3)
351 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000352
353 # node raises exception when not given enough arguments
Georg Brandl0c77a822008-06-10 16:37:50 +0000354 self.assertRaises(TypeError, ast.BinOp, 1, 2)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500355 # node raises exception when given too many arguments
356 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
357 # node raises exception when not given enough arguments
358 self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
359 # node raises exception when given too many arguments
360 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000361
362 # can set attributes through kwargs too
Georg Brandl0c77a822008-06-10 16:37:50 +0000363 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000364 self.assertEqual(x.left, 1)
365 self.assertEqual(x.op, 2)
366 self.assertEqual(x.right, 3)
367 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000368
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500369 # Random kwargs also allowed
370 x = ast.BinOp(1, 2, 3, foobarbaz=42)
371 self.assertEqual(x.foobarbaz, 42)
372
373 def test_no_fields(self):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000374 # this used to fail because Sub._fields was None
Georg Brandl0c77a822008-06-10 16:37:50 +0000375 x = ast.Sub()
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500376 self.assertEqual(x._fields, ())
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000377
378 def test_pickling(self):
379 import pickle
380 mods = [pickle]
381 try:
382 import cPickle
383 mods.append(cPickle)
384 except ImportError:
385 pass
386 protocols = [0, 1, 2]
387 for mod in mods:
388 for protocol in protocols:
389 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
390 ast2 = mod.loads(mod.dumps(ast, protocol))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000391 self.assertEqual(to_tuple(ast2), to_tuple(ast))
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000392
Benjamin Peterson5b066812010-11-20 01:38:49 +0000393 def test_invalid_sum(self):
394 pos = dict(lineno=2, col_offset=3)
395 m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
396 with self.assertRaises(TypeError) as cm:
397 compile(m, "<test>", "exec")
398 self.assertIn("but got <_ast.expr", str(cm.exception))
399
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500400 def test_invalid_identitifer(self):
401 m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
402 ast.fix_missing_locations(m)
403 with self.assertRaises(TypeError) as cm:
404 compile(m, "<test>", "exec")
405 self.assertIn("identifier must be of type str", str(cm.exception))
406
407 def test_invalid_string(self):
408 m = ast.Module([ast.Expr(ast.Str(42))])
409 ast.fix_missing_locations(m)
410 with self.assertRaises(TypeError) as cm:
411 compile(m, "<test>", "exec")
412 self.assertIn("string must be of type str", str(cm.exception))
413
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000414 def test_empty_yield_from(self):
415 # Issue 16546: yield from value is not optional.
416 empty_yield_from = ast.parse("def f():\n yield from g()")
417 empty_yield_from.body[0].body[0].value.value = None
418 with self.assertRaises(ValueError) as cm:
419 compile(empty_yield_from, "<test>", "exec")
420 self.assertIn("field value is required", str(cm.exception))
421
Georg Brandl0c77a822008-06-10 16:37:50 +0000422
423class ASTHelpers_Test(unittest.TestCase):
424
425 def test_parse(self):
426 a = ast.parse('foo(1 + 1)')
427 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
428 self.assertEqual(ast.dump(a), ast.dump(b))
429
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400430 def test_parse_in_error(self):
431 try:
432 1/0
433 except Exception:
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400434 with self.assertRaises(SyntaxError) as e:
435 ast.literal_eval(r"'\U'")
436 self.assertIsNotNone(e.exception.__context__)
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400437
Georg Brandl0c77a822008-06-10 16:37:50 +0000438 def test_dump(self):
439 node = ast.parse('spam(eggs, "and cheese")')
440 self.assertEqual(ast.dump(node),
441 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
442 "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400443 "keywords=[]))])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000444 )
445 self.assertEqual(ast.dump(node, annotate_fields=False),
446 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400447 "Str('and cheese')], []))])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000448 )
449 self.assertEqual(ast.dump(node, include_attributes=True),
450 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
451 "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
452 "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400453 "col_offset=11)], keywords=[], "
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500454 "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000455 )
456
457 def test_copy_location(self):
458 src = ast.parse('1 + 1', mode='eval')
459 src.body.right = ast.copy_location(ast.Num(2), src.body.right)
460 self.assertEqual(ast.dump(src, include_attributes=True),
461 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
462 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
463 'col_offset=0))'
464 )
465
466 def test_fix_missing_locations(self):
467 src = ast.parse('write("spam")')
468 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400469 [ast.Str('eggs')], [])))
Georg Brandl0c77a822008-06-10 16:37:50 +0000470 self.assertEqual(src, ast.fix_missing_locations(src))
471 self.assertEqual(ast.dump(src, include_attributes=True),
472 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
473 "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400474 "col_offset=6)], keywords=[], "
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500475 "lineno=1, col_offset=0), lineno=1, col_offset=0), "
Georg Brandl0c77a822008-06-10 16:37:50 +0000476 "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
477 "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400478 "keywords=[], lineno=1, "
Georg Brandl0c77a822008-06-10 16:37:50 +0000479 "col_offset=0), lineno=1, col_offset=0)])"
480 )
481
482 def test_increment_lineno(self):
483 src = ast.parse('1 + 1', mode='eval')
484 self.assertEqual(ast.increment_lineno(src, n=3), src)
485 self.assertEqual(ast.dump(src, include_attributes=True),
486 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
487 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
488 'col_offset=0))'
489 )
Georg Brandl619e7ba2011-01-09 07:38:51 +0000490 # issue10869: do not increment lineno of root twice
Georg Brandlefb69022011-01-09 07:50:48 +0000491 src = ast.parse('1 + 1', mode='eval')
Georg Brandl619e7ba2011-01-09 07:38:51 +0000492 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
493 self.assertEqual(ast.dump(src, include_attributes=True),
494 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
495 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
496 'col_offset=0))'
497 )
Georg Brandl0c77a822008-06-10 16:37:50 +0000498
499 def test_iter_fields(self):
500 node = ast.parse('foo()', mode='eval')
501 d = dict(ast.iter_fields(node.body))
502 self.assertEqual(d.pop('func').id, 'foo')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400503 self.assertEqual(d, {'keywords': [], 'args': []})
Georg Brandl0c77a822008-06-10 16:37:50 +0000504
505 def test_iter_child_nodes(self):
506 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
507 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
508 iterator = ast.iter_child_nodes(node.body)
509 self.assertEqual(next(iterator).id, 'spam')
510 self.assertEqual(next(iterator).n, 23)
511 self.assertEqual(next(iterator).n, 42)
512 self.assertEqual(ast.dump(next(iterator)),
513 "keyword(arg='eggs', value=Str(s='leek'))"
514 )
515
516 def test_get_docstring(self):
517 node = ast.parse('def foo():\n """line one\n line two"""')
518 self.assertEqual(ast.get_docstring(node.body[0]),
519 'line one\nline two')
520
Yury Selivanov2f07a662015-07-23 08:54:35 +0300521 node = ast.parse('async def foo():\n """spam\n ham"""')
522 self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
523
Georg Brandl0c77a822008-06-10 16:37:50 +0000524 def test_literal_eval(self):
525 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
526 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
527 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
Benjamin Peterson3e742892010-07-11 12:59:24 +0000528 self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
Benjamin Peterson5ef96e52010-07-11 23:06:06 +0000529 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
Georg Brandl0c77a822008-06-10 16:37:50 +0000530 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
Raymond Hettingerbc959732010-10-08 00:47:45 +0000531 self.assertEqual(ast.literal_eval('-6'), -6)
532 self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
533 self.assertEqual(ast.literal_eval('3.25'), 3.25)
Georg Brandl0c77a822008-06-10 16:37:50 +0000534
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000535 def test_literal_eval_issue4907(self):
536 self.assertEqual(ast.literal_eval('2j'), 2j)
537 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
538 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000539
Amaury Forgeot d'Arc58e87612011-11-22 21:51:55 +0100540 def test_bad_integer(self):
541 # issue13436: Bad error message with invalid numeric values
542 body = [ast.ImportFrom(module='time',
543 names=[ast.alias(name='sleep')],
544 level=None,
545 lineno=None, col_offset=None)]
546 mod = ast.Module(body)
547 with self.assertRaises(ValueError) as cm:
548 compile(mod, 'test', 'exec')
549 self.assertIn("invalid integer value: None", str(cm.exception))
550
Georg Brandl0c77a822008-06-10 16:37:50 +0000551
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500552class ASTValidatorTests(unittest.TestCase):
553
554 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
555 mod.lineno = mod.col_offset = 0
556 ast.fix_missing_locations(mod)
557 with self.assertRaises(exc) as cm:
558 compile(mod, "<test>", mode)
559 if msg is not None:
560 self.assertIn(msg, str(cm.exception))
561
562 def expr(self, node, msg=None, *, exc=ValueError):
563 mod = ast.Module([ast.Expr(node)])
564 self.mod(mod, msg, exc=exc)
565
566 def stmt(self, stmt, msg=None):
567 mod = ast.Module([stmt])
568 self.mod(mod, msg)
569
570 def test_module(self):
571 m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
572 self.mod(m, "must have Load context", "single")
573 m = ast.Expression(ast.Name("x", ast.Store()))
574 self.mod(m, "must have Load context", "eval")
575
576 def _check_arguments(self, fac, check):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700577 def arguments(args=None, vararg=None,
578 kwonlyargs=None, kwarg=None,
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500579 defaults=None, kw_defaults=None):
580 if args is None:
581 args = []
582 if kwonlyargs is None:
583 kwonlyargs = []
584 if defaults is None:
585 defaults = []
586 if kw_defaults is None:
587 kw_defaults = []
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700588 args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
589 kwarg, defaults)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500590 return fac(args)
591 args = [ast.arg("x", ast.Name("x", ast.Store()))]
592 check(arguments(args=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500593 check(arguments(kwonlyargs=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500594 check(arguments(defaults=[ast.Num(3)]),
595 "more positional defaults than args")
596 check(arguments(kw_defaults=[ast.Num(4)]),
597 "length of kwonlyargs is not the same as kw_defaults")
598 args = [ast.arg("x", ast.Name("x", ast.Load()))]
599 check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
600 "must have Load context")
601 args = [ast.arg("a", ast.Name("x", ast.Load())),
602 ast.arg("b", ast.Name("y", ast.Load()))]
603 check(arguments(kwonlyargs=args,
604 kw_defaults=[None, ast.Name("x", ast.Store())]),
605 "must have Load context")
606
607 def test_funcdef(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700608 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500609 f = ast.FunctionDef("x", a, [], [], None)
610 self.stmt(f, "empty body on FunctionDef")
611 f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
612 None)
613 self.stmt(f, "must have Load context")
614 f = ast.FunctionDef("x", a, [ast.Pass()], [],
615 ast.Name("x", ast.Store()))
616 self.stmt(f, "must have Load context")
617 def fac(args):
618 return ast.FunctionDef("x", args, [ast.Pass()], [], None)
619 self._check_arguments(fac, self.stmt)
620
621 def test_classdef(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400622 def cls(bases=None, keywords=None, body=None, decorator_list=None):
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500623 if bases is None:
624 bases = []
625 if keywords is None:
626 keywords = []
627 if body is None:
628 body = [ast.Pass()]
629 if decorator_list is None:
630 decorator_list = []
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400631 return ast.ClassDef("myclass", bases, keywords,
632 body, decorator_list)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500633 self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
634 "must have Load context")
635 self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
636 "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500637 self.stmt(cls(body=[]), "empty body on ClassDef")
638 self.stmt(cls(body=[None]), "None disallowed")
639 self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
640 "must have Load context")
641
642 def test_delete(self):
643 self.stmt(ast.Delete([]), "empty targets on Delete")
644 self.stmt(ast.Delete([None]), "None disallowed")
645 self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
646 "must have Del context")
647
648 def test_assign(self):
649 self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
650 self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
651 self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
652 "must have Store context")
653 self.stmt(ast.Assign([ast.Name("x", ast.Store())],
654 ast.Name("y", ast.Store())),
655 "must have Load context")
656
657 def test_augassign(self):
658 aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
659 ast.Name("y", ast.Load()))
660 self.stmt(aug, "must have Store context")
661 aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
662 ast.Name("y", ast.Store()))
663 self.stmt(aug, "must have Load context")
664
665 def test_for(self):
666 x = ast.Name("x", ast.Store())
667 y = ast.Name("y", ast.Load())
668 p = ast.Pass()
669 self.stmt(ast.For(x, y, [], []), "empty body on For")
670 self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
671 "must have Store context")
672 self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
673 "must have Load context")
674 e = ast.Expr(ast.Name("x", ast.Store()))
675 self.stmt(ast.For(x, y, [e], []), "must have Load context")
676 self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
677
678 def test_while(self):
679 self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
680 self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
681 "must have Load context")
682 self.stmt(ast.While(ast.Num(3), [ast.Pass()],
683 [ast.Expr(ast.Name("x", ast.Store()))]),
684 "must have Load context")
685
686 def test_if(self):
687 self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
688 i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
689 self.stmt(i, "must have Load context")
690 i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
691 self.stmt(i, "must have Load context")
692 i = ast.If(ast.Num(3), [ast.Pass()],
693 [ast.Expr(ast.Name("x", ast.Store()))])
694 self.stmt(i, "must have Load context")
695
696 def test_with(self):
697 p = ast.Pass()
698 self.stmt(ast.With([], [p]), "empty items on With")
699 i = ast.withitem(ast.Num(3), None)
700 self.stmt(ast.With([i], []), "empty body on With")
701 i = ast.withitem(ast.Name("x", ast.Store()), None)
702 self.stmt(ast.With([i], [p]), "must have Load context")
703 i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
704 self.stmt(ast.With([i], [p]), "must have Store context")
705
706 def test_raise(self):
707 r = ast.Raise(None, ast.Num(3))
708 self.stmt(r, "Raise with cause but no exception")
709 r = ast.Raise(ast.Name("x", ast.Store()), None)
710 self.stmt(r, "must have Load context")
711 r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
712 self.stmt(r, "must have Load context")
713
714 def test_try(self):
715 p = ast.Pass()
716 t = ast.Try([], [], [], [p])
717 self.stmt(t, "empty body on Try")
718 t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
719 self.stmt(t, "must have Load context")
720 t = ast.Try([p], [], [], [])
721 self.stmt(t, "Try has neither except handlers nor finalbody")
722 t = ast.Try([p], [], [p], [p])
723 self.stmt(t, "Try has orelse but no except handlers")
724 t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
725 self.stmt(t, "empty body on ExceptHandler")
726 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
727 self.stmt(ast.Try([p], e, [], []), "must have Load context")
728 e = [ast.ExceptHandler(None, "x", [p])]
729 t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
730 self.stmt(t, "must have Load context")
731 t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
732 self.stmt(t, "must have Load context")
733
734 def test_assert(self):
735 self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
736 "must have Load context")
737 assrt = ast.Assert(ast.Name("x", ast.Load()),
738 ast.Name("y", ast.Store()))
739 self.stmt(assrt, "must have Load context")
740
741 def test_import(self):
742 self.stmt(ast.Import([]), "empty names on Import")
743
744 def test_importfrom(self):
745 imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
746 self.stmt(imp, "level less than -1")
747 self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
748
749 def test_global(self):
750 self.stmt(ast.Global([]), "empty names on Global")
751
752 def test_nonlocal(self):
753 self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
754
755 def test_expr(self):
756 e = ast.Expr(ast.Name("x", ast.Store()))
757 self.stmt(e, "must have Load context")
758
759 def test_boolop(self):
760 b = ast.BoolOp(ast.And(), [])
761 self.expr(b, "less than 2 values")
762 b = ast.BoolOp(ast.And(), [ast.Num(3)])
763 self.expr(b, "less than 2 values")
764 b = ast.BoolOp(ast.And(), [ast.Num(4), None])
765 self.expr(b, "None disallowed")
766 b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
767 self.expr(b, "must have Load context")
768
769 def test_unaryop(self):
770 u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
771 self.expr(u, "must have Load context")
772
773 def test_lambda(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700774 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500775 self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
776 "must have Load context")
777 def fac(args):
778 return ast.Lambda(args, ast.Name("x", ast.Load()))
779 self._check_arguments(fac, self.expr)
780
781 def test_ifexp(self):
782 l = ast.Name("x", ast.Load())
783 s = ast.Name("y", ast.Store())
784 for args in (s, l, l), (l, s, l), (l, l, s):
Benjamin Peterson71ce8972011-08-09 16:17:12 -0500785 self.expr(ast.IfExp(*args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500786
787 def test_dict(self):
788 d = ast.Dict([], [ast.Name("x", ast.Load())])
789 self.expr(d, "same number of keys as values")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500790 d = ast.Dict([ast.Name("x", ast.Load())], [None])
791 self.expr(d, "None disallowed")
792
793 def test_set(self):
794 self.expr(ast.Set([None]), "None disallowed")
795 s = ast.Set([ast.Name("x", ast.Store())])
796 self.expr(s, "must have Load context")
797
798 def _check_comprehension(self, fac):
799 self.expr(fac([]), "comprehension with no generators")
800 g = ast.comprehension(ast.Name("x", ast.Load()),
801 ast.Name("x", ast.Load()), [])
802 self.expr(fac([g]), "must have Store context")
803 g = ast.comprehension(ast.Name("x", ast.Store()),
804 ast.Name("x", ast.Store()), [])
805 self.expr(fac([g]), "must have Load context")
806 x = ast.Name("x", ast.Store())
807 y = ast.Name("y", ast.Load())
808 g = ast.comprehension(x, y, [None])
809 self.expr(fac([g]), "None disallowed")
810 g = ast.comprehension(x, y, [ast.Name("x", ast.Store())])
811 self.expr(fac([g]), "must have Load context")
812
813 def _simple_comp(self, fac):
814 g = ast.comprehension(ast.Name("x", ast.Store()),
815 ast.Name("x", ast.Load()), [])
816 self.expr(fac(ast.Name("x", ast.Store()), [g]),
817 "must have Load context")
818 def wrap(gens):
819 return fac(ast.Name("x", ast.Store()), gens)
820 self._check_comprehension(wrap)
821
822 def test_listcomp(self):
823 self._simple_comp(ast.ListComp)
824
825 def test_setcomp(self):
826 self._simple_comp(ast.SetComp)
827
828 def test_generatorexp(self):
829 self._simple_comp(ast.GeneratorExp)
830
831 def test_dictcomp(self):
832 g = ast.comprehension(ast.Name("y", ast.Store()),
833 ast.Name("p", ast.Load()), [])
834 c = ast.DictComp(ast.Name("x", ast.Store()),
835 ast.Name("y", ast.Load()), [g])
836 self.expr(c, "must have Load context")
837 c = ast.DictComp(ast.Name("x", ast.Load()),
838 ast.Name("y", ast.Store()), [g])
839 self.expr(c, "must have Load context")
840 def factory(comps):
841 k = ast.Name("x", ast.Load())
842 v = ast.Name("y", ast.Load())
843 return ast.DictComp(k, v, comps)
844 self._check_comprehension(factory)
845
846 def test_yield(self):
Benjamin Peterson527c6222012-01-14 08:58:23 -0500847 self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
848 self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500849
850 def test_compare(self):
851 left = ast.Name("x", ast.Load())
852 comp = ast.Compare(left, [ast.In()], [])
853 self.expr(comp, "no comparators")
854 comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
855 self.expr(comp, "different number of comparators and operands")
856 comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
857 self.expr(comp, "non-numeric", exc=TypeError)
858 comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
859 self.expr(comp, "non-numeric", exc=TypeError)
860
861 def test_call(self):
862 func = ast.Name("x", ast.Load())
863 args = [ast.Name("y", ast.Load())]
864 keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400865 call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500866 self.expr(call, "must have Load context")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400867 call = ast.Call(func, [None], keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500868 self.expr(call, "None disallowed")
869 bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400870 call = ast.Call(func, args, bad_keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500871 self.expr(call, "must have Load context")
872
873 def test_num(self):
874 class subint(int):
875 pass
876 class subfloat(float):
877 pass
878 class subcomplex(complex):
879 pass
880 for obj in "0", "hello", subint(), subfloat(), subcomplex():
881 self.expr(ast.Num(obj), "non-numeric", exc=TypeError)
882
883 def test_attribute(self):
884 attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
885 self.expr(attr, "must have Load context")
886
887 def test_subscript(self):
888 sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
889 ast.Load())
890 self.expr(sub, "must have Load context")
891 x = ast.Name("x", ast.Load())
892 sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
893 ast.Load())
894 self.expr(sub, "must have Load context")
895 s = ast.Name("x", ast.Store())
896 for args in (s, None, None), (None, s, None), (None, None, s):
897 sl = ast.Slice(*args)
898 self.expr(ast.Subscript(x, sl, ast.Load()),
899 "must have Load context")
900 sl = ast.ExtSlice([])
901 self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
902 sl = ast.ExtSlice([ast.Index(s)])
903 self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
904
905 def test_starred(self):
906 left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
907 ast.Store())
908 assign = ast.Assign([left], ast.Num(4))
909 self.stmt(assign, "must have Store context")
910
911 def _sequence(self, fac):
912 self.expr(fac([None], ast.Load()), "None disallowed")
913 self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
914 "must have Load context")
915
916 def test_list(self):
917 self._sequence(ast.List)
918
919 def test_tuple(self):
920 self._sequence(ast.Tuple)
921
Benjamin Peterson442f2092012-12-06 17:41:04 -0500922 def test_nameconstant(self):
923 self.expr(ast.NameConstant(4), "singleton must be True, False, or None")
924
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500925 def test_stdlib_validates(self):
926 stdlib = os.path.dirname(ast.__file__)
927 tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
928 tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
929 for module in tests:
930 fn = os.path.join(stdlib, module)
931 with open(fn, "r", encoding="utf-8") as fp:
932 source = fp.read()
Victor Stinnerd502a072013-03-22 00:06:20 +0100933 mod = ast.parse(source, fn)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500934 compile(mod, fn, "exec")
935
936
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100937class ConstantTests(unittest.TestCase):
938 """Tests on the ast.Constant node type."""
939
940 def compile_constant(self, value):
941 tree = ast.parse("x = 123")
942
943 node = tree.body[0].value
944 new_node = ast.Constant(value=value)
945 ast.copy_location(new_node, node)
946 tree.body[0].value = new_node
947
948 code = compile(tree, "<string>", "exec")
949
950 ns = {}
951 exec(code, ns)
952 return ns['x']
953
Victor Stinnerbe59d142016-01-27 00:39:12 +0100954 def test_validation(self):
955 with self.assertRaises(TypeError) as cm:
956 self.compile_constant([1, 2, 3])
957 self.assertEqual(str(cm.exception),
958 "got an invalid type in Constant: list")
959
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100960 def test_singletons(self):
961 for const in (None, False, True, Ellipsis, b'', frozenset()):
962 with self.subTest(const=const):
963 value = self.compile_constant(const)
964 self.assertIs(value, const)
965
966 def test_values(self):
967 nested_tuple = (1,)
968 nested_frozenset = frozenset({1})
969 for level in range(3):
970 nested_tuple = (nested_tuple, 2)
971 nested_frozenset = frozenset({nested_frozenset, 2})
972 values = (123, 123.0, 123j,
973 "unicode", b'bytes',
974 tuple("tuple"), frozenset("frozenset"),
975 nested_tuple, nested_frozenset)
976 for value in values:
977 with self.subTest(value=value):
978 result = self.compile_constant(value)
979 self.assertEqual(result, value)
980
981 def test_assign_to_constant(self):
982 tree = ast.parse("x = 1")
983
984 target = tree.body[0].targets[0]
985 new_target = ast.Constant(value=1)
986 ast.copy_location(new_target, target)
987 tree.body[0].targets[0] = new_target
988
989 with self.assertRaises(ValueError) as cm:
990 compile(tree, "string", "exec")
991 self.assertEqual(str(cm.exception),
992 "expression which can't be assigned "
993 "to in Store context")
994
995 def test_get_docstring(self):
996 tree = ast.parse("'docstring'\nx = 1")
997 self.assertEqual(ast.get_docstring(tree), 'docstring')
998
999 tree.body[0].value = ast.Constant(value='constant docstring')
1000 self.assertEqual(ast.get_docstring(tree), 'constant docstring')
1001
1002 def get_load_const(self, tree):
1003 # Compile to bytecode, disassemble and get parameter of LOAD_CONST
1004 # instructions
1005 co = compile(tree, '<string>', 'exec')
1006 consts = []
1007 for instr in dis.get_instructions(co):
1008 if instr.opname == 'LOAD_CONST':
1009 consts.append(instr.argval)
1010 return consts
1011
1012 @support.cpython_only
1013 def test_load_const(self):
1014 consts = [None,
1015 True, False,
1016 124,
1017 2.0,
1018 3j,
1019 "unicode",
1020 b'bytes',
1021 (1, 2, 3)]
1022
Victor Stinnera2724092016-02-08 18:17:58 +01001023 code = '\n'.join(['x={!r}'.format(const) for const in consts])
1024 code += '\nx = ...'
1025 consts.extend((Ellipsis, None))
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001026
1027 tree = ast.parse(code)
Victor Stinnera2724092016-02-08 18:17:58 +01001028 self.assertEqual(self.get_load_const(tree),
1029 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001030
1031 # Replace expression nodes with constants
Victor Stinnera2724092016-02-08 18:17:58 +01001032 for assign, const in zip(tree.body, consts):
1033 assert isinstance(assign, ast.Assign), ast.dump(assign)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001034 new_node = ast.Constant(value=const)
Victor Stinnera2724092016-02-08 18:17:58 +01001035 ast.copy_location(new_node, assign.value)
1036 assign.value = new_node
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001037
Victor Stinnera2724092016-02-08 18:17:58 +01001038 self.assertEqual(self.get_load_const(tree),
1039 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001040
1041 def test_literal_eval(self):
1042 tree = ast.parse("1 + 2")
1043 binop = tree.body[0].value
1044
1045 new_left = ast.Constant(value=10)
1046 ast.copy_location(new_left, binop.left)
1047 binop.left = new_left
1048
1049 new_right = ast.Constant(value=20)
1050 ast.copy_location(new_right, binop.right)
1051 binop.right = new_right
1052
1053 self.assertEqual(ast.literal_eval(binop), 30)
1054
1055
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001056def main():
1057 if __name__ != '__main__':
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001058 return
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001059 if sys.argv[1:] == ['-g']:
1060 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
1061 (eval_tests, "eval")):
1062 print(kind+"_results = [")
Victor Stinnerf0891962016-02-08 17:15:21 +01001063 for statement in statements:
1064 tree = ast.parse(statement, "?", kind)
1065 print("%r," % (to_tuple(tree),))
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001066 print("]")
1067 print("main()")
1068 raise SystemExit
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04001069 unittest.main()
Tim Peters400cbc32006-02-28 18:44:41 +00001070
1071#### EVERYTHING BELOW IS GENERATED #####
1072exec_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001073('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001074('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]),
1075('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]),
1076('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
1077('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
1078('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001079('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 -04001080('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]),
1081('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001082('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 +00001083('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
1084('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001085('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001086('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
1087('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
1088('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -05001089('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
1090('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 -04001091('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]),
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001092('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
1093('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001094('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
1095('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
1096('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001097('Module', [('Global', (1, 0), ['v'])]),
1098('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
1099('Module', [('Pass', (1, 0))]),
Yury Selivanovb3d53132015-09-01 16:10:49 -04001100('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
1101('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001102('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))], [])]),
1103('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',)), [])]))]),
1104('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 -05001105('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',)), [])]))]),
1106('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 -07001107('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',))])]))]),
1108('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',)), [])]))]),
1109('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',))])]))]),
1110('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 -04001111('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
1112('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)]),
1113('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 -07001114('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)]))]),
1115('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 +00001116]
1117single_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001118('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Tim Peters400cbc32006-02-28 18:44:41 +00001119]
1120eval_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001121('Expression', ('NameConstant', (1, 0), None)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001122('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
1123('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
1124('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001125('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001126('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001127('Expression', ('Dict', (1, 0), [], [])),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001128('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
1129('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001130('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',))])])),
1131('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',))])])),
1132('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 -04001133('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 +00001134('Expression', ('Num', (1, 0), 10)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001135('Expression', ('Str', (1, 0), 'string')),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001136('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1137('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 +00001138('Expression', ('Name', (1, 0), 'v', ('Load',))),
1139('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001140('Expression', ('List', (1, 0), [], ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001141('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001142('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1143('Expression', ('Tuple', (1, 0), [], ('Load',))),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001144('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 +00001145]
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001146main()