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