blob: aa0d214b8a6064cbf635172ee9c0b557a8d6ef68 [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
Serhiy Storchakac3ea41e2019-08-26 10:13:19 +03006import warnings
Benjamin Peterson9ed37432012-07-08 11:13:36 -07007import weakref
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008from textwrap import dedent
Benjamin Peterson9ed37432012-07-08 11:13:36 -07009
10from test import support
Tim Peters400cbc32006-02-28 18:44:41 +000011
12def to_tuple(t):
Guido van Rossum3172c5d2007-10-16 18:12:55 +000013 if t is None or isinstance(t, (str, int, complex)):
Tim Peters400cbc32006-02-28 18:44:41 +000014 return t
15 elif isinstance(t, list):
16 return [to_tuple(e) for e in t]
17 result = [t.__class__.__name__]
Martin v. Löwis49c5da12006-03-01 22:49:05 +000018 if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
19 result.append((t.lineno, t.col_offset))
Serhiy Storchaka850a8852020-01-10 10:12:55 +020020 if hasattr(t, 'end_lineno') and hasattr(t, 'end_col_offset'):
21 result[-1] += (t.end_lineno, t.end_col_offset)
Tim Peters400cbc32006-02-28 18:44:41 +000022 if t._fields is None:
23 return tuple(result)
24 for f in t._fields:
25 result.append(to_tuple(getattr(t, f)))
26 return tuple(result)
27
Neal Norwitzee9b10a2008-03-31 05:29:39 +000028
Tim Peters400cbc32006-02-28 18:44:41 +000029# These tests are compiled through "exec"
Ezio Melotti85a86292013-08-17 16:57:41 +030030# There should be at least one test per statement
Tim Peters400cbc32006-02-28 18:44:41 +000031exec_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050032 # None
33 "None",
INADA Naokicb41b272017-02-23 00:31:59 +090034 # Module docstring
35 "'module docstring'",
Tim Peters400cbc32006-02-28 18:44:41 +000036 # FunctionDef
37 "def f(): pass",
INADA Naokicb41b272017-02-23 00:31:59 +090038 # FunctionDef with docstring
39 "def f(): 'function docstring'",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050040 # FunctionDef with arg
41 "def f(a): pass",
42 # FunctionDef with arg and default value
43 "def f(a=0): pass",
44 # FunctionDef with varargs
45 "def f(*args): pass",
46 # FunctionDef with kwargs
47 "def f(**kwargs): pass",
INADA Naokicb41b272017-02-23 00:31:59 +090048 # FunctionDef with all kind of args and docstring
49 "def f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): 'doc for f()'",
Tim Peters400cbc32006-02-28 18:44:41 +000050 # ClassDef
51 "class C:pass",
INADA Naokicb41b272017-02-23 00:31:59 +090052 # ClassDef with docstring
53 "class C: 'docstring for class C'",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -050054 # ClassDef, new style class
55 "class C(object): pass",
Tim Peters400cbc32006-02-28 18:44:41 +000056 # Return
57 "def f():return 1",
58 # Delete
59 "del v",
60 # Assign
61 "v = 1",
Serhiy Storchakab619b092018-11-27 09:40:29 +020062 "a,b = c",
63 "(a,b) = c",
64 "[a,b] = c",
Tim Peters400cbc32006-02-28 18:44:41 +000065 # AugAssign
66 "v += 1",
Tim Peters400cbc32006-02-28 18:44:41 +000067 # For
68 "for v in v:pass",
69 # While
70 "while v:pass",
71 # If
72 "if v:pass",
Lysandros Nikolaou025a6022019-12-12 22:40:21 +010073 # If-Elif
74 "if a:\n pass\nelif b:\n pass",
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +010075 # If-Elif-Else
76 "if a:\n pass\nelif b:\n pass\nelse:\n pass",
Benjamin Petersonaeabd5f2011-05-27 15:02:03 -050077 # With
78 "with x as y: pass",
79 "with x as y, z as q: pass",
Tim Peters400cbc32006-02-28 18:44:41 +000080 # Raise
Collin Winter828f04a2007-08-31 00:04:24 +000081 "raise Exception('string')",
Tim Peters400cbc32006-02-28 18:44:41 +000082 # TryExcept
83 "try:\n pass\nexcept Exception:\n pass",
84 # TryFinally
85 "try:\n pass\nfinally:\n pass",
86 # Assert
87 "assert v",
88 # Import
89 "import sys",
90 # ImportFrom
91 "from sys import v",
Tim Peters400cbc32006-02-28 18:44:41 +000092 # Global
93 "global v",
94 # Expr
95 "1",
96 # Pass,
97 "pass",
98 # Break
Yury Selivanovb3d53132015-09-01 16:10:49 -040099 "for v in v:break",
Tim Peters400cbc32006-02-28 18:44:41 +0000100 # Continue
Yury Selivanovb3d53132015-09-01 16:10:49 -0400101 "for v in v:continue",
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +0000102 # for statements with naked tuples (see http://bugs.python.org/issue6704)
103 "for a,b in c: pass",
Serhiy Storchakab619b092018-11-27 09:40:29 +0200104 "for (a,b) in c: pass",
105 "for [a,b] in c: pass",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500106 # Multiline generator expression (test for .lineno & .col_offset)
107 """(
108 (
109 Aa
110 ,
111 Bb
112 )
113 for
114 Aa
115 ,
116 Bb in Cc
117 )""",
118 # dictcomp
119 "{a : b for w in x for m in p if g}",
120 # dictcomp with naked tuple
121 "{a : b for v,w in x}",
122 # setcomp
123 "{r for l in x if g}",
124 # setcomp with naked tuple
125 "{r for l,m in x}",
Yury Selivanov75445082015-05-11 22:57:16 -0400126 # AsyncFunctionDef
INADA Naokicb41b272017-02-23 00:31:59 +0900127 "async def f():\n 'async function'\n await something()",
Yury Selivanov75445082015-05-11 22:57:16 -0400128 # AsyncFor
129 "async def f():\n async for e in i: 1\n else: 2",
130 # AsyncWith
131 "async def f():\n async with a as b: 1",
Yury Selivanovb3d53132015-09-01 16:10:49 -0400132 # PEP 448: Additional Unpacking Generalizations
133 "{**{1:2}, 2:3}",
134 "{*{1, 2}, 3}",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700135 # Asynchronous comprehensions
136 "async def f():\n [i async for b in c]",
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200137 # Decorated FunctionDef
Serhiy Storchaka26ae9f62019-10-26 16:46:05 +0300138 "@deco1\n@deco2()\n@deco3(1)\ndef f(): pass",
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200139 # Decorated AsyncFunctionDef
Serhiy Storchaka26ae9f62019-10-26 16:46:05 +0300140 "@deco1\n@deco2()\n@deco3(1)\nasync def f(): pass",
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200141 # Decorated ClassDef
Serhiy Storchaka26ae9f62019-10-26 16:46:05 +0300142 "@deco1\n@deco2()\n@deco3(1)\nclass C: pass",
Serhiy Storchakab619b092018-11-27 09:40:29 +0200143 # Decorator with generator argument
144 "@deco(a for a in b)\ndef f(): pass",
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000145 # Simple assignment expression
146 "(a := 1)",
Pablo Galindo2f58a842019-05-31 14:09:49 +0100147 # Positional-only arguments
148 "def f(a, /,): pass",
149 "def f(a, /, c, d, e): pass",
150 "def f(a, /, c, *, d, e): pass",
151 "def f(a, /, c, *, d, e, **kwargs): pass",
152 # Positional-only arguments with defaults
153 "def f(a=1, /,): pass",
154 "def f(a=1, /, b=2, c=4): pass",
155 "def f(a=1, /, b=2, *, c=4): pass",
156 "def f(a=1, /, b=2, *, c): pass",
157 "def f(a=1, /, b=2, *, c=4, **kwargs): pass",
158 "def f(a=1, /, b=2, *, c, **kwargs): pass",
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000159
Tim Peters400cbc32006-02-28 18:44:41 +0000160]
161
162# These are compiled through "single"
163# because of overlap with "eval", it just tests what
164# can't be tested with "eval"
165single_tests = [
166 "1+2"
167]
168
169# These are compiled through "eval"
170# It should test all expressions
171eval_tests = [
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500172 # None
173 "None",
Tim Peters400cbc32006-02-28 18:44:41 +0000174 # BoolOp
175 "a and b",
176 # BinOp
177 "a + b",
178 # UnaryOp
179 "not v",
180 # Lambda
181 "lambda:None",
182 # Dict
183 "{ 1:2 }",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500184 # Empty dict
185 "{}",
186 # Set
187 "{None,}",
188 # Multiline dict (test for .lineno & .col_offset)
189 """{
190 1
191 :
192 2
193 }""",
Tim Peters400cbc32006-02-28 18:44:41 +0000194 # ListComp
195 "[a for b in c if d]",
196 # GeneratorExp
197 "(a for b in c if d)",
Serhiy Storchakab619b092018-11-27 09:40:29 +0200198 # Comprehensions with multiple for targets
199 "[(a,b) for a,b in c]",
200 "[(a,b) for (a,b) in c]",
201 "[(a,b) for [a,b] in c]",
202 "{(a,b) for a,b in c}",
203 "{(a,b) for (a,b) in c}",
204 "{(a,b) for [a,b] in c}",
205 "((a,b) for a,b in c)",
206 "((a,b) for (a,b) in c)",
207 "((a,b) for [a,b] in c)",
Tim Peters400cbc32006-02-28 18:44:41 +0000208 # Yield - yield expressions can't work outside a function
209 #
210 # Compare
211 "1 < 2 < 3",
212 # Call
213 "f(1,2,c=3,*d,**e)",
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +0100214 # Call with multi-character starred
215 "f(*[0, 1])",
Serhiy Storchakab619b092018-11-27 09:40:29 +0200216 # Call with a generator argument
217 "f(a for a in b)",
Tim Peters400cbc32006-02-28 18:44:41 +0000218 # Num
Guido van Rossume2a383d2007-01-15 16:59:06 +0000219 "10",
Tim Peters400cbc32006-02-28 18:44:41 +0000220 # Str
221 "'string'",
222 # Attribute
223 "a.b",
224 # Subscript
225 "a[b:c]",
226 # Name
227 "v",
228 # List
229 "[1,2,3]",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500230 # Empty list
231 "[]",
Tim Peters400cbc32006-02-28 18:44:41 +0000232 # Tuple
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000233 "1,2,3",
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500234 # Tuple
235 "(1,2,3)",
236 # Empty tuple
237 "()",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000238 # Combination
239 "a.b.c.d(a.b[1:2])",
240
Tim Peters400cbc32006-02-28 18:44:41 +0000241]
242
243# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
244# excepthandler, arguments, keywords, alias
245
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000246class AST_Tests(unittest.TestCase):
Tim Peters400cbc32006-02-28 18:44:41 +0000247
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500248 def _assertTrueorder(self, ast_node, parent_pos):
Georg Brandl0c77a822008-06-10 16:37:50 +0000249 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000250 return
Georg Brandl0c77a822008-06-10 16:37:50 +0000251 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000252 node_pos = (ast_node.lineno, ast_node.col_offset)
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200253 self.assertGreaterEqual(node_pos, parent_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000254 parent_pos = (ast_node.lineno, ast_node.col_offset)
255 for name in ast_node._fields:
256 value = getattr(ast_node, name)
257 if isinstance(value, list):
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200258 first_pos = parent_pos
259 if value and name == 'decorator_list':
260 first_pos = (value[0].lineno, value[0].col_offset)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000261 for child in value:
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200262 self._assertTrueorder(child, first_pos)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000263 elif value is not None:
Benjamin Peterson7a66fc22015-02-02 10:51:20 -0500264 self._assertTrueorder(value, parent_pos)
Tim Peters5ddfe412006-03-01 23:02:57 +0000265
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500266 def test_AST_objects(self):
267 x = ast.AST()
268 self.assertEqual(x._fields, ())
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700269 x.foobar = 42
270 self.assertEqual(x.foobar, 42)
271 self.assertEqual(x.__dict__["foobar"], 42)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500272
273 with self.assertRaises(AttributeError):
274 x.vararg
275
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500276 with self.assertRaises(TypeError):
277 # "_ast.AST constructor takes 0 positional arguments"
278 ast.AST(2)
279
Benjamin Peterson9ed37432012-07-08 11:13:36 -0700280 def test_AST_garbage_collection(self):
281 class X:
282 pass
283 a = ast.AST()
284 a.x = X()
285 a.x.a = a
286 ref = weakref.ref(a.x)
287 del a
288 support.gc_collect()
289 self.assertIsNone(ref())
290
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000291 def test_snippets(self):
292 for input, output, kind in ((exec_tests, exec_results, "exec"),
293 (single_tests, single_results, "single"),
294 (eval_tests, eval_results, "eval")):
295 for i, o in zip(input, output):
Yury Selivanovb3d53132015-09-01 16:10:49 -0400296 with self.subTest(action="parsing", input=i):
297 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
298 self.assertEqual(to_tuple(ast_tree), o)
299 self._assertTrueorder(ast_tree, (0, 0))
Victor Stinner15a30952016-02-08 22:45:06 +0100300 with self.subTest(action="compiling", input=i, kind=kind):
301 compile(ast_tree, "?", kind)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000302
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000303 def test_ast_validation(self):
304 # compile() is the only function that calls PyAST_Validate
305 snippets_to_validate = exec_tests + single_tests + eval_tests
306 for snippet in snippets_to_validate:
307 tree = ast.parse(snippet)
308 compile(tree, '<string>', 'exec')
309
Benjamin Peterson78565b22009-06-28 19:19:51 +0000310 def test_slice(self):
311 slc = ast.parse("x[::]").body[0].value.slice
312 self.assertIsNone(slc.upper)
313 self.assertIsNone(slc.lower)
314 self.assertIsNone(slc.step)
315
316 def test_from_import(self):
317 im = ast.parse("from . import y").body[0]
318 self.assertIsNone(im.module)
319
Benjamin Petersona4e4e352012-03-22 08:19:04 -0400320 def test_non_interned_future_from_ast(self):
321 mod = ast.parse("from __future__ import division")
322 self.assertIsInstance(mod.body[0], ast.ImportFrom)
323 mod.body[0].module = " __future__ ".strip()
324 compile(mod, "<test>", "exec")
325
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000326 def test_base_classes(self):
327 self.assertTrue(issubclass(ast.For, ast.stmt))
328 self.assertTrue(issubclass(ast.Name, ast.expr))
329 self.assertTrue(issubclass(ast.stmt, ast.AST))
330 self.assertTrue(issubclass(ast.expr, ast.AST))
331 self.assertTrue(issubclass(ast.comprehension, ast.AST))
332 self.assertTrue(issubclass(ast.Gt, ast.AST))
333
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500334 def test_field_attr_existence(self):
335 for name, item in ast.__dict__.items():
336 if isinstance(item, type) and name != 'AST' and name[0].isupper():
337 x = item()
338 if isinstance(x, ast.AST):
339 self.assertEqual(type(x._fields), tuple)
340
341 def test_arguments(self):
342 x = ast.arguments()
Pablo Galindocd6e83b2019-07-15 01:32:18 +0200343 self.assertEqual(x._fields, ('posonlyargs', 'args', 'vararg', 'kwonlyargs',
344 'kw_defaults', 'kwarg', 'defaults'))
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500345
346 with self.assertRaises(AttributeError):
347 x.vararg
348
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100349 x = ast.arguments(*range(1, 8))
350 self.assertEqual(x.vararg, 3)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500351
352 def test_field_attr_writable(self):
353 x = ast.Num()
354 # We can assign to _fields
355 x._fields = 666
356 self.assertEqual(x._fields, 666)
357
358 def test_classattrs(self):
359 x = ast.Num()
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700360 self.assertEqual(x._fields, ('value', 'kind'))
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300361
362 with self.assertRaises(AttributeError):
363 x.value
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500364
365 with self.assertRaises(AttributeError):
366 x.n
367
368 x = ast.Num(42)
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300369 self.assertEqual(x.value, 42)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500370 self.assertEqual(x.n, 42)
371
372 with self.assertRaises(AttributeError):
373 x.lineno
374
375 with self.assertRaises(AttributeError):
376 x.foobar
377
378 x = ast.Num(lineno=2)
379 self.assertEqual(x.lineno, 2)
380
381 x = ast.Num(42, lineno=0)
382 self.assertEqual(x.lineno, 0)
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700383 self.assertEqual(x._fields, ('value', 'kind'))
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300384 self.assertEqual(x.value, 42)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500385 self.assertEqual(x.n, 42)
386
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700387 self.assertRaises(TypeError, ast.Num, 1, None, 2)
388 self.assertRaises(TypeError, ast.Num, 1, None, 2, lineno=0)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500389
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300390 self.assertEqual(ast.Num(42).n, 42)
391 self.assertEqual(ast.Num(4.25).n, 4.25)
392 self.assertEqual(ast.Num(4.25j).n, 4.25j)
393 self.assertEqual(ast.Str('42').s, '42')
394 self.assertEqual(ast.Bytes(b'42').s, b'42')
395 self.assertIs(ast.NameConstant(True).value, True)
396 self.assertIs(ast.NameConstant(False).value, False)
397 self.assertIs(ast.NameConstant(None).value, None)
398
399 self.assertEqual(ast.Constant(42).value, 42)
400 self.assertEqual(ast.Constant(4.25).value, 4.25)
401 self.assertEqual(ast.Constant(4.25j).value, 4.25j)
402 self.assertEqual(ast.Constant('42').value, '42')
403 self.assertEqual(ast.Constant(b'42').value, b'42')
404 self.assertIs(ast.Constant(True).value, True)
405 self.assertIs(ast.Constant(False).value, False)
406 self.assertIs(ast.Constant(None).value, None)
407 self.assertIs(ast.Constant(...).value, ...)
408
409 def test_realtype(self):
410 self.assertEqual(type(ast.Num(42)), ast.Constant)
411 self.assertEqual(type(ast.Num(4.25)), ast.Constant)
412 self.assertEqual(type(ast.Num(4.25j)), ast.Constant)
413 self.assertEqual(type(ast.Str('42')), ast.Constant)
414 self.assertEqual(type(ast.Bytes(b'42')), ast.Constant)
415 self.assertEqual(type(ast.NameConstant(True)), ast.Constant)
416 self.assertEqual(type(ast.NameConstant(False)), ast.Constant)
417 self.assertEqual(type(ast.NameConstant(None)), ast.Constant)
418 self.assertEqual(type(ast.Ellipsis()), ast.Constant)
419
420 def test_isinstance(self):
421 self.assertTrue(isinstance(ast.Num(42), ast.Num))
422 self.assertTrue(isinstance(ast.Num(4.2), ast.Num))
423 self.assertTrue(isinstance(ast.Num(4.2j), ast.Num))
424 self.assertTrue(isinstance(ast.Str('42'), ast.Str))
425 self.assertTrue(isinstance(ast.Bytes(b'42'), ast.Bytes))
426 self.assertTrue(isinstance(ast.NameConstant(True), ast.NameConstant))
427 self.assertTrue(isinstance(ast.NameConstant(False), ast.NameConstant))
428 self.assertTrue(isinstance(ast.NameConstant(None), ast.NameConstant))
429 self.assertTrue(isinstance(ast.Ellipsis(), ast.Ellipsis))
430
431 self.assertTrue(isinstance(ast.Constant(42), ast.Num))
432 self.assertTrue(isinstance(ast.Constant(4.2), ast.Num))
433 self.assertTrue(isinstance(ast.Constant(4.2j), ast.Num))
434 self.assertTrue(isinstance(ast.Constant('42'), ast.Str))
435 self.assertTrue(isinstance(ast.Constant(b'42'), ast.Bytes))
436 self.assertTrue(isinstance(ast.Constant(True), ast.NameConstant))
437 self.assertTrue(isinstance(ast.Constant(False), ast.NameConstant))
438 self.assertTrue(isinstance(ast.Constant(None), ast.NameConstant))
439 self.assertTrue(isinstance(ast.Constant(...), ast.Ellipsis))
440
441 self.assertFalse(isinstance(ast.Str('42'), ast.Num))
442 self.assertFalse(isinstance(ast.Num(42), ast.Str))
443 self.assertFalse(isinstance(ast.Str('42'), ast.Bytes))
444 self.assertFalse(isinstance(ast.Num(42), ast.NameConstant))
445 self.assertFalse(isinstance(ast.Num(42), ast.Ellipsis))
Anthony Sottile74176222019-01-18 11:30:28 -0800446 self.assertFalse(isinstance(ast.NameConstant(True), ast.Num))
447 self.assertFalse(isinstance(ast.NameConstant(False), ast.Num))
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300448
449 self.assertFalse(isinstance(ast.Constant('42'), ast.Num))
450 self.assertFalse(isinstance(ast.Constant(42), ast.Str))
451 self.assertFalse(isinstance(ast.Constant('42'), ast.Bytes))
452 self.assertFalse(isinstance(ast.Constant(42), ast.NameConstant))
453 self.assertFalse(isinstance(ast.Constant(42), ast.Ellipsis))
Anthony Sottile74176222019-01-18 11:30:28 -0800454 self.assertFalse(isinstance(ast.Constant(True), ast.Num))
455 self.assertFalse(isinstance(ast.Constant(False), ast.Num))
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300456
457 self.assertFalse(isinstance(ast.Constant(), ast.Num))
458 self.assertFalse(isinstance(ast.Constant(), ast.Str))
459 self.assertFalse(isinstance(ast.Constant(), ast.Bytes))
460 self.assertFalse(isinstance(ast.Constant(), ast.NameConstant))
461 self.assertFalse(isinstance(ast.Constant(), ast.Ellipsis))
462
Serhiy Storchaka6015cc52018-10-28 13:43:03 +0200463 class S(str): pass
464 self.assertTrue(isinstance(ast.Constant(S('42')), ast.Str))
465 self.assertFalse(isinstance(ast.Constant(S('42')), ast.Num))
466
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300467 def test_subclasses(self):
468 class N(ast.Num):
469 def __init__(self, *args, **kwargs):
470 super().__init__(*args, **kwargs)
471 self.z = 'spam'
472 class N2(ast.Num):
473 pass
474
475 n = N(42)
476 self.assertEqual(n.n, 42)
477 self.assertEqual(n.z, 'spam')
478 self.assertEqual(type(n), N)
479 self.assertTrue(isinstance(n, N))
480 self.assertTrue(isinstance(n, ast.Num))
481 self.assertFalse(isinstance(n, N2))
482 self.assertFalse(isinstance(ast.Num(42), N))
483 n = N(n=42)
484 self.assertEqual(n.n, 42)
485 self.assertEqual(type(n), N)
486
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500487 def test_module(self):
488 body = [ast.Num(42)]
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800489 x = ast.Module(body, [])
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500490 self.assertEqual(x.body, body)
491
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000492 def test_nodeclasses(self):
Florent Xicluna992d9e02011-11-11 19:35:42 +0100493 # Zero arguments constructor explicitly allowed
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500494 x = ast.BinOp()
495 self.assertEqual(x._fields, ('left', 'op', 'right'))
496
497 # Random attribute allowed too
498 x.foobarbaz = 5
499 self.assertEqual(x.foobarbaz, 5)
500
501 n1 = ast.Num(1)
502 n3 = ast.Num(3)
503 addop = ast.Add()
504 x = ast.BinOp(n1, addop, n3)
505 self.assertEqual(x.left, n1)
506 self.assertEqual(x.op, addop)
507 self.assertEqual(x.right, n3)
Benjamin Peterson68b543a2011-06-27 17:51:18 -0500508
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500509 x = ast.BinOp(1, 2, 3)
510 self.assertEqual(x.left, 1)
511 self.assertEqual(x.op, 2)
512 self.assertEqual(x.right, 3)
513
Georg Brandl0c77a822008-06-10 16:37:50 +0000514 x = ast.BinOp(1, 2, 3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000515 self.assertEqual(x.left, 1)
516 self.assertEqual(x.op, 2)
517 self.assertEqual(x.right, 3)
518 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000519
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500520 # node raises exception when given too many arguments
521 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500522 # node raises exception when given too many arguments
523 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000524
525 # can set attributes through kwargs too
Georg Brandl0c77a822008-06-10 16:37:50 +0000526 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000527 self.assertEqual(x.left, 1)
528 self.assertEqual(x.op, 2)
529 self.assertEqual(x.right, 3)
530 self.assertEqual(x.lineno, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000531
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500532 # Random kwargs also allowed
533 x = ast.BinOp(1, 2, 3, foobarbaz=42)
534 self.assertEqual(x.foobarbaz, 42)
535
536 def test_no_fields(self):
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000537 # this used to fail because Sub._fields was None
Georg Brandl0c77a822008-06-10 16:37:50 +0000538 x = ast.Sub()
Benjamin Peterson6ccfe852011-06-27 17:46:06 -0500539 self.assertEqual(x._fields, ())
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000540
541 def test_pickling(self):
542 import pickle
543 mods = [pickle]
544 try:
545 import cPickle
546 mods.append(cPickle)
547 except ImportError:
548 pass
549 protocols = [0, 1, 2]
550 for mod in mods:
551 for protocol in protocols:
552 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
553 ast2 = mod.loads(mod.dumps(ast, protocol))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000554 self.assertEqual(to_tuple(ast2), to_tuple(ast))
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000555
Benjamin Peterson5b066812010-11-20 01:38:49 +0000556 def test_invalid_sum(self):
557 pos = dict(lineno=2, col_offset=3)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800558 m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
Benjamin Peterson5b066812010-11-20 01:38:49 +0000559 with self.assertRaises(TypeError) as cm:
560 compile(m, "<test>", "exec")
561 self.assertIn("but got <_ast.expr", str(cm.exception))
562
Min ho Kimc4cacc82019-07-31 08:16:13 +1000563 def test_invalid_identifier(self):
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800564 m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500565 ast.fix_missing_locations(m)
566 with self.assertRaises(TypeError) as cm:
567 compile(m, "<test>", "exec")
568 self.assertIn("identifier must be of type str", str(cm.exception))
569
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000570 def test_empty_yield_from(self):
571 # Issue 16546: yield from value is not optional.
572 empty_yield_from = ast.parse("def f():\n yield from g()")
573 empty_yield_from.body[0].body[0].value.value = None
574 with self.assertRaises(ValueError) as cm:
575 compile(empty_yield_from, "<test>", "exec")
576 self.assertIn("field value is required", str(cm.exception))
577
Oren Milman7dc46d82017-09-30 20:16:24 +0300578 @support.cpython_only
579 def test_issue31592(self):
580 # There shouldn't be an assertion failure in case of a bad
581 # unicodedata.normalize().
582 import unicodedata
583 def bad_normalize(*args):
584 return None
585 with support.swap_attr(unicodedata, 'normalize', bad_normalize):
586 self.assertRaises(TypeError, ast.parse, '\u03D5')
587
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +0200588 def test_issue18374_binop_col_offset(self):
589 tree = ast.parse('4+5+6+7')
590 parent_binop = tree.body[0].value
591 child_binop = parent_binop.left
592 grandchild_binop = child_binop.left
593 self.assertEqual(parent_binop.col_offset, 0)
594 self.assertEqual(parent_binop.end_col_offset, 7)
595 self.assertEqual(child_binop.col_offset, 0)
596 self.assertEqual(child_binop.end_col_offset, 5)
597 self.assertEqual(grandchild_binop.col_offset, 0)
598 self.assertEqual(grandchild_binop.end_col_offset, 3)
599
600 tree = ast.parse('4+5-\\\n 6-7')
601 parent_binop = tree.body[0].value
602 child_binop = parent_binop.left
603 grandchild_binop = child_binop.left
604 self.assertEqual(parent_binop.col_offset, 0)
605 self.assertEqual(parent_binop.lineno, 1)
606 self.assertEqual(parent_binop.end_col_offset, 4)
607 self.assertEqual(parent_binop.end_lineno, 2)
608
609 self.assertEqual(child_binop.col_offset, 0)
Carl Friedrich Bolz-Tereick430a9f42019-07-09 14:20:01 +0200610 self.assertEqual(child_binop.lineno, 1)
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +0200611 self.assertEqual(child_binop.end_col_offset, 2)
Carl Friedrich Bolz-Tereick430a9f42019-07-09 14:20:01 +0200612 self.assertEqual(child_binop.end_lineno, 2)
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +0200613
614 self.assertEqual(grandchild_binop.col_offset, 0)
Carl Friedrich Bolz-Tereick430a9f42019-07-09 14:20:01 +0200615 self.assertEqual(grandchild_binop.lineno, 1)
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +0200616 self.assertEqual(grandchild_binop.end_col_offset, 3)
Carl Friedrich Bolz-Tereick430a9f42019-07-09 14:20:01 +0200617 self.assertEqual(grandchild_binop.end_lineno, 1)
Georg Brandl0c77a822008-06-10 16:37:50 +0000618
619class ASTHelpers_Test(unittest.TestCase):
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700620 maxDiff = None
Georg Brandl0c77a822008-06-10 16:37:50 +0000621
622 def test_parse(self):
623 a = ast.parse('foo(1 + 1)')
624 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
625 self.assertEqual(ast.dump(a), ast.dump(b))
626
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400627 def test_parse_in_error(self):
628 try:
629 1/0
630 except Exception:
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400631 with self.assertRaises(SyntaxError) as e:
632 ast.literal_eval(r"'\U'")
633 self.assertIsNotNone(e.exception.__context__)
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400634
Georg Brandl0c77a822008-06-10 16:37:50 +0000635 def test_dump(self):
636 node = ast.parse('spam(eggs, "and cheese")')
637 self.assertEqual(ast.dump(node),
638 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700639 "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese', kind=None)], "
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800640 "keywords=[]))], type_ignores=[])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000641 )
642 self.assertEqual(ast.dump(node, annotate_fields=False),
643 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700644 "Constant('and cheese', None)], []))], [])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000645 )
646 self.assertEqual(ast.dump(node, include_attributes=True),
647 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000648 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), "
649 "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, "
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700650 "end_lineno=1, end_col_offset=9), Constant(value='and cheese', kind=None, "
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000651 "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], "
652 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), "
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800653 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000654 )
655
Serhiy Storchaka850573b2019-09-09 19:33:13 +0300656 def test_dump_indent(self):
657 node = ast.parse('spam(eggs, "and cheese")')
658 self.assertEqual(ast.dump(node, indent=3), """\
659Module(
660 body=[
661 Expr(
662 value=Call(
663 func=Name(id='spam', ctx=Load()),
664 args=[
665 Name(id='eggs', ctx=Load()),
666 Constant(value='and cheese', kind=None)],
667 keywords=[]))],
668 type_ignores=[])""")
669 self.assertEqual(ast.dump(node, annotate_fields=False, indent='\t'), """\
670Module(
671\t[
672\t\tExpr(
673\t\t\tCall(
674\t\t\t\tName('spam', Load()),
675\t\t\t\t[
676\t\t\t\t\tName('eggs', Load()),
677\t\t\t\t\tConstant('and cheese', None)],
678\t\t\t\t[]))],
679\t[])""")
680 self.assertEqual(ast.dump(node, include_attributes=True, indent=3), """\
681Module(
682 body=[
683 Expr(
684 value=Call(
685 func=Name(
686 id='spam',
687 ctx=Load(),
688 lineno=1,
689 col_offset=0,
690 end_lineno=1,
691 end_col_offset=4),
692 args=[
693 Name(
694 id='eggs',
695 ctx=Load(),
696 lineno=1,
697 col_offset=5,
698 end_lineno=1,
699 end_col_offset=9),
700 Constant(
701 value='and cheese',
702 kind=None,
703 lineno=1,
704 col_offset=11,
705 end_lineno=1,
706 end_col_offset=23)],
707 keywords=[],
708 lineno=1,
709 col_offset=0,
710 end_lineno=1,
711 end_col_offset=24),
712 lineno=1,
713 col_offset=0,
714 end_lineno=1,
715 end_col_offset=24)],
716 type_ignores=[])""")
717
Serhiy Storchakae64f9482019-08-29 09:30:23 +0300718 def test_dump_incomplete(self):
719 node = ast.Raise(lineno=3, col_offset=4)
720 self.assertEqual(ast.dump(node),
721 "Raise()"
722 )
723 self.assertEqual(ast.dump(node, include_attributes=True),
724 "Raise(lineno=3, col_offset=4)"
725 )
726 node = ast.Raise(exc=ast.Name(id='e', ctx=ast.Load()), lineno=3, col_offset=4)
727 self.assertEqual(ast.dump(node),
728 "Raise(exc=Name(id='e', ctx=Load()))"
729 )
730 self.assertEqual(ast.dump(node, annotate_fields=False),
731 "Raise(Name('e', Load()))"
732 )
733 self.assertEqual(ast.dump(node, include_attributes=True),
734 "Raise(exc=Name(id='e', ctx=Load()), lineno=3, col_offset=4)"
735 )
736 self.assertEqual(ast.dump(node, annotate_fields=False, include_attributes=True),
737 "Raise(Name('e', Load()), lineno=3, col_offset=4)"
738 )
739 node = ast.Raise(cause=ast.Name(id='e', ctx=ast.Load()))
740 self.assertEqual(ast.dump(node),
741 "Raise(cause=Name(id='e', ctx=Load()))"
742 )
743 self.assertEqual(ast.dump(node, annotate_fields=False),
744 "Raise(cause=Name('e', Load()))"
745 )
746
Georg Brandl0c77a822008-06-10 16:37:50 +0000747 def test_copy_location(self):
748 src = ast.parse('1 + 1', mode='eval')
749 src.body.right = ast.copy_location(ast.Num(2), src.body.right)
750 self.assertEqual(ast.dump(src, include_attributes=True),
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700751 'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=1, col_offset=0, '
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000752 'end_lineno=1, end_col_offset=1), op=Add(), right=Constant(value=2, '
753 'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, '
754 'col_offset=0, end_lineno=1, end_col_offset=5))'
Georg Brandl0c77a822008-06-10 16:37:50 +0000755 )
756
757 def test_fix_missing_locations(self):
758 src = ast.parse('write("spam")')
759 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400760 [ast.Str('eggs')], [])))
Georg Brandl0c77a822008-06-10 16:37:50 +0000761 self.assertEqual(src, ast.fix_missing_locations(src))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000762 self.maxDiff = None
Georg Brandl0c77a822008-06-10 16:37:50 +0000763 self.assertEqual(ast.dump(src, include_attributes=True),
764 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000765 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), "
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700766 "args=[Constant(value='spam', kind=None, lineno=1, col_offset=6, end_lineno=1, "
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000767 "end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
768 "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, "
769 "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), "
770 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), "
771 "args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, "
772 "end_col_offset=0)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800773 "end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)], "
774 "type_ignores=[])"
Georg Brandl0c77a822008-06-10 16:37:50 +0000775 )
776
777 def test_increment_lineno(self):
778 src = ast.parse('1 + 1', mode='eval')
779 self.assertEqual(ast.increment_lineno(src, n=3), src)
780 self.assertEqual(ast.dump(src, include_attributes=True),
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700781 'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=4, col_offset=0, '
782 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, kind=None, '
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000783 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
784 'col_offset=0, end_lineno=4, end_col_offset=5))'
Georg Brandl0c77a822008-06-10 16:37:50 +0000785 )
Georg Brandl619e7ba2011-01-09 07:38:51 +0000786 # issue10869: do not increment lineno of root twice
Georg Brandlefb69022011-01-09 07:50:48 +0000787 src = ast.parse('1 + 1', mode='eval')
Georg Brandl619e7ba2011-01-09 07:38:51 +0000788 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
789 self.assertEqual(ast.dump(src, include_attributes=True),
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700790 'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=4, col_offset=0, '
791 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, kind=None, '
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000792 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
793 'col_offset=0, end_lineno=4, end_col_offset=5))'
Georg Brandl619e7ba2011-01-09 07:38:51 +0000794 )
Georg Brandl0c77a822008-06-10 16:37:50 +0000795
796 def test_iter_fields(self):
797 node = ast.parse('foo()', mode='eval')
798 d = dict(ast.iter_fields(node.body))
799 self.assertEqual(d.pop('func').id, 'foo')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400800 self.assertEqual(d, {'keywords': [], 'args': []})
Georg Brandl0c77a822008-06-10 16:37:50 +0000801
802 def test_iter_child_nodes(self):
803 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
804 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
805 iterator = ast.iter_child_nodes(node.body)
806 self.assertEqual(next(iterator).id, 'spam')
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300807 self.assertEqual(next(iterator).value, 23)
808 self.assertEqual(next(iterator).value, 42)
Georg Brandl0c77a822008-06-10 16:37:50 +0000809 self.assertEqual(ast.dump(next(iterator)),
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700810 "keyword(arg='eggs', value=Constant(value='leek', kind=None))"
Georg Brandl0c77a822008-06-10 16:37:50 +0000811 )
812
813 def test_get_docstring(self):
Serhiy Storchaka08f127a2018-06-15 11:05:15 +0300814 node = ast.parse('"""line one\n line two"""')
815 self.assertEqual(ast.get_docstring(node),
816 'line one\nline two')
817
818 node = ast.parse('class foo:\n """line one\n line two"""')
819 self.assertEqual(ast.get_docstring(node.body[0]),
820 'line one\nline two')
821
Georg Brandl0c77a822008-06-10 16:37:50 +0000822 node = ast.parse('def foo():\n """line one\n line two"""')
823 self.assertEqual(ast.get_docstring(node.body[0]),
824 'line one\nline two')
825
Yury Selivanov2f07a662015-07-23 08:54:35 +0300826 node = ast.parse('async def foo():\n """spam\n ham"""')
827 self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
Serhiy Storchaka08f127a2018-06-15 11:05:15 +0300828
829 def test_get_docstring_none(self):
Matthias Bussonnier41cea702017-02-23 22:44:19 -0800830 self.assertIsNone(ast.get_docstring(ast.parse('')))
Serhiy Storchaka08f127a2018-06-15 11:05:15 +0300831 node = ast.parse('x = "not docstring"')
832 self.assertIsNone(ast.get_docstring(node))
833 node = ast.parse('def foo():\n pass')
834 self.assertIsNone(ast.get_docstring(node))
835
836 node = ast.parse('class foo:\n pass')
837 self.assertIsNone(ast.get_docstring(node.body[0]))
838 node = ast.parse('class foo:\n x = "not docstring"')
839 self.assertIsNone(ast.get_docstring(node.body[0]))
840 node = ast.parse('class foo:\n def bar(self): pass')
841 self.assertIsNone(ast.get_docstring(node.body[0]))
842
843 node = ast.parse('def foo():\n pass')
844 self.assertIsNone(ast.get_docstring(node.body[0]))
845 node = ast.parse('def foo():\n x = "not docstring"')
846 self.assertIsNone(ast.get_docstring(node.body[0]))
847
848 node = ast.parse('async def foo():\n pass')
849 self.assertIsNone(ast.get_docstring(node.body[0]))
850 node = ast.parse('async def foo():\n x = "not docstring"')
851 self.assertIsNone(ast.get_docstring(node.body[0]))
Yury Selivanov2f07a662015-07-23 08:54:35 +0300852
Anthony Sottile995d9b92019-01-12 20:05:13 -0800853 def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
854 node = ast.parse(
855 '"""line one\nline two"""\n\n'
856 'def foo():\n """line one\n line two"""\n\n'
857 ' def bar():\n """line one\n line two"""\n'
858 ' """line one\n line two"""\n'
859 '"""line one\nline two"""\n\n'
860 )
861 self.assertEqual(node.body[0].col_offset, 0)
862 self.assertEqual(node.body[0].lineno, 1)
863 self.assertEqual(node.body[1].body[0].col_offset, 2)
864 self.assertEqual(node.body[1].body[0].lineno, 5)
865 self.assertEqual(node.body[1].body[1].body[0].col_offset, 4)
866 self.assertEqual(node.body[1].body[1].body[0].lineno, 9)
867 self.assertEqual(node.body[1].body[2].col_offset, 2)
868 self.assertEqual(node.body[1].body[2].lineno, 11)
869 self.assertEqual(node.body[2].col_offset, 0)
870 self.assertEqual(node.body[2].lineno, 13)
871
Lysandros Nikolaou025a6022019-12-12 22:40:21 +0100872 def test_elif_stmt_start_position(self):
873 node = ast.parse('if a:\n pass\nelif b:\n pass\n')
874 elif_stmt = node.body[0].orelse[0]
875 self.assertEqual(elif_stmt.lineno, 3)
876 self.assertEqual(elif_stmt.col_offset, 0)
877
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +0100878 def test_elif_stmt_start_position_with_else(self):
879 node = ast.parse('if a:\n pass\nelif b:\n pass\nelse:\n pass\n')
880 elif_stmt = node.body[0].orelse[0]
881 self.assertEqual(elif_stmt.lineno, 3)
882 self.assertEqual(elif_stmt.col_offset, 0)
883
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +0100884 def test_starred_expr_end_position_within_call(self):
885 node = ast.parse('f(*[0, 1])')
886 starred_expr = node.body[0].value.args[0]
887 self.assertEqual(starred_expr.end_lineno, 1)
888 self.assertEqual(starred_expr.end_col_offset, 9)
889
Georg Brandl0c77a822008-06-10 16:37:50 +0000890 def test_literal_eval(self):
891 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
892 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
893 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
Benjamin Peterson3e742892010-07-11 12:59:24 +0000894 self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
Benjamin Peterson5ef96e52010-07-11 23:06:06 +0000895 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
Raymond Hettinger4fcf5c12020-01-02 22:21:18 -0700896 self.assertEqual(ast.literal_eval('set()'), set())
Georg Brandl0c77a822008-06-10 16:37:50 +0000897 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +0200898 self.assertEqual(ast.literal_eval('6'), 6)
899 self.assertEqual(ast.literal_eval('+6'), 6)
Raymond Hettingerbc959732010-10-08 00:47:45 +0000900 self.assertEqual(ast.literal_eval('-6'), -6)
Raymond Hettingerbc959732010-10-08 00:47:45 +0000901 self.assertEqual(ast.literal_eval('3.25'), 3.25)
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +0200902 self.assertEqual(ast.literal_eval('+3.25'), 3.25)
903 self.assertEqual(ast.literal_eval('-3.25'), -3.25)
904 self.assertEqual(repr(ast.literal_eval('-0.0')), '-0.0')
905 self.assertRaises(ValueError, ast.literal_eval, '++6')
906 self.assertRaises(ValueError, ast.literal_eval, '+True')
907 self.assertRaises(ValueError, ast.literal_eval, '2+3')
Georg Brandl0c77a822008-06-10 16:37:50 +0000908
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +0200909 def test_literal_eval_complex(self):
910 # Issue #4907
911 self.assertEqual(ast.literal_eval('6j'), 6j)
912 self.assertEqual(ast.literal_eval('-6j'), -6j)
913 self.assertEqual(ast.literal_eval('6.75j'), 6.75j)
914 self.assertEqual(ast.literal_eval('-6.75j'), -6.75j)
915 self.assertEqual(ast.literal_eval('3+6j'), 3+6j)
916 self.assertEqual(ast.literal_eval('-3+6j'), -3+6j)
917 self.assertEqual(ast.literal_eval('3-6j'), 3-6j)
918 self.assertEqual(ast.literal_eval('-3-6j'), -3-6j)
919 self.assertEqual(ast.literal_eval('3.25+6.75j'), 3.25+6.75j)
920 self.assertEqual(ast.literal_eval('-3.25+6.75j'), -3.25+6.75j)
921 self.assertEqual(ast.literal_eval('3.25-6.75j'), 3.25-6.75j)
922 self.assertEqual(ast.literal_eval('-3.25-6.75j'), -3.25-6.75j)
923 self.assertEqual(ast.literal_eval('(3+6j)'), 3+6j)
924 self.assertRaises(ValueError, ast.literal_eval, '-6j+3')
925 self.assertRaises(ValueError, ast.literal_eval, '-6j+3j')
926 self.assertRaises(ValueError, ast.literal_eval, '3+-6j')
927 self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)')
928 self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)')
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000929
Amaury Forgeot d'Arc58e87612011-11-22 21:51:55 +0100930 def test_bad_integer(self):
931 # issue13436: Bad error message with invalid numeric values
932 body = [ast.ImportFrom(module='time',
933 names=[ast.alias(name='sleep')],
934 level=None,
935 lineno=None, col_offset=None)]
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800936 mod = ast.Module(body, [])
Amaury Forgeot d'Arc58e87612011-11-22 21:51:55 +0100937 with self.assertRaises(ValueError) as cm:
938 compile(mod, 'test', 'exec')
939 self.assertIn("invalid integer value: None", str(cm.exception))
940
Berker Peksag0a5bd512016-04-29 19:50:02 +0300941 def test_level_as_none(self):
942 body = [ast.ImportFrom(module='time',
943 names=[ast.alias(name='sleep')],
944 level=None,
945 lineno=0, col_offset=0)]
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800946 mod = ast.Module(body, [])
Berker Peksag0a5bd512016-04-29 19:50:02 +0300947 code = compile(mod, 'test', 'exec')
948 ns = {}
949 exec(code, ns)
950 self.assertIn('sleep', ns)
951
Georg Brandl0c77a822008-06-10 16:37:50 +0000952
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500953class ASTValidatorTests(unittest.TestCase):
954
955 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
956 mod.lineno = mod.col_offset = 0
957 ast.fix_missing_locations(mod)
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300958 if msg is None:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500959 compile(mod, "<test>", mode)
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300960 else:
961 with self.assertRaises(exc) as cm:
962 compile(mod, "<test>", mode)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500963 self.assertIn(msg, str(cm.exception))
964
965 def expr(self, node, msg=None, *, exc=ValueError):
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800966 mod = ast.Module([ast.Expr(node)], [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500967 self.mod(mod, msg, exc=exc)
968
969 def stmt(self, stmt, msg=None):
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800970 mod = ast.Module([stmt], [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500971 self.mod(mod, msg)
972
973 def test_module(self):
974 m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
975 self.mod(m, "must have Load context", "single")
976 m = ast.Expression(ast.Name("x", ast.Store()))
977 self.mod(m, "must have Load context", "eval")
978
979 def _check_arguments(self, fac, check):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100980 def arguments(args=None, posonlyargs=None, vararg=None,
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700981 kwonlyargs=None, kwarg=None,
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500982 defaults=None, kw_defaults=None):
983 if args is None:
984 args = []
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100985 if posonlyargs is None:
986 posonlyargs = []
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500987 if kwonlyargs is None:
988 kwonlyargs = []
989 if defaults is None:
990 defaults = []
991 if kw_defaults is None:
992 kw_defaults = []
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100993 args = ast.arguments(args, posonlyargs, vararg, kwonlyargs,
994 kw_defaults, kwarg, defaults)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500995 return fac(args)
996 args = [ast.arg("x", ast.Name("x", ast.Store()))]
997 check(arguments(args=args), "must have Load context")
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100998 check(arguments(posonlyargs=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500999 check(arguments(kwonlyargs=args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001000 check(arguments(defaults=[ast.Num(3)]),
1001 "more positional defaults than args")
1002 check(arguments(kw_defaults=[ast.Num(4)]),
1003 "length of kwonlyargs is not the same as kw_defaults")
1004 args = [ast.arg("x", ast.Name("x", ast.Load()))]
1005 check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
1006 "must have Load context")
1007 args = [ast.arg("a", ast.Name("x", ast.Load())),
1008 ast.arg("b", ast.Name("y", ast.Load()))]
1009 check(arguments(kwonlyargs=args,
1010 kw_defaults=[None, ast.Name("x", ast.Store())]),
1011 "must have Load context")
1012
1013 def test_funcdef(self):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001014 a = ast.arguments([], [], None, [], [], None, [])
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001015 f = ast.FunctionDef("x", a, [], [], None)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001016 self.stmt(f, "empty body on FunctionDef")
1017 f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001018 None)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001019 self.stmt(f, "must have Load context")
1020 f = ast.FunctionDef("x", a, [ast.Pass()], [],
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001021 ast.Name("x", ast.Store()))
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001022 self.stmt(f, "must have Load context")
1023 def fac(args):
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001024 return ast.FunctionDef("x", args, [ast.Pass()], [], None)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001025 self._check_arguments(fac, self.stmt)
1026
1027 def test_classdef(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001028 def cls(bases=None, keywords=None, body=None, decorator_list=None):
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001029 if bases is None:
1030 bases = []
1031 if keywords is None:
1032 keywords = []
1033 if body is None:
1034 body = [ast.Pass()]
1035 if decorator_list is None:
1036 decorator_list = []
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001037 return ast.ClassDef("myclass", bases, keywords,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001038 body, decorator_list)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001039 self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
1040 "must have Load context")
1041 self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
1042 "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001043 self.stmt(cls(body=[]), "empty body on ClassDef")
1044 self.stmt(cls(body=[None]), "None disallowed")
1045 self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
1046 "must have Load context")
1047
1048 def test_delete(self):
1049 self.stmt(ast.Delete([]), "empty targets on Delete")
1050 self.stmt(ast.Delete([None]), "None disallowed")
1051 self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
1052 "must have Del context")
1053
1054 def test_assign(self):
1055 self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
1056 self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
1057 self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
1058 "must have Store context")
1059 self.stmt(ast.Assign([ast.Name("x", ast.Store())],
1060 ast.Name("y", ast.Store())),
1061 "must have Load context")
1062
1063 def test_augassign(self):
1064 aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
1065 ast.Name("y", ast.Load()))
1066 self.stmt(aug, "must have Store context")
1067 aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
1068 ast.Name("y", ast.Store()))
1069 self.stmt(aug, "must have Load context")
1070
1071 def test_for(self):
1072 x = ast.Name("x", ast.Store())
1073 y = ast.Name("y", ast.Load())
1074 p = ast.Pass()
1075 self.stmt(ast.For(x, y, [], []), "empty body on For")
1076 self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
1077 "must have Store context")
1078 self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
1079 "must have Load context")
1080 e = ast.Expr(ast.Name("x", ast.Store()))
1081 self.stmt(ast.For(x, y, [e], []), "must have Load context")
1082 self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
1083
1084 def test_while(self):
1085 self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
1086 self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
1087 "must have Load context")
1088 self.stmt(ast.While(ast.Num(3), [ast.Pass()],
1089 [ast.Expr(ast.Name("x", ast.Store()))]),
1090 "must have Load context")
1091
1092 def test_if(self):
1093 self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
1094 i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
1095 self.stmt(i, "must have Load context")
1096 i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
1097 self.stmt(i, "must have Load context")
1098 i = ast.If(ast.Num(3), [ast.Pass()],
1099 [ast.Expr(ast.Name("x", ast.Store()))])
1100 self.stmt(i, "must have Load context")
1101
1102 def test_with(self):
1103 p = ast.Pass()
1104 self.stmt(ast.With([], [p]), "empty items on With")
1105 i = ast.withitem(ast.Num(3), None)
1106 self.stmt(ast.With([i], []), "empty body on With")
1107 i = ast.withitem(ast.Name("x", ast.Store()), None)
1108 self.stmt(ast.With([i], [p]), "must have Load context")
1109 i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
1110 self.stmt(ast.With([i], [p]), "must have Store context")
1111
1112 def test_raise(self):
1113 r = ast.Raise(None, ast.Num(3))
1114 self.stmt(r, "Raise with cause but no exception")
1115 r = ast.Raise(ast.Name("x", ast.Store()), None)
1116 self.stmt(r, "must have Load context")
1117 r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
1118 self.stmt(r, "must have Load context")
1119
1120 def test_try(self):
1121 p = ast.Pass()
1122 t = ast.Try([], [], [], [p])
1123 self.stmt(t, "empty body on Try")
1124 t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
1125 self.stmt(t, "must have Load context")
1126 t = ast.Try([p], [], [], [])
1127 self.stmt(t, "Try has neither except handlers nor finalbody")
1128 t = ast.Try([p], [], [p], [p])
1129 self.stmt(t, "Try has orelse but no except handlers")
1130 t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
1131 self.stmt(t, "empty body on ExceptHandler")
1132 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
1133 self.stmt(ast.Try([p], e, [], []), "must have Load context")
1134 e = [ast.ExceptHandler(None, "x", [p])]
1135 t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
1136 self.stmt(t, "must have Load context")
1137 t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
1138 self.stmt(t, "must have Load context")
1139
1140 def test_assert(self):
1141 self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
1142 "must have Load context")
1143 assrt = ast.Assert(ast.Name("x", ast.Load()),
1144 ast.Name("y", ast.Store()))
1145 self.stmt(assrt, "must have Load context")
1146
1147 def test_import(self):
1148 self.stmt(ast.Import([]), "empty names on Import")
1149
1150 def test_importfrom(self):
1151 imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
Serhiy Storchaka7de28402016-06-27 23:40:43 +03001152 self.stmt(imp, "Negative ImportFrom level")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001153 self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
1154
1155 def test_global(self):
1156 self.stmt(ast.Global([]), "empty names on Global")
1157
1158 def test_nonlocal(self):
1159 self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
1160
1161 def test_expr(self):
1162 e = ast.Expr(ast.Name("x", ast.Store()))
1163 self.stmt(e, "must have Load context")
1164
1165 def test_boolop(self):
1166 b = ast.BoolOp(ast.And(), [])
1167 self.expr(b, "less than 2 values")
1168 b = ast.BoolOp(ast.And(), [ast.Num(3)])
1169 self.expr(b, "less than 2 values")
1170 b = ast.BoolOp(ast.And(), [ast.Num(4), None])
1171 self.expr(b, "None disallowed")
1172 b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
1173 self.expr(b, "must have Load context")
1174
1175 def test_unaryop(self):
1176 u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
1177 self.expr(u, "must have Load context")
1178
1179 def test_lambda(self):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001180 a = ast.arguments([], [], None, [], [], None, [])
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001181 self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
1182 "must have Load context")
1183 def fac(args):
1184 return ast.Lambda(args, ast.Name("x", ast.Load()))
1185 self._check_arguments(fac, self.expr)
1186
1187 def test_ifexp(self):
1188 l = ast.Name("x", ast.Load())
1189 s = ast.Name("y", ast.Store())
1190 for args in (s, l, l), (l, s, l), (l, l, s):
Benjamin Peterson71ce8972011-08-09 16:17:12 -05001191 self.expr(ast.IfExp(*args), "must have Load context")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001192
1193 def test_dict(self):
1194 d = ast.Dict([], [ast.Name("x", ast.Load())])
1195 self.expr(d, "same number of keys as values")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001196 d = ast.Dict([ast.Name("x", ast.Load())], [None])
1197 self.expr(d, "None disallowed")
1198
1199 def test_set(self):
1200 self.expr(ast.Set([None]), "None disallowed")
1201 s = ast.Set([ast.Name("x", ast.Store())])
1202 self.expr(s, "must have Load context")
1203
1204 def _check_comprehension(self, fac):
1205 self.expr(fac([]), "comprehension with no generators")
1206 g = ast.comprehension(ast.Name("x", ast.Load()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001207 ast.Name("x", ast.Load()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001208 self.expr(fac([g]), "must have Store context")
1209 g = ast.comprehension(ast.Name("x", ast.Store()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001210 ast.Name("x", ast.Store()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001211 self.expr(fac([g]), "must have Load context")
1212 x = ast.Name("x", ast.Store())
1213 y = ast.Name("y", ast.Load())
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001214 g = ast.comprehension(x, y, [None], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001215 self.expr(fac([g]), "None disallowed")
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001216 g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001217 self.expr(fac([g]), "must have Load context")
1218
1219 def _simple_comp(self, fac):
1220 g = ast.comprehension(ast.Name("x", ast.Store()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001221 ast.Name("x", ast.Load()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001222 self.expr(fac(ast.Name("x", ast.Store()), [g]),
1223 "must have Load context")
1224 def wrap(gens):
1225 return fac(ast.Name("x", ast.Store()), gens)
1226 self._check_comprehension(wrap)
1227
1228 def test_listcomp(self):
1229 self._simple_comp(ast.ListComp)
1230
1231 def test_setcomp(self):
1232 self._simple_comp(ast.SetComp)
1233
1234 def test_generatorexp(self):
1235 self._simple_comp(ast.GeneratorExp)
1236
1237 def test_dictcomp(self):
1238 g = ast.comprehension(ast.Name("y", ast.Store()),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001239 ast.Name("p", ast.Load()), [], 0)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001240 c = ast.DictComp(ast.Name("x", ast.Store()),
1241 ast.Name("y", ast.Load()), [g])
1242 self.expr(c, "must have Load context")
1243 c = ast.DictComp(ast.Name("x", ast.Load()),
1244 ast.Name("y", ast.Store()), [g])
1245 self.expr(c, "must have Load context")
1246 def factory(comps):
1247 k = ast.Name("x", ast.Load())
1248 v = ast.Name("y", ast.Load())
1249 return ast.DictComp(k, v, comps)
1250 self._check_comprehension(factory)
1251
1252 def test_yield(self):
Benjamin Peterson527c6222012-01-14 08:58:23 -05001253 self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
1254 self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001255
1256 def test_compare(self):
1257 left = ast.Name("x", ast.Load())
1258 comp = ast.Compare(left, [ast.In()], [])
1259 self.expr(comp, "no comparators")
1260 comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
1261 self.expr(comp, "different number of comparators and operands")
1262 comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001263 self.expr(comp)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001264 comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001265 self.expr(comp)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001266
1267 def test_call(self):
1268 func = ast.Name("x", ast.Load())
1269 args = [ast.Name("y", ast.Load())]
1270 keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001271 call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001272 self.expr(call, "must have Load context")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001273 call = ast.Call(func, [None], keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001274 self.expr(call, "None disallowed")
1275 bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001276 call = ast.Call(func, args, bad_keywords)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001277 self.expr(call, "must have Load context")
1278
1279 def test_num(self):
1280 class subint(int):
1281 pass
1282 class subfloat(float):
1283 pass
1284 class subcomplex(complex):
1285 pass
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001286 for obj in "0", "hello":
1287 self.expr(ast.Num(obj))
1288 for obj in subint(), subfloat(), subcomplex():
1289 self.expr(ast.Num(obj), "invalid type", exc=TypeError)
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001290
1291 def test_attribute(self):
1292 attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
1293 self.expr(attr, "must have Load context")
1294
1295 def test_subscript(self):
1296 sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
1297 ast.Load())
1298 self.expr(sub, "must have Load context")
1299 x = ast.Name("x", ast.Load())
1300 sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
1301 ast.Load())
1302 self.expr(sub, "must have Load context")
1303 s = ast.Name("x", ast.Store())
1304 for args in (s, None, None), (None, s, None), (None, None, s):
1305 sl = ast.Slice(*args)
1306 self.expr(ast.Subscript(x, sl, ast.Load()),
1307 "must have Load context")
1308 sl = ast.ExtSlice([])
1309 self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
1310 sl = ast.ExtSlice([ast.Index(s)])
1311 self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
1312
1313 def test_starred(self):
1314 left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
1315 ast.Store())
1316 assign = ast.Assign([left], ast.Num(4))
1317 self.stmt(assign, "must have Store context")
1318
1319 def _sequence(self, fac):
1320 self.expr(fac([None], ast.Load()), "None disallowed")
1321 self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
1322 "must have Load context")
1323
1324 def test_list(self):
1325 self._sequence(ast.List)
1326
1327 def test_tuple(self):
1328 self._sequence(ast.Tuple)
1329
Benjamin Peterson442f2092012-12-06 17:41:04 -05001330 def test_nameconstant(self):
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001331 self.expr(ast.NameConstant(4))
Benjamin Peterson442f2092012-12-06 17:41:04 -05001332
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001333 def test_stdlib_validates(self):
1334 stdlib = os.path.dirname(ast.__file__)
1335 tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
1336 tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
1337 for module in tests:
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02001338 with self.subTest(module):
1339 fn = os.path.join(stdlib, module)
1340 with open(fn, "r", encoding="utf-8") as fp:
1341 source = fp.read()
1342 mod = ast.parse(source, fn)
1343 compile(mod, fn, "exec")
Benjamin Peterson832bfe22011-08-09 16:15:04 -05001344
1345
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001346class ConstantTests(unittest.TestCase):
1347 """Tests on the ast.Constant node type."""
1348
1349 def compile_constant(self, value):
1350 tree = ast.parse("x = 123")
1351
1352 node = tree.body[0].value
1353 new_node = ast.Constant(value=value)
1354 ast.copy_location(new_node, node)
1355 tree.body[0].value = new_node
1356
1357 code = compile(tree, "<string>", "exec")
1358
1359 ns = {}
1360 exec(code, ns)
1361 return ns['x']
1362
Victor Stinnerbe59d142016-01-27 00:39:12 +01001363 def test_validation(self):
1364 with self.assertRaises(TypeError) as cm:
1365 self.compile_constant([1, 2, 3])
1366 self.assertEqual(str(cm.exception),
1367 "got an invalid type in Constant: list")
1368
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001369 def test_singletons(self):
1370 for const in (None, False, True, Ellipsis, b'', frozenset()):
1371 with self.subTest(const=const):
1372 value = self.compile_constant(const)
1373 self.assertIs(value, const)
1374
1375 def test_values(self):
1376 nested_tuple = (1,)
1377 nested_frozenset = frozenset({1})
1378 for level in range(3):
1379 nested_tuple = (nested_tuple, 2)
1380 nested_frozenset = frozenset({nested_frozenset, 2})
1381 values = (123, 123.0, 123j,
1382 "unicode", b'bytes',
1383 tuple("tuple"), frozenset("frozenset"),
1384 nested_tuple, nested_frozenset)
1385 for value in values:
1386 with self.subTest(value=value):
1387 result = self.compile_constant(value)
1388 self.assertEqual(result, value)
1389
1390 def test_assign_to_constant(self):
1391 tree = ast.parse("x = 1")
1392
1393 target = tree.body[0].targets[0]
1394 new_target = ast.Constant(value=1)
1395 ast.copy_location(new_target, target)
1396 tree.body[0].targets[0] = new_target
1397
1398 with self.assertRaises(ValueError) as cm:
1399 compile(tree, "string", "exec")
1400 self.assertEqual(str(cm.exception),
1401 "expression which can't be assigned "
1402 "to in Store context")
1403
1404 def test_get_docstring(self):
1405 tree = ast.parse("'docstring'\nx = 1")
1406 self.assertEqual(ast.get_docstring(tree), 'docstring')
1407
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001408 def get_load_const(self, tree):
1409 # Compile to bytecode, disassemble and get parameter of LOAD_CONST
1410 # instructions
1411 co = compile(tree, '<string>', 'exec')
1412 consts = []
1413 for instr in dis.get_instructions(co):
1414 if instr.opname == 'LOAD_CONST':
1415 consts.append(instr.argval)
1416 return consts
1417
1418 @support.cpython_only
1419 def test_load_const(self):
1420 consts = [None,
1421 True, False,
1422 124,
1423 2.0,
1424 3j,
1425 "unicode",
1426 b'bytes',
1427 (1, 2, 3)]
1428
Victor Stinnera2724092016-02-08 18:17:58 +01001429 code = '\n'.join(['x={!r}'.format(const) for const in consts])
1430 code += '\nx = ...'
1431 consts.extend((Ellipsis, None))
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001432
1433 tree = ast.parse(code)
Victor Stinnera2724092016-02-08 18:17:58 +01001434 self.assertEqual(self.get_load_const(tree),
1435 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001436
1437 # Replace expression nodes with constants
Victor Stinnera2724092016-02-08 18:17:58 +01001438 for assign, const in zip(tree.body, consts):
1439 assert isinstance(assign, ast.Assign), ast.dump(assign)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001440 new_node = ast.Constant(value=const)
Victor Stinnera2724092016-02-08 18:17:58 +01001441 ast.copy_location(new_node, assign.value)
1442 assign.value = new_node
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001443
Victor Stinnera2724092016-02-08 18:17:58 +01001444 self.assertEqual(self.get_load_const(tree),
1445 consts)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001446
1447 def test_literal_eval(self):
1448 tree = ast.parse("1 + 2")
1449 binop = tree.body[0].value
1450
1451 new_left = ast.Constant(value=10)
1452 ast.copy_location(new_left, binop.left)
1453 binop.left = new_left
1454
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +02001455 new_right = ast.Constant(value=20j)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001456 ast.copy_location(new_right, binop.right)
1457 binop.right = new_right
1458
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +02001459 self.assertEqual(ast.literal_eval(binop), 10+20j)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001460
Guido van Rossum10f8ce62019-03-13 13:00:46 -07001461 def test_string_kind(self):
1462 c = ast.parse('"x"', mode='eval').body
1463 self.assertEqual(c.value, "x")
1464 self.assertEqual(c.kind, None)
1465
1466 c = ast.parse('u"x"', mode='eval').body
1467 self.assertEqual(c.value, "x")
1468 self.assertEqual(c.kind, "u")
1469
1470 c = ast.parse('r"x"', mode='eval').body
1471 self.assertEqual(c.value, "x")
1472 self.assertEqual(c.kind, None)
1473
1474 c = ast.parse('b"x"', mode='eval').body
1475 self.assertEqual(c.value, b"x")
1476 self.assertEqual(c.kind, None)
1477
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001478
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001479class EndPositionTests(unittest.TestCase):
1480 """Tests for end position of AST nodes.
1481
1482 Testing end positions of nodes requires a bit of extra care
1483 because of how LL parsers work.
1484 """
1485 def _check_end_pos(self, ast_node, end_lineno, end_col_offset):
1486 self.assertEqual(ast_node.end_lineno, end_lineno)
1487 self.assertEqual(ast_node.end_col_offset, end_col_offset)
1488
1489 def _check_content(self, source, ast_node, content):
1490 self.assertEqual(ast.get_source_segment(source, ast_node), content)
1491
1492 def _parse_value(self, s):
1493 # Use duck-typing to support both single expression
1494 # and a right hand side of an assignment statement.
1495 return ast.parse(s).body[0].value
1496
1497 def test_lambda(self):
1498 s = 'lambda x, *y: None'
1499 lam = self._parse_value(s)
1500 self._check_content(s, lam.body, 'None')
1501 self._check_content(s, lam.args.args[0], 'x')
1502 self._check_content(s, lam.args.vararg, 'y')
1503
1504 def test_func_def(self):
1505 s = dedent('''
1506 def func(x: int,
1507 *args: str,
1508 z: float = 0,
1509 **kwargs: Any) -> bool:
1510 return True
1511 ''').strip()
1512 fdef = ast.parse(s).body[0]
1513 self._check_end_pos(fdef, 5, 15)
1514 self._check_content(s, fdef.body[0], 'return True')
1515 self._check_content(s, fdef.args.args[0], 'x: int')
1516 self._check_content(s, fdef.args.args[0].annotation, 'int')
1517 self._check_content(s, fdef.args.kwarg, 'kwargs: Any')
1518 self._check_content(s, fdef.args.kwarg.annotation, 'Any')
1519
1520 def test_call(self):
1521 s = 'func(x, y=2, **kw)'
1522 call = self._parse_value(s)
1523 self._check_content(s, call.func, 'func')
1524 self._check_content(s, call.keywords[0].value, '2')
1525 self._check_content(s, call.keywords[1].value, 'kw')
1526
1527 def test_call_noargs(self):
1528 s = 'x[0]()'
1529 call = self._parse_value(s)
1530 self._check_content(s, call.func, 'x[0]')
1531 self._check_end_pos(call, 1, 6)
1532
1533 def test_class_def(self):
1534 s = dedent('''
1535 class C(A, B):
1536 x: int = 0
1537 ''').strip()
1538 cdef = ast.parse(s).body[0]
1539 self._check_end_pos(cdef, 2, 14)
1540 self._check_content(s, cdef.bases[1], 'B')
1541 self._check_content(s, cdef.body[0], 'x: int = 0')
1542
1543 def test_class_kw(self):
1544 s = 'class S(metaclass=abc.ABCMeta): pass'
1545 cdef = ast.parse(s).body[0]
1546 self._check_content(s, cdef.keywords[0].value, 'abc.ABCMeta')
1547
1548 def test_multi_line_str(self):
1549 s = dedent('''
1550 x = """Some multi-line text.
1551
1552 It goes on starting from same indent."""
1553 ''').strip()
1554 assign = ast.parse(s).body[0]
1555 self._check_end_pos(assign, 3, 40)
1556 self._check_end_pos(assign.value, 3, 40)
1557
1558 def test_continued_str(self):
1559 s = dedent('''
1560 x = "first part" \\
1561 "second part"
1562 ''').strip()
1563 assign = ast.parse(s).body[0]
1564 self._check_end_pos(assign, 2, 13)
1565 self._check_end_pos(assign.value, 2, 13)
1566
1567 def test_suites(self):
1568 # We intentionally put these into the same string to check
1569 # that empty lines are not part of the suite.
1570 s = dedent('''
1571 while True:
1572 pass
1573
1574 if one():
1575 x = None
1576 elif other():
1577 y = None
1578 else:
1579 z = None
1580
1581 for x, y in stuff:
1582 assert True
1583
1584 try:
1585 raise RuntimeError
1586 except TypeError as e:
1587 pass
1588
1589 pass
1590 ''').strip()
1591 mod = ast.parse(s)
1592 while_loop = mod.body[0]
1593 if_stmt = mod.body[1]
1594 for_loop = mod.body[2]
1595 try_stmt = mod.body[3]
1596 pass_stmt = mod.body[4]
1597
1598 self._check_end_pos(while_loop, 2, 8)
1599 self._check_end_pos(if_stmt, 9, 12)
1600 self._check_end_pos(for_loop, 12, 15)
1601 self._check_end_pos(try_stmt, 17, 8)
1602 self._check_end_pos(pass_stmt, 19, 4)
1603
1604 self._check_content(s, while_loop.test, 'True')
1605 self._check_content(s, if_stmt.body[0], 'x = None')
1606 self._check_content(s, if_stmt.orelse[0].test, 'other()')
1607 self._check_content(s, for_loop.target, 'x, y')
1608 self._check_content(s, try_stmt.body[0], 'raise RuntimeError')
1609 self._check_content(s, try_stmt.handlers[0].type, 'TypeError')
1610
1611 def test_fstring(self):
1612 s = 'x = f"abc {x + y} abc"'
1613 fstr = self._parse_value(s)
1614 binop = fstr.values[1].value
1615 self._check_content(s, binop, 'x + y')
1616
1617 def test_fstring_multi_line(self):
1618 s = dedent('''
1619 f"""Some multi-line text.
1620 {
1621 arg_one
1622 +
1623 arg_two
1624 }
1625 It goes on..."""
1626 ''').strip()
1627 fstr = self._parse_value(s)
1628 binop = fstr.values[1].value
1629 self._check_end_pos(binop, 5, 7)
1630 self._check_content(s, binop.left, 'arg_one')
1631 self._check_content(s, binop.right, 'arg_two')
1632
1633 def test_import_from_multi_line(self):
1634 s = dedent('''
1635 from x.y.z import (
1636 a, b, c as c
1637 )
1638 ''').strip()
1639 imp = ast.parse(s).body[0]
1640 self._check_end_pos(imp, 3, 1)
1641
1642 def test_slices(self):
1643 s1 = 'f()[1, 2] [0]'
1644 s2 = 'x[ a.b: c.d]'
1645 sm = dedent('''
1646 x[ a.b: f () ,
1647 g () : c.d
1648 ]
1649 ''').strip()
1650 i1, i2, im = map(self._parse_value, (s1, s2, sm))
1651 self._check_content(s1, i1.value, 'f()[1, 2]')
1652 self._check_content(s1, i1.value.slice.value, '1, 2')
1653 self._check_content(s2, i2.slice.lower, 'a.b')
1654 self._check_content(s2, i2.slice.upper, 'c.d')
1655 self._check_content(sm, im.slice.dims[0].upper, 'f ()')
1656 self._check_content(sm, im.slice.dims[1].lower, 'g ()')
1657 self._check_end_pos(im, 3, 3)
1658
1659 def test_binop(self):
1660 s = dedent('''
1661 (1 * 2 + (3 ) +
1662 4
1663 )
1664 ''').strip()
1665 binop = self._parse_value(s)
1666 self._check_end_pos(binop, 2, 6)
1667 self._check_content(s, binop.right, '4')
1668 self._check_content(s, binop.left, '1 * 2 + (3 )')
1669 self._check_content(s, binop.left.right, '3')
1670
1671 def test_boolop(self):
1672 s = dedent('''
1673 if (one_condition and
1674 (other_condition or yet_another_one)):
1675 pass
1676 ''').strip()
1677 bop = ast.parse(s).body[0].test
1678 self._check_end_pos(bop, 2, 44)
1679 self._check_content(s, bop.values[1],
1680 'other_condition or yet_another_one')
1681
1682 def test_tuples(self):
1683 s1 = 'x = () ;'
1684 s2 = 'x = 1 , ;'
1685 s3 = 'x = (1 , 2 ) ;'
1686 sm = dedent('''
1687 x = (
1688 a, b,
1689 )
1690 ''').strip()
1691 t1, t2, t3, tm = map(self._parse_value, (s1, s2, s3, sm))
1692 self._check_content(s1, t1, '()')
1693 self._check_content(s2, t2, '1 ,')
1694 self._check_content(s3, t3, '(1 , 2 )')
1695 self._check_end_pos(tm, 3, 1)
1696
1697 def test_attribute_spaces(self):
1698 s = 'func(x. y .z)'
1699 call = self._parse_value(s)
1700 self._check_content(s, call, s)
1701 self._check_content(s, call.args[0], 'x. y .z')
1702
1703 def test_displays(self):
1704 s1 = '[{}, {1, }, {1, 2,} ]'
1705 s2 = '{a: b, f (): g () ,}'
1706 c1 = self._parse_value(s1)
1707 c2 = self._parse_value(s2)
1708 self._check_content(s1, c1.elts[0], '{}')
1709 self._check_content(s1, c1.elts[1], '{1, }')
1710 self._check_content(s1, c1.elts[2], '{1, 2,}')
1711 self._check_content(s2, c2.keys[1], 'f ()')
1712 self._check_content(s2, c2.values[1], 'g ()')
1713
1714 def test_comprehensions(self):
1715 s = dedent('''
1716 x = [{x for x, y in stuff
1717 if cond.x} for stuff in things]
1718 ''').strip()
1719 cmp = self._parse_value(s)
1720 self._check_end_pos(cmp, 2, 37)
1721 self._check_content(s, cmp.generators[0].iter, 'things')
1722 self._check_content(s, cmp.elt.generators[0].iter, 'stuff')
1723 self._check_content(s, cmp.elt.generators[0].ifs[0], 'cond.x')
1724 self._check_content(s, cmp.elt.generators[0].target, 'x, y')
1725
1726 def test_yield_await(self):
1727 s = dedent('''
1728 async def f():
1729 yield x
1730 await y
1731 ''').strip()
1732 fdef = ast.parse(s).body[0]
1733 self._check_content(s, fdef.body[0].value, 'yield x')
1734 self._check_content(s, fdef.body[1].value, 'await y')
1735
1736 def test_source_segment_multi(self):
1737 s_orig = dedent('''
1738 x = (
1739 a, b,
1740 ) + ()
1741 ''').strip()
1742 s_tuple = dedent('''
1743 (
1744 a, b,
1745 )
1746 ''').strip()
1747 binop = self._parse_value(s_orig)
1748 self.assertEqual(ast.get_source_segment(s_orig, binop.left), s_tuple)
1749
1750 def test_source_segment_padded(self):
1751 s_orig = dedent('''
1752 class C:
1753 def fun(self) -> None:
1754 "Đ–Đ–Đ–Đ–Đ–"
1755 ''').strip()
1756 s_method = ' def fun(self) -> None:\n' \
1757 ' "Đ–Đ–Đ–Đ–Đ–"'
1758 cdef = ast.parse(s_orig).body[0]
1759 self.assertEqual(ast.get_source_segment(s_orig, cdef.body[0], padded=True),
1760 s_method)
1761
1762 def test_source_segment_endings(self):
1763 s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\rz = 1\r\n'
1764 v, w, x, y, z = ast.parse(s).body
1765 self._check_content(s, v, 'v = 1')
1766 self._check_content(s, w, 'w = 1')
1767 self._check_content(s, x, 'x = 1')
1768 self._check_content(s, y, 'y = 1')
1769 self._check_content(s, z, 'z = 1')
1770
1771 def test_source_segment_tabs(self):
1772 s = dedent('''
1773 class C:
1774 \t\f def fun(self) -> None:
1775 \t\f pass
1776 ''').strip()
1777 s_method = ' \t\f def fun(self) -> None:\n' \
1778 ' \t\f pass'
1779
1780 cdef = ast.parse(s).body[0]
1781 self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method)
1782
1783
Serhiy Storchakac3ea41e2019-08-26 10:13:19 +03001784class NodeVisitorTests(unittest.TestCase):
1785 def test_old_constant_nodes(self):
1786 class Visitor(ast.NodeVisitor):
1787 def visit_Num(self, node):
1788 log.append((node.lineno, 'Num', node.n))
1789 def visit_Str(self, node):
1790 log.append((node.lineno, 'Str', node.s))
1791 def visit_Bytes(self, node):
1792 log.append((node.lineno, 'Bytes', node.s))
1793 def visit_NameConstant(self, node):
1794 log.append((node.lineno, 'NameConstant', node.value))
1795 def visit_Ellipsis(self, node):
1796 log.append((node.lineno, 'Ellipsis', ...))
1797 mod = ast.parse(dedent('''\
1798 i = 42
1799 f = 4.25
1800 c = 4.25j
1801 s = 'string'
1802 b = b'bytes'
1803 t = True
1804 n = None
1805 e = ...
1806 '''))
1807 visitor = Visitor()
1808 log = []
1809 with warnings.catch_warnings(record=True) as wlog:
1810 warnings.filterwarnings('always', '', DeprecationWarning)
1811 visitor.visit(mod)
1812 self.assertEqual(log, [
1813 (1, 'Num', 42),
1814 (2, 'Num', 4.25),
1815 (3, 'Num', 4.25j),
1816 (4, 'Str', 'string'),
1817 (5, 'Bytes', b'bytes'),
1818 (6, 'NameConstant', True),
1819 (7, 'NameConstant', None),
1820 (8, 'Ellipsis', ...),
1821 ])
1822 self.assertEqual([str(w.message) for w in wlog], [
1823 'visit_Num is deprecated; add visit_Constant',
1824 'visit_Num is deprecated; add visit_Constant',
1825 'visit_Num is deprecated; add visit_Constant',
1826 'visit_Str is deprecated; add visit_Constant',
1827 'visit_Bytes is deprecated; add visit_Constant',
1828 'visit_NameConstant is deprecated; add visit_Constant',
1829 'visit_NameConstant is deprecated; add visit_Constant',
1830 'visit_Ellipsis is deprecated; add visit_Constant',
1831 ])
1832
1833
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001834def main():
1835 if __name__ != '__main__':
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001836 return
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001837 if sys.argv[1:] == ['-g']:
1838 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
1839 (eval_tests, "eval")):
1840 print(kind+"_results = [")
Victor Stinnerf0891962016-02-08 17:15:21 +01001841 for statement in statements:
1842 tree = ast.parse(statement, "?", kind)
1843 print("%r," % (to_tuple(tree),))
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001844 print("]")
1845 print("main()")
1846 raise SystemExit
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04001847 unittest.main()
Tim Peters400cbc32006-02-28 18:44:41 +00001848
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001849#### EVERYTHING BELOW IS GENERATED BY python Lib/test/test_ast.py -g #####
Tim Peters400cbc32006-02-28 18:44:41 +00001850exec_results = [
Serhiy Storchaka850a8852020-01-10 10:12:55 +02001851('Module', [('Expr', (1, 0, 1, 4), ('Constant', (1, 0, 1, 4), None, None))], []),
1852('Module', [('Expr', (1, 0, 1, 18), ('Constant', (1, 0, 1, 18), 'module docstring', None))], []),
1853('Module', [('FunctionDef', (1, 0, 1, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (1, 9, 1, 13))], [], None, None)], []),
1854('Module', [('FunctionDef', (1, 0, 1, 29), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (1, 9, 1, 29), ('Constant', (1, 9, 1, 29), 'function docstring', None))], [], None, None)], []),
1855('Module', [('FunctionDef', (1, 0, 1, 14), 'f', ('arguments', [], [('arg', (1, 6, 1, 7), 'a', None, None)], None, [], [], None, []), [('Pass', (1, 10, 1, 14))], [], None, None)], []),
1856('Module', [('FunctionDef', (1, 0, 1, 16), 'f', ('arguments', [], [('arg', (1, 6, 1, 7), 'a', None, None)], None, [], [], None, [('Constant', (1, 8, 1, 9), 0, None)]), [('Pass', (1, 12, 1, 16))], [], None, None)], []),
1857('Module', [('FunctionDef', (1, 0, 1, 18), 'f', ('arguments', [], [], ('arg', (1, 7, 1, 11), 'args', None, None), [], [], None, []), [('Pass', (1, 14, 1, 18))], [], None, None)], []),
1858('Module', [('FunctionDef', (1, 0, 1, 21), 'f', ('arguments', [], [], None, [], [], ('arg', (1, 8, 1, 14), 'kwargs', None, None), []), [('Pass', (1, 17, 1, 21))], [], None, None)], []),
1859('Module', [('FunctionDef', (1, 0, 1, 71), 'f', ('arguments', [], [('arg', (1, 6, 1, 7), 'a', None, None), ('arg', (1, 9, 1, 10), 'b', None, None), ('arg', (1, 14, 1, 15), 'c', None, None), ('arg', (1, 22, 1, 23), 'd', None, None), ('arg', (1, 28, 1, 29), 'e', None, None)], ('arg', (1, 35, 1, 39), 'args', None, None), [('arg', (1, 41, 1, 42), 'f', None, None)], [('Constant', (1, 43, 1, 45), 42, None)], ('arg', (1, 49, 1, 55), 'kwargs', None, None), [('Constant', (1, 11, 1, 12), 1, None), ('Constant', (1, 16, 1, 20), None, None), ('List', (1, 24, 1, 26), [], ('Load',)), ('Dict', (1, 30, 1, 32), [], [])]), [('Expr', (1, 58, 1, 71), ('Constant', (1, 58, 1, 71), 'doc for f()', None))], [], None, None)], []),
1860('Module', [('ClassDef', (1, 0, 1, 12), 'C', [], [], [('Pass', (1, 8, 1, 12))], [])], []),
1861('Module', [('ClassDef', (1, 0, 1, 32), 'C', [], [], [('Expr', (1, 9, 1, 32), ('Constant', (1, 9, 1, 32), 'docstring for class C', None))], [])], []),
1862('Module', [('ClassDef', (1, 0, 1, 21), 'C', [('Name', (1, 8, 1, 14), 'object', ('Load',))], [], [('Pass', (1, 17, 1, 21))], [])], []),
1863('Module', [('FunctionDef', (1, 0, 1, 16), 'f', ('arguments', [], [], None, [], [], None, []), [('Return', (1, 8, 1, 16), ('Constant', (1, 15, 1, 16), 1, None))], [], None, None)], []),
1864('Module', [('Delete', (1, 0, 1, 5), [('Name', (1, 4, 1, 5), 'v', ('Del',))])], []),
1865('Module', [('Assign', (1, 0, 1, 5), [('Name', (1, 0, 1, 1), 'v', ('Store',))], ('Constant', (1, 4, 1, 5), 1, None), None)], []),
1866('Module', [('Assign', (1, 0, 1, 7), [('Tuple', (1, 0, 1, 3), [('Name', (1, 0, 1, 1), 'a', ('Store',)), ('Name', (1, 2, 1, 3), 'b', ('Store',))], ('Store',))], ('Name', (1, 6, 1, 7), 'c', ('Load',)), None)], []),
1867('Module', [('Assign', (1, 0, 1, 9), [('Tuple', (1, 0, 1, 5), [('Name', (1, 1, 1, 2), 'a', ('Store',)), ('Name', (1, 3, 1, 4), 'b', ('Store',))], ('Store',))], ('Name', (1, 8, 1, 9), 'c', ('Load',)), None)], []),
1868('Module', [('Assign', (1, 0, 1, 9), [('List', (1, 0, 1, 5), [('Name', (1, 1, 1, 2), 'a', ('Store',)), ('Name', (1, 3, 1, 4), 'b', ('Store',))], ('Store',))], ('Name', (1, 8, 1, 9), 'c', ('Load',)), None)], []),
1869('Module', [('AugAssign', (1, 0, 1, 6), ('Name', (1, 0, 1, 1), 'v', ('Store',)), ('Add',), ('Constant', (1, 5, 1, 6), 1, None))], []),
1870('Module', [('For', (1, 0, 1, 15), ('Name', (1, 4, 1, 5), 'v', ('Store',)), ('Name', (1, 9, 1, 10), 'v', ('Load',)), [('Pass', (1, 11, 1, 15))], [], None)], []),
1871('Module', [('While', (1, 0, 1, 12), ('Name', (1, 6, 1, 7), 'v', ('Load',)), [('Pass', (1, 8, 1, 12))], [])], []),
1872('Module', [('If', (1, 0, 1, 9), ('Name', (1, 3, 1, 4), 'v', ('Load',)), [('Pass', (1, 5, 1, 9))], [])], []),
1873('Module', [('If', (1, 0, 4, 6), ('Name', (1, 3, 1, 4), 'a', ('Load',)), [('Pass', (2, 2, 2, 6))], [('If', (3, 0, 4, 6), ('Name', (3, 5, 3, 6), 'b', ('Load',)), [('Pass', (4, 2, 4, 6))], [])])], []),
1874('Module', [('If', (1, 0, 6, 6), ('Name', (1, 3, 1, 4), 'a', ('Load',)), [('Pass', (2, 2, 2, 6))], [('If', (3, 0, 6, 6), ('Name', (3, 5, 3, 6), 'b', ('Load',)), [('Pass', (4, 2, 4, 6))], [('Pass', (6, 2, 6, 6))])])], []),
1875('Module', [('With', (1, 0, 1, 17), [('withitem', ('Name', (1, 5, 1, 6), 'x', ('Load',)), ('Name', (1, 10, 1, 11), 'y', ('Store',)))], [('Pass', (1, 13, 1, 17))], None)], []),
1876('Module', [('With', (1, 0, 1, 25), [('withitem', ('Name', (1, 5, 1, 6), 'x', ('Load',)), ('Name', (1, 10, 1, 11), 'y', ('Store',))), ('withitem', ('Name', (1, 13, 1, 14), 'z', ('Load',)), ('Name', (1, 18, 1, 19), 'q', ('Store',)))], [('Pass', (1, 21, 1, 25))], None)], []),
1877('Module', [('Raise', (1, 0, 1, 25), ('Call', (1, 6, 1, 25), ('Name', (1, 6, 1, 15), 'Exception', ('Load',)), [('Constant', (1, 16, 1, 24), 'string', None)], []), None)], []),
1878('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 7, 3, 16), 'Exception', ('Load',)), None, [('Pass', (4, 2, 4, 6))])], [], [])], []),
1879('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [], [], [('Pass', (4, 2, 4, 6))])], []),
1880('Module', [('Assert', (1, 0, 1, 8), ('Name', (1, 7, 1, 8), 'v', ('Load',)), None)], []),
1881('Module', [('Import', (1, 0, 1, 10), [('alias', 'sys', None)])], []),
1882('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', 'v', None)], 0)], []),
1883('Module', [('Global', (1, 0, 1, 8), ['v'])], []),
1884('Module', [('Expr', (1, 0, 1, 1), ('Constant', (1, 0, 1, 1), 1, None))], []),
1885('Module', [('Pass', (1, 0, 1, 4))], []),
1886('Module', [('For', (1, 0, 1, 16), ('Name', (1, 4, 1, 5), 'v', ('Store',)), ('Name', (1, 9, 1, 10), 'v', ('Load',)), [('Break', (1, 11, 1, 16))], [], None)], []),
1887('Module', [('For', (1, 0, 1, 19), ('Name', (1, 4, 1, 5), 'v', ('Store',)), ('Name', (1, 9, 1, 10), 'v', ('Load',)), [('Continue', (1, 11, 1, 19))], [], None)], []),
1888('Module', [('For', (1, 0, 1, 18), ('Tuple', (1, 4, 1, 7), [('Name', (1, 4, 1, 5), 'a', ('Store',)), ('Name', (1, 6, 1, 7), 'b', ('Store',))], ('Store',)), ('Name', (1, 11, 1, 12), 'c', ('Load',)), [('Pass', (1, 14, 1, 18))], [], None)], []),
1889('Module', [('For', (1, 0, 1, 20), ('Tuple', (1, 4, 1, 9), [('Name', (1, 5, 1, 6), 'a', ('Store',)), ('Name', (1, 7, 1, 8), 'b', ('Store',))], ('Store',)), ('Name', (1, 13, 1, 14), 'c', ('Load',)), [('Pass', (1, 16, 1, 20))], [], None)], []),
1890('Module', [('For', (1, 0, 1, 20), ('List', (1, 4, 1, 9), [('Name', (1, 5, 1, 6), 'a', ('Store',)), ('Name', (1, 7, 1, 8), 'b', ('Store',))], ('Store',)), ('Name', (1, 13, 1, 14), 'c', ('Load',)), [('Pass', (1, 16, 1, 20))], [], None)], []),
1891('Module', [('Expr', (1, 0, 11, 5), ('GeneratorExp', (1, 0, 11, 5), ('Tuple', (2, 4, 6, 5), [('Name', (3, 4, 3, 6), 'Aa', ('Load',)), ('Name', (5, 7, 5, 9), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4, 10, 6), [('Name', (8, 4, 8, 6), 'Aa', ('Store',)), ('Name', (10, 4, 10, 6), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10, 10, 12), 'Cc', ('Load',)), [], 0)]))], []),
1892('Module', [('Expr', (1, 0, 1, 34), ('DictComp', (1, 0, 1, 34), ('Name', (1, 1, 1, 2), 'a', ('Load',)), ('Name', (1, 5, 1, 6), 'b', ('Load',)), [('comprehension', ('Name', (1, 11, 1, 12), 'w', ('Store',)), ('Name', (1, 16, 1, 17), 'x', ('Load',)), [], 0), ('comprehension', ('Name', (1, 22, 1, 23), 'm', ('Store',)), ('Name', (1, 27, 1, 28), 'p', ('Load',)), [('Name', (1, 32, 1, 33), 'g', ('Load',))], 0)]))], []),
1893('Module', [('Expr', (1, 0, 1, 20), ('DictComp', (1, 0, 1, 20), ('Name', (1, 1, 1, 2), 'a', ('Load',)), ('Name', (1, 5, 1, 6), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'v', ('Store',)), ('Name', (1, 13, 1, 14), 'w', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'x', ('Load',)), [], 0)]))], []),
1894('Module', [('Expr', (1, 0, 1, 19), ('SetComp', (1, 0, 1, 19), ('Name', (1, 1, 1, 2), 'r', ('Load',)), [('comprehension', ('Name', (1, 7, 1, 8), 'l', ('Store',)), ('Name', (1, 12, 1, 13), 'x', ('Load',)), [('Name', (1, 17, 1, 18), 'g', ('Load',))], 0)]))], []),
1895('Module', [('Expr', (1, 0, 1, 16), ('SetComp', (1, 0, 1, 16), ('Name', (1, 1, 1, 2), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7, 1, 10), [('Name', (1, 7, 1, 8), 'l', ('Store',)), ('Name', (1, 9, 1, 10), 'm', ('Store',))], ('Store',)), ('Name', (1, 14, 1, 15), 'x', ('Load',)), [], 0)]))], []),
1896('Module', [('AsyncFunctionDef', (1, 0, 3, 18), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (2, 1, 2, 17), ('Constant', (2, 1, 2, 17), 'async function', None)), ('Expr', (3, 1, 3, 18), ('Await', (3, 1, 3, 18), ('Call', (3, 7, 3, 18), ('Name', (3, 7, 3, 16), 'something', ('Load',)), [], [])))], [], None, None)], []),
1897('Module', [('AsyncFunctionDef', (1, 0, 3, 8), 'f', ('arguments', [], [], None, [], [], None, []), [('AsyncFor', (2, 1, 3, 8), ('Name', (2, 11, 2, 12), 'e', ('Store',)), ('Name', (2, 16, 2, 17), 'i', ('Load',)), [('Expr', (2, 19, 2, 20), ('Constant', (2, 19, 2, 20), 1, None))], [('Expr', (3, 7, 3, 8), ('Constant', (3, 7, 3, 8), 2, None))], None)], [], None, None)], []),
1898('Module', [('AsyncFunctionDef', (1, 0, 2, 21), 'f', ('arguments', [], [], None, [], [], None, []), [('AsyncWith', (2, 1, 2, 21), [('withitem', ('Name', (2, 12, 2, 13), 'a', ('Load',)), ('Name', (2, 17, 2, 18), 'b', ('Store',)))], [('Expr', (2, 20, 2, 21), ('Constant', (2, 20, 2, 21), 1, None))], None)], [], None, None)], []),
1899('Module', [('Expr', (1, 0, 1, 14), ('Dict', (1, 0, 1, 14), [None, ('Constant', (1, 10, 1, 11), 2, None)], [('Dict', (1, 3, 1, 8), [('Constant', (1, 4, 1, 5), 1, None)], [('Constant', (1, 6, 1, 7), 2, None)]), ('Constant', (1, 12, 1, 13), 3, None)]))], []),
1900('Module', [('Expr', (1, 0, 1, 12), ('Set', (1, 0, 1, 12), [('Starred', (1, 1, 1, 8), ('Set', (1, 2, 1, 8), [('Constant', (1, 3, 1, 4), 1, None), ('Constant', (1, 6, 1, 7), 2, None)]), ('Load',)), ('Constant', (1, 10, 1, 11), 3, None)]))], []),
1901('Module', [('AsyncFunctionDef', (1, 0, 2, 21), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (2, 1, 2, 21), ('ListComp', (2, 1, 2, 21), ('Name', (2, 2, 2, 3), 'i', ('Load',)), [('comprehension', ('Name', (2, 14, 2, 15), 'b', ('Store',)), ('Name', (2, 19, 2, 20), 'c', ('Load',)), [], 1)]))], [], None, None)], []),
1902('Module', [('FunctionDef', (4, 0, 4, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (4, 9, 4, 13))], [('Name', (1, 1, 1, 6), 'deco1', ('Load',)), ('Call', (2, 1, 2, 8), ('Name', (2, 1, 2, 6), 'deco2', ('Load',)), [], []), ('Call', (3, 1, 3, 9), ('Name', (3, 1, 3, 6), 'deco3', ('Load',)), [('Constant', (3, 7, 3, 8), 1, None)], [])], None, None)], []),
1903('Module', [('AsyncFunctionDef', (4, 0, 4, 19), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (4, 15, 4, 19))], [('Name', (1, 1, 1, 6), 'deco1', ('Load',)), ('Call', (2, 1, 2, 8), ('Name', (2, 1, 2, 6), 'deco2', ('Load',)), [], []), ('Call', (3, 1, 3, 9), ('Name', (3, 1, 3, 6), 'deco3', ('Load',)), [('Constant', (3, 7, 3, 8), 1, None)], [])], None, None)], []),
1904('Module', [('ClassDef', (4, 0, 4, 13), 'C', [], [], [('Pass', (4, 9, 4, 13))], [('Name', (1, 1, 1, 6), 'deco1', ('Load',)), ('Call', (2, 1, 2, 8), ('Name', (2, 1, 2, 6), 'deco2', ('Load',)), [], []), ('Call', (3, 1, 3, 9), ('Name', (3, 1, 3, 6), 'deco3', ('Load',)), [('Constant', (3, 7, 3, 8), 1, None)], [])])], []),
1905('Module', [('FunctionDef', (2, 0, 2, 13), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (2, 9, 2, 13))], [('Call', (1, 1, 1, 19), ('Name', (1, 1, 1, 5), 'deco', ('Load',)), [('GeneratorExp', (1, 5, 1, 19), ('Name', (1, 6, 1, 7), 'a', ('Load',)), [('comprehension', ('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 17, 1, 18), 'b', ('Load',)), [], 0)])], [])], None, None)], []),
1906('Module', [('Expr', (1, 0, 1, 8), ('NamedExpr', (1, 1, 1, 7), ('Name', (1, 1, 1, 2), 'a', ('Store',)), ('Constant', (1, 6, 1, 7), 1, None)))], []),
1907('Module', [('FunctionDef', (1, 0, 1, 18), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [], None, [], [], None, []), [('Pass', (1, 14, 1, 18))], [], None, None)], []),
1908('Module', [('FunctionDef', (1, 0, 1, 26), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 12, 1, 13), 'c', None, None), ('arg', (1, 15, 1, 16), 'd', None, None), ('arg', (1, 18, 1, 19), 'e', None, None)], None, [], [], None, []), [('Pass', (1, 22, 1, 26))], [], None, None)], []),
1909('Module', [('FunctionDef', (1, 0, 1, 29), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 12, 1, 13), 'c', None, None)], None, [('arg', (1, 18, 1, 19), 'd', None, None), ('arg', (1, 21, 1, 22), 'e', None, None)], [None, None], None, []), [('Pass', (1, 25, 1, 29))], [], None, None)], []),
1910('Module', [('FunctionDef', (1, 0, 1, 39), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 12, 1, 13), 'c', None, None)], None, [('arg', (1, 18, 1, 19), 'd', None, None), ('arg', (1, 21, 1, 22), 'e', None, None)], [None, None], ('arg', (1, 26, 1, 32), 'kwargs', None, None), []), [('Pass', (1, 35, 1, 39))], [], None, None)], []),
1911('Module', [('FunctionDef', (1, 0, 1, 20), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [], None, [], [], None, [('Constant', (1, 8, 1, 9), 1, None)]), [('Pass', (1, 16, 1, 20))], [], None, None)], []),
1912('Module', [('FunctionDef', (1, 0, 1, 29), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None), ('arg', (1, 19, 1, 20), 'c', None, None)], None, [], [], None, [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None), ('Constant', (1, 21, 1, 22), 4, None)]), [('Pass', (1, 25, 1, 29))], [], None, None)], []),
1913('Module', [('FunctionDef', (1, 0, 1, 32), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [('Constant', (1, 24, 1, 25), 4, None)], None, [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 28, 1, 32))], [], None, None)], []),
1914('Module', [('FunctionDef', (1, 0, 1, 30), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [None], None, [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 26, 1, 30))], [], None, None)], []),
1915('Module', [('FunctionDef', (1, 0, 1, 42), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [('Constant', (1, 24, 1, 25), 4, None)], ('arg', (1, 29, 1, 35), 'kwargs', None, None), [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 38, 1, 42))], [], None, None)], []),
1916('Module', [('FunctionDef', (1, 0, 1, 40), 'f', ('arguments', [('arg', (1, 6, 1, 7), 'a', None, None)], [('arg', (1, 14, 1, 15), 'b', None, None)], None, [('arg', (1, 22, 1, 23), 'c', None, None)], [None], ('arg', (1, 27, 1, 33), 'kwargs', None, None), [('Constant', (1, 8, 1, 9), 1, None), ('Constant', (1, 16, 1, 17), 2, None)]), [('Pass', (1, 36, 1, 40))], [], None, None)], []),
Tim Peters400cbc32006-02-28 18:44:41 +00001917]
1918single_results = [
Serhiy Storchaka850a8852020-01-10 10:12:55 +02001919('Interactive', [('Expr', (1, 0, 1, 3), ('BinOp', (1, 0, 1, 3), ('Constant', (1, 0, 1, 1), 1, None), ('Add',), ('Constant', (1, 2, 1, 3), 2, None)))]),
Tim Peters400cbc32006-02-28 18:44:41 +00001920]
1921eval_results = [
Serhiy Storchaka850a8852020-01-10 10:12:55 +02001922('Expression', ('Constant', (1, 0, 1, 4), None, None)),
1923('Expression', ('BoolOp', (1, 0, 1, 7), ('And',), [('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Name', (1, 6, 1, 7), 'b', ('Load',))])),
1924('Expression', ('BinOp', (1, 0, 1, 5), ('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Add',), ('Name', (1, 4, 1, 5), 'b', ('Load',)))),
1925('Expression', ('UnaryOp', (1, 0, 1, 5), ('Not',), ('Name', (1, 4, 1, 5), 'v', ('Load',)))),
1926('Expression', ('Lambda', (1, 0, 1, 11), ('arguments', [], [], None, [], [], None, []), ('Constant', (1, 7, 1, 11), None, None))),
1927('Expression', ('Dict', (1, 0, 1, 7), [('Constant', (1, 2, 1, 3), 1, None)], [('Constant', (1, 4, 1, 5), 2, None)])),
1928('Expression', ('Dict', (1, 0, 1, 2), [], [])),
1929('Expression', ('Set', (1, 0, 1, 7), [('Constant', (1, 1, 1, 5), None, None)])),
1930('Expression', ('Dict', (1, 0, 5, 6), [('Constant', (2, 6, 2, 7), 1, None)], [('Constant', (4, 10, 4, 11), 2, None)])),
1931('Expression', ('ListComp', (1, 0, 1, 19), ('Name', (1, 1, 1, 2), 'a', ('Load',)), [('comprehension', ('Name', (1, 7, 1, 8), 'b', ('Store',)), ('Name', (1, 12, 1, 13), 'c', ('Load',)), [('Name', (1, 17, 1, 18), 'd', ('Load',))], 0)])),
1932('Expression', ('GeneratorExp', (1, 0, 1, 19), ('Name', (1, 1, 1, 2), 'a', ('Load',)), [('comprehension', ('Name', (1, 7, 1, 8), 'b', ('Store',)), ('Name', (1, 12, 1, 13), 'c', ('Load',)), [('Name', (1, 17, 1, 18), 'd', ('Load',))], 0)])),
1933('Expression', ('ListComp', (1, 0, 1, 20), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'c', ('Load',)), [], 0)])),
1934('Expression', ('ListComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
1935('Expression', ('ListComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
1936('Expression', ('SetComp', (1, 0, 1, 20), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'c', ('Load',)), [], 0)])),
1937('Expression', ('SetComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
1938('Expression', ('SetComp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
1939('Expression', ('GeneratorExp', (1, 0, 1, 20), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 14), [('Name', (1, 11, 1, 12), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 18, 1, 19), 'c', ('Load',)), [], 0)])),
1940('Expression', ('GeneratorExp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
1941('Expression', ('GeneratorExp', (1, 0, 1, 22), ('Tuple', (1, 1, 1, 6), [('Name', (1, 2, 1, 3), 'a', ('Load',)), ('Name', (1, 4, 1, 5), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11, 1, 16), [('Name', (1, 12, 1, 13), 'a', ('Store',)), ('Name', (1, 14, 1, 15), 'b', ('Store',))], ('Store',)), ('Name', (1, 20, 1, 21), 'c', ('Load',)), [], 0)])),
1942('Expression', ('Compare', (1, 0, 1, 9), ('Constant', (1, 0, 1, 1), 1, None), [('Lt',), ('Lt',)], [('Constant', (1, 4, 1, 5), 2, None), ('Constant', (1, 8, 1, 9), 3, None)])),
1943('Expression', ('Call', (1, 0, 1, 17), ('Name', (1, 0, 1, 1), 'f', ('Load',)), [('Constant', (1, 2, 1, 3), 1, None), ('Constant', (1, 4, 1, 5), 2, None), ('Starred', (1, 10, 1, 12), ('Name', (1, 11, 1, 12), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Constant', (1, 8, 1, 9), 3, None)), ('keyword', None, ('Name', (1, 15, 1, 16), 'e', ('Load',)))])),
1944('Expression', ('Call', (1, 0, 1, 10), ('Name', (1, 0, 1, 1), 'f', ('Load',)), [('Starred', (1, 2, 1, 9), ('List', (1, 3, 1, 9), [('Constant', (1, 4, 1, 5), 0, None), ('Constant', (1, 7, 1, 8), 1, None)], ('Load',)), ('Load',))], [])),
1945('Expression', ('Call', (1, 0, 1, 15), ('Name', (1, 0, 1, 1), 'f', ('Load',)), [('GeneratorExp', (1, 1, 1, 15), ('Name', (1, 2, 1, 3), 'a', ('Load',)), [('comprehension', ('Name', (1, 8, 1, 9), 'a', ('Store',)), ('Name', (1, 13, 1, 14), 'b', ('Load',)), [], 0)])], [])),
1946('Expression', ('Constant', (1, 0, 1, 2), 10, None)),
1947('Expression', ('Constant', (1, 0, 1, 8), 'string', None)),
1948('Expression', ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',))),
1949('Expression', ('Subscript', (1, 0, 1, 6), ('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Slice', ('Name', (1, 2, 1, 3), 'b', ('Load',)), ('Name', (1, 4, 1, 5), 'c', ('Load',)), None), ('Load',))),
1950('Expression', ('Name', (1, 0, 1, 1), 'v', ('Load',))),
1951('Expression', ('List', (1, 0, 1, 7), [('Constant', (1, 1, 1, 2), 1, None), ('Constant', (1, 3, 1, 4), 2, None), ('Constant', (1, 5, 1, 6), 3, None)], ('Load',))),
1952('Expression', ('List', (1, 0, 1, 2), [], ('Load',))),
1953('Expression', ('Tuple', (1, 0, 1, 5), [('Constant', (1, 0, 1, 1), 1, None), ('Constant', (1, 2, 1, 3), 2, None), ('Constant', (1, 4, 1, 5), 3, None)], ('Load',))),
1954('Expression', ('Tuple', (1, 0, 1, 7), [('Constant', (1, 1, 1, 2), 1, None), ('Constant', (1, 3, 1, 4), 2, None), ('Constant', (1, 5, 1, 6), 3, None)], ('Load',))),
1955('Expression', ('Tuple', (1, 0, 1, 2), [], ('Load',))),
1956('Expression', ('Call', (1, 0, 1, 17), ('Attribute', (1, 0, 1, 7), ('Attribute', (1, 0, 1, 5), ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8, 1, 16), ('Attribute', (1, 8, 1, 11), ('Name', (1, 8, 1, 9), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Constant', (1, 12, 1, 13), 1, None), ('Constant', (1, 14, 1, 15), 2, None), None), ('Load',))], [])),
Tim Peters400cbc32006-02-28 18:44:41 +00001957]
Neal Norwitzee9b10a2008-03-31 05:29:39 +00001958main()