blob: e032f6d27a8f1be2fac6aa11f59cddad3799100c [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
Berker Peksag0a5bd512016-04-29 19:50:02 +0300551 def test_level_as_none(self):
552 body = [ast.ImportFrom(module='time',
553 names=[ast.alias(name='sleep')],
554 level=None,
555 lineno=0, col_offset=0)]
556 mod = ast.Module(body)
557 code = compile(mod, 'test', 'exec')
558 ns = {}
559 exec(code, ns)
560 self.assertIn('sleep', ns)
561
Georg Brandl0c77a822008-06-10 16:37:50 +0000562
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500563class ASTValidatorTests(unittest.TestCase):
564
565 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
566 mod.lineno = mod.col_offset = 0
567 ast.fix_missing_locations(mod)
568 with self.assertRaises(exc) as cm:
569 compile(mod, "<test>", mode)
570 if msg is not None:
571 self.assertIn(msg, str(cm.exception))
572
573 def expr(self, node, msg=None, *, exc=ValueError):
574 mod = ast.Module([ast.Expr(node)])
575 self.mod(mod, msg, exc=exc)
576
577 def stmt(self, stmt, msg=None):
578 mod = ast.Module([stmt])
579 self.mod(mod, msg)
580
581 def test_module(self):
582 m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
583 self.mod(m, "must have Load context", "single")
584 m = ast.Expression(ast.Name("x", ast.Store()))
585 self.mod(m, "must have Load context", "eval")
586
587 def _check_arguments(self, fac, check):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700588 def arguments(args=None, vararg=None,
589 kwonlyargs=None, kwarg=None,
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500590 defaults=None, kw_defaults=None):
591 if args is None:
592 args = []
593 if kwonlyargs is None:
594 kwonlyargs = []
595 if defaults is None:
596 defaults = []
597 if kw_defaults is None:
598 kw_defaults = []
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700599 args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
600 kwarg, defaults)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500601 return fac(args)
602 args = [ast.arg("x", ast.Name("x", ast.Store()))]
603 check(arguments(args=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500604 check(arguments(kwonlyargs=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500605 check(arguments(defaults=[ast.Num(3)]),
606 "more positional defaults than args")
607 check(arguments(kw_defaults=[ast.Num(4)]),
608 "length of kwonlyargs is not the same as kw_defaults")
609 args = [ast.arg("x", ast.Name("x", ast.Load()))]
610 check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
611 "must have Load context")
612 args = [ast.arg("a", ast.Name("x", ast.Load())),
613 ast.arg("b", ast.Name("y", ast.Load()))]
614 check(arguments(kwonlyargs=args,
615 kw_defaults=[None, ast.Name("x", ast.Store())]),
616 "must have Load context")
617
618 def test_funcdef(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700619 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500620 f = ast.FunctionDef("x", a, [], [], None)
621 self.stmt(f, "empty body on FunctionDef")
622 f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
623 None)
624 self.stmt(f, "must have Load context")
625 f = ast.FunctionDef("x", a, [ast.Pass()], [],
626 ast.Name("x", ast.Store()))
627 self.stmt(f, "must have Load context")
628 def fac(args):
629 return ast.FunctionDef("x", args, [ast.Pass()], [], None)
630 self._check_arguments(fac, self.stmt)
631
632 def test_classdef(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400633 def cls(bases=None, keywords=None, body=None, decorator_list=None):
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500634 if bases is None:
635 bases = []
636 if keywords is None:
637 keywords = []
638 if body is None:
639 body = [ast.Pass()]
640 if decorator_list is None:
641 decorator_list = []
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400642 return ast.ClassDef("myclass", bases, keywords,
643 body, decorator_list)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500644 self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
645 "must have Load context")
646 self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
647 "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500648 self.stmt(cls(body=[]), "empty body on ClassDef")
649 self.stmt(cls(body=[None]), "None disallowed")
650 self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
651 "must have Load context")
652
653 def test_delete(self):
654 self.stmt(ast.Delete([]), "empty targets on Delete")
655 self.stmt(ast.Delete([None]), "None disallowed")
656 self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
657 "must have Del context")
658
659 def test_assign(self):
660 self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
661 self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
662 self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
663 "must have Store context")
664 self.stmt(ast.Assign([ast.Name("x", ast.Store())],
665 ast.Name("y", ast.Store())),
666 "must have Load context")
667
668 def test_augassign(self):
669 aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
670 ast.Name("y", ast.Load()))
671 self.stmt(aug, "must have Store context")
672 aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
673 ast.Name("y", ast.Store()))
674 self.stmt(aug, "must have Load context")
675
676 def test_for(self):
677 x = ast.Name("x", ast.Store())
678 y = ast.Name("y", ast.Load())
679 p = ast.Pass()
680 self.stmt(ast.For(x, y, [], []), "empty body on For")
681 self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
682 "must have Store context")
683 self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
684 "must have Load context")
685 e = ast.Expr(ast.Name("x", ast.Store()))
686 self.stmt(ast.For(x, y, [e], []), "must have Load context")
687 self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
688
689 def test_while(self):
690 self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
691 self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
692 "must have Load context")
693 self.stmt(ast.While(ast.Num(3), [ast.Pass()],
694 [ast.Expr(ast.Name("x", ast.Store()))]),
695 "must have Load context")
696
697 def test_if(self):
698 self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
699 i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
700 self.stmt(i, "must have Load context")
701 i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
702 self.stmt(i, "must have Load context")
703 i = ast.If(ast.Num(3), [ast.Pass()],
704 [ast.Expr(ast.Name("x", ast.Store()))])
705 self.stmt(i, "must have Load context")
706
707 def test_with(self):
708 p = ast.Pass()
709 self.stmt(ast.With([], [p]), "empty items on With")
710 i = ast.withitem(ast.Num(3), None)
711 self.stmt(ast.With([i], []), "empty body on With")
712 i = ast.withitem(ast.Name("x", ast.Store()), None)
713 self.stmt(ast.With([i], [p]), "must have Load context")
714 i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
715 self.stmt(ast.With([i], [p]), "must have Store context")
716
717 def test_raise(self):
718 r = ast.Raise(None, ast.Num(3))
719 self.stmt(r, "Raise with cause but no exception")
720 r = ast.Raise(ast.Name("x", ast.Store()), None)
721 self.stmt(r, "must have Load context")
722 r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
723 self.stmt(r, "must have Load context")
724
725 def test_try(self):
726 p = ast.Pass()
727 t = ast.Try([], [], [], [p])
728 self.stmt(t, "empty body on Try")
729 t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
730 self.stmt(t, "must have Load context")
731 t = ast.Try([p], [], [], [])
732 self.stmt(t, "Try has neither except handlers nor finalbody")
733 t = ast.Try([p], [], [p], [p])
734 self.stmt(t, "Try has orelse but no except handlers")
735 t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
736 self.stmt(t, "empty body on ExceptHandler")
737 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
738 self.stmt(ast.Try([p], e, [], []), "must have Load context")
739 e = [ast.ExceptHandler(None, "x", [p])]
740 t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
741 self.stmt(t, "must have Load context")
742 t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
743 self.stmt(t, "must have Load context")
744
745 def test_assert(self):
746 self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
747 "must have Load context")
748 assrt = ast.Assert(ast.Name("x", ast.Load()),
749 ast.Name("y", ast.Store()))
750 self.stmt(assrt, "must have Load context")
751
752 def test_import(self):
753 self.stmt(ast.Import([]), "empty names on Import")
754
755 def test_importfrom(self):
756 imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
Serhiy Storchaka7de28402016-06-27 23:40:43 +0300757 self.stmt(imp, "Negative ImportFrom level")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500758 self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
759
760 def test_global(self):
761 self.stmt(ast.Global([]), "empty names on Global")
762
763 def test_nonlocal(self):
764 self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
765
766 def test_expr(self):
767 e = ast.Expr(ast.Name("x", ast.Store()))
768 self.stmt(e, "must have Load context")
769
770 def test_boolop(self):
771 b = ast.BoolOp(ast.And(), [])
772 self.expr(b, "less than 2 values")
773 b = ast.BoolOp(ast.And(), [ast.Num(3)])
774 self.expr(b, "less than 2 values")
775 b = ast.BoolOp(ast.And(), [ast.Num(4), None])
776 self.expr(b, "None disallowed")
777 b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
778 self.expr(b, "must have Load context")
779
780 def test_unaryop(self):
781 u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
782 self.expr(u, "must have Load context")
783
784 def test_lambda(self):
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700785 a = ast.arguments([], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500786 self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
787 "must have Load context")
788 def fac(args):
789 return ast.Lambda(args, ast.Name("x", ast.Load()))
790 self._check_arguments(fac, self.expr)
791
792 def test_ifexp(self):
793 l = ast.Name("x", ast.Load())
794 s = ast.Name("y", ast.Store())
795 for args in (s, l, l), (l, s, l), (l, l, s):
Benjamin Peterson71ce8972011-08-09 16:17:12 -0500796 self.expr(ast.IfExp(*args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500797
798 def test_dict(self):
799 d = ast.Dict([], [ast.Name("x", ast.Load())])
800 self.expr(d, "same number of keys as values")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500801 d = ast.Dict([ast.Name("x", ast.Load())], [None])
802 self.expr(d, "None disallowed")
803
804 def test_set(self):
805 self.expr(ast.Set([None]), "None disallowed")
806 s = ast.Set([ast.Name("x", ast.Store())])
807 self.expr(s, "must have Load context")
808
809 def _check_comprehension(self, fac):
810 self.expr(fac([]), "comprehension with no generators")
811 g = ast.comprehension(ast.Name("x", ast.Load()),
812 ast.Name("x", ast.Load()), [])
813 self.expr(fac([g]), "must have Store context")
814 g = ast.comprehension(ast.Name("x", ast.Store()),
815 ast.Name("x", ast.Store()), [])
816 self.expr(fac([g]), "must have Load context")
817 x = ast.Name("x", ast.Store())
818 y = ast.Name("y", ast.Load())
819 g = ast.comprehension(x, y, [None])
820 self.expr(fac([g]), "None disallowed")
821 g = ast.comprehension(x, y, [ast.Name("x", ast.Store())])
822 self.expr(fac([g]), "must have Load context")
823
824 def _simple_comp(self, fac):
825 g = ast.comprehension(ast.Name("x", ast.Store()),
826 ast.Name("x", ast.Load()), [])
827 self.expr(fac(ast.Name("x", ast.Store()), [g]),
828 "must have Load context")
829 def wrap(gens):
830 return fac(ast.Name("x", ast.Store()), gens)
831 self._check_comprehension(wrap)
832
833 def test_listcomp(self):
834 self._simple_comp(ast.ListComp)
835
836 def test_setcomp(self):
837 self._simple_comp(ast.SetComp)
838
839 def test_generatorexp(self):
840 self._simple_comp(ast.GeneratorExp)
841
842 def test_dictcomp(self):
843 g = ast.comprehension(ast.Name("y", ast.Store()),
844 ast.Name("p", ast.Load()), [])
845 c = ast.DictComp(ast.Name("x", ast.Store()),
846 ast.Name("y", ast.Load()), [g])
847 self.expr(c, "must have Load context")
848 c = ast.DictComp(ast.Name("x", ast.Load()),
849 ast.Name("y", ast.Store()), [g])
850 self.expr(c, "must have Load context")
851 def factory(comps):
852 k = ast.Name("x", ast.Load())
853 v = ast.Name("y", ast.Load())
854 return ast.DictComp(k, v, comps)
855 self._check_comprehension(factory)
856
857 def test_yield(self):
Benjamin Peterson527c6222012-01-14 08:58:23 -0500858 self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
859 self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500860
861 def test_compare(self):
862 left = ast.Name("x", ast.Load())
863 comp = ast.Compare(left, [ast.In()], [])
864 self.expr(comp, "no comparators")
865 comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
866 self.expr(comp, "different number of comparators and operands")
867 comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
868 self.expr(comp, "non-numeric", exc=TypeError)
869 comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
870 self.expr(comp, "non-numeric", exc=TypeError)
871
872 def test_call(self):
873 func = ast.Name("x", ast.Load())
874 args = [ast.Name("y", ast.Load())]
875 keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400876 call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500877 self.expr(call, "must have Load context")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400878 call = ast.Call(func, [None], keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500879 self.expr(call, "None disallowed")
880 bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400881 call = ast.Call(func, args, bad_keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500882 self.expr(call, "must have Load context")
883
884 def test_num(self):
885 class subint(int):
886 pass
887 class subfloat(float):
888 pass
889 class subcomplex(complex):
890 pass
891 for obj in "0", "hello", subint(), subfloat(), subcomplex():
892 self.expr(ast.Num(obj), "non-numeric", exc=TypeError)
893
894 def test_attribute(self):
895 attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
896 self.expr(attr, "must have Load context")
897
898 def test_subscript(self):
899 sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
900 ast.Load())
901 self.expr(sub, "must have Load context")
902 x = ast.Name("x", ast.Load())
903 sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
904 ast.Load())
905 self.expr(sub, "must have Load context")
906 s = ast.Name("x", ast.Store())
907 for args in (s, None, None), (None, s, None), (None, None, s):
908 sl = ast.Slice(*args)
909 self.expr(ast.Subscript(x, sl, ast.Load()),
910 "must have Load context")
911 sl = ast.ExtSlice([])
912 self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
913 sl = ast.ExtSlice([ast.Index(s)])
914 self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
915
916 def test_starred(self):
917 left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
918 ast.Store())
919 assign = ast.Assign([left], ast.Num(4))
920 self.stmt(assign, "must have Store context")
921
922 def _sequence(self, fac):
923 self.expr(fac([None], ast.Load()), "None disallowed")
924 self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
925 "must have Load context")
926
927 def test_list(self):
928 self._sequence(ast.List)
929
930 def test_tuple(self):
931 self._sequence(ast.Tuple)
932
Benjamin Peterson442f2092012-12-06 17:41:04 -0500933 def test_nameconstant(self):
934 self.expr(ast.NameConstant(4), "singleton must be True, False, or None")
935
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500936 def test_stdlib_validates(self):
937 stdlib = os.path.dirname(ast.__file__)
938 tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
939 tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
940 for module in tests:
941 fn = os.path.join(stdlib, module)
942 with open(fn, "r", encoding="utf-8") as fp:
943 source = fp.read()
Victor Stinnerd502a072013-03-22 00:06:20 +0100944 mod = ast.parse(source, fn)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500945 compile(mod, fn, "exec")
946
947
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100948class ConstantTests(unittest.TestCase):
949 """Tests on the ast.Constant node type."""
950
951 def compile_constant(self, value):
952 tree = ast.parse("x = 123")
953
954 node = tree.body[0].value
955 new_node = ast.Constant(value=value)
956 ast.copy_location(new_node, node)
957 tree.body[0].value = new_node
958
959 code = compile(tree, "<string>", "exec")
960
961 ns = {}
962 exec(code, ns)
963 return ns['x']
964
Victor Stinnerbe59d142016-01-27 00:39:12 +0100965 def test_validation(self):
966 with self.assertRaises(TypeError) as cm:
967 self.compile_constant([1, 2, 3])
968 self.assertEqual(str(cm.exception),
969 "got an invalid type in Constant: list")
970
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100971 def test_singletons(self):
972 for const in (None, False, True, Ellipsis, b'', frozenset()):
973 with self.subTest(const=const):
974 value = self.compile_constant(const)
975 self.assertIs(value, const)
976
977 def test_values(self):
978 nested_tuple = (1,)
979 nested_frozenset = frozenset({1})
980 for level in range(3):
981 nested_tuple = (nested_tuple, 2)
982 nested_frozenset = frozenset({nested_frozenset, 2})
983 values = (123, 123.0, 123j,
984 "unicode", b'bytes',
985 tuple("tuple"), frozenset("frozenset"),
986 nested_tuple, nested_frozenset)
987 for value in values:
988 with self.subTest(value=value):
989 result = self.compile_constant(value)
990 self.assertEqual(result, value)
991
992 def test_assign_to_constant(self):
993 tree = ast.parse("x = 1")
994
995 target = tree.body[0].targets[0]
996 new_target = ast.Constant(value=1)
997 ast.copy_location(new_target, target)
998 tree.body[0].targets[0] = new_target
999
1000 with self.assertRaises(ValueError) as cm:
1001 compile(tree, "string", "exec")
1002 self.assertEqual(str(cm.exception),
1003 "expression which can't be assigned "
1004 "to in Store context")
1005
1006 def test_get_docstring(self):
1007 tree = ast.parse("'docstring'\nx = 1")
1008 self.assertEqual(ast.get_docstring(tree), 'docstring')
1009
1010 tree.body[0].value = ast.Constant(value='constant docstring')
1011 self.assertEqual(ast.get_docstring(tree), 'constant docstring')
1012
1013 def get_load_const(self, tree):
1014 # Compile to bytecode, disassemble and get parameter of LOAD_CONST
1015 # instructions
1016 co = compile(tree, '<string>', 'exec')
1017 consts = []
1018 for instr in dis.get_instructions(co):
1019 if instr.opname == 'LOAD_CONST':
1020 consts.append(instr.argval)
1021 return consts
1022
1023 @support.cpython_only
1024 def test_load_const(self):
1025 consts = [None,
1026 True, False,
1027 124,
1028 2.0,
1029 3j,
1030 "unicode",
1031 b'bytes',
1032 (1, 2, 3)]
1033
Victor Stinnera2724092016-02-08 18:17:58 +01001034 code = '\n'.join(['x={!r}'.format(const) for const in consts])
1035 code += '\nx = ...'
1036 consts.extend((Ellipsis, None))
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001037
1038 tree = ast.parse(code)
Victor Stinnera2724092016-02-08 18:17:58 +01001039 self.assertEqual(self.get_load_const(tree),
1040 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001041
1042 # Replace expression nodes with constants
Victor Stinnera2724092016-02-08 18:17:58 +01001043 for assign, const in zip(tree.body, consts):
1044 assert isinstance(assign, ast.Assign), ast.dump(assign)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001045 new_node = ast.Constant(value=const)
Victor Stinnera2724092016-02-08 18:17:58 +01001046 ast.copy_location(new_node, assign.value)
1047 assign.value = new_node
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001048
Victor Stinnera2724092016-02-08 18:17:58 +01001049 self.assertEqual(self.get_load_const(tree),
1050 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001051
1052 def test_literal_eval(self):
1053 tree = ast.parse("1 + 2")
1054 binop = tree.body[0].value
1055
1056 new_left = ast.Constant(value=10)
1057 ast.copy_location(new_left, binop.left)
1058 binop.left = new_left
1059
1060 new_right = ast.Constant(value=20)
1061 ast.copy_location(new_right, binop.right)
1062 binop.right = new_right
1063
1064 self.assertEqual(ast.literal_eval(binop), 30)
1065
1066
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001067def main():
1068 if __name__ != '__main__':
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001069 return
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001070 if sys.argv[1:] == ['-g']:
1071 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
1072 (eval_tests, "eval")):
1073 print(kind+"_results = [")
Victor Stinnerf0891962016-02-08 17:15:21 +01001074 for statement in statements:
1075 tree = ast.parse(statement, "?", kind)
1076 print("%r," % (to_tuple(tree),))
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001077 print("]")
1078 print("main()")
1079 raise SystemExit
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04001080 unittest.main()
Tim Peters400cbc32006-02-28 18:44:41 +00001081
1082#### EVERYTHING BELOW IS GENERATED #####
1083exec_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001084('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001085('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]),
1086('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]),
1087('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
1088('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
1089('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001090('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 -04001091('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]),
1092('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001093('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 +00001094('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
1095('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001096('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001097('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
1098('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
1099('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -05001100('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
1101('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 -04001102('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]),
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001103('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
1104('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001105('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
1106('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
1107('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001108('Module', [('Global', (1, 0), ['v'])]),
1109('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
1110('Module', [('Pass', (1, 0))]),
Yury Selivanovb3d53132015-09-01 16:10:49 -04001111('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
1112('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001113('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))], [])]),
1114('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',)), [])]))]),
1115('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 -05001116('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',)), [])]))]),
1117('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 -07001118('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',))])]))]),
1119('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',)), [])]))]),
1120('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',))])]))]),
1121('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 -04001122('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
1123('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)]),
1124('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 -07001125('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)]))]),
1126('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 +00001127]
1128single_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001129('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Tim Peters400cbc32006-02-28 18:44:41 +00001130]
1131eval_results = [
Benjamin Peterson442f2092012-12-06 17:41:04 -05001132('Expression', ('NameConstant', (1, 0), None)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001133('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
1134('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
1135('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001136('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001137('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001138('Expression', ('Dict', (1, 0), [], [])),
Benjamin Peterson58b53952015-09-25 22:44:43 -07001139('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
1140('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001141('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',))])])),
1142('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',))])])),
1143('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 -04001144('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 +00001145('Expression', ('Num', (1, 0), 10)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001146('Expression', ('Str', (1, 0), 'string')),
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001147('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1148('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 +00001149('Expression', ('Name', (1, 0), 'v', ('Load',))),
1150('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001151('Expression', ('List', (1, 0), [], ('Load',))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001152('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
Benjamin Peterson6ccfe852011-06-27 17:46:06 -05001153('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1154('Expression', ('Tuple', (1, 0), [], ('Load',))),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001155('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 +00001156]
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001157main()