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