ยปCore Development>Code coverage>Lib/test/test_ast.py

Python code coverage for Lib/test/test_ast.py

#countcontent
1n/aimport ast
2n/aimport dis
3n/aimport os
4n/aimport sys
5n/aimport unittest
6n/aimport weakref
7n/a
8n/afrom test import support
9n/a
10n/adef to_tuple(t):
11n/a if t is None or isinstance(t, (str, int, complex)):
12n/a return t
13n/a elif isinstance(t, list):
14n/a return [to_tuple(e) for e in t]
15n/a result = [t.__class__.__name__]
16n/a if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
17n/a result.append((t.lineno, t.col_offset))
18n/a if t._fields is None:
19n/a return tuple(result)
20n/a for f in t._fields:
21n/a result.append(to_tuple(getattr(t, f)))
22n/a return tuple(result)
23n/a
24n/a
25n/a# These tests are compiled through "exec"
26n/a# There should be at least one test per statement
27n/aexec_tests = [
28n/a # None
29n/a "None",
30n/a # FunctionDef
31n/a "def f(): pass",
32n/a # FunctionDef with arg
33n/a "def f(a): pass",
34n/a # FunctionDef with arg and default value
35n/a "def f(a=0): pass",
36n/a # FunctionDef with varargs
37n/a "def f(*args): pass",
38n/a # FunctionDef with kwargs
39n/a "def f(**kwargs): pass",
40n/a # FunctionDef with all kind of args
41n/a "def f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): pass",
42n/a # ClassDef
43n/a "class C:pass",
44n/a # ClassDef, new style class
45n/a "class C(object): pass",
46n/a # Return
47n/a "def f():return 1",
48n/a # Delete
49n/a "del v",
50n/a # Assign
51n/a "v = 1",
52n/a # AugAssign
53n/a "v += 1",
54n/a # For
55n/a "for v in v:pass",
56n/a # While
57n/a "while v:pass",
58n/a # If
59n/a "if v:pass",
60n/a # With
61n/a "with x as y: pass",
62n/a "with x as y, z as q: pass",
63n/a # Raise
64n/a "raise Exception('string')",
65n/a # TryExcept
66n/a "try:\n pass\nexcept Exception:\n pass",
67n/a # TryFinally
68n/a "try:\n pass\nfinally:\n pass",
69n/a # Assert
70n/a "assert v",
71n/a # Import
72n/a "import sys",
73n/a # ImportFrom
74n/a "from sys import v",
75n/a # Global
76n/a "global v",
77n/a # Expr
78n/a "1",
79n/a # Pass,
80n/a "pass",
81n/a # Break
82n/a "for v in v:break",
83n/a # Continue
84n/a "for v in v:continue",
85n/a # for statements with naked tuples (see http://bugs.python.org/issue6704)
86n/a "for a,b in c: pass",
87n/a "[(a,b) for a,b in c]",
88n/a "((a,b) for a,b in c)",
89n/a "((a,b) for (a,b) in c)",
90n/a # Multiline generator expression (test for .lineno & .col_offset)
91n/a """(
92n/a (
93n/a Aa
94n/a ,
95n/a Bb
96n/a )
97n/a for
98n/a Aa
99n/a ,
100n/a Bb in Cc
101n/a )""",
102n/a # dictcomp
103n/a "{a : b for w in x for m in p if g}",
104n/a # dictcomp with naked tuple
105n/a "{a : b for v,w in x}",
106n/a # setcomp
107n/a "{r for l in x if g}",
108n/a # setcomp with naked tuple
109n/a "{r for l,m in x}",
110n/a # AsyncFunctionDef
111n/a "async def f():\n await something()",
112n/a # AsyncFor
113n/a "async def f():\n async for e in i: 1\n else: 2",
114n/a # AsyncWith
115n/a "async def f():\n async with a as b: 1",
116n/a # PEP 448: Additional Unpacking Generalizations
117n/a "{**{1:2}, 2:3}",
118n/a "{*{1, 2}, 3}",
119n/a # Asynchronous comprehensions
120n/a "async def f():\n [i async for b in c]",
121n/a]
122n/a
123n/a# These are compiled through "single"
124n/a# because of overlap with "eval", it just tests what
125n/a# can't be tested with "eval"
126n/asingle_tests = [
127n/a "1+2"
128n/a]
129n/a
130n/a# These are compiled through "eval"
131n/a# It should test all expressions
132n/aeval_tests = [
133n/a # None
134n/a "None",
135n/a # BoolOp
136n/a "a and b",
137n/a # BinOp
138n/a "a + b",
139n/a # UnaryOp
140n/a "not v",
141n/a # Lambda
142n/a "lambda:None",
143n/a # Dict
144n/a "{ 1:2 }",
145n/a # Empty dict
146n/a "{}",
147n/a # Set
148n/a "{None,}",
149n/a # Multiline dict (test for .lineno & .col_offset)
150n/a """{
151n/a 1
152n/a :
153n/a 2
154n/a }""",
155n/a # ListComp
156n/a "[a for b in c if d]",
157n/a # GeneratorExp
158n/a "(a for b in c if d)",
159n/a # Yield - yield expressions can't work outside a function
160n/a #
161n/a # Compare
162n/a "1 < 2 < 3",
163n/a # Call
164n/a "f(1,2,c=3,*d,**e)",
165n/a # Num
166n/a "10",
167n/a # Str
168n/a "'string'",
169n/a # Attribute
170n/a "a.b",
171n/a # Subscript
172n/a "a[b:c]",
173n/a # Name
174n/a "v",
175n/a # List
176n/a "[1,2,3]",
177n/a # Empty list
178n/a "[]",
179n/a # Tuple
180n/a "1,2,3",
181n/a # Tuple
182n/a "(1,2,3)",
183n/a # Empty tuple
184n/a "()",
185n/a # Combination
186n/a "a.b.c.d(a.b[1:2])",
187n/a
188n/a]
189n/a
190n/a# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
191n/a# excepthandler, arguments, keywords, alias
192n/a
193n/aclass AST_Tests(unittest.TestCase):
194n/a
195n/a def _assertTrueorder(self, ast_node, parent_pos):
196n/a if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
197n/a return
198n/a if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
199n/a node_pos = (ast_node.lineno, ast_node.col_offset)
200n/a self.assertTrue(node_pos >= parent_pos)
201n/a parent_pos = (ast_node.lineno, ast_node.col_offset)
202n/a for name in ast_node._fields:
203n/a value = getattr(ast_node, name)
204n/a if isinstance(value, list):
205n/a for child in value:
206n/a self._assertTrueorder(child, parent_pos)
207n/a elif value is not None:
208n/a self._assertTrueorder(value, parent_pos)
209n/a
210n/a def test_AST_objects(self):
211n/a x = ast.AST()
212n/a self.assertEqual(x._fields, ())
213n/a x.foobar = 42
214n/a self.assertEqual(x.foobar, 42)
215n/a self.assertEqual(x.__dict__["foobar"], 42)
216n/a
217n/a with self.assertRaises(AttributeError):
218n/a x.vararg
219n/a
220n/a with self.assertRaises(TypeError):
221n/a # "_ast.AST constructor takes 0 positional arguments"
222n/a ast.AST(2)
223n/a
224n/a def test_AST_garbage_collection(self):
225n/a class X:
226n/a pass
227n/a a = ast.AST()
228n/a a.x = X()
229n/a a.x.a = a
230n/a ref = weakref.ref(a.x)
231n/a del a
232n/a support.gc_collect()
233n/a self.assertIsNone(ref())
234n/a
235n/a def test_snippets(self):
236n/a for input, output, kind in ((exec_tests, exec_results, "exec"),
237n/a (single_tests, single_results, "single"),
238n/a (eval_tests, eval_results, "eval")):
239n/a for i, o in zip(input, output):
240n/a with self.subTest(action="parsing", input=i):
241n/a ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
242n/a self.assertEqual(to_tuple(ast_tree), o)
243n/a self._assertTrueorder(ast_tree, (0, 0))
244n/a with self.subTest(action="compiling", input=i, kind=kind):
245n/a compile(ast_tree, "?", kind)
246n/a
247n/a def test_slice(self):
248n/a slc = ast.parse("x[::]").body[0].value.slice
249n/a self.assertIsNone(slc.upper)
250n/a self.assertIsNone(slc.lower)
251n/a self.assertIsNone(slc.step)
252n/a
253n/a def test_from_import(self):
254n/a im = ast.parse("from . import y").body[0]
255n/a self.assertIsNone(im.module)
256n/a
257n/a def test_non_interned_future_from_ast(self):
258n/a mod = ast.parse("from __future__ import division")
259n/a self.assertIsInstance(mod.body[0], ast.ImportFrom)
260n/a mod.body[0].module = " __future__ ".strip()
261n/a compile(mod, "<test>", "exec")
262n/a
263n/a def test_base_classes(self):
264n/a self.assertTrue(issubclass(ast.For, ast.stmt))
265n/a self.assertTrue(issubclass(ast.Name, ast.expr))
266n/a self.assertTrue(issubclass(ast.stmt, ast.AST))
267n/a self.assertTrue(issubclass(ast.expr, ast.AST))
268n/a self.assertTrue(issubclass(ast.comprehension, ast.AST))
269n/a self.assertTrue(issubclass(ast.Gt, ast.AST))
270n/a
271n/a def test_field_attr_existence(self):
272n/a for name, item in ast.__dict__.items():
273n/a if isinstance(item, type) and name != 'AST' and name[0].isupper():
274n/a x = item()
275n/a if isinstance(x, ast.AST):
276n/a self.assertEqual(type(x._fields), tuple)
277n/a
278n/a def test_arguments(self):
279n/a x = ast.arguments()
280n/a self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs',
281n/a 'kw_defaults', 'kwarg', 'defaults'))
282n/a
283n/a with self.assertRaises(AttributeError):
284n/a x.vararg
285n/a
286n/a x = ast.arguments(*range(1, 7))
287n/a self.assertEqual(x.vararg, 2)
288n/a
289n/a def test_field_attr_writable(self):
290n/a x = ast.Num()
291n/a # We can assign to _fields
292n/a x._fields = 666
293n/a self.assertEqual(x._fields, 666)
294n/a
295n/a def test_classattrs(self):
296n/a x = ast.Num()
297n/a self.assertEqual(x._fields, ('n',))
298n/a
299n/a with self.assertRaises(AttributeError):
300n/a x.n
301n/a
302n/a x = ast.Num(42)
303n/a self.assertEqual(x.n, 42)
304n/a
305n/a with self.assertRaises(AttributeError):
306n/a x.lineno
307n/a
308n/a with self.assertRaises(AttributeError):
309n/a x.foobar
310n/a
311n/a x = ast.Num(lineno=2)
312n/a self.assertEqual(x.lineno, 2)
313n/a
314n/a x = ast.Num(42, lineno=0)
315n/a self.assertEqual(x.lineno, 0)
316n/a self.assertEqual(x._fields, ('n',))
317n/a self.assertEqual(x.n, 42)
318n/a
319n/a self.assertRaises(TypeError, ast.Num, 1, 2)
320n/a self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
321n/a
322n/a def test_module(self):
323n/a body = [ast.Num(42)]
324n/a x = ast.Module(body)
325n/a self.assertEqual(x.body, body)
326n/a
327n/a def test_nodeclasses(self):
328n/a # Zero arguments constructor explicitly allowed
329n/a x = ast.BinOp()
330n/a self.assertEqual(x._fields, ('left', 'op', 'right'))
331n/a
332n/a # Random attribute allowed too
333n/a x.foobarbaz = 5
334n/a self.assertEqual(x.foobarbaz, 5)
335n/a
336n/a n1 = ast.Num(1)
337n/a n3 = ast.Num(3)
338n/a addop = ast.Add()
339n/a x = ast.BinOp(n1, addop, n3)
340n/a self.assertEqual(x.left, n1)
341n/a self.assertEqual(x.op, addop)
342n/a self.assertEqual(x.right, n3)
343n/a
344n/a x = ast.BinOp(1, 2, 3)
345n/a self.assertEqual(x.left, 1)
346n/a self.assertEqual(x.op, 2)
347n/a self.assertEqual(x.right, 3)
348n/a
349n/a x = ast.BinOp(1, 2, 3, lineno=0)
350n/a self.assertEqual(x.left, 1)
351n/a self.assertEqual(x.op, 2)
352n/a self.assertEqual(x.right, 3)
353n/a self.assertEqual(x.lineno, 0)
354n/a
355n/a # node raises exception when not given enough arguments
356n/a self.assertRaises(TypeError, ast.BinOp, 1, 2)
357n/a # node raises exception when given too many arguments
358n/a self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
359n/a # node raises exception when not given enough arguments
360n/a self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
361n/a # node raises exception when given too many arguments
362n/a self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
363n/a
364n/a # can set attributes through kwargs too
365n/a x = ast.BinOp(left=1, op=2, right=3, lineno=0)
366n/a self.assertEqual(x.left, 1)
367n/a self.assertEqual(x.op, 2)
368n/a self.assertEqual(x.right, 3)
369n/a self.assertEqual(x.lineno, 0)
370n/a
371n/a # Random kwargs also allowed
372n/a x = ast.BinOp(1, 2, 3, foobarbaz=42)
373n/a self.assertEqual(x.foobarbaz, 42)
374n/a
375n/a def test_no_fields(self):
376n/a # this used to fail because Sub._fields was None
377n/a x = ast.Sub()
378n/a self.assertEqual(x._fields, ())
379n/a
380n/a def test_pickling(self):
381n/a import pickle
382n/a mods = [pickle]
383n/a try:
384n/a import cPickle
385n/a mods.append(cPickle)
386n/a except ImportError:
387n/a pass
388n/a protocols = [0, 1, 2]
389n/a for mod in mods:
390n/a for protocol in protocols:
391n/a for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
392n/a ast2 = mod.loads(mod.dumps(ast, protocol))
393n/a self.assertEqual(to_tuple(ast2), to_tuple(ast))
394n/a
395n/a def test_invalid_sum(self):
396n/a pos = dict(lineno=2, col_offset=3)
397n/a m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
398n/a with self.assertRaises(TypeError) as cm:
399n/a compile(m, "<test>", "exec")
400n/a self.assertIn("but got <_ast.expr", str(cm.exception))
401n/a
402n/a def test_invalid_identitifer(self):
403n/a m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
404n/a ast.fix_missing_locations(m)
405n/a with self.assertRaises(TypeError) as cm:
406n/a compile(m, "<test>", "exec")
407n/a self.assertIn("identifier must be of type str", str(cm.exception))
408n/a
409n/a def test_invalid_string(self):
410n/a m = ast.Module([ast.Expr(ast.Str(42))])
411n/a ast.fix_missing_locations(m)
412n/a with self.assertRaises(TypeError) as cm:
413n/a compile(m, "<test>", "exec")
414n/a self.assertIn("string must be of type str", str(cm.exception))
415n/a
416n/a def test_empty_yield_from(self):
417n/a # Issue 16546: yield from value is not optional.
418n/a empty_yield_from = ast.parse("def f():\n yield from g()")
419n/a empty_yield_from.body[0].body[0].value.value = None
420n/a with self.assertRaises(ValueError) as cm:
421n/a compile(empty_yield_from, "<test>", "exec")
422n/a self.assertIn("field value is required", str(cm.exception))
423n/a
424n/a
425n/aclass ASTHelpers_Test(unittest.TestCase):
426n/a
427n/a def test_parse(self):
428n/a a = ast.parse('foo(1 + 1)')
429n/a b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
430n/a self.assertEqual(ast.dump(a), ast.dump(b))
431n/a
432n/a def test_parse_in_error(self):
433n/a try:
434n/a 1/0
435n/a except Exception:
436n/a with self.assertRaises(SyntaxError) as e:
437n/a ast.literal_eval(r"'\U'")
438n/a self.assertIsNotNone(e.exception.__context__)
439n/a
440n/a def test_dump(self):
441n/a node = ast.parse('spam(eggs, "and cheese")')
442n/a self.assertEqual(ast.dump(node),
443n/a "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
444n/a "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
445n/a "keywords=[]))])"
446n/a )
447n/a self.assertEqual(ast.dump(node, annotate_fields=False),
448n/a "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
449n/a "Str('and cheese')], []))])"
450n/a )
451n/a self.assertEqual(ast.dump(node, include_attributes=True),
452n/a "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
453n/a "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
454n/a "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
455n/a "col_offset=11)], keywords=[], "
456n/a "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
457n/a )
458n/a
459n/a def test_copy_location(self):
460n/a src = ast.parse('1 + 1', mode='eval')
461n/a src.body.right = ast.copy_location(ast.Num(2), src.body.right)
462n/a self.assertEqual(ast.dump(src, include_attributes=True),
463n/a 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
464n/a 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
465n/a 'col_offset=0))'
466n/a )
467n/a
468n/a def test_fix_missing_locations(self):
469n/a src = ast.parse('write("spam")')
470n/a src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
471n/a [ast.Str('eggs')], [])))
472n/a self.assertEqual(src, ast.fix_missing_locations(src))
473n/a self.assertEqual(ast.dump(src, include_attributes=True),
474n/a "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
475n/a "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
476n/a "col_offset=6)], keywords=[], "
477n/a "lineno=1, col_offset=0), lineno=1, col_offset=0), "
478n/a "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
479n/a "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
480n/a "keywords=[], lineno=1, "
481n/a "col_offset=0), lineno=1, col_offset=0)])"
482n/a )
483n/a
484n/a def test_increment_lineno(self):
485n/a src = ast.parse('1 + 1', mode='eval')
486n/a self.assertEqual(ast.increment_lineno(src, n=3), src)
487n/a self.assertEqual(ast.dump(src, include_attributes=True),
488n/a 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
489n/a 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
490n/a 'col_offset=0))'
491n/a )
492n/a # issue10869: do not increment lineno of root twice
493n/a src = ast.parse('1 + 1', mode='eval')
494n/a self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
495n/a self.assertEqual(ast.dump(src, include_attributes=True),
496n/a 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
497n/a 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
498n/a 'col_offset=0))'
499n/a )
500n/a
501n/a def test_iter_fields(self):
502n/a node = ast.parse('foo()', mode='eval')
503n/a d = dict(ast.iter_fields(node.body))
504n/a self.assertEqual(d.pop('func').id, 'foo')
505n/a self.assertEqual(d, {'keywords': [], 'args': []})
506n/a
507n/a def test_iter_child_nodes(self):
508n/a node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
509n/a self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
510n/a iterator = ast.iter_child_nodes(node.body)
511n/a self.assertEqual(next(iterator).id, 'spam')
512n/a self.assertEqual(next(iterator).n, 23)
513n/a self.assertEqual(next(iterator).n, 42)
514n/a self.assertEqual(ast.dump(next(iterator)),
515n/a "keyword(arg='eggs', value=Str(s='leek'))"
516n/a )
517n/a
518n/a def test_get_docstring(self):
519n/a node = ast.parse('def foo():\n """line one\n line two"""')
520n/a self.assertEqual(ast.get_docstring(node.body[0]),
521n/a 'line one\nline two')
522n/a
523n/a node = ast.parse('async def foo():\n """spam\n ham"""')
524n/a self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
525n/a
526n/a def test_literal_eval(self):
527n/a self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
528n/a self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
529n/a self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
530n/a self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
531n/a self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
532n/a self.assertRaises(ValueError, ast.literal_eval, 'foo()')
533n/a self.assertEqual(ast.literal_eval('-6'), -6)
534n/a self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
535n/a self.assertEqual(ast.literal_eval('3.25'), 3.25)
536n/a
537n/a def test_literal_eval_issue4907(self):
538n/a self.assertEqual(ast.literal_eval('2j'), 2j)
539n/a self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
540n/a self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
541n/a
542n/a def test_bad_integer(self):
543n/a # issue13436: Bad error message with invalid numeric values
544n/a body = [ast.ImportFrom(module='time',
545n/a names=[ast.alias(name='sleep')],
546n/a level=None,
547n/a lineno=None, col_offset=None)]
548n/a mod = ast.Module(body)
549n/a with self.assertRaises(ValueError) as cm:
550n/a compile(mod, 'test', 'exec')
551n/a self.assertIn("invalid integer value: None", str(cm.exception))
552n/a
553n/a def test_level_as_none(self):
554n/a body = [ast.ImportFrom(module='time',
555n/a names=[ast.alias(name='sleep')],
556n/a level=None,
557n/a lineno=0, col_offset=0)]
558n/a mod = ast.Module(body)
559n/a code = compile(mod, 'test', 'exec')
560n/a ns = {}
561n/a exec(code, ns)
562n/a self.assertIn('sleep', ns)
563n/a
564n/a
565n/aclass ASTValidatorTests(unittest.TestCase):
566n/a
567n/a def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
568n/a mod.lineno = mod.col_offset = 0
569n/a ast.fix_missing_locations(mod)
570n/a with self.assertRaises(exc) as cm:
571n/a compile(mod, "<test>", mode)
572n/a if msg is not None:
573n/a self.assertIn(msg, str(cm.exception))
574n/a
575n/a def expr(self, node, msg=None, *, exc=ValueError):
576n/a mod = ast.Module([ast.Expr(node)])
577n/a self.mod(mod, msg, exc=exc)
578n/a
579n/a def stmt(self, stmt, msg=None):
580n/a mod = ast.Module([stmt])
581n/a self.mod(mod, msg)
582n/a
583n/a def test_module(self):
584n/a m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
585n/a self.mod(m, "must have Load context", "single")
586n/a m = ast.Expression(ast.Name("x", ast.Store()))
587n/a self.mod(m, "must have Load context", "eval")
588n/a
589n/a def _check_arguments(self, fac, check):
590n/a def arguments(args=None, vararg=None,
591n/a kwonlyargs=None, kwarg=None,
592n/a defaults=None, kw_defaults=None):
593n/a if args is None:
594n/a args = []
595n/a if kwonlyargs is None:
596n/a kwonlyargs = []
597n/a if defaults is None:
598n/a defaults = []
599n/a if kw_defaults is None:
600n/a kw_defaults = []
601n/a args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
602n/a kwarg, defaults)
603n/a return fac(args)
604n/a args = [ast.arg("x", ast.Name("x", ast.Store()))]
605n/a check(arguments(args=args), "must have Load context")
606n/a check(arguments(kwonlyargs=args), "must have Load context")
607n/a check(arguments(defaults=[ast.Num(3)]),
608n/a "more positional defaults than args")
609n/a check(arguments(kw_defaults=[ast.Num(4)]),
610n/a "length of kwonlyargs is not the same as kw_defaults")
611n/a args = [ast.arg("x", ast.Name("x", ast.Load()))]
612n/a check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
613n/a "must have Load context")
614n/a args = [ast.arg("a", ast.Name("x", ast.Load())),
615n/a ast.arg("b", ast.Name("y", ast.Load()))]
616n/a check(arguments(kwonlyargs=args,
617n/a kw_defaults=[None, ast.Name("x", ast.Store())]),
618n/a "must have Load context")
619n/a
620n/a def test_funcdef(self):
621n/a a = ast.arguments([], None, [], [], None, [])
622n/a f = ast.FunctionDef("x", a, [], [], None)
623n/a self.stmt(f, "empty body on FunctionDef")
624n/a f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
625n/a None)
626n/a self.stmt(f, "must have Load context")
627n/a f = ast.FunctionDef("x", a, [ast.Pass()], [],
628n/a ast.Name("x", ast.Store()))
629n/a self.stmt(f, "must have Load context")
630n/a def fac(args):
631n/a return ast.FunctionDef("x", args, [ast.Pass()], [], None)
632n/a self._check_arguments(fac, self.stmt)
633n/a
634n/a def test_classdef(self):
635n/a def cls(bases=None, keywords=None, body=None, decorator_list=None):
636n/a if bases is None:
637n/a bases = []
638n/a if keywords is None:
639n/a keywords = []
640n/a if body is None:
641n/a body = [ast.Pass()]
642n/a if decorator_list is None:
643n/a decorator_list = []
644n/a return ast.ClassDef("myclass", bases, keywords,
645n/a body, decorator_list)
646n/a self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
647n/a "must have Load context")
648n/a self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
649n/a "must have Load context")
650n/a self.stmt(cls(body=[]), "empty body on ClassDef")
651n/a self.stmt(cls(body=[None]), "None disallowed")
652n/a self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
653n/a "must have Load context")
654n/a
655n/a def test_delete(self):
656n/a self.stmt(ast.Delete([]), "empty targets on Delete")
657n/a self.stmt(ast.Delete([None]), "None disallowed")
658n/a self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
659n/a "must have Del context")
660n/a
661n/a def test_assign(self):
662n/a self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign")
663n/a self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed")
664n/a self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)),
665n/a "must have Store context")
666n/a self.stmt(ast.Assign([ast.Name("x", ast.Store())],
667n/a ast.Name("y", ast.Store())),
668n/a "must have Load context")
669n/a
670n/a def test_augassign(self):
671n/a aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
672n/a ast.Name("y", ast.Load()))
673n/a self.stmt(aug, "must have Store context")
674n/a aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
675n/a ast.Name("y", ast.Store()))
676n/a self.stmt(aug, "must have Load context")
677n/a
678n/a def test_for(self):
679n/a x = ast.Name("x", ast.Store())
680n/a y = ast.Name("y", ast.Load())
681n/a p = ast.Pass()
682n/a self.stmt(ast.For(x, y, [], []), "empty body on For")
683n/a self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
684n/a "must have Store context")
685n/a self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
686n/a "must have Load context")
687n/a e = ast.Expr(ast.Name("x", ast.Store()))
688n/a self.stmt(ast.For(x, y, [e], []), "must have Load context")
689n/a self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
690n/a
691n/a def test_while(self):
692n/a self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
693n/a self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
694n/a "must have Load context")
695n/a self.stmt(ast.While(ast.Num(3), [ast.Pass()],
696n/a [ast.Expr(ast.Name("x", ast.Store()))]),
697n/a "must have Load context")
698n/a
699n/a def test_if(self):
700n/a self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
701n/a i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
702n/a self.stmt(i, "must have Load context")
703n/a i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
704n/a self.stmt(i, "must have Load context")
705n/a i = ast.If(ast.Num(3), [ast.Pass()],
706n/a [ast.Expr(ast.Name("x", ast.Store()))])
707n/a self.stmt(i, "must have Load context")
708n/a
709n/a def test_with(self):
710n/a p = ast.Pass()
711n/a self.stmt(ast.With([], [p]), "empty items on With")
712n/a i = ast.withitem(ast.Num(3), None)
713n/a self.stmt(ast.With([i], []), "empty body on With")
714n/a i = ast.withitem(ast.Name("x", ast.Store()), None)
715n/a self.stmt(ast.With([i], [p]), "must have Load context")
716n/a i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
717n/a self.stmt(ast.With([i], [p]), "must have Store context")
718n/a
719n/a def test_raise(self):
720n/a r = ast.Raise(None, ast.Num(3))
721n/a self.stmt(r, "Raise with cause but no exception")
722n/a r = ast.Raise(ast.Name("x", ast.Store()), None)
723n/a self.stmt(r, "must have Load context")
724n/a r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store()))
725n/a self.stmt(r, "must have Load context")
726n/a
727n/a def test_try(self):
728n/a p = ast.Pass()
729n/a t = ast.Try([], [], [], [p])
730n/a self.stmt(t, "empty body on Try")
731n/a t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
732n/a self.stmt(t, "must have Load context")
733n/a t = ast.Try([p], [], [], [])
734n/a self.stmt(t, "Try has neither except handlers nor finalbody")
735n/a t = ast.Try([p], [], [p], [p])
736n/a self.stmt(t, "Try has orelse but no except handlers")
737n/a t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
738n/a self.stmt(t, "empty body on ExceptHandler")
739n/a e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
740n/a self.stmt(ast.Try([p], e, [], []), "must have Load context")
741n/a e = [ast.ExceptHandler(None, "x", [p])]
742n/a t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
743n/a self.stmt(t, "must have Load context")
744n/a t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
745n/a self.stmt(t, "must have Load context")
746n/a
747n/a def test_assert(self):
748n/a self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
749n/a "must have Load context")
750n/a assrt = ast.Assert(ast.Name("x", ast.Load()),
751n/a ast.Name("y", ast.Store()))
752n/a self.stmt(assrt, "must have Load context")
753n/a
754n/a def test_import(self):
755n/a self.stmt(ast.Import([]), "empty names on Import")
756n/a
757n/a def test_importfrom(self):
758n/a imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
759n/a self.stmt(imp, "Negative ImportFrom level")
760n/a self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
761n/a
762n/a def test_global(self):
763n/a self.stmt(ast.Global([]), "empty names on Global")
764n/a
765n/a def test_nonlocal(self):
766n/a self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
767n/a
768n/a def test_expr(self):
769n/a e = ast.Expr(ast.Name("x", ast.Store()))
770n/a self.stmt(e, "must have Load context")
771n/a
772n/a def test_boolop(self):
773n/a b = ast.BoolOp(ast.And(), [])
774n/a self.expr(b, "less than 2 values")
775n/a b = ast.BoolOp(ast.And(), [ast.Num(3)])
776n/a self.expr(b, "less than 2 values")
777n/a b = ast.BoolOp(ast.And(), [ast.Num(4), None])
778n/a self.expr(b, "None disallowed")
779n/a b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())])
780n/a self.expr(b, "must have Load context")
781n/a
782n/a def test_unaryop(self):
783n/a u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
784n/a self.expr(u, "must have Load context")
785n/a
786n/a def test_lambda(self):
787n/a a = ast.arguments([], None, [], [], None, [])
788n/a self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
789n/a "must have Load context")
790n/a def fac(args):
791n/a return ast.Lambda(args, ast.Name("x", ast.Load()))
792n/a self._check_arguments(fac, self.expr)
793n/a
794n/a def test_ifexp(self):
795n/a l = ast.Name("x", ast.Load())
796n/a s = ast.Name("y", ast.Store())
797n/a for args in (s, l, l), (l, s, l), (l, l, s):
798n/a self.expr(ast.IfExp(*args), "must have Load context")
799n/a
800n/a def test_dict(self):
801n/a d = ast.Dict([], [ast.Name("x", ast.Load())])
802n/a self.expr(d, "same number of keys as values")
803n/a d = ast.Dict([ast.Name("x", ast.Load())], [None])
804n/a self.expr(d, "None disallowed")
805n/a
806n/a def test_set(self):
807n/a self.expr(ast.Set([None]), "None disallowed")
808n/a s = ast.Set([ast.Name("x", ast.Store())])
809n/a self.expr(s, "must have Load context")
810n/a
811n/a def _check_comprehension(self, fac):
812n/a self.expr(fac([]), "comprehension with no generators")
813n/a g = ast.comprehension(ast.Name("x", ast.Load()),
814n/a ast.Name("x", ast.Load()), [], 0)
815n/a self.expr(fac([g]), "must have Store context")
816n/a g = ast.comprehension(ast.Name("x", ast.Store()),
817n/a ast.Name("x", ast.Store()), [], 0)
818n/a self.expr(fac([g]), "must have Load context")
819n/a x = ast.Name("x", ast.Store())
820n/a y = ast.Name("y", ast.Load())
821n/a g = ast.comprehension(x, y, [None], 0)
822n/a self.expr(fac([g]), "None disallowed")
823n/a g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0)
824n/a self.expr(fac([g]), "must have Load context")
825n/a
826n/a def _simple_comp(self, fac):
827n/a g = ast.comprehension(ast.Name("x", ast.Store()),
828n/a ast.Name("x", ast.Load()), [], 0)
829n/a self.expr(fac(ast.Name("x", ast.Store()), [g]),
830n/a "must have Load context")
831n/a def wrap(gens):
832n/a return fac(ast.Name("x", ast.Store()), gens)
833n/a self._check_comprehension(wrap)
834n/a
835n/a def test_listcomp(self):
836n/a self._simple_comp(ast.ListComp)
837n/a
838n/a def test_setcomp(self):
839n/a self._simple_comp(ast.SetComp)
840n/a
841n/a def test_generatorexp(self):
842n/a self._simple_comp(ast.GeneratorExp)
843n/a
844n/a def test_dictcomp(self):
845n/a g = ast.comprehension(ast.Name("y", ast.Store()),
846n/a ast.Name("p", ast.Load()), [], 0)
847n/a c = ast.DictComp(ast.Name("x", ast.Store()),
848n/a ast.Name("y", ast.Load()), [g])
849n/a self.expr(c, "must have Load context")
850n/a c = ast.DictComp(ast.Name("x", ast.Load()),
851n/a ast.Name("y", ast.Store()), [g])
852n/a self.expr(c, "must have Load context")
853n/a def factory(comps):
854n/a k = ast.Name("x", ast.Load())
855n/a v = ast.Name("y", ast.Load())
856n/a return ast.DictComp(k, v, comps)
857n/a self._check_comprehension(factory)
858n/a
859n/a def test_yield(self):
860n/a self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
861n/a self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
862n/a
863n/a def test_compare(self):
864n/a left = ast.Name("x", ast.Load())
865n/a comp = ast.Compare(left, [ast.In()], [])
866n/a self.expr(comp, "no comparators")
867n/a comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
868n/a self.expr(comp, "different number of comparators and operands")
869n/a comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
870n/a self.expr(comp, "non-numeric", exc=TypeError)
871n/a comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
872n/a self.expr(comp, "non-numeric", exc=TypeError)
873n/a
874n/a def test_call(self):
875n/a func = ast.Name("x", ast.Load())
876n/a args = [ast.Name("y", ast.Load())]
877n/a keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
878n/a call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
879n/a self.expr(call, "must have Load context")
880n/a call = ast.Call(func, [None], keywords)
881n/a self.expr(call, "None disallowed")
882n/a bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
883n/a call = ast.Call(func, args, bad_keywords)
884n/a self.expr(call, "must have Load context")
885n/a
886n/a def test_num(self):
887n/a class subint(int):
888n/a pass
889n/a class subfloat(float):
890n/a pass
891n/a class subcomplex(complex):
892n/a pass
893n/a for obj in "0", "hello", subint(), subfloat(), subcomplex():
894n/a self.expr(ast.Num(obj), "non-numeric", exc=TypeError)
895n/a
896n/a def test_attribute(self):
897n/a attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
898n/a self.expr(attr, "must have Load context")
899n/a
900n/a def test_subscript(self):
901n/a sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
902n/a ast.Load())
903n/a self.expr(sub, "must have Load context")
904n/a x = ast.Name("x", ast.Load())
905n/a sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
906n/a ast.Load())
907n/a self.expr(sub, "must have Load context")
908n/a s = ast.Name("x", ast.Store())
909n/a for args in (s, None, None), (None, s, None), (None, None, s):
910n/a sl = ast.Slice(*args)
911n/a self.expr(ast.Subscript(x, sl, ast.Load()),
912n/a "must have Load context")
913n/a sl = ast.ExtSlice([])
914n/a self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
915n/a sl = ast.ExtSlice([ast.Index(s)])
916n/a self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
917n/a
918n/a def test_starred(self):
919n/a left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
920n/a ast.Store())
921n/a assign = ast.Assign([left], ast.Num(4))
922n/a self.stmt(assign, "must have Store context")
923n/a
924n/a def _sequence(self, fac):
925n/a self.expr(fac([None], ast.Load()), "None disallowed")
926n/a self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
927n/a "must have Load context")
928n/a
929n/a def test_list(self):
930n/a self._sequence(ast.List)
931n/a
932n/a def test_tuple(self):
933n/a self._sequence(ast.Tuple)
934n/a
935n/a def test_nameconstant(self):
936n/a self.expr(ast.NameConstant(4), "singleton must be True, False, or None")
937n/a
938n/a def test_stdlib_validates(self):
939n/a stdlib = os.path.dirname(ast.__file__)
940n/a tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")]
941n/a tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
942n/a for module in tests:
943n/a fn = os.path.join(stdlib, module)
944n/a with open(fn, "r", encoding="utf-8") as fp:
945n/a source = fp.read()
946n/a mod = ast.parse(source, fn)
947n/a compile(mod, fn, "exec")
948n/a
949n/a
950n/aclass ConstantTests(unittest.TestCase):
951n/a """Tests on the ast.Constant node type."""
952n/a
953n/a def compile_constant(self, value):
954n/a tree = ast.parse("x = 123")
955n/a
956n/a node = tree.body[0].value
957n/a new_node = ast.Constant(value=value)
958n/a ast.copy_location(new_node, node)
959n/a tree.body[0].value = new_node
960n/a
961n/a code = compile(tree, "<string>", "exec")
962n/a
963n/a ns = {}
964n/a exec(code, ns)
965n/a return ns['x']
966n/a
967n/a def test_validation(self):
968n/a with self.assertRaises(TypeError) as cm:
969n/a self.compile_constant([1, 2, 3])
970n/a self.assertEqual(str(cm.exception),
971n/a "got an invalid type in Constant: list")
972n/a
973n/a def test_singletons(self):
974n/a for const in (None, False, True, Ellipsis, b'', frozenset()):
975n/a with self.subTest(const=const):
976n/a value = self.compile_constant(const)
977n/a self.assertIs(value, const)
978n/a
979n/a def test_values(self):
980n/a nested_tuple = (1,)
981n/a nested_frozenset = frozenset({1})
982n/a for level in range(3):
983n/a nested_tuple = (nested_tuple, 2)
984n/a nested_frozenset = frozenset({nested_frozenset, 2})
985n/a values = (123, 123.0, 123j,
986n/a "unicode", b'bytes',
987n/a tuple("tuple"), frozenset("frozenset"),
988n/a nested_tuple, nested_frozenset)
989n/a for value in values:
990n/a with self.subTest(value=value):
991n/a result = self.compile_constant(value)
992n/a self.assertEqual(result, value)
993n/a
994n/a def test_assign_to_constant(self):
995n/a tree = ast.parse("x = 1")
996n/a
997n/a target = tree.body[0].targets[0]
998n/a new_target = ast.Constant(value=1)
999n/a ast.copy_location(new_target, target)
1000n/a tree.body[0].targets[0] = new_target
1001n/a
1002n/a with self.assertRaises(ValueError) as cm:
1003n/a compile(tree, "string", "exec")
1004n/a self.assertEqual(str(cm.exception),
1005n/a "expression which can't be assigned "
1006n/a "to in Store context")
1007n/a
1008n/a def test_get_docstring(self):
1009n/a tree = ast.parse("'docstring'\nx = 1")
1010n/a self.assertEqual(ast.get_docstring(tree), 'docstring')
1011n/a
1012n/a tree.body[0].value = ast.Constant(value='constant docstring')
1013n/a self.assertEqual(ast.get_docstring(tree), 'constant docstring')
1014n/a
1015n/a def get_load_const(self, tree):
1016n/a # Compile to bytecode, disassemble and get parameter of LOAD_CONST
1017n/a # instructions
1018n/a co = compile(tree, '<string>', 'exec')
1019n/a consts = []
1020n/a for instr in dis.get_instructions(co):
1021n/a if instr.opname == 'LOAD_CONST':
1022n/a consts.append(instr.argval)
1023n/a return consts
1024n/a
1025n/a @support.cpython_only
1026n/a def test_load_const(self):
1027n/a consts = [None,
1028n/a True, False,
1029n/a 124,
1030n/a 2.0,
1031n/a 3j,
1032n/a "unicode",
1033n/a b'bytes',
1034n/a (1, 2, 3)]
1035n/a
1036n/a code = '\n'.join(['x={!r}'.format(const) for const in consts])
1037n/a code += '\nx = ...'
1038n/a consts.extend((Ellipsis, None))
1039n/a
1040n/a tree = ast.parse(code)
1041n/a self.assertEqual(self.get_load_const(tree),
1042n/a consts)
1043n/a
1044n/a # Replace expression nodes with constants
1045n/a for assign, const in zip(tree.body, consts):
1046n/a assert isinstance(assign, ast.Assign), ast.dump(assign)
1047n/a new_node = ast.Constant(value=const)
1048n/a ast.copy_location(new_node, assign.value)
1049n/a assign.value = new_node
1050n/a
1051n/a self.assertEqual(self.get_load_const(tree),
1052n/a consts)
1053n/a
1054n/a def test_literal_eval(self):
1055n/a tree = ast.parse("1 + 2")
1056n/a binop = tree.body[0].value
1057n/a
1058n/a new_left = ast.Constant(value=10)
1059n/a ast.copy_location(new_left, binop.left)
1060n/a binop.left = new_left
1061n/a
1062n/a new_right = ast.Constant(value=20)
1063n/a ast.copy_location(new_right, binop.right)
1064n/a binop.right = new_right
1065n/a
1066n/a self.assertEqual(ast.literal_eval(binop), 30)
1067n/a
1068n/a
1069n/adef main():
1070n/a if __name__ != '__main__':
1071n/a return
1072n/a if sys.argv[1:] == ['-g']:
1073n/a for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
1074n/a (eval_tests, "eval")):
1075n/a print(kind+"_results = [")
1076n/a for statement in statements:
1077n/a tree = ast.parse(statement, "?", kind)
1078n/a print("%r," % (to_tuple(tree),))
1079n/a print("]")
1080n/a print("main()")
1081n/a raise SystemExit
1082n/a unittest.main()
1083n/a
1084n/a#### EVERYTHING BELOW IS GENERATED #####
1085n/aexec_results = [
1086n/a('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]),
1087n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]),
1088n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]),
1089n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
1090n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
1091n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
1092n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [('arg', (1, 41), 'f', None)], [('Num', (1, 43), 42)], ('arg', (1, 49), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 58))], [], None)]),
1093n/a('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]),
1094n/a('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]),
1095n/a('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
1096n/a('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
1097n/a('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
1098n/a('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
1099n/a('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
1100n/a('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
1101n/a('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
1102n/a('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
1103n/a('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',))), ('withitem', ('Name', (1, 13), 'z', ('Load',)), ('Name', (1, 18), 'q', ('Store',)))], [('Pass', (1, 21))])]),
1104n/a('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]),
1105n/a('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
1106n/a('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
1107n/a('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
1108n/a('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
1109n/a('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
1110n/a('Module', [('Global', (1, 0), ['v'])]),
1111n/a('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
1112n/a('Module', [('Pass', (1, 0))]),
1113n/a('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
1114n/a('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
1115n/a('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
1116n/a('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)]))]),
1117n/a('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)]))]),
1118n/a('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)]))]),
1119n/a('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [], 0)]))]),
1120n/a('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), [], 0), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))], 0)]))]),
1121n/a('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [], 0)]))]),
1122n/a('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))], 0)]))]),
1123n/a('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [], 0)]))]),
1124n/a('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
1125n/a('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 7), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]),
1126n/a('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 7), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]),
1127n/a('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Num', (1, 10), 2)], [('Dict', (1, 3), [('Num', (1, 4), 1)], [('Num', (1, 6), 2)]), ('Num', (1, 12), 3)]))]),
1128n/a('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Num', (1, 3), 1), ('Num', (1, 6), 2)]), ('Load',)), ('Num', (1, 10), 3)]))]),
1129n/a('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]),
1130n/a]
1131n/asingle_results = [
1132n/a('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
1133n/a]
1134n/aeval_results = [
1135n/a('Expression', ('NameConstant', (1, 0), None)),
1136n/a('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
1137n/a('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
1138n/a('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
1139n/a('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
1140n/a('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
1141n/a('Expression', ('Dict', (1, 0), [], [])),
1142n/a('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
1143n/a('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
1144n/a('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
1145n/a('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
1146n/a('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
1147n/a('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Num', (1, 8), 3)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])),
1148n/a('Expression', ('Num', (1, 0), 10)),
1149n/a('Expression', ('Str', (1, 0), 'string')),
1150n/a('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
1151n/a('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
1152n/a('Expression', ('Name', (1, 0), 'v', ('Load',))),
1153n/a('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1154n/a('Expression', ('List', (1, 0), [], ('Load',))),
1155n/a('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
1156n/a('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1157n/a('Expression', ('Tuple', (1, 0), [], ('Load',))),
1158n/a('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [])),
1159n/a]
1160n/amain()