Debian Codegraph @

Lib/test/test_ast/test_ast.py

raw
1 import _ast_unparse
2 import ast
3 import builtins
4 import contextlib
5 import copy
6 import dis
7 import enum
8 import itertools
9 import os
10 import re
11 import sys
12 import tempfile
13 import textwrap
14 import types
15 import unittest
16 import weakref
17 from io import StringIO
18 from pathlib import Path
19 from textwrap import dedent
20 try:
21 import _testinternalcapi
22 except ImportError:
23 _testinternalcapi = None
24
25 from test import support
26 from test.support import os_helper
27 from test.support import skip_emscripten_stack_overflow, skip_wasi_stack_overflow, skip_if_unlimited_stack_size
28 from test.support.ast_helper import ASTTestMixin
29 from test.support.import_helper import ensure_lazy_imports
30 from test.test_ast.utils import to_tuple
31 from test.test_ast.snippets import (
32 eval_tests, eval_results, exec_tests, exec_results, single_tests, single_results
33 )
34
35
36 STDLIB = os.path.dirname(ast.__file__)
37 STDLIB_FILES = [fn for fn in os.listdir(STDLIB) if fn.endswith(".py")]
38 STDLIB_FILES.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
39
40 AST_REPR_DATA_FILE = Path(__file__).parent / "data" / "ast_repr.txt"
41
42 def ast_repr_get_test_cases() -> list[str]:
43 return exec_tests + eval_tests
44
45
46 def ast_repr_update_snapshots() -> None:
47 data = [repr(ast.parse(test)) for test in ast_repr_get_test_cases()]
48 AST_REPR_DATA_FILE.write_text("\n".join(data))
49
50
51 class LazyImportTest(unittest.TestCase):
52 @support.cpython_only
53 def test_lazy_import(self):
54 ensure_lazy_imports("ast", {"contextlib", "enum", "inspect", "re", "collections", "argparse"})
55
56
57 class AST_Tests(unittest.TestCase):
58 maxDiff = None
59
60 def _is_ast_node(self, name, node):
61 if not isinstance(node, type):
62 return False
63 if "ast" not in node.__module__:
64 return False
65 return name != 'AST' and name[0].isupper()
66
67 def _assertTrueorder(self, ast_node, parent_pos):
68 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
69 return
70 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
71 node_pos = (ast_node.lineno, ast_node.col_offset)
72 self.assertGreaterEqual(node_pos, parent_pos)
73 parent_pos = (ast_node.lineno, ast_node.col_offset)
74 for name in ast_node._fields:
75 value = getattr(ast_node, name)
76 if isinstance(value, list):
77 first_pos = parent_pos
78 if value and name == 'decorator_list':
79 first_pos = (value[0].lineno, value[0].col_offset)
80 for child in value:
81 self._assertTrueorder(child, first_pos)
82 elif value is not None:
83 self._assertTrueorder(value, parent_pos)
84 self.assertEqual(ast_node._fields, ast_node.__match_args__)
85
86 def test_AST_objects(self):
87 x = ast.AST()
88 self.assertEqual(x._fields, ())
89 x.foobar = 42
90 self.assertEqual(x.foobar, 42)
91 self.assertEqual(x.__dict__["foobar"], 42)
92
93 with self.assertRaises(AttributeError):
94 x.vararg
95
96 with self.assertRaises(TypeError):
97 # "ast.AST constructor takes 0 positional arguments"
98 ast.AST(2)
99
100 def test_AST_fields_NULL_check(self):
101 # See: https://github.com/python/cpython/issues/126105
102 old_value = ast.AST._fields
103
104 def cleanup():
105 ast.AST._fields = old_value
106 self.addCleanup(cleanup)
107
108 del ast.AST._fields
109
110 msg = "type object 'ast.AST' has no attribute '_fields'"
111 # Both examples used to crash:
112 with self.assertRaisesRegex(AttributeError, msg):
113 ast.AST(arg1=123)
114 with self.assertRaisesRegex(AttributeError, msg):
115 ast.AST()
116
117 def test_AST_garbage_collection(self):
118 class X:
119 pass
120 a = ast.AST()
121 a.x = X()
122 a.x.a = a
123 ref = weakref.ref(a.x)
124 del a
125 support.gc_collect()
126 self.assertIsNone(ref())
127
128 def test_snippets(self):
129 for input, output, kind in ((exec_tests, exec_results, "exec"),
130 (single_tests, single_results, "single"),
131 (eval_tests, eval_results, "eval")):
132 for i, o in zip(input, output):
133 with self.subTest(action="parsing", input=i):
134 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
135 self.assertEqual(to_tuple(ast_tree), o)
136 self._assertTrueorder(ast_tree, (0, 0))
137 with self.subTest(action="compiling", input=i, kind=kind):
138 compile(ast_tree, "?", kind)
139
140 def test_ast_validation(self):
141 # compile() is the only function that calls PyAST_Validate
142 snippets_to_validate = exec_tests + single_tests + eval_tests
143 for snippet in snippets_to_validate:
144 tree = ast.parse(snippet)
145 compile(tree, '<string>', 'exec')
146
147 def test_parse_invalid_ast(self):
148 # see gh-130139
149 for optval in (-1, 0, 1, 2):
150 self.assertRaises(TypeError, ast.parse, ast.Constant(42),
151 optimize=optval)
152
153 def test_optimization_levels__debug__(self):
154 cases = [(-1, '__debug__'), (0, '__debug__'), (1, False), (2, False)]
155 for (optval, expected) in cases:
156 with self.subTest(optval=optval, expected=expected):
157 res1 = ast.parse("__debug__", optimize=optval)
158 res2 = ast.parse(ast.parse("__debug__"), optimize=optval)
159 for res in [res1, res2]:
160 self.assertIsInstance(res.body[0], ast.Expr)
161 if isinstance(expected, bool):
162 self.assertIsInstance(res.body[0].value, ast.Constant)
163 self.assertEqual(res.body[0].value.value, expected)
164 else:
165 self.assertIsInstance(res.body[0].value, ast.Name)
166 self.assertEqual(res.body[0].value.id, expected)
167
168 def test_invalid_position_information(self):
169 invalid_linenos = [
170 (10, 1), (-10, -11), (10, -11), (-5, -2), (-5, 1)
171 ]
172
173 for lineno, end_lineno in invalid_linenos:
174 with self.subTest(f"Check invalid linenos {lineno}:{end_lineno}"):
175 snippet = "a = 1"
176 tree = ast.parse(snippet)
177 tree.body[0].lineno = lineno
178 tree.body[0].end_lineno = end_lineno
179 with self.assertRaises(ValueError):
180 compile(tree, '<string>', 'exec')
181
182 invalid_col_offsets = [
183 (10, 1), (-10, -11), (10, -11), (-5, -2), (-5, 1)
184 ]
185 for col_offset, end_col_offset in invalid_col_offsets:
186 with self.subTest(f"Check invalid col_offset {col_offset}:{end_col_offset}"):
187 snippet = "a = 1"
188 tree = ast.parse(snippet)
189 tree.body[0].col_offset = col_offset
190 tree.body[0].end_col_offset = end_col_offset
191 with self.assertRaises(ValueError):
192 compile(tree, '<string>', 'exec')
193
194 def test_compilation_of_ast_nodes_with_default_end_position_values(self):
195 tree = ast.Module(body=[
196 ast.Import(names=[ast.alias(name='builtins', lineno=1, col_offset=0)], lineno=1, col_offset=0),
197 ast.Import(names=[ast.alias(name='traceback', lineno=0, col_offset=0)], lineno=0, col_offset=1)
198 ], type_ignores=[])
199
200 # Check that compilation doesn't crash. Note: this may crash explicitly only on debug mode.
201 compile(tree, "<string>", "exec")
202
203 def test_negative_locations_for_compile(self):
204 # See https://github.com/python/cpython/issues/130775
205 alias = ast.alias(name='traceback', lineno=0, col_offset=0)
206 for attrs in (
207 {'lineno': -2, 'col_offset': 0},
208 {'lineno': 0, 'col_offset': -2},
209 {'lineno': 0, 'col_offset': -2, 'end_col_offset': -2},
210 {'lineno': -2, 'end_lineno': -2, 'col_offset': 0},
211 ):
212 with self.subTest(attrs=attrs):
213 tree = ast.Module(body=[
214 ast.Import(names=[alias], **attrs)
215 ], type_ignores=[])
216
217 # It used to crash on this step:
218 compile(tree, "<string>", "exec")
219
220 # This also must not crash:
221 ast.parse(tree, optimize=2)
222
223 def test_docstring_optimization_single_node(self):
224 # https://github.com/python/cpython/issues/137308
225 class_example1 = textwrap.dedent('''
226 class A:
227 """Docstring"""
228 ''')
229 class_example2 = textwrap.dedent('''
230 class A:
231 """
232 Docstring"""
233 ''')
234 def_example1 = textwrap.dedent('''
235 def some():
236 """Docstring"""
237 ''')
238 def_example2 = textwrap.dedent('''
239 def some():
240 """Docstring
241 """
242 ''')
243 async_def_example1 = textwrap.dedent('''
244 async def some():
245 """Docstring"""
246 ''')
247 async_def_example2 = textwrap.dedent('''
248 async def some():
249 """
250 Docstring
251 """
252 ''')
253 for code in [
254 class_example1,
255 class_example2,
256 def_example1,
257 def_example2,
258 async_def_example1,
259 async_def_example2,
260 ]:
261 for opt_level in [0, 1, 2]:
262 with self.subTest(code=code, opt_level=opt_level):
263 mod = ast.parse(code, optimize=opt_level)
264 self.assertEqual(len(mod.body[0].body), 1)
265 if opt_level == 2:
266 pass_stmt = mod.body[0].body[0]
267 self.assertIsInstance(pass_stmt, ast.Pass)
268 self.assertEqual(
269 vars(pass_stmt),
270 {
271 'lineno': 3,
272 'col_offset': 4,
273 'end_lineno': 3,
274 'end_col_offset': 8,
275 },
276 )
277 else:
278 self.assertIsInstance(mod.body[0].body[0], ast.Expr)
279 self.assertIsInstance(
280 mod.body[0].body[0].value,
281 ast.Constant,
282 )
283
284 compile(code, "a", "exec")
285 compile(code, "a", "exec", optimize=opt_level)
286 compile(mod, "a", "exec")
287 compile(mod, "a", "exec", optimize=opt_level)
288
289 def test_docstring_optimization_multiple_nodes(self):
290 # https://github.com/python/cpython/issues/137308
291 class_example = textwrap.dedent(
292 """
293 class A:
294 '''
295 Docstring
296 '''
297 x = 1
298 """
299 )
300
301 def_example = textwrap.dedent(
302 """
303 def some():
304 '''
305 Docstring
306
307 '''
308 x = 1
309 """
310 )
311
312 async_def_example = textwrap.dedent(
313 """
314 async def some():
315
316 '''Docstring
317
318 '''
319 x = 1
320 """
321 )
322
323 for code in [
324 class_example,
325 def_example,
326 async_def_example,
327 ]:
328 for opt_level in [0, 1, 2]:
329 with self.subTest(code=code, opt_level=opt_level):
330 mod = ast.parse(code, optimize=opt_level)
331 if opt_level == 2:
332 self.assertNotIsInstance(
333 mod.body[0].body[0],
334 (ast.Pass, ast.Expr),
335 )
336 else:
337 self.assertIsInstance(mod.body[0].body[0], ast.Expr)
338 self.assertIsInstance(
339 mod.body[0].body[0].value,
340 ast.Constant,
341 )
342
343 compile(code, "a", "exec")
344 compile(code, "a", "exec", optimize=opt_level)
345 compile(mod, "a", "exec")
346 compile(mod, "a", "exec", optimize=opt_level)
347
348 def test_slice(self):
349 slc = ast.parse("x[::]").body[0].value.slice
350 self.assertIsNone(slc.upper)
351 self.assertIsNone(slc.lower)
352 self.assertIsNone(slc.step)
353
354 def test_from_import(self):
355 im = ast.parse("from . import y").body[0]
356 self.assertIsNone(im.module)
357
358 def test_non_interned_future_from_ast(self):
359 mod = ast.parse("from __future__ import division")
360 self.assertIsInstance(mod.body[0], ast.ImportFrom)
361 mod.body[0].module = " __future__ ".strip()
362 compile(mod, "<test>", "exec")
363
364 def test_alias(self):
365 im = ast.parse("from bar import y").body[0]
366 self.assertEqual(len(im.names), 1)
367 alias = im.names[0]
368 self.assertEqual(alias.name, 'y')
369 self.assertIsNone(alias.asname)
370 self.assertEqual(alias.lineno, 1)
371 self.assertEqual(alias.end_lineno, 1)
372 self.assertEqual(alias.col_offset, 16)
373 self.assertEqual(alias.end_col_offset, 17)
374
375 im = ast.parse("from bar import *").body[0]
376 alias = im.names[0]
377 self.assertEqual(alias.name, '*')
378 self.assertIsNone(alias.asname)
379 self.assertEqual(alias.lineno, 1)
380 self.assertEqual(alias.end_lineno, 1)
381 self.assertEqual(alias.col_offset, 16)
382 self.assertEqual(alias.end_col_offset, 17)
383
384 im = ast.parse("from bar import y as z").body[0]
385 alias = im.names[0]
386 self.assertEqual(alias.name, "y")
387 self.assertEqual(alias.asname, "z")
388 self.assertEqual(alias.lineno, 1)
389 self.assertEqual(alias.end_lineno, 1)
390 self.assertEqual(alias.col_offset, 16)
391 self.assertEqual(alias.end_col_offset, 22)
392
393 im = ast.parse("import bar as foo").body[0]
394 alias = im.names[0]
395 self.assertEqual(alias.name, "bar")
396 self.assertEqual(alias.asname, "foo")
397 self.assertEqual(alias.lineno, 1)
398 self.assertEqual(alias.end_lineno, 1)
399 self.assertEqual(alias.col_offset, 7)
400 self.assertEqual(alias.end_col_offset, 17)
401
402 def test_base_classes(self):
403 self.assertIsSubclass(ast.For, ast.stmt)
404 self.assertIsSubclass(ast.Name, ast.expr)
405 self.assertIsSubclass(ast.stmt, ast.AST)
406 self.assertIsSubclass(ast.expr, ast.AST)
407 self.assertIsSubclass(ast.comprehension, ast.AST)
408 self.assertIsSubclass(ast.Gt, ast.AST)
409
410 def test_field_attr_existence(self):
411 for name, item in ast.__dict__.items():
412 # constructor has a different signature
413 if name == 'Index':
414 continue
415 if self._is_ast_node(name, item):
416 x = self._construct_ast_class(item)
417 if isinstance(x, ast.AST):
418 self.assertIs(type(x._fields), tuple)
419
420 def _construct_ast_class(self, cls):
421 kwargs = {}
422 for name, typ in cls.__annotations__.items():
423 if typ is str:
424 kwargs[name] = 'capybara'
425 elif typ is int:
426 kwargs[name] = 42
427 elif typ is object:
428 kwargs[name] = b'capybara'
429 elif isinstance(typ, type) and issubclass(typ, ast.AST):
430 kwargs[name] = self._construct_ast_class(typ)
431 return cls(**kwargs)
432
433 def test_arguments(self):
434 x = ast.arguments()
435 self.assertEqual(x._fields, ('posonlyargs', 'args', 'vararg', 'kwonlyargs',
436 'kw_defaults', 'kwarg', 'defaults'))
437 self.assertEqual(ast.arguments.__annotations__, {
438 'posonlyargs': list[ast.arg],
439 'args': list[ast.arg],
440 'vararg': ast.arg | None,
441 'kwonlyargs': list[ast.arg],
442 'kw_defaults': list[ast.expr],
443 'kwarg': ast.arg | None,
444 'defaults': list[ast.expr],
445 })
446
447 self.assertEqual(x.args, [])
448 self.assertIsNone(x.vararg)
449
450 x = ast.arguments(*range(1, 8))
451 self.assertEqual(x.args, 2)
452 self.assertEqual(x.vararg, 3)
453
454 def test_field_attr_writable(self):
455 x = ast.Constant(1)
456 # We can assign to _fields
457 x._fields = 666
458 self.assertEqual(x._fields, 666)
459
460 def test_classattrs(self):
461 with self.assertWarns(DeprecationWarning):
462 x = ast.Constant()
463 self.assertEqual(x._fields, ('value', 'kind'))
464
465 with self.assertRaises(AttributeError):
466 x.value
467
468 x = ast.Constant(42)
469 self.assertEqual(x.value, 42)
470
471 with self.assertRaises(AttributeError):
472 x.lineno
473
474 with self.assertRaises(AttributeError):
475 x.foobar
476
477 x = ast.Constant(lineno=2, value=3)
478 self.assertEqual(x.lineno, 2)
479
480 x = ast.Constant(42, lineno=0)
481 self.assertEqual(x.lineno, 0)
482 self.assertEqual(x._fields, ('value', 'kind'))
483 self.assertEqual(x.value, 42)
484
485 self.assertRaises(TypeError, ast.Constant, 1, None, 2)
486 self.assertRaises(TypeError, ast.Constant, 1, None, 2, lineno=0)
487
488 # Arbitrary keyword arguments are supported (but deprecated)
489 with self.assertWarns(DeprecationWarning):
490 self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar')
491
492 with self.assertRaisesRegex(TypeError, "Constant got multiple values for argument 'value'"):
493 ast.Constant(1, value=2)
494
495 self.assertEqual(ast.Constant(42).value, 42)
496 self.assertEqual(ast.Constant(4.25).value, 4.25)
497 self.assertEqual(ast.Constant(4.25j).value, 4.25j)
498 self.assertEqual(ast.Constant('42').value, '42')
499 self.assertEqual(ast.Constant(b'42').value, b'42')
500 self.assertIs(ast.Constant(True).value, True)
501 self.assertIs(ast.Constant(False).value, False)
502 self.assertIs(ast.Constant(None).value, None)
503 self.assertIs(ast.Constant(...).value, ...)
504
505 def test_constant_subclasses(self):
506 class N(ast.Constant):
507 def __init__(self, *args, **kwargs):
508 super().__init__(*args, **kwargs)
509 self.z = 'spam'
510 class N2(ast.Constant):
511 pass
512
513 n = N(42)
514 self.assertEqual(n.value, 42)
515 self.assertEqual(n.z, 'spam')
516 self.assertEqual(type(n), N)
517 self.assertTrue(isinstance(n, N))
518 self.assertTrue(isinstance(n, ast.Constant))
519 self.assertFalse(isinstance(n, N2))
520 self.assertFalse(isinstance(ast.Constant(42), N))
521 n = N(value=42)
522 self.assertEqual(n.value, 42)
523 self.assertEqual(type(n), N)
524
525 def test_module(self):
526 body = [ast.Constant(42)]
527 x = ast.Module(body, [])
528 self.assertEqual(x.body, body)
529
530 def test_nodeclasses(self):
531 # Zero arguments constructor explicitly allowed (but deprecated)
532 with self.assertWarns(DeprecationWarning):
533 x = ast.BinOp()
534 self.assertEqual(x._fields, ('left', 'op', 'right'))
535
536 # Random attribute allowed too
537 x.foobarbaz = 5
538 self.assertEqual(x.foobarbaz, 5)
539
540 n1 = ast.Constant(1)
541 n3 = ast.Constant(3)
542 addop = ast.Add()
543 x = ast.BinOp(n1, addop, n3)
544 self.assertEqual(x.left, n1)
545 self.assertEqual(x.op, addop)
546 self.assertEqual(x.right, n3)
547
548 x = ast.BinOp(1, 2, 3)
549 self.assertEqual(x.left, 1)
550 self.assertEqual(x.op, 2)
551 self.assertEqual(x.right, 3)
552
553 x = ast.BinOp(1, 2, 3, lineno=0)
554 self.assertEqual(x.left, 1)
555 self.assertEqual(x.op, 2)
556 self.assertEqual(x.right, 3)
557 self.assertEqual(x.lineno, 0)
558
559 # node raises exception when given too many arguments
560 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
561 # node raises exception when given too many arguments
562 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
563
564 # can set attributes through kwargs too
565 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
566 self.assertEqual(x.left, 1)
567 self.assertEqual(x.op, 2)
568 self.assertEqual(x.right, 3)
569 self.assertEqual(x.lineno, 0)
570
571 # Random kwargs also allowed (but deprecated)
572 with self.assertWarns(DeprecationWarning):
573 x = ast.BinOp(1, 2, 3, foobarbaz=42)
574 self.assertEqual(x.foobarbaz, 42)
575
576 def test_no_fields(self):
577 # this used to fail because Sub._fields was None
578 x = ast.Sub()
579 self.assertEqual(x._fields, ())
580
581 def test_invalid_sum(self):
582 pos = dict(lineno=2, col_offset=3)
583 m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
584 with self.assertRaises(TypeError) as cm:
585 compile(m, "<test>", "exec")
586 self.assertIn("but got expr()", str(cm.exception))
587
588 def test_invalid_identifier(self):
589 m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])
590 ast.fix_missing_locations(m)
591 with self.assertRaises(TypeError) as cm:
592 compile(m, "<test>", "exec")
593 self.assertIn("identifier must be of type str", str(cm.exception))
594
595 def test_invalid_constant(self):
596 for invalid_constant in int, (1, 2, int), frozenset((1, 2, int)):
597 e = ast.Expression(body=ast.Constant(invalid_constant))
598 ast.fix_missing_locations(e)
599 with self.assertRaisesRegex(
600 TypeError, "invalid type in Constant: type"
601 ):
602 compile(e, "<test>", "eval")
603
604 def test_empty_yield_from(self):
605 # Issue 16546: yield from value is not optional.
606 empty_yield_from = ast.parse("def f():\n yield from g()")
607 empty_yield_from.body[0].body[0].value.value = None
608 with self.assertRaises(ValueError) as cm:
609 compile(empty_yield_from, "<test>", "exec")
610 self.assertIn("field 'value' is required", str(cm.exception))
611
612 @support.cpython_only
613 def test_issue31592(self):
614 # There shouldn't be an assertion failure in case of a bad
615 # unicodedata.normalize().
616 import unicodedata
617 def bad_normalize(*args):
618 return None
619 with support.swap_attr(unicodedata, 'normalize', bad_normalize):
620 self.assertRaises(TypeError, ast.parse, '\u03D5')
621
622 def test_issue18374_binop_col_offset(self):
623 tree = ast.parse('4+5+6+7')
624 parent_binop = tree.body[0].value
625 child_binop = parent_binop.left
626 grandchild_binop = child_binop.left
627 self.assertEqual(parent_binop.col_offset, 0)
628 self.assertEqual(parent_binop.end_col_offset, 7)
629 self.assertEqual(child_binop.col_offset, 0)
630 self.assertEqual(child_binop.end_col_offset, 5)
631 self.assertEqual(grandchild_binop.col_offset, 0)
632 self.assertEqual(grandchild_binop.end_col_offset, 3)
633
634 tree = ast.parse('4+5-\\\n 6-7')
635 parent_binop = tree.body[0].value
636 child_binop = parent_binop.left
637 grandchild_binop = child_binop.left
638 self.assertEqual(parent_binop.col_offset, 0)
639 self.assertEqual(parent_binop.lineno, 1)
640 self.assertEqual(parent_binop.end_col_offset, 4)
641 self.assertEqual(parent_binop.end_lineno, 2)
642
643 self.assertEqual(child_binop.col_offset, 0)
644 self.assertEqual(child_binop.lineno, 1)
645 self.assertEqual(child_binop.end_col_offset, 2)
646 self.assertEqual(child_binop.end_lineno, 2)
647
648 self.assertEqual(grandchild_binop.col_offset, 0)
649 self.assertEqual(grandchild_binop.lineno, 1)
650 self.assertEqual(grandchild_binop.end_col_offset, 3)
651 self.assertEqual(grandchild_binop.end_lineno, 1)
652
653 def test_issue39579_dotted_name_end_col_offset(self):
654 tree = ast.parse('@a.b.c\ndef f(): pass')
655 attr_b = tree.body[0].decorator_list[0].value
656 self.assertEqual(attr_b.end_col_offset, 4)
657
658 def test_ast_asdl_signature(self):
659 self.assertEqual(ast.withitem.__doc__, "withitem(expr context_expr, expr? optional_vars)")
660 self.assertEqual(ast.GtE.__doc__, "GtE")
661 self.assertEqual(ast.Name.__doc__, "Name(identifier id, expr_context ctx)")
662 self.assertEqual(ast.cmpop.__doc__, "cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn")
663 expressions = [f" | {node.__doc__}" for node in ast.expr.__subclasses__()]
664 expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
665 self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
666
667 def test_compare_basics(self):
668 self.assertTrue(ast.compare(ast.parse("x = 10"), ast.parse("x = 10")))
669 self.assertFalse(ast.compare(ast.parse("x = 10"), ast.parse("")))
670 self.assertFalse(ast.compare(ast.parse("x = 10"), ast.parse("x")))
671 self.assertFalse(
672 ast.compare(ast.parse("x = 10;y = 20"), ast.parse("class C:pass"))
673 )
674
675 def test_compare_modified_ast(self):
676 # The ast API is a bit underspecified. The objects are mutable,
677 # and even _fields and _attributes are mutable. The compare() does
678 # some simple things to accommodate mutability.
679 a = ast.parse("m * x + b", mode="eval")
680 b = ast.parse("m * x + b", mode="eval")
681 self.assertTrue(ast.compare(a, b))
682
683 a._fields = a._fields + ("spam",)
684 a.spam = "Spam"
685 self.assertNotEqual(a._fields, b._fields)
686 self.assertFalse(ast.compare(a, b))
687 self.assertFalse(ast.compare(b, a))
688
689 b._fields = a._fields
690 b.spam = a.spam
691 self.assertTrue(ast.compare(a, b))
692 self.assertTrue(ast.compare(b, a))
693
694 b._attributes = b._attributes + ("eggs",)
695 b.eggs = "eggs"
696 self.assertNotEqual(a._attributes, b._attributes)
697 self.assertFalse(ast.compare(a, b, compare_attributes=True))
698 self.assertFalse(ast.compare(b, a, compare_attributes=True))
699
700 a._attributes = b._attributes
701 a.eggs = b.eggs
702 self.assertTrue(ast.compare(a, b, compare_attributes=True))
703 self.assertTrue(ast.compare(b, a, compare_attributes=True))
704
705 def test_compare_literals(self):
706 constants = (
707 -20,
708 20,
709 20.0,
710 1,
711 1.0,
712 True,
713 0,
714 False,
715 frozenset(),
716 tuple(),
717 "ABCD",
718 "abcd",
719 "中文字",
720 1e1000,
721 -1e1000,
722 )
723 for next_index, constant in enumerate(constants[:-1], 1):
724 next_constant = constants[next_index]
725 with self.subTest(literal=constant, next_literal=next_constant):
726 self.assertTrue(
727 ast.compare(ast.Constant(constant), ast.Constant(constant))
728 )
729 self.assertFalse(
730 ast.compare(
731 ast.Constant(constant), ast.Constant(next_constant)
732 )
733 )
734
735 same_looking_literal_cases = [
736 {1, 1.0, True, 1 + 0j},
737 {0, 0.0, False, 0 + 0j},
738 ]
739 for same_looking_literals in same_looking_literal_cases:
740 for literal in same_looking_literals:
741 for same_looking_literal in same_looking_literals - {literal}:
742 self.assertFalse(
743 ast.compare(
744 ast.Constant(literal),
745 ast.Constant(same_looking_literal),
746 )
747 )
748
749 def test_compare_fieldless(self):
750 self.assertTrue(ast.compare(ast.Add(), ast.Add()))
751 self.assertFalse(ast.compare(ast.Sub(), ast.Add()))
752
753 # test that missing runtime fields is handled in ast.compare()
754 a1, a2 = ast.Name('a'), ast.Name('a')
755 self.assertTrue(ast.compare(a1, a2))
756 self.assertTrue(ast.compare(a1, a2))
757 del a1.id
758 self.assertFalse(ast.compare(a1, a2))
759 del a2.id
760 self.assertTrue(ast.compare(a1, a2))
761
762 def test_compare_modes(self):
763 for mode, sources in (
764 ("exec", exec_tests),
765 ("eval", eval_tests),
766 ("single", single_tests),
767 ):
768 for source in sources:
769 a = ast.parse(source, mode=mode)
770 b = ast.parse(source, mode=mode)
771 self.assertTrue(
772 ast.compare(a, b), f"{ast.dump(a)} != {ast.dump(b)}"
773 )
774
775 def test_compare_attributes_option(self):
776 def parse(a, b):
777 return ast.parse(a), ast.parse(b)
778
779 a, b = parse("2 + 2", "2+2")
780 self.assertTrue(ast.compare(a, b))
781 self.assertTrue(ast.compare(a, b, compare_attributes=False))
782 self.assertFalse(ast.compare(a, b, compare_attributes=True))
783
784 def test_compare_attributes_option_missing_attribute(self):
785 # test that missing runtime attributes is handled in ast.compare()
786 a1, a2 = ast.Name('a', lineno=1), ast.Name('a', lineno=1)
787 self.assertTrue(ast.compare(a1, a2))
788 self.assertTrue(ast.compare(a1, a2, compare_attributes=True))
789 del a1.lineno
790 self.assertFalse(ast.compare(a1, a2, compare_attributes=True))
791 del a2.lineno
792 self.assertTrue(ast.compare(a1, a2, compare_attributes=True))
793
794 def test_positional_only_feature_version(self):
795 ast.parse('def foo(x, /): ...', feature_version=(3, 8))
796 ast.parse('def bar(x=1, /): ...', feature_version=(3, 8))
797 with self.assertRaises(SyntaxError):
798 ast.parse('def foo(x, /): ...', feature_version=(3, 7))
799 with self.assertRaises(SyntaxError):
800 ast.parse('def bar(x=1, /): ...', feature_version=(3, 7))
801
802 ast.parse('lambda x, /: ...', feature_version=(3, 8))
803 ast.parse('lambda x=1, /: ...', feature_version=(3, 8))
804 with self.assertRaises(SyntaxError):
805 ast.parse('lambda x, /: ...', feature_version=(3, 7))
806 with self.assertRaises(SyntaxError):
807 ast.parse('lambda x=1, /: ...', feature_version=(3, 7))
808
809 def test_assignment_expression_feature_version(self):
810 ast.parse('(x := 0)', feature_version=(3, 8))
811 with self.assertRaises(SyntaxError):
812 ast.parse('(x := 0)', feature_version=(3, 7))
813
814 def test_pep750_tstring(self):
815 code = 't""'
816 ast.parse(code, feature_version=(3, 14))
817 with self.assertRaises(SyntaxError):
818 ast.parse(code, feature_version=(3, 13))
819
820 def test_pep758_except_without_parens(self):
821 code = textwrap.dedent("""
822 try:
823 ...
824 except ValueError, TypeError:
825 ...
826 """)
827 ast.parse(code, feature_version=(3, 14))
828 with self.assertRaises(SyntaxError):
829 ast.parse(code, feature_version=(3, 13))
830
831 def test_pep758_except_with_single_expr(self):
832 single_expr = textwrap.dedent("""
833 try:
834 ...
835 except{0} TypeError:
836 ...
837 """)
838
839 single_expr_with_as = textwrap.dedent("""
840 try:
841 ...
842 except{0} TypeError as exc:
843 ...
844 """)
845
846 single_tuple_expr = textwrap.dedent("""
847 try:
848 ...
849 except{0} (TypeError,):
850 ...
851 """)
852
853 single_tuple_expr_with_as = textwrap.dedent("""
854 try:
855 ...
856 except{0} (TypeError,) as exc:
857 ...
858 """)
859
860 single_parens_expr = textwrap.dedent("""
861 try:
862 ...
863 except{0} (TypeError):
864 ...
865 """)
866
867 single_parens_expr_with_as = textwrap.dedent("""
868 try:
869 ...
870 except{0} (TypeError) as exc:
871 ...
872 """)
873
874 for code in [
875 single_expr,
876 single_expr_with_as,
877 single_tuple_expr,
878 single_tuple_expr_with_as,
879 single_parens_expr,
880 single_parens_expr_with_as,
881 ]:
882 for star in [True, False]:
883 code = code.format('*' if star else '')
884 with self.subTest(code=code, star=star):
885 ast.parse(code, feature_version=(3, 14))
886 ast.parse(code, feature_version=(3, 13))
887
888 def test_pep758_except_star_without_parens(self):
889 code = textwrap.dedent("""
890 try:
891 ...
892 except* ValueError, TypeError:
893 ...
894 """)
895 ast.parse(code, feature_version=(3, 14))
896 with self.assertRaises(SyntaxError):
897 ast.parse(code, feature_version=(3, 13))
898
899 def test_conditional_context_managers_parse_with_low_feature_version(self):
900 # regression test for gh-115881
901 ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
902
903 def test_exception_groups_feature_version(self):
904 code = dedent('''
905 try: ...
906 except* Exception: ...
907 ''')
908 ast.parse(code)
909 with self.assertRaises(SyntaxError):
910 ast.parse(code, feature_version=(3, 10))
911
912 def test_type_params_feature_version(self):
913 samples = [
914 "type X = int",
915 "class X[T]: pass",
916 "def f[T](): pass",
917 ]
918 for sample in samples:
919 with self.subTest(sample):
920 ast.parse(sample)
921 with self.assertRaises(SyntaxError):
922 ast.parse(sample, feature_version=(3, 11))
923
924 def test_type_params_default_feature_version(self):
925 samples = [
926 "type X[*Ts=int] = int",
927 "class X[T=int]: pass",
928 "def f[**P=int](): pass",
929 ]
930 for sample in samples:
931 with self.subTest(sample):
932 ast.parse(sample)
933 with self.assertRaises(SyntaxError):
934 ast.parse(sample, feature_version=(3, 12))
935
936 def test_invalid_major_feature_version(self):
937 with self.assertRaises(ValueError):
938 ast.parse('pass', feature_version=(2, 7))
939 with self.assertRaises(ValueError):
940 ast.parse('pass', feature_version=(4, 0))
941
942 def test_constant_as_name(self):
943 for constant in "True", "False", "None":
944 expr = ast.Expression(ast.Name(constant, ast.Load()))
945 ast.fix_missing_locations(expr)
946 with self.assertRaisesRegex(ValueError, f"identifier field can't represent '{constant}' constant"):
947 compile(expr, "<test>", "eval")
948
949 def test_constant_as_unicode_name(self):
950 constants = [
951 ("True", b"Tru\xe1\xb5\x89"),
952 ("False", b"Fal\xc5\xbfe"),
953 ("None", b"N\xc2\xbane"),
954 ]
955 for constant in constants:
956 with self.assertRaisesRegex(ValueError,
957 f"identifier field can't represent '{constant[0]}' constant"):
958 ast.parse(constant[1], mode="eval")
959
960 def test_precedence_enum(self):
961 class _Precedence(enum.IntEnum):
962 """Precedence table that originated from python grammar."""
963 NAMED_EXPR = enum.auto() # <target> := <expr1>
964 TUPLE = enum.auto() # <expr1>, <expr2>
965 YIELD = enum.auto() # 'yield', 'yield from'
966 TEST = enum.auto() # 'if'-'else', 'lambda'
967 OR = enum.auto() # 'or'
968 AND = enum.auto() # 'and'
969 NOT = enum.auto() # 'not'
970 CMP = enum.auto() # '<', '>', '==', '>=', '<=', '!=',
971 # 'in', 'not in', 'is', 'is not'
972 EXPR = enum.auto()
973 BOR = EXPR # '|'
974 BXOR = enum.auto() # '^'
975 BAND = enum.auto() # '&'
976 SHIFT = enum.auto() # '<<', '>>'
977 ARITH = enum.auto() # '+', '-'
978 TERM = enum.auto() # '*', '@', '/', '%', '//'
979 FACTOR = enum.auto() # unary '+', '-', '~'
980 POWER = enum.auto() # '**'
981 AWAIT = enum.auto() # 'await'
982 ATOM = enum.auto()
983 def next(self):
984 try:
985 return self.__class__(self + 1)
986 except ValueError:
987 return self
988 enum._test_simple_enum(_Precedence, _ast_unparse._Precedence)
989
990 @support.cpython_only
991 @skip_if_unlimited_stack_size
992 @skip_wasi_stack_overflow()
993 @skip_emscripten_stack_overflow()
994 def test_ast_recursion_limit(self):
995 # Android test devices have less memory.
996 crash_depth = 100_000 if sys.platform == "android" else 500_000
997 success_depth = 200
998 if _testinternalcapi is not None:
999 remaining = _testinternalcapi.get_c_recursion_remaining()
1000 success_depth = min(success_depth, remaining)
1001
1002 def check_limit(prefix, repeated):
1003 expect_ok = prefix + repeated * success_depth
1004 ast.parse(expect_ok)
1005
1006 broken = prefix + repeated * crash_depth
1007 details = "Compiling ({!r} + {!r} * {})".format(
1008 prefix, repeated, crash_depth)
1009 with self.assertRaises(RecursionError, msg=details):
1010 with support.infinite_recursion():
1011 ast.parse(broken)
1012
1013 check_limit("a", "()")
1014 check_limit("a", ".b")
1015 check_limit("a", "[0]")
1016 check_limit("a", "*a")
1017
1018 def test_null_bytes(self):
1019 with self.assertRaises(SyntaxError,
1020 msg="source code string cannot contain null bytes"):
1021 ast.parse("a\0b")
1022
1023 def assert_none_check(self, node: type[ast.AST], attr: str, source: str) -> None:
1024 with self.subTest(f"{node.__name__}.{attr}"):
1025 tree = ast.parse(source)
1026 found = 0
1027 for child in ast.walk(tree):
1028 if isinstance(child, node):
1029 setattr(child, attr, None)
1030 found += 1
1031 self.assertEqual(found, 1)
1032 e = re.escape(f"field '{attr}' is required for {node.__name__}")
1033 with self.assertRaisesRegex(ValueError, f"^{e}$"):
1034 compile(tree, "<test>", "exec")
1035
1036 def test_none_checks(self) -> None:
1037 tests = [
1038 (ast.alias, "name", "import spam as SPAM"),
1039 (ast.arg, "arg", "def spam(SPAM): spam"),
1040 (ast.comprehension, "target", "[spam for SPAM in spam]"),
1041 (ast.comprehension, "iter", "[spam for spam in SPAM]"),
1042 (ast.keyword, "value", "spam(**SPAM)"),
1043 (ast.match_case, "pattern", "match spam:\n case SPAM: spam"),
1044 (ast.withitem, "context_expr", "with SPAM: spam"),
1045 ]
1046 for node, attr, source in tests:
1047 self.assert_none_check(node, attr, source)
1048
1049 def test_repr(self) -> None:
1050 snapshots = AST_REPR_DATA_FILE.read_text().split("\n")
1051 for test, snapshot in zip(ast_repr_get_test_cases(), snapshots, strict=True):
1052 with self.subTest(test_input=test):
1053 self.assertEqual(repr(ast.parse(test)), snapshot)
1054
1055 def test_repr_large_input_crash(self):
1056 # gh-125010: Fix use-after-free in ast repr()
1057 source = "0x0" + "e" * 10_000
1058 with self.assertRaisesRegex(ValueError,
1059 r"Exceeds the limit \(\d+ digits\)"):
1060 repr(ast.Constant(value=eval(source)))
1061
1062 def test_tstring(self):
1063 # Test AST structure for simple t-string
1064 tree = ast.parse('t"Hello"')
1065 self.assertIsInstance(tree.body[0].value, ast.TemplateStr)
1066 self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
1067
1068 # Test AST for t-string with interpolation
1069 tree = ast.parse('t"Hello {name}"')
1070 self.assertIsInstance(tree.body[0].value, ast.TemplateStr)
1071 self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
1072 self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)
1073
1074
1075 class CopyTests(unittest.TestCase):
1076 """Test copying and pickling AST nodes."""
1077
1078 @staticmethod
1079 def iter_ast_classes():
1080 """Iterate over the (native) subclasses of ast.AST recursively.
1081
1082 This excludes the special class ast.Index since its constructor
1083 returns an integer.
1084 """
1085 def do(cls):
1086 if cls.__module__ != 'ast':
1087 return
1088 if cls is ast.Index:
1089 return
1090
1091 yield cls
1092 for sub in cls.__subclasses__():
1093 yield from do(sub)
1094
1095 yield from do(ast.AST)
1096
1097 def test_pickling(self):
1098 import pickle
1099
1100 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1101 for code in exec_tests:
1102 with self.subTest(code=code, protocol=protocol):
1103 tree = compile(code, "?", "exec", 0x400)
1104 ast2 = pickle.loads(pickle.dumps(tree, protocol))
1105 self.assertEqual(to_tuple(ast2), to_tuple(tree))
1106
1107 @skip_if_unlimited_stack_size
1108 def test_copy_with_parents(self):
1109 # gh-120108
1110 code = """
1111 ('',)
1112 while i < n:
1113 if ch == '':
1114 ch = format[i]
1115 if ch == '':
1116 if freplace is None:
1117 '' % getattr(object)
1118 elif ch == '':
1119 if zreplace is None:
1120 if hasattr:
1121 offset = object.utcoffset()
1122 if offset is not None:
1123 if offset.days < 0:
1124 offset = -offset
1125 h = divmod(timedelta(hours=0))
1126 if u:
1127 zreplace = '' % (sign,)
1128 elif s:
1129 zreplace = '' % (sign,)
1130 else:
1131 zreplace = '' % (sign,)
1132 elif ch == '':
1133 if Zreplace is None:
1134 Zreplace = ''
1135 if hasattr(object):
1136 s = object.tzname()
1137 if s is not None:
1138 Zreplace = s.replace('')
1139 newformat.append(Zreplace)
1140 else:
1141 push('')
1142 else:
1143 push(ch)
1144
1145 """
1146 tree = ast.parse(textwrap.dedent(code))
1147 for node in ast.walk(tree):
1148 for child in ast.iter_child_nodes(node):
1149 child.parent = node
1150 try:
1151 with support.infinite_recursion(200):
1152 tree2 = copy.deepcopy(tree)
1153 finally:
1154 # Singletons like ast.Load() are shared; make sure we don't
1155 # leave them mutated after this test.
1156 for node in ast.walk(tree):
1157 if hasattr(node, "parent"):
1158 del node.parent
1159
1160 for node in ast.walk(tree2):
1161 for child in ast.iter_child_nodes(node):
1162 if hasattr(child, "parent") and not isinstance(child, (
1163 ast.expr_context, ast.boolop, ast.unaryop, ast.cmpop, ast.operator,
1164 )):
1165 self.assertEqual(to_tuple(child.parent), to_tuple(node))
1166
1167 def test_replace_interface(self):
1168 for klass in self.iter_ast_classes():
1169 with self.subTest(klass=klass):
1170 self.assertHasAttr(klass, '__replace__')
1171
1172 fields = set(klass._fields)
1173 with self.subTest(klass=klass, fields=fields):
1174 node = klass(**dict.fromkeys(fields))
1175 # forbid positional arguments in replace()
1176 self.assertRaises(TypeError, copy.replace, node, 1)
1177 self.assertRaises(TypeError, node.__replace__, 1)
1178
1179 def test_replace_native(self):
1180 for klass in self.iter_ast_classes():
1181 fields = set(klass._fields)
1182 attributes = set(klass._attributes)
1183
1184 with self.subTest(klass=klass, fields=fields, attributes=attributes):
1185 # use of object() to ensure that '==' and 'is'
1186 # behave similarly in ast.compare(node, repl)
1187 old_fields = {field: object() for field in fields}
1188 old_attrs = {attr: object() for attr in attributes}
1189
1190 # check shallow copy
1191 node = klass(**old_fields)
1192 repl = copy.replace(node)
1193 self.assertTrue(ast.compare(node, repl, compare_attributes=True))
1194 # check when passing using attributes (they may be optional!)
1195 node = klass(**old_fields, **old_attrs)
1196 repl = copy.replace(node)
1197 self.assertTrue(ast.compare(node, repl, compare_attributes=True))
1198
1199 for field in fields:
1200 # check when we sometimes have attributes and sometimes not
1201 for init_attrs in [{}, old_attrs]:
1202 node = klass(**old_fields, **init_attrs)
1203 # only change a single field (do not change attributes)
1204 new_value = object()
1205 repl = copy.replace(node, **{field: new_value})
1206 for f in fields:
1207 old_value = old_fields[f]
1208 # assert that there is no side-effect
1209 self.assertIs(getattr(node, f), old_value)
1210 # check the changes
1211 if f != field:
1212 self.assertIs(getattr(repl, f), old_value)
1213 else:
1214 self.assertIs(getattr(repl, f), new_value)
1215 self.assertFalse(ast.compare(node, repl, compare_attributes=True))
1216
1217 for attribute in attributes:
1218 node = klass(**old_fields, **old_attrs)
1219 # only change a single attribute (do not change fields)
1220 new_attr = object()
1221 repl = copy.replace(node, **{attribute: new_attr})
1222 for a in attributes:
1223 old_attr = old_attrs[a]
1224 # assert that there is no side-effect
1225 self.assertIs(getattr(node, a), old_attr)
1226 # check the changes
1227 if a != attribute:
1228 self.assertIs(getattr(repl, a), old_attr)
1229 else:
1230 self.assertIs(getattr(repl, a), new_attr)
1231 self.assertFalse(ast.compare(node, repl, compare_attributes=True))
1232
1233 def test_replace_accept_known_class_fields(self):
1234 nid, ctx = object(), object()
1235
1236 node = ast.Name(id=nid, ctx=ctx)
1237 self.assertIs(node.id, nid)
1238 self.assertIs(node.ctx, ctx)
1239
1240 new_nid = object()
1241 repl = copy.replace(node, id=new_nid)
1242 # assert that there is no side-effect
1243 self.assertIs(node.id, nid)
1244 self.assertIs(node.ctx, ctx)
1245 # check the changes
1246 self.assertIs(repl.id, new_nid)
1247 self.assertIs(repl.ctx, node.ctx) # no changes
1248
1249 def test_replace_accept_known_class_attributes(self):
1250 node = ast.parse('x').body[0].value
1251 self.assertEqual(node.id, 'x')
1252 self.assertEqual(node.lineno, 1)
1253
1254 # constructor allows any type so replace() should do the same
1255 lineno = object()
1256 repl = copy.replace(node, lineno=lineno)
1257 # assert that there is no side-effect
1258 self.assertEqual(node.lineno, 1)
1259 # check the changes
1260 self.assertEqual(repl.id, node.id)
1261 self.assertEqual(repl.ctx, node.ctx)
1262 self.assertEqual(repl.lineno, lineno)
1263
1264 _, _, state = node.__reduce__()
1265 self.assertEqual(state['id'], 'x')
1266 self.assertEqual(state['ctx'], node.ctx)
1267 self.assertEqual(state['lineno'], 1)
1268
1269 _, _, state = repl.__reduce__()
1270 self.assertEqual(state['id'], 'x')
1271 self.assertEqual(state['ctx'], node.ctx)
1272 self.assertEqual(state['lineno'], lineno)
1273
1274 def test_replace_accept_known_custom_class_fields(self):
1275 class MyNode(ast.AST):
1276 _fields = ('name', 'data')
1277 __annotations__ = {'name': str, 'data': object}
1278 __match_args__ = ('name', 'data')
1279
1280 name, data = 'name', object()
1281
1282 node = MyNode(name, data)
1283 self.assertIs(node.name, name)
1284 self.assertIs(node.data, data)
1285 # check shallow copy
1286 repl = copy.replace(node)
1287 # assert that there is no side-effect
1288 self.assertIs(node.name, name)
1289 self.assertIs(node.data, data)
1290 # check the shallow copy
1291 self.assertIs(repl.name, name)
1292 self.assertIs(repl.data, data)
1293
1294 node = MyNode(name, data)
1295 repl_data = object()
1296 # replace custom but known field
1297 repl = copy.replace(node, data=repl_data)
1298 # assert that there is no side-effect
1299 self.assertIs(node.name, name)
1300 self.assertIs(node.data, data)
1301 # check the changes
1302 self.assertIs(repl.name, node.name)
1303 self.assertIs(repl.data, repl_data)
1304
1305 def test_replace_accept_known_custom_class_attributes(self):
1306 class MyNode(ast.AST):
1307 x = 0
1308 y = 1
1309 _attributes = ('x', 'y')
1310
1311 node = MyNode()
1312 self.assertEqual(node.x, 0)
1313 self.assertEqual(node.y, 1)
1314
1315 y = object()
1316 repl = copy.replace(node, y=y)
1317 # assert that there is no side-effect
1318 self.assertEqual(node.x, 0)
1319 self.assertEqual(node.y, 1)
1320 # check the changes
1321 self.assertEqual(repl.x, 0)
1322 self.assertEqual(repl.y, y)
1323
1324 def test_replace_ignore_known_custom_instance_fields(self):
1325 node = ast.parse('x').body[0].value
1326 node.extra = extra = object() # add instance 'extra' field
1327 context = node.ctx
1328
1329 # assert initial values
1330 self.assertIs(node.id, 'x')
1331 self.assertIs(node.ctx, context)
1332 self.assertIs(node.extra, extra)
1333 # shallow copy, but drops extra fields
1334 repl = copy.replace(node)
1335 # assert that there is no side-effect
1336 self.assertIs(node.id, 'x')
1337 self.assertIs(node.ctx, context)
1338 self.assertIs(node.extra, extra)
1339 # verify that the 'extra' field is not kept
1340 self.assertIs(repl.id, 'x')
1341 self.assertIs(repl.ctx, context)
1342 self.assertRaises(AttributeError, getattr, repl, 'extra')
1343
1344 # change known native field
1345 repl = copy.replace(node, id='y')
1346 # assert that there is no side-effect
1347 self.assertIs(node.id, 'x')
1348 self.assertIs(node.ctx, context)
1349 self.assertIs(node.extra, extra)
1350 # verify that the 'extra' field is not kept
1351 self.assertIs(repl.id, 'y')
1352 self.assertIs(repl.ctx, context)
1353 self.assertRaises(AttributeError, getattr, repl, 'extra')
1354
1355 def test_replace_reject_missing_field(self):
1356 # case: warn if deleted field is not replaced
1357 node = ast.parse('x').body[0].value
1358 context = node.ctx
1359 del node.id
1360
1361 self.assertRaises(AttributeError, getattr, node, 'id')
1362 self.assertIs(node.ctx, context)
1363 msg = "Name.__replace__ missing 1 keyword argument: 'id'."
1364 with self.assertRaisesRegex(TypeError, re.escape(msg)):
1365 copy.replace(node)
1366 # assert that there is no side-effect
1367 self.assertRaises(AttributeError, getattr, node, 'id')
1368 self.assertIs(node.ctx, context)
1369
1370 # case: do not raise if deleted field is replaced
1371 node = ast.parse('x').body[0].value
1372 context = node.ctx
1373 del node.id
1374
1375 self.assertRaises(AttributeError, getattr, node, 'id')
1376 self.assertIs(node.ctx, context)
1377 repl = copy.replace(node, id='y')
1378 # assert that there is no side-effect
1379 self.assertRaises(AttributeError, getattr, node, 'id')
1380 self.assertIs(node.ctx, context)
1381 self.assertIs(repl.id, 'y')
1382 self.assertIs(repl.ctx, context)
1383
1384 def test_replace_accept_missing_field_with_default(self):
1385 node = ast.FunctionDef(name="foo", args=ast.arguments())
1386 self.assertIs(node.returns, None)
1387 self.assertEqual(node.decorator_list, [])
1388 node2 = copy.replace(node, name="bar")
1389 self.assertEqual(node2.name, "bar")
1390 self.assertIs(node2.returns, None)
1391 self.assertEqual(node2.decorator_list, [])
1392
1393 def test_replace_reject_known_custom_instance_fields_commits(self):
1394 node = ast.parse('x').body[0].value
1395 node.extra = extra = object() # add instance 'extra' field
1396 context = node.ctx
1397
1398 # explicit rejection of known instance fields
1399 self.assertHasAttr(node, 'extra')
1400 msg = "Name.__replace__ got an unexpected keyword argument 'extra'."
1401 with self.assertRaisesRegex(TypeError, re.escape(msg)):
1402 copy.replace(node, extra=1)
1403 # assert that there is no side-effect
1404 self.assertIs(node.id, 'x')
1405 self.assertIs(node.ctx, context)
1406 self.assertIs(node.extra, extra)
1407
1408 def test_replace_reject_unknown_instance_fields(self):
1409 node = ast.parse('x').body[0].value
1410 context = node.ctx
1411
1412 # explicit rejection of unknown extra fields
1413 self.assertRaises(AttributeError, getattr, node, 'unknown')
1414 msg = "Name.__replace__ got an unexpected keyword argument 'unknown'."
1415 with self.assertRaisesRegex(TypeError, re.escape(msg)):
1416 copy.replace(node, unknown=1)
1417 # assert that there is no side-effect
1418 self.assertIs(node.id, 'x')
1419 self.assertIs(node.ctx, context)
1420 self.assertRaises(AttributeError, getattr, node, 'unknown')
1421
1422 def test_replace_non_str_kwarg(self):
1423 node = ast.Name(id="x")
1424 errmsg = "got an unexpected keyword argument <object object"
1425 with self.assertRaisesRegex(TypeError, errmsg):
1426 node.__replace__(**{object(): "y"})
1427
1428
1429 class ASTHelpers_Test(unittest.TestCase):
1430 maxDiff = None
1431
1432 def test_parse(self):
1433 a = ast.parse('foo(1 + 1)')
1434 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
1435 self.assertEqual(ast.dump(a), ast.dump(b))
1436
1437 def test_parse_in_error(self):
1438 try:
1439 1/0
1440 except Exception:
1441 with self.assertRaises(SyntaxError) as e:
1442 ast.literal_eval(r"'\U'")
1443 self.assertIsNotNone(e.exception.__context__)
1444
1445 def test_dump(self):
1446 node = ast.parse('spam(eggs, "and cheese")')
1447 self.assertEqual(ast.dump(node),
1448 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
1449 "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')]))])"
1450 )
1451 self.assertEqual(ast.dump(node, annotate_fields=False),
1452 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
1453 "Constant('and cheese')]))])"
1454 )
1455 self.assertEqual(ast.dump(node, include_attributes=True),
1456 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
1457 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), "
1458 "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, "
1459 "end_lineno=1, end_col_offset=9), Constant(value='and cheese', "
1460 "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], "
1461 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), "
1462 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)])"
1463 )
1464
1465 def test_dump_indent(self):
1466 node = ast.parse('spam(eggs, "and cheese")')
1467 self.assertEqual(ast.dump(node, indent=3), """\
1468 Module(
1469 body=[
1470 Expr(
1471 value=Call(
1472 func=Name(id='spam', ctx=Load()),
1473 args=[
1474 Name(id='eggs', ctx=Load()),
1475 Constant(value='and cheese')]))])""")
1476 self.assertEqual(ast.dump(node, annotate_fields=False, indent='\t'), """\
1477 Module(
1478 \t[
1479 \t\tExpr(
1480 \t\t\tCall(
1481 \t\t\t\tName('spam', Load()),
1482 \t\t\t\t[
1483 \t\t\t\t\tName('eggs', Load()),
1484 \t\t\t\t\tConstant('and cheese')]))])""")
1485 self.assertEqual(ast.dump(node, include_attributes=True, indent=3), """\
1486 Module(
1487 body=[
1488 Expr(
1489 value=Call(
1490 func=Name(
1491 id='spam',
1492 ctx=Load(),
1493 lineno=1,
1494 col_offset=0,
1495 end_lineno=1,
1496 end_col_offset=4),
1497 args=[
1498 Name(
1499 id='eggs',
1500 ctx=Load(),
1501 lineno=1,
1502 col_offset=5,
1503 end_lineno=1,
1504 end_col_offset=9),
1505 Constant(
1506 value='and cheese',
1507 lineno=1,
1508 col_offset=11,
1509 end_lineno=1,
1510 end_col_offset=23)],
1511 lineno=1,
1512 col_offset=0,
1513 end_lineno=1,
1514 end_col_offset=24),
1515 lineno=1,
1516 col_offset=0,
1517 end_lineno=1,
1518 end_col_offset=24)])""")
1519
1520 def test_dump_incomplete(self):
1521 node = ast.Raise(lineno=3, col_offset=4)
1522 self.assertEqual(ast.dump(node),
1523 "Raise()"
1524 )
1525 self.assertEqual(ast.dump(node, include_attributes=True),
1526 "Raise(lineno=3, col_offset=4)"
1527 )
1528 node = ast.Raise(exc=ast.Name(id='e', ctx=ast.Load()), lineno=3, col_offset=4)
1529 self.assertEqual(ast.dump(node),
1530 "Raise(exc=Name(id='e', ctx=Load()))"
1531 )
1532 self.assertEqual(ast.dump(node, annotate_fields=False),
1533 "Raise(Name('e', Load()))"
1534 )
1535 self.assertEqual(ast.dump(node, include_attributes=True),
1536 "Raise(exc=Name(id='e', ctx=Load()), lineno=3, col_offset=4)"
1537 )
1538 self.assertEqual(ast.dump(node, annotate_fields=False, include_attributes=True),
1539 "Raise(Name('e', Load()), lineno=3, col_offset=4)"
1540 )
1541 node = ast.Raise(cause=ast.Name(id='e', ctx=ast.Load()))
1542 self.assertEqual(ast.dump(node),
1543 "Raise(cause=Name(id='e', ctx=Load()))"
1544 )
1545 self.assertEqual(ast.dump(node, annotate_fields=False),
1546 "Raise(cause=Name('e', Load()))"
1547 )
1548 # Arguments:
1549 node = ast.arguments(args=[ast.arg("x")])
1550 self.assertEqual(ast.dump(node, annotate_fields=False),
1551 "arguments([], [arg('x')])",
1552 )
1553 node = ast.arguments(posonlyargs=[ast.arg("x")])
1554 self.assertEqual(ast.dump(node, annotate_fields=False),
1555 "arguments([arg('x')])",
1556 )
1557 node = ast.arguments(posonlyargs=[ast.arg("x")], kwonlyargs=[ast.arg('y')])
1558 self.assertEqual(ast.dump(node, annotate_fields=False),
1559 "arguments([arg('x')], kwonlyargs=[arg('y')])",
1560 )
1561 node = ast.arguments(args=[ast.arg("x")], kwonlyargs=[ast.arg('y')])
1562 self.assertEqual(ast.dump(node, annotate_fields=False),
1563 "arguments([], [arg('x')], kwonlyargs=[arg('y')])",
1564 )
1565 node = ast.arguments()
1566 self.assertEqual(ast.dump(node, annotate_fields=False),
1567 "arguments()",
1568 )
1569 # Classes:
1570 node = ast.ClassDef(
1571 'T',
1572 [],
1573 [ast.keyword('a', ast.Constant(None))],
1574 [],
1575 [ast.Name('dataclass', ctx=ast.Load())],
1576 )
1577 self.assertEqual(ast.dump(node),
1578 "ClassDef(name='T', keywords=[keyword(arg='a', value=Constant(value=None))], decorator_list=[Name(id='dataclass', ctx=Load())])",
1579 )
1580 self.assertEqual(ast.dump(node, annotate_fields=False),
1581 "ClassDef('T', [], [keyword('a', Constant(None))], [], [Name('dataclass', Load())])",
1582 )
1583
1584 def test_dump_show_empty(self):
1585 def check_node(node, empty, full, **kwargs):
1586 with self.subTest(show_empty=False):
1587 self.assertEqual(
1588 ast.dump(node, show_empty=False, **kwargs),
1589 empty,
1590 )
1591 with self.subTest(show_empty=True):
1592 self.assertEqual(
1593 ast.dump(node, show_empty=True, **kwargs),
1594 full,
1595 )
1596
1597 def check_text(code, empty, full, **kwargs):
1598 check_node(ast.parse(code), empty, full, **kwargs)
1599
1600 check_node(
1601 ast.arguments(),
1602 empty="arguments()",
1603 full="arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[])",
1604 )
1605
1606 check_node(
1607 # Corner case: there are no real `Name` instances with `id=''`:
1608 ast.Name(id='', ctx=ast.Load()),
1609 empty="Name(id='', ctx=Load())",
1610 full="Name(id='', ctx=Load())",
1611 )
1612
1613 check_node(
1614 ast.MatchSingleton(value=None),
1615 empty="MatchSingleton(value=None)",
1616 full="MatchSingleton(value=None)",
1617 )
1618
1619 check_node(
1620 ast.MatchSingleton(value=[]),
1621 empty="MatchSingleton(value=[])",
1622 full="MatchSingleton(value=[])",
1623 )
1624
1625 check_node(
1626 ast.Constant(value=None),
1627 empty="Constant(value=None)",
1628 full="Constant(value=None)",
1629 )
1630
1631 check_node(
1632 ast.Constant(value=[]),
1633 empty="Constant(value=[])",
1634 full="Constant(value=[])",
1635 )
1636
1637 check_node(
1638 ast.Constant(value=''),
1639 empty="Constant(value='')",
1640 full="Constant(value='')",
1641 )
1642
1643 check_node(
1644 ast.Interpolation(value=ast.Constant(42), str=None, conversion=-1),
1645 empty="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
1646 full="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
1647 )
1648
1649 check_node(
1650 ast.Interpolation(value=ast.Constant(42), str=[], conversion=-1),
1651 empty="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
1652 full="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
1653 )
1654
1655 check_text(
1656 "def a(b: int = 0, *, c): ...",
1657 empty="Module(body=[FunctionDef(name='a', args=arguments(args=[arg(arg='b', annotation=Name(id='int', ctx=Load()))], kwonlyargs=[arg(arg='c')], kw_defaults=[None], defaults=[Constant(value=0)]), body=[Expr(value=Constant(value=Ellipsis))])])",
1658 full="Module(body=[FunctionDef(name='a', args=arguments(posonlyargs=[], args=[arg(arg='b', annotation=Name(id='int', ctx=Load()))], kwonlyargs=[arg(arg='c')], kw_defaults=[None], defaults=[Constant(value=0)]), body=[Expr(value=Constant(value=Ellipsis))], decorator_list=[], type_params=[])], type_ignores=[])",
1659 )
1660
1661 check_text(
1662 "def a(b: int = 0, *, c): ...",
1663 empty="Module(body=[FunctionDef(name='a', args=arguments(args=[arg(arg='b', annotation=Name(id='int', ctx=Load(), lineno=1, col_offset=9, end_lineno=1, end_col_offset=12), lineno=1, col_offset=6, end_lineno=1, end_col_offset=12)], kwonlyargs=[arg(arg='c', lineno=1, col_offset=21, end_lineno=1, end_col_offset=22)], kw_defaults=[None], defaults=[Constant(value=0, lineno=1, col_offset=15, end_lineno=1, end_col_offset=16)]), body=[Expr(value=Constant(value=Ellipsis, lineno=1, col_offset=25, end_lineno=1, end_col_offset=28), lineno=1, col_offset=25, end_lineno=1, end_col_offset=28)], lineno=1, col_offset=0, end_lineno=1, end_col_offset=28)])",
1664 full="Module(body=[FunctionDef(name='a', args=arguments(posonlyargs=[], args=[arg(arg='b', annotation=Name(id='int', ctx=Load(), lineno=1, col_offset=9, end_lineno=1, end_col_offset=12), lineno=1, col_offset=6, end_lineno=1, end_col_offset=12)], kwonlyargs=[arg(arg='c', lineno=1, col_offset=21, end_lineno=1, end_col_offset=22)], kw_defaults=[None], defaults=[Constant(value=0, lineno=1, col_offset=15, end_lineno=1, end_col_offset=16)]), body=[Expr(value=Constant(value=Ellipsis, lineno=1, col_offset=25, end_lineno=1, end_col_offset=28), lineno=1, col_offset=25, end_lineno=1, end_col_offset=28)], decorator_list=[], type_params=[], lineno=1, col_offset=0, end_lineno=1, end_col_offset=28)], type_ignores=[])",
1665 include_attributes=True,
1666 )
1667
1668 check_text(
1669 'spam(eggs, "and cheese")',
1670 empty="Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')]))])",
1671 full="Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')], keywords=[]))], type_ignores=[])",
1672 )
1673
1674 check_text(
1675 'spam(eggs, text="and cheese")',
1676 empty="Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), args=[Name(id='eggs', ctx=Load())], keywords=[keyword(arg='text', value=Constant(value='and cheese'))]))])",
1677 full="Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), args=[Name(id='eggs', ctx=Load())], keywords=[keyword(arg='text', value=Constant(value='and cheese'))]))], type_ignores=[])",
1678 )
1679
1680 check_text(
1681 "import _ast as ast; from module import sub",
1682 empty="Module(body=[Import(names=[alias(name='_ast', asname='ast')]), ImportFrom(module='module', names=[alias(name='sub')], level=0)])",
1683 full="Module(body=[Import(names=[alias(name='_ast', asname='ast')]), ImportFrom(module='module', names=[alias(name='sub')], level=0)], type_ignores=[])",
1684 )
1685
1686 def test_copy_location(self):
1687 src = ast.parse('1 + 1', mode='eval')
1688 src.body.right = ast.copy_location(ast.Constant(2), src.body.right)
1689 self.assertEqual(ast.dump(src, include_attributes=True),
1690 'Expression(body=BinOp(left=Constant(value=1, lineno=1, col_offset=0, '
1691 'end_lineno=1, end_col_offset=1), op=Add(), right=Constant(value=2, '
1692 'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, '
1693 'col_offset=0, end_lineno=1, end_col_offset=5))'
1694 )
1695 func = ast.Name('spam', ast.Load())
1696 src = ast.Call(col_offset=1, lineno=1, end_lineno=1, end_col_offset=1, func=func)
1697 new = ast.copy_location(src, ast.Call(col_offset=None, lineno=None, func=func))
1698 self.assertIsNone(new.end_lineno)
1699 self.assertIsNone(new.end_col_offset)
1700 self.assertEqual(new.lineno, 1)
1701 self.assertEqual(new.col_offset, 1)
1702
1703 def test_fix_missing_locations(self):
1704 src = ast.parse('write("spam")')
1705 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
1706 [ast.Constant('eggs')], [])))
1707 self.assertEqual(src, ast.fix_missing_locations(src))
1708 self.maxDiff = None
1709 self.assertEqual(ast.dump(src, include_attributes=True),
1710 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
1711 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), "
1712 "args=[Constant(value='spam', lineno=1, col_offset=6, end_lineno=1, "
1713 "end_col_offset=12)], lineno=1, col_offset=0, end_lineno=1, "
1714 "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, "
1715 "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), "
1716 "lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), "
1717 "args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, "
1718 "end_col_offset=0)], lineno=1, col_offset=0, end_lineno=1, "
1719 "end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)])"
1720 )
1721
1722 def test_increment_lineno(self):
1723 src = ast.parse('1 + 1', mode='eval')
1724 self.assertEqual(ast.increment_lineno(src, n=3), src)
1725 self.assertEqual(ast.dump(src, include_attributes=True),
1726 'Expression(body=BinOp(left=Constant(value=1, lineno=4, col_offset=0, '
1727 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, '
1728 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
1729 'col_offset=0, end_lineno=4, end_col_offset=5))'
1730 )
1731 # issue10869: do not increment lineno of root twice
1732 src = ast.parse('1 + 1', mode='eval')
1733 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
1734 self.assertEqual(ast.dump(src, include_attributes=True),
1735 'Expression(body=BinOp(left=Constant(value=1, lineno=4, col_offset=0, '
1736 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, '
1737 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
1738 'col_offset=0, end_lineno=4, end_col_offset=5))'
1739 )
1740 src = ast.Call(
1741 func=ast.Name("test", ast.Load()), args=[], keywords=[], lineno=1
1742 )
1743 self.assertEqual(ast.increment_lineno(src).lineno, 2)
1744 self.assertIsNone(ast.increment_lineno(src).end_lineno)
1745
1746 def test_increment_lineno_on_module(self):
1747 src = ast.parse(dedent("""\
1748 a = 1
1749 b = 2 # type: ignore
1750 c = 3
1751 d = 4 # type: ignore@tag
1752 """), type_comments=True)
1753 ast.increment_lineno(src, n=5)
1754 self.assertEqual(src.type_ignores[0].lineno, 7)
1755 self.assertEqual(src.type_ignores[1].lineno, 9)
1756 self.assertEqual(src.type_ignores[1].tag, '@tag')
1757
1758 def test_iter_fields(self):
1759 node = ast.parse('foo()', mode='eval')
1760 d = dict(ast.iter_fields(node.body))
1761 self.assertEqual(d.pop('func').id, 'foo')
1762 self.assertEqual(d, {'keywords': [], 'args': []})
1763
1764 def test_iter_child_nodes(self):
1765 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
1766 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
1767 iterator = ast.iter_child_nodes(node.body)
1768 self.assertEqual(next(iterator).id, 'spam')
1769 self.assertEqual(next(iterator).value, 23)
1770 self.assertEqual(next(iterator).value, 42)
1771 self.assertEqual(ast.dump(next(iterator)),
1772 "keyword(arg='eggs', value=Constant(value='leek'))"
1773 )
1774
1775 def test_get_docstring(self):
1776 node = ast.parse('"""line one\n line two"""')
1777 self.assertEqual(ast.get_docstring(node),
1778 'line one\nline two')
1779
1780 node = ast.parse('class foo:\n """line one\n line two"""')
1781 self.assertEqual(ast.get_docstring(node.body[0]),
1782 'line one\nline two')
1783
1784 node = ast.parse('def foo():\n """line one\n line two"""')
1785 self.assertEqual(ast.get_docstring(node.body[0]),
1786 'line one\nline two')
1787
1788 node = ast.parse('async def foo():\n """spam\n ham"""')
1789 self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
1790
1791 node = ast.parse('async def foo():\n """spam\n ham"""')
1792 self.assertEqual(ast.get_docstring(node.body[0], clean=False), 'spam\n ham')
1793
1794 node = ast.parse('x')
1795 self.assertRaises(TypeError, ast.get_docstring, node.body[0])
1796
1797 def test_get_docstring_none(self):
1798 self.assertIsNone(ast.get_docstring(ast.parse('')))
1799 node = ast.parse('x = "not docstring"')
1800 self.assertIsNone(ast.get_docstring(node))
1801 node = ast.parse('def foo():\n pass')
1802 self.assertIsNone(ast.get_docstring(node))
1803
1804 node = ast.parse('class foo:\n pass')
1805 self.assertIsNone(ast.get_docstring(node.body[0]))
1806 node = ast.parse('class foo:\n x = "not docstring"')
1807 self.assertIsNone(ast.get_docstring(node.body[0]))
1808 node = ast.parse('class foo:\n def bar(self): pass')
1809 self.assertIsNone(ast.get_docstring(node.body[0]))
1810
1811 node = ast.parse('def foo():\n pass')
1812 self.assertIsNone(ast.get_docstring(node.body[0]))
1813 node = ast.parse('def foo():\n x = "not docstring"')
1814 self.assertIsNone(ast.get_docstring(node.body[0]))
1815
1816 node = ast.parse('async def foo():\n pass')
1817 self.assertIsNone(ast.get_docstring(node.body[0]))
1818 node = ast.parse('async def foo():\n x = "not docstring"')
1819 self.assertIsNone(ast.get_docstring(node.body[0]))
1820
1821 node = ast.parse('async def foo():\n 42')
1822 self.assertIsNone(ast.get_docstring(node.body[0]))
1823
1824 def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
1825 node = ast.parse(
1826 '"""line one\nline two"""\n\n'
1827 'def foo():\n """line one\n line two"""\n\n'
1828 ' def bar():\n """line one\n line two"""\n'
1829 ' """line one\n line two"""\n'
1830 '"""line one\nline two"""\n\n'
1831 )
1832 self.assertEqual(node.body[0].col_offset, 0)
1833 self.assertEqual(node.body[0].lineno, 1)
1834 self.assertEqual(node.body[1].body[0].col_offset, 2)
1835 self.assertEqual(node.body[1].body[0].lineno, 5)
1836 self.assertEqual(node.body[1].body[1].body[0].col_offset, 4)
1837 self.assertEqual(node.body[1].body[1].body[0].lineno, 9)
1838 self.assertEqual(node.body[1].body[2].col_offset, 2)
1839 self.assertEqual(node.body[1].body[2].lineno, 11)
1840 self.assertEqual(node.body[2].col_offset, 0)
1841 self.assertEqual(node.body[2].lineno, 13)
1842
1843 def test_elif_stmt_start_position(self):
1844 node = ast.parse('if a:\n pass\nelif b:\n pass\n')
1845 elif_stmt = node.body[0].orelse[0]
1846 self.assertEqual(elif_stmt.lineno, 3)
1847 self.assertEqual(elif_stmt.col_offset, 0)
1848
1849 def test_elif_stmt_start_position_with_else(self):
1850 node = ast.parse('if a:\n pass\nelif b:\n pass\nelse:\n pass\n')
1851 elif_stmt = node.body[0].orelse[0]
1852 self.assertEqual(elif_stmt.lineno, 3)
1853 self.assertEqual(elif_stmt.col_offset, 0)
1854
1855 def test_starred_expr_end_position_within_call(self):
1856 node = ast.parse('f(*[0, 1])')
1857 starred_expr = node.body[0].value.args[0]
1858 self.assertEqual(starred_expr.end_lineno, 1)
1859 self.assertEqual(starred_expr.end_col_offset, 9)
1860
1861 def test_literal_eval(self):
1862 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
1863 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
1864 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
1865 self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
1866 self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
1867 self.assertEqual(ast.literal_eval('set()'), set())
1868 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
1869 self.assertEqual(ast.literal_eval('6'), 6)
1870 self.assertEqual(ast.literal_eval('+6'), 6)
1871 self.assertEqual(ast.literal_eval('-6'), -6)
1872 self.assertEqual(ast.literal_eval('3.25'), 3.25)
1873 self.assertEqual(ast.literal_eval('+3.25'), 3.25)
1874 self.assertEqual(ast.literal_eval('-3.25'), -3.25)
1875 self.assertEqual(repr(ast.literal_eval('-0.0')), '-0.0')
1876 self.assertRaises(ValueError, ast.literal_eval, '++6')
1877 self.assertRaises(ValueError, ast.literal_eval, '+True')
1878 self.assertRaises(ValueError, ast.literal_eval, '2+3')
1879
1880 def test_literal_eval_str_int_limit(self):
1881 with support.adjust_int_max_str_digits(4000):
1882 ast.literal_eval('3'*4000) # no error
1883 with self.assertRaises(SyntaxError) as err_ctx:
1884 ast.literal_eval('3'*4001)
1885 self.assertIn('Exceeds the limit ', str(err_ctx.exception))
1886 self.assertIn(' Consider hexadecimal ', str(err_ctx.exception))
1887
1888 def test_literal_eval_complex(self):
1889 # Issue #4907
1890 self.assertEqual(ast.literal_eval('6j'), 6j)
1891 self.assertEqual(ast.literal_eval('-6j'), -6j)
1892 self.assertEqual(ast.literal_eval('6.75j'), 6.75j)
1893 self.assertEqual(ast.literal_eval('-6.75j'), -6.75j)
1894 self.assertEqual(ast.literal_eval('3+6j'), 3+6j)
1895 self.assertEqual(ast.literal_eval('-3+6j'), -3+6j)
1896 self.assertEqual(ast.literal_eval('3-6j'), 3-6j)
1897 self.assertEqual(ast.literal_eval('-3-6j'), -3-6j)
1898 self.assertEqual(ast.literal_eval('3.25+6.75j'), 3.25+6.75j)
1899 self.assertEqual(ast.literal_eval('-3.25+6.75j'), -3.25+6.75j)
1900 self.assertEqual(ast.literal_eval('3.25-6.75j'), 3.25-6.75j)
1901 self.assertEqual(ast.literal_eval('-3.25-6.75j'), -3.25-6.75j)
1902 self.assertEqual(ast.literal_eval('(3+6j)'), 3+6j)
1903 self.assertRaises(ValueError, ast.literal_eval, '-6j+3')
1904 self.assertRaises(ValueError, ast.literal_eval, '-6j+3j')
1905 self.assertRaises(ValueError, ast.literal_eval, '3+-6j')
1906 self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)')
1907 self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)')
1908
1909 def test_literal_eval_malformed_dict_nodes(self):
1910 malformed = ast.Dict(keys=[ast.Constant(1), ast.Constant(2)], values=[ast.Constant(3)])
1911 self.assertRaises(ValueError, ast.literal_eval, malformed)
1912 malformed = ast.Dict(keys=[ast.Constant(1)], values=[ast.Constant(2), ast.Constant(3)])
1913 self.assertRaises(ValueError, ast.literal_eval, malformed)
1914
1915 def test_literal_eval_trailing_ws(self):
1916 self.assertEqual(ast.literal_eval(" -1"), -1)
1917 self.assertEqual(ast.literal_eval("\t\t-1"), -1)
1918 self.assertEqual(ast.literal_eval(" \t -1"), -1)
1919 self.assertRaises(IndentationError, ast.literal_eval, "\n -1")
1920
1921 def test_literal_eval_malformed_lineno(self):
1922 msg = r'malformed node or string on line 3:'
1923 with self.assertRaisesRegex(ValueError, msg):
1924 ast.literal_eval("{'a': 1,\n'b':2,\n'c':++3,\n'd':4}")
1925
1926 node = ast.UnaryOp(
1927 ast.UAdd(), ast.UnaryOp(ast.UAdd(), ast.Constant(6)))
1928 self.assertIsNone(getattr(node, 'lineno', None))
1929 msg = r'malformed node or string:'
1930 with self.assertRaisesRegex(ValueError, msg):
1931 ast.literal_eval(node)
1932
1933 def test_literal_eval_syntax_errors(self):
1934 with self.assertRaisesRegex(SyntaxError, "unexpected indent"):
1935 ast.literal_eval(r'''
1936 \
1937 (\
1938 \ ''')
1939
1940 def test_bad_integer(self):
1941 # issue13436: Bad error message with invalid numeric values
1942 body = [ast.ImportFrom(module='time',
1943 names=[ast.alias(name='sleep')],
1944 level=None,
1945 lineno=None, col_offset=None)]
1946 mod = ast.Module(body, [])
1947 with self.assertRaises(ValueError) as cm:
1948 compile(mod, 'test', 'exec')
1949 self.assertIn("invalid integer value: None", str(cm.exception))
1950
1951 def test_level_as_none(self):
1952 body = [ast.ImportFrom(module='time',
1953 names=[ast.alias(name='sleep',
1954 lineno=0, col_offset=0)],
1955 level=None,
1956 lineno=0, col_offset=0)]
1957 mod = ast.Module(body, [])
1958 code = compile(mod, 'test', 'exec')
1959 ns = {}
1960 exec(code, ns)
1961 self.assertIn('sleep', ns)
1962
1963 @skip_if_unlimited_stack_size
1964 @skip_emscripten_stack_overflow()
1965 def test_recursion_direct(self):
1966 e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0, operand=ast.Constant(1))
1967 e.operand = e
1968 with self.assertRaises(RecursionError):
1969 with support.infinite_recursion():
1970 compile(ast.Expression(e), "<test>", "eval")
1971
1972 @skip_if_unlimited_stack_size
1973 @skip_emscripten_stack_overflow()
1974 def test_recursion_indirect(self):
1975 e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0, operand=ast.Constant(1))
1976 f = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0, operand=ast.Constant(1))
1977 e.operand = f
1978 f.operand = e
1979 with self.assertRaises(RecursionError):
1980 with support.infinite_recursion():
1981 compile(ast.Expression(e), "<test>", "eval")
1982
1983
1984 class ASTValidatorTests(unittest.TestCase):
1985
1986 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
1987 mod.lineno = mod.col_offset = 0
1988 ast.fix_missing_locations(mod)
1989 if msg is None:
1990 compile(mod, "<test>", mode)
1991 else:
1992 with self.assertRaises(exc) as cm:
1993 compile(mod, "<test>", mode)
1994 self.assertIn(msg, str(cm.exception))
1995
1996 def expr(self, node, msg=None, *, exc=ValueError):
1997 mod = ast.Module([ast.Expr(node)], [])
1998 self.mod(mod, msg, exc=exc)
1999
2000 def stmt(self, stmt, msg=None):
2001 mod = ast.Module([stmt], [])
2002 self.mod(mod, msg)
2003
2004 def test_module(self):
2005 m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
2006 self.mod(m, "must have Load context", "single")
2007 m = ast.Expression(ast.Name("x", ast.Store()))
2008 self.mod(m, "must have Load context", "eval")
2009
2010 def _check_arguments(self, fac, check):
2011 def arguments(args=None, posonlyargs=None, vararg=None,
2012 kwonlyargs=None, kwarg=None,
2013 defaults=None, kw_defaults=None):
2014 if args is None:
2015 args = []
2016 if posonlyargs is None:
2017 posonlyargs = []
2018 if kwonlyargs is None:
2019 kwonlyargs = []
2020 if defaults is None:
2021 defaults = []
2022 if kw_defaults is None:
2023 kw_defaults = []
2024 args = ast.arguments(args, posonlyargs, vararg, kwonlyargs,
2025 kw_defaults, kwarg, defaults)
2026 return fac(args)
2027 args = [ast.arg("x", ast.Name("x", ast.Store()))]
2028 check(arguments(args=args), "must have Load context")
2029 check(arguments(posonlyargs=args), "must have Load context")
2030 check(arguments(kwonlyargs=args), "must have Load context")
2031 check(arguments(defaults=[ast.Constant(3)]),
2032 "more positional defaults than args")
2033 check(arguments(kw_defaults=[ast.Constant(4)]),
2034 "length of kwonlyargs is not the same as kw_defaults")
2035 args = [ast.arg("x", ast.Name("x", ast.Load()))]
2036 check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
2037 "must have Load context")
2038 args = [ast.arg("a", ast.Name("x", ast.Load())),
2039 ast.arg("b", ast.Name("y", ast.Load()))]
2040 check(arguments(kwonlyargs=args,
2041 kw_defaults=[None, ast.Name("x", ast.Store())]),
2042 "must have Load context")
2043
2044 def test_funcdef(self):
2045 a = ast.arguments([], [], None, [], [], None, [])
2046 f = ast.FunctionDef("x", a, [], [], None, None, [])
2047 self.stmt(f, "empty body on FunctionDef")
2048 f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())], None, None, [])
2049 self.stmt(f, "must have Load context")
2050 f = ast.FunctionDef("x", a, [ast.Pass()], [],
2051 ast.Name("x", ast.Store()), None, [])
2052 self.stmt(f, "must have Load context")
2053 f = ast.FunctionDef("x", ast.arguments(), [ast.Pass()])
2054 self.stmt(f)
2055 def fac(args):
2056 return ast.FunctionDef("x", args, [ast.Pass()], [], None, None, [])
2057 self._check_arguments(fac, self.stmt)
2058
2059 def test_funcdef_pattern_matching(self):
2060 # gh-104799: New fields on FunctionDef should be added at the end
2061 def matcher(node):
2062 match node:
2063 case ast.FunctionDef("foo", ast.arguments(args=[ast.arg("bar")]),
2064 [ast.Pass()],
2065 [ast.Name("capybara", ast.Load())],
2066 ast.Name("pacarana", ast.Load())):
2067 return True
2068 case _:
2069 return False
2070
2071 code = """
2072 @capybara
2073 def foo(bar) -> pacarana:
2074 pass
2075 """
2076 source = ast.parse(textwrap.dedent(code))
2077 funcdef = source.body[0]
2078 self.assertIsInstance(funcdef, ast.FunctionDef)
2079 self.assertTrue(matcher(funcdef))
2080
2081 def test_classdef(self):
2082 def cls(bases=None, keywords=None, body=None, decorator_list=None, type_params=None):
2083 if bases is None:
2084 bases = []
2085 if keywords is None:
2086 keywords = []
2087 if body is None:
2088 body = [ast.Pass()]
2089 if decorator_list is None:
2090 decorator_list = []
2091 if type_params is None:
2092 type_params = []
2093 return ast.ClassDef("myclass", bases, keywords,
2094 body, decorator_list, type_params)
2095 self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
2096 "must have Load context")
2097 self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
2098 "must have Load context")
2099 self.stmt(cls(body=[]), "empty body on ClassDef")
2100 self.stmt(cls(body=[None]), "None disallowed")
2101 self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
2102 "must have Load context")
2103
2104 def test_delete(self):
2105 self.stmt(ast.Delete([]), "empty targets on Delete")
2106 self.stmt(ast.Delete([None]), "None disallowed")
2107 self.stmt(ast.Delete([ast.Name("x", ast.Load())]),
2108 "must have Del context")
2109
2110 def test_assign(self):
2111 self.stmt(ast.Assign([], ast.Constant(3)), "empty targets on Assign")
2112 self.stmt(ast.Assign([None], ast.Constant(3)), "None disallowed")
2113 self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Constant(3)),
2114 "must have Store context")
2115 self.stmt(ast.Assign([ast.Name("x", ast.Store())],
2116 ast.Name("y", ast.Store())),
2117 "must have Load context")
2118
2119 def test_augassign(self):
2120 aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
2121 ast.Name("y", ast.Load()))
2122 self.stmt(aug, "must have Store context")
2123 aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
2124 ast.Name("y", ast.Store()))
2125 self.stmt(aug, "must have Load context")
2126
2127 def test_for(self):
2128 x = ast.Name("x", ast.Store())
2129 y = ast.Name("y", ast.Load())
2130 p = ast.Pass()
2131 self.stmt(ast.For(x, y, [], []), "empty body on For")
2132 self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
2133 "must have Store context")
2134 self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
2135 "must have Load context")
2136 e = ast.Expr(ast.Name("x", ast.Store()))
2137 self.stmt(ast.For(x, y, [e], []), "must have Load context")
2138 self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
2139
2140 def test_while(self):
2141 self.stmt(ast.While(ast.Constant(3), [], []), "empty body on While")
2142 self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
2143 "must have Load context")
2144 self.stmt(ast.While(ast.Constant(3), [ast.Pass()],
2145 [ast.Expr(ast.Name("x", ast.Store()))]),
2146 "must have Load context")
2147
2148 def test_if(self):
2149 self.stmt(ast.If(ast.Constant(3), [], []), "empty body on If")
2150 i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
2151 self.stmt(i, "must have Load context")
2152 i = ast.If(ast.Constant(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
2153 self.stmt(i, "must have Load context")
2154 i = ast.If(ast.Constant(3), [ast.Pass()],
2155 [ast.Expr(ast.Name("x", ast.Store()))])
2156 self.stmt(i, "must have Load context")
2157
2158 def test_with(self):
2159 p = ast.Pass()
2160 self.stmt(ast.With([], [p]), "empty items on With")
2161 i = ast.withitem(ast.Constant(3), None)
2162 self.stmt(ast.With([i], []), "empty body on With")
2163 i = ast.withitem(ast.Name("x", ast.Store()), None)
2164 self.stmt(ast.With([i], [p]), "must have Load context")
2165 i = ast.withitem(ast.Constant(3), ast.Name("x", ast.Load()))
2166 self.stmt(ast.With([i], [p]), "must have Store context")
2167
2168 def test_raise(self):
2169 r = ast.Raise(None, ast.Constant(3))
2170 self.stmt(r, "Raise with cause but no exception")
2171 r = ast.Raise(ast.Name("x", ast.Store()), None)
2172 self.stmt(r, "must have Load context")
2173 r = ast.Raise(ast.Constant(4), ast.Name("x", ast.Store()))
2174 self.stmt(r, "must have Load context")
2175
2176 def test_try(self):
2177 p = ast.Pass()
2178 t = ast.Try([], [], [], [p])
2179 self.stmt(t, "empty body on Try")
2180 t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
2181 self.stmt(t, "must have Load context")
2182 t = ast.Try([p], [], [], [])
2183 self.stmt(t, "Try has neither except handlers nor finalbody")
2184 t = ast.Try([p], [], [p], [p])
2185 self.stmt(t, "Try has orelse but no except handlers")
2186 t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
2187 self.stmt(t, "empty body on ExceptHandler")
2188 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
2189 self.stmt(ast.Try([p], e, [], []), "must have Load context")
2190 e = [ast.ExceptHandler(None, "x", [p])]
2191 t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
2192 self.stmt(t, "must have Load context")
2193 t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
2194 self.stmt(t, "must have Load context")
2195
2196 def test_try_star(self):
2197 p = ast.Pass()
2198 t = ast.TryStar([], [], [], [p])
2199 self.stmt(t, "empty body on TryStar")
2200 t = ast.TryStar([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
2201 self.stmt(t, "must have Load context")
2202 t = ast.TryStar([p], [], [], [])
2203 self.stmt(t, "TryStar has neither except handlers nor finalbody")
2204 t = ast.TryStar([p], [], [p], [p])
2205 self.stmt(t, "TryStar has orelse but no except handlers")
2206 t = ast.TryStar([p], [ast.ExceptHandler(None, "x", [])], [], [])
2207 self.stmt(t, "empty body on ExceptHandler")
2208 e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
2209 self.stmt(ast.TryStar([p], e, [], []), "must have Load context")
2210 e = [ast.ExceptHandler(None, "x", [p])]
2211 t = ast.TryStar([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
2212 self.stmt(t, "must have Load context")
2213 t = ast.TryStar([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
2214 self.stmt(t, "must have Load context")
2215
2216 def test_assert(self):
2217 self.stmt(ast.Assert(ast.Name("x", ast.Store()), None),
2218 "must have Load context")
2219 assrt = ast.Assert(ast.Name("x", ast.Load()),
2220 ast.Name("y", ast.Store()))
2221 self.stmt(assrt, "must have Load context")
2222
2223 def test_import(self):
2224 self.stmt(ast.Import([]), "empty names on Import")
2225
2226 def test_importfrom(self):
2227 imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
2228 self.stmt(imp, "Negative ImportFrom level")
2229 self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
2230
2231 def test_global(self):
2232 self.stmt(ast.Global([]), "empty names on Global")
2233
2234 def test_nonlocal(self):
2235 self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
2236
2237 def test_expr(self):
2238 e = ast.Expr(ast.Name("x", ast.Store()))
2239 self.stmt(e, "must have Load context")
2240
2241 def test_boolop(self):
2242 b = ast.BoolOp(ast.And(), [])
2243 self.expr(b, "less than 2 values")
2244 b = ast.BoolOp(ast.And(), [ast.Constant(3)])
2245 self.expr(b, "less than 2 values")
2246 b = ast.BoolOp(ast.And(), [ast.Constant(4), None])
2247 self.expr(b, "None disallowed")
2248 b = ast.BoolOp(ast.And(), [ast.Constant(4), ast.Name("x", ast.Store())])
2249 self.expr(b, "must have Load context")
2250
2251 def test_unaryop(self):
2252 u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store()))
2253 self.expr(u, "must have Load context")
2254
2255 def test_lambda(self):
2256 a = ast.arguments([], [], None, [], [], None, [])
2257 self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
2258 "must have Load context")
2259 def fac(args):
2260 return ast.Lambda(args, ast.Name("x", ast.Load()))
2261 self._check_arguments(fac, self.expr)
2262
2263 def test_ifexp(self):
2264 l = ast.Name("x", ast.Load())
2265 s = ast.Name("y", ast.Store())
2266 for args in (s, l, l), (l, s, l), (l, l, s):
2267 self.expr(ast.IfExp(*args), "must have Load context")
2268
2269 def test_dict(self):
2270 d = ast.Dict([], [ast.Name("x", ast.Load())])
2271 self.expr(d, "same number of keys as values")
2272 d = ast.Dict([ast.Name("x", ast.Load())], [None])
2273 self.expr(d, "None disallowed")
2274
2275 def test_set(self):
2276 self.expr(ast.Set([None]), "None disallowed")
2277 s = ast.Set([ast.Name("x", ast.Store())])
2278 self.expr(s, "must have Load context")
2279
2280 def _check_comprehension(self, fac):
2281 self.expr(fac([]), "comprehension with no generators")
2282 g = ast.comprehension(ast.Name("x", ast.Load()),
2283 ast.Name("x", ast.Load()), [], 0)
2284 self.expr(fac([g]), "must have Store context")
2285 g = ast.comprehension(ast.Name("x", ast.Store()),
2286 ast.Name("x", ast.Store()), [], 0)
2287 self.expr(fac([g]), "must have Load context")
2288 x = ast.Name("x", ast.Store())
2289 y = ast.Name("y", ast.Load())
2290 g = ast.comprehension(x, y, [None], 0)
2291 self.expr(fac([g]), "None disallowed")
2292 g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0)
2293 self.expr(fac([g]), "must have Load context")
2294
2295 def _simple_comp(self, fac):
2296 g = ast.comprehension(ast.Name("x", ast.Store()),
2297 ast.Name("x", ast.Load()), [], 0)
2298 self.expr(fac(ast.Name("x", ast.Store()), [g]),
2299 "must have Load context")
2300 def wrap(gens):
2301 return fac(ast.Name("x", ast.Store()), gens)
2302 self._check_comprehension(wrap)
2303
2304 def test_listcomp(self):
2305 self._simple_comp(ast.ListComp)
2306
2307 def test_setcomp(self):
2308 self._simple_comp(ast.SetComp)
2309
2310 def test_generatorexp(self):
2311 self._simple_comp(ast.GeneratorExp)
2312
2313 def test_dictcomp(self):
2314 g = ast.comprehension(ast.Name("y", ast.Store()),
2315 ast.Name("p", ast.Load()), [], 0)
2316 c = ast.DictComp(ast.Name("x", ast.Store()),
2317 ast.Name("y", ast.Load()), [g])
2318 self.expr(c, "must have Load context")
2319 c = ast.DictComp(ast.Name("x", ast.Load()),
2320 ast.Name("y", ast.Store()), [g])
2321 self.expr(c, "must have Load context")
2322 def factory(comps):
2323 k = ast.Name("x", ast.Load())
2324 v = ast.Name("y", ast.Load())
2325 return ast.DictComp(k, v, comps)
2326 self._check_comprehension(factory)
2327
2328 def test_yield(self):
2329 self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
2330 self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
2331
2332 def test_compare(self):
2333 left = ast.Name("x", ast.Load())
2334 comp = ast.Compare(left, [ast.In()], [])
2335 self.expr(comp, "no comparators")
2336 comp = ast.Compare(left, [ast.In()], [ast.Constant(4), ast.Constant(5)])
2337 self.expr(comp, "different number of comparators and operands")
2338 comp = ast.Compare(ast.Constant("blah"), [ast.In()], [left])
2339 self.expr(comp)
2340 comp = ast.Compare(left, [ast.In()], [ast.Constant("blah")])
2341 self.expr(comp)
2342
2343 def test_call(self):
2344 func = ast.Name("x", ast.Load())
2345 args = [ast.Name("y", ast.Load())]
2346 keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
2347 call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
2348 self.expr(call, "must have Load context")
2349 call = ast.Call(func, [None], keywords)
2350 self.expr(call, "None disallowed")
2351 bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
2352 call = ast.Call(func, args, bad_keywords)
2353 self.expr(call, "must have Load context")
2354
2355 def test_attribute(self):
2356 attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load())
2357 self.expr(attr, "must have Load context")
2358
2359 def test_subscript(self):
2360 sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Constant(3),
2361 ast.Load())
2362 self.expr(sub, "must have Load context")
2363 x = ast.Name("x", ast.Load())
2364 sub = ast.Subscript(x, ast.Name("y", ast.Store()),
2365 ast.Load())
2366 self.expr(sub, "must have Load context")
2367 s = ast.Name("x", ast.Store())
2368 for args in (s, None, None), (None, s, None), (None, None, s):
2369 sl = ast.Slice(*args)
2370 self.expr(ast.Subscript(x, sl, ast.Load()),
2371 "must have Load context")
2372 sl = ast.Tuple([], ast.Load())
2373 self.expr(ast.Subscript(x, sl, ast.Load()))
2374 sl = ast.Tuple([s], ast.Load())
2375 self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
2376
2377 def test_starred(self):
2378 left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())],
2379 ast.Store())
2380 assign = ast.Assign([left], ast.Constant(4))
2381 self.stmt(assign, "must have Store context")
2382
2383 def _sequence(self, fac):
2384 self.expr(fac([None], ast.Load()), "None disallowed")
2385 self.expr(fac([ast.Name("x", ast.Store())], ast.Load()),
2386 "must have Load context")
2387
2388 def test_list(self):
2389 self._sequence(ast.List)
2390
2391 def test_tuple(self):
2392 self._sequence(ast.Tuple)
2393
2394 @support.requires_resource('cpu')
2395 def test_stdlib_validates(self):
2396 for module in STDLIB_FILES:
2397 with self.subTest(module):
2398 fn = os.path.join(STDLIB, module)
2399 with open(fn, "r", encoding="utf-8") as fp:
2400 source = fp.read()
2401 mod = ast.parse(source, fn)
2402 compile(mod, fn, "exec")
2403 mod2 = ast.parse(source, fn)
2404 self.assertTrue(ast.compare(mod, mod2))
2405
2406 constant_1 = ast.Constant(1)
2407 pattern_1 = ast.MatchValue(constant_1)
2408
2409 constant_x = ast.Constant('x')
2410 pattern_x = ast.MatchValue(constant_x)
2411
2412 constant_true = ast.Constant(True)
2413 pattern_true = ast.MatchSingleton(True)
2414
2415 name_carter = ast.Name('carter', ast.Load())
2416
2417 _MATCH_PATTERNS = [
2418 ast.MatchValue(
2419 ast.Attribute(
2420 ast.Attribute(
2421 ast.Name('x', ast.Store()),
2422 'y', ast.Load()
2423 ),
2424 'z', ast.Load()
2425 )
2426 ),
2427 ast.MatchValue(
2428 ast.Attribute(
2429 ast.Attribute(
2430 ast.Name('x', ast.Load()),
2431 'y', ast.Store()
2432 ),
2433 'z', ast.Load()
2434 )
2435 ),
2436 ast.MatchValue(
2437 ast.Constant(...)
2438 ),
2439 ast.MatchValue(
2440 ast.Constant(True)
2441 ),
2442 ast.MatchValue(
2443 ast.Constant((1,2,3))
2444 ),
2445 ast.MatchSingleton('string'),
2446 ast.MatchSequence([
2447 ast.MatchSingleton('string')
2448 ]),
2449 ast.MatchSequence(
2450 [
2451 ast.MatchSequence(
2452 [
2453 ast.MatchSingleton('string')
2454 ]
2455 )
2456 ]
2457 ),
2458 ast.MatchMapping(
2459 [constant_1, constant_true],
2460 [pattern_x]
2461 ),
2462 ast.MatchMapping(
2463 [constant_true, constant_1],
2464 [pattern_x, pattern_1],
2465 rest='True'
2466 ),
2467 ast.MatchMapping(
2468 [constant_true, ast.Starred(ast.Name('lol', ast.Load()), ast.Load())],
2469 [pattern_x, pattern_1],
2470 rest='legit'
2471 ),
2472 ast.MatchClass(
2473 ast.Attribute(
2474 ast.Attribute(
2475 constant_x,
2476 'y', ast.Load()),
2477 'z', ast.Load()),
2478 patterns=[], kwd_attrs=[], kwd_patterns=[]
2479 ),
2480 ast.MatchClass(
2481 name_carter,
2482 patterns=[],
2483 kwd_attrs=['True'],
2484 kwd_patterns=[pattern_1]
2485 ),
2486 ast.MatchClass(
2487 name_carter,
2488 patterns=[],
2489 kwd_attrs=[],
2490 kwd_patterns=[pattern_1]
2491 ),
2492 ast.MatchClass(
2493 name_carter,
2494 patterns=[ast.MatchSingleton('string')],
2495 kwd_attrs=[],
2496 kwd_patterns=[]
2497 ),
2498 ast.MatchClass(
2499 name_carter,
2500 patterns=[ast.MatchStar()],
2501 kwd_attrs=[],
2502 kwd_patterns=[]
2503 ),
2504 ast.MatchClass(
2505 name_carter,
2506 patterns=[],
2507 kwd_attrs=[],
2508 kwd_patterns=[ast.MatchStar()]
2509 ),
2510 ast.MatchClass(
2511 constant_true, # invalid name
2512 patterns=[],
2513 kwd_attrs=['True'],
2514 kwd_patterns=[pattern_1]
2515 ),
2516 ast.MatchSequence(
2517 [
2518 ast.MatchStar("True")
2519 ]
2520 ),
2521 ast.MatchAs(
2522 name='False'
2523 ),
2524 ast.MatchOr(
2525 []
2526 ),
2527 ast.MatchOr(
2528 [pattern_1]
2529 ),
2530 ast.MatchOr(
2531 [pattern_1, pattern_x, ast.MatchSingleton('xxx')]
2532 ),
2533 ast.MatchAs(name="_"),
2534 ast.MatchStar(name="x"),
2535 ast.MatchSequence([ast.MatchStar("_")]),
2536 ast.MatchMapping([], [], rest="_"),
2537 ]
2538
2539 def test_match_validation_pattern(self):
2540 name_x = ast.Name('x', ast.Load())
2541 for pattern in self._MATCH_PATTERNS:
2542 with self.subTest(ast.dump(pattern, indent=4)):
2543 node = ast.Match(
2544 subject=name_x,
2545 cases = [
2546 ast.match_case(
2547 pattern=pattern,
2548 body = [ast.Pass()]
2549 )
2550 ]
2551 )
2552 node = ast.fix_missing_locations(node)
2553 module = ast.Module([node], [])
2554 with self.assertRaises(ValueError):
2555 compile(module, "<test>", "exec")
2556
2557
2558 class ConstantTests(unittest.TestCase):
2559 """Tests on the ast.Constant node type."""
2560
2561 def compile_constant(self, value):
2562 tree = ast.parse("x = 123")
2563
2564 node = tree.body[0].value
2565 new_node = ast.Constant(value=value)
2566 ast.copy_location(new_node, node)
2567 tree.body[0].value = new_node
2568
2569 code = compile(tree, "<string>", "exec")
2570
2571 ns = {}
2572 exec(code, ns)
2573 return ns['x']
2574
2575 def test_validation(self):
2576 with self.assertRaises(TypeError) as cm:
2577 self.compile_constant([1, 2, 3])
2578 self.assertEqual(str(cm.exception),
2579 "got an invalid type in Constant: list")
2580
2581 def test_singletons(self):
2582 for const in (None, False, True, Ellipsis, b''):
2583 with self.subTest(const=const):
2584 value = self.compile_constant(const)
2585 self.assertIs(value, const)
2586
2587 def test_values(self):
2588 nested_tuple = (1,)
2589 nested_frozenset = frozenset({1})
2590 for level in range(3):
2591 nested_tuple = (nested_tuple, 2)
2592 nested_frozenset = frozenset({nested_frozenset, 2})
2593 values = (123, 123.0, 123j,
2594 "unicode", b'bytes',
2595 tuple("tuple"), frozenset("frozenset"),
2596 nested_tuple, nested_frozenset)
2597 for value in values:
2598 with self.subTest(value=value):
2599 result = self.compile_constant(value)
2600 self.assertEqual(result, value)
2601
2602 def test_assign_to_constant(self):
2603 tree = ast.parse("x = 1")
2604
2605 target = tree.body[0].targets[0]
2606 new_target = ast.Constant(value=1)
2607 ast.copy_location(new_target, target)
2608 tree.body[0].targets[0] = new_target
2609
2610 with self.assertRaises(ValueError) as cm:
2611 compile(tree, "string", "exec")
2612 self.assertEqual(str(cm.exception),
2613 "expression which can't be assigned "
2614 "to in Store context")
2615
2616 def test_get_docstring(self):
2617 tree = ast.parse("'docstring'\nx = 1")
2618 self.assertEqual(ast.get_docstring(tree), 'docstring')
2619
2620 def get_load_const(self, tree):
2621 # Compile to bytecode, disassemble and get parameter of LOAD_CONST
2622 # instructions
2623 co = compile(tree, '<string>', 'exec')
2624 consts = []
2625 for instr in dis.get_instructions(co):
2626 if instr.opcode in dis.hasconst:
2627 consts.append(instr.argval)
2628 return consts
2629
2630 @support.cpython_only
2631 def test_load_const(self):
2632 consts = [None,
2633 True, False,
2634 1000,
2635 2.0,
2636 3j,
2637 "unicode",
2638 b'bytes',
2639 (1, 2, 3)]
2640
2641 code = '\n'.join(['x={!r}'.format(const) for const in consts])
2642 code += '\nx = ...'
2643 consts.extend((Ellipsis, None))
2644
2645 tree = ast.parse(code)
2646 self.assertEqual(self.get_load_const(tree),
2647 consts)
2648
2649 # Replace expression nodes with constants
2650 for assign, const in zip(tree.body, consts):
2651 assert isinstance(assign, ast.Assign), ast.dump(assign)
2652 new_node = ast.Constant(value=const)
2653 ast.copy_location(new_node, assign.value)
2654 assign.value = new_node
2655
2656 self.assertEqual(self.get_load_const(tree),
2657 consts)
2658
2659 def test_literal_eval(self):
2660 tree = ast.parse("1 + 2")
2661 binop = tree.body[0].value
2662
2663 new_left = ast.Constant(value=10)
2664 ast.copy_location(new_left, binop.left)
2665 binop.left = new_left
2666
2667 new_right = ast.Constant(value=20j)
2668 ast.copy_location(new_right, binop.right)
2669 binop.right = new_right
2670
2671 self.assertEqual(ast.literal_eval(binop), 10+20j)
2672
2673 def test_string_kind(self):
2674 c = ast.parse('"x"', mode='eval').body
2675 self.assertEqual(c.value, "x")
2676 self.assertEqual(c.kind, None)
2677
2678 c = ast.parse('u"x"', mode='eval').body
2679 self.assertEqual(c.value, "x")
2680 self.assertEqual(c.kind, "u")
2681
2682 c = ast.parse('r"x"', mode='eval').body
2683 self.assertEqual(c.value, "x")
2684 self.assertEqual(c.kind, None)
2685
2686 c = ast.parse('b"x"', mode='eval').body
2687 self.assertEqual(c.value, b"x")
2688 self.assertEqual(c.kind, None)
2689
2690
2691 class EndPositionTests(unittest.TestCase):
2692 """Tests for end position of AST nodes.
2693
2694 Testing end positions of nodes requires a bit of extra care
2695 because of how LL parsers work.
2696 """
2697 def _check_end_pos(self, ast_node, end_lineno, end_col_offset):
2698 self.assertEqual(ast_node.end_lineno, end_lineno)
2699 self.assertEqual(ast_node.end_col_offset, end_col_offset)
2700
2701 def _check_content(self, source, ast_node, content):
2702 self.assertEqual(ast.get_source_segment(source, ast_node), content)
2703
2704 def _parse_value(self, s):
2705 # Use duck-typing to support both single expression
2706 # and a right hand side of an assignment statement.
2707 return ast.parse(s).body[0].value
2708
2709 def test_lambda(self):
2710 s = 'lambda x, *y: None'
2711 lam = self._parse_value(s)
2712 self._check_content(s, lam.body, 'None')
2713 self._check_content(s, lam.args.args[0], 'x')
2714 self._check_content(s, lam.args.vararg, 'y')
2715
2716 def test_func_def(self):
2717 s = dedent('''
2718 def func(x: int,
2719 *args: str,
2720 z: float = 0,
2721 **kwargs: Any) -> bool:
2722 return True
2723 ''').strip()
2724 fdef = ast.parse(s).body[0]
2725 self._check_end_pos(fdef, 5, 15)
2726 self._check_content(s, fdef.body[0], 'return True')
2727 self._check_content(s, fdef.args.args[0], 'x: int')
2728 self._check_content(s, fdef.args.args[0].annotation, 'int')
2729 self._check_content(s, fdef.args.kwarg, 'kwargs: Any')
2730 self._check_content(s, fdef.args.kwarg.annotation, 'Any')
2731
2732 def test_call(self):
2733 s = 'func(x, y=2, **kw)'
2734 call = self._parse_value(s)
2735 self._check_content(s, call.func, 'func')
2736 self._check_content(s, call.keywords[0].value, '2')
2737 self._check_content(s, call.keywords[1].value, 'kw')
2738
2739 def test_call_noargs(self):
2740 s = 'x[0]()'
2741 call = self._parse_value(s)
2742 self._check_content(s, call.func, 'x[0]')
2743 self._check_end_pos(call, 1, 6)
2744
2745 def test_class_def(self):
2746 s = dedent('''
2747 class C(A, B):
2748 x: int = 0
2749 ''').strip()
2750 cdef = ast.parse(s).body[0]
2751 self._check_end_pos(cdef, 2, 14)
2752 self._check_content(s, cdef.bases[1], 'B')
2753 self._check_content(s, cdef.body[0], 'x: int = 0')
2754
2755 def test_class_kw(self):
2756 s = 'class S(metaclass=abc.ABCMeta): pass'
2757 cdef = ast.parse(s).body[0]
2758 self._check_content(s, cdef.keywords[0].value, 'abc.ABCMeta')
2759
2760 def test_multi_line_str(self):
2761 s = dedent('''
2762 x = """Some multi-line text.
2763
2764 It goes on starting from same indent."""
2765 ''').strip()
2766 assign = ast.parse(s).body[0]
2767 self._check_end_pos(assign, 3, 40)
2768 self._check_end_pos(assign.value, 3, 40)
2769
2770 def test_continued_str(self):
2771 s = dedent('''
2772 x = "first part" \\
2773 "second part"
2774 ''').strip()
2775 assign = ast.parse(s).body[0]
2776 self._check_end_pos(assign, 2, 13)
2777 self._check_end_pos(assign.value, 2, 13)
2778
2779 def test_suites(self):
2780 # We intentionally put these into the same string to check
2781 # that empty lines are not part of the suite.
2782 s = dedent('''
2783 while True:
2784 pass
2785
2786 if one():
2787 x = None
2788 elif other():
2789 y = None
2790 else:
2791 z = None
2792
2793 for x, y in stuff:
2794 assert True
2795
2796 try:
2797 raise RuntimeError
2798 except TypeError as e:
2799 pass
2800
2801 pass
2802 ''').strip()
2803 mod = ast.parse(s)
2804 while_loop = mod.body[0]
2805 if_stmt = mod.body[1]
2806 for_loop = mod.body[2]
2807 try_stmt = mod.body[3]
2808 pass_stmt = mod.body[4]
2809
2810 self._check_end_pos(while_loop, 2, 8)
2811 self._check_end_pos(if_stmt, 9, 12)
2812 self._check_end_pos(for_loop, 12, 15)
2813 self._check_end_pos(try_stmt, 17, 8)
2814 self._check_end_pos(pass_stmt, 19, 4)
2815
2816 self._check_content(s, while_loop.test, 'True')
2817 self._check_content(s, if_stmt.body[0], 'x = None')
2818 self._check_content(s, if_stmt.orelse[0].test, 'other()')
2819 self._check_content(s, for_loop.target, 'x, y')
2820 self._check_content(s, try_stmt.body[0], 'raise RuntimeError')
2821 self._check_content(s, try_stmt.handlers[0].type, 'TypeError')
2822
2823 def test_fstring(self):
2824 s = 'x = f"abc {x + y} abc"'
2825 fstr = self._parse_value(s)
2826 binop = fstr.values[1].value
2827 self._check_content(s, binop, 'x + y')
2828
2829 def test_fstring_multi_line(self):
2830 s = dedent('''
2831 f"""Some multi-line text.
2832 {
2833 arg_one
2834 +
2835 arg_two
2836 }
2837 It goes on..."""
2838 ''').strip()
2839 fstr = self._parse_value(s)
2840 binop = fstr.values[1].value
2841 self._check_end_pos(binop, 5, 7)
2842 self._check_content(s, binop.left, 'arg_one')
2843 self._check_content(s, binop.right, 'arg_two')
2844
2845 def test_import_from_multi_line(self):
2846 s = dedent('''
2847 from x.y.z import (
2848 a, b, c as c
2849 )
2850 ''').strip()
2851 imp = ast.parse(s).body[0]
2852 self._check_end_pos(imp, 3, 1)
2853 self._check_end_pos(imp.names[2], 2, 16)
2854
2855 def test_slices(self):
2856 s1 = 'f()[1, 2] [0]'
2857 s2 = 'x[ a.b: c.d]'
2858 sm = dedent('''
2859 x[ a.b: f () ,
2860 g () : c.d
2861 ]
2862 ''').strip()
2863 i1, i2, im = map(self._parse_value, (s1, s2, sm))
2864 self._check_content(s1, i1.value, 'f()[1, 2]')
2865 self._check_content(s1, i1.value.slice, '1, 2')
2866 self._check_content(s2, i2.slice.lower, 'a.b')
2867 self._check_content(s2, i2.slice.upper, 'c.d')
2868 self._check_content(sm, im.slice.elts[0].upper, 'f ()')
2869 self._check_content(sm, im.slice.elts[1].lower, 'g ()')
2870 self._check_end_pos(im, 3, 3)
2871
2872 def test_binop(self):
2873 s = dedent('''
2874 (1 * 2 + (3 ) +
2875 4
2876 )
2877 ''').strip()
2878 binop = self._parse_value(s)
2879 self._check_end_pos(binop, 2, 6)
2880 self._check_content(s, binop.right, '4')
2881 self._check_content(s, binop.left, '1 * 2 + (3 )')
2882 self._check_content(s, binop.left.right, '3')
2883
2884 def test_boolop(self):
2885 s = dedent('''
2886 if (one_condition and
2887 (other_condition or yet_another_one)):
2888 pass
2889 ''').strip()
2890 bop = ast.parse(s).body[0].test
2891 self._check_end_pos(bop, 2, 44)
2892 self._check_content(s, bop.values[1],
2893 'other_condition or yet_another_one')
2894
2895 def test_tuples(self):
2896 s1 = 'x = () ;'
2897 s2 = 'x = 1 , ;'
2898 s3 = 'x = (1 , 2 ) ;'
2899 sm = dedent('''
2900 x = (
2901 a, b,
2902 )
2903 ''').strip()
2904 t1, t2, t3, tm = map(self._parse_value, (s1, s2, s3, sm))
2905 self._check_content(s1, t1, '()')
2906 self._check_content(s2, t2, '1 ,')
2907 self._check_content(s3, t3, '(1 , 2 )')
2908 self._check_end_pos(tm, 3, 1)
2909
2910 def test_attribute_spaces(self):
2911 s = 'func(x. y .z)'
2912 call = self._parse_value(s)
2913 self._check_content(s, call, s)
2914 self._check_content(s, call.args[0], 'x. y .z')
2915
2916 def test_redundant_parenthesis(self):
2917 s = '( ( ( a + b ) ) )'
2918 v = ast.parse(s).body[0].value
2919 self.assertEqual(type(v).__name__, 'BinOp')
2920 self._check_content(s, v, 'a + b')
2921 s2 = 'await ' + s
2922 v = ast.parse(s2).body[0].value.value
2923 self.assertEqual(type(v).__name__, 'BinOp')
2924 self._check_content(s2, v, 'a + b')
2925
2926 def test_trailers_with_redundant_parenthesis(self):
2927 tests = (
2928 ('( ( ( a ) ) ) ( )', 'Call'),
2929 ('( ( ( a ) ) ) ( b )', 'Call'),
2930 ('( ( ( a ) ) ) [ b ]', 'Subscript'),
2931 ('( ( ( a ) ) ) . b', 'Attribute'),
2932 )
2933 for s, t in tests:
2934 with self.subTest(s):
2935 v = ast.parse(s).body[0].value
2936 self.assertEqual(type(v).__name__, t)
2937 self._check_content(s, v, s)
2938 s2 = 'await ' + s
2939 v = ast.parse(s2).body[0].value.value
2940 self.assertEqual(type(v).__name__, t)
2941 self._check_content(s2, v, s)
2942
2943 def test_displays(self):
2944 s1 = '[{}, {1, }, {1, 2,} ]'
2945 s2 = '{a: b, f (): g () ,}'
2946 c1 = self._parse_value(s1)
2947 c2 = self._parse_value(s2)
2948 self._check_content(s1, c1.elts[0], '{}')
2949 self._check_content(s1, c1.elts[1], '{1, }')
2950 self._check_content(s1, c1.elts[2], '{1, 2,}')
2951 self._check_content(s2, c2.keys[1], 'f ()')
2952 self._check_content(s2, c2.values[1], 'g ()')
2953
2954 def test_comprehensions(self):
2955 s = dedent('''
2956 x = [{x for x, y in stuff
2957 if cond.x} for stuff in things]
2958 ''').strip()
2959 cmp = self._parse_value(s)
2960 self._check_end_pos(cmp, 2, 37)
2961 self._check_content(s, cmp.generators[0].iter, 'things')
2962 self._check_content(s, cmp.elt.generators[0].iter, 'stuff')
2963 self._check_content(s, cmp.elt.generators[0].ifs[0], 'cond.x')
2964 self._check_content(s, cmp.elt.generators[0].target, 'x, y')
2965
2966 def test_yield_await(self):
2967 s = dedent('''
2968 async def f():
2969 yield x
2970 await y
2971 ''').strip()
2972 fdef = ast.parse(s).body[0]
2973 self._check_content(s, fdef.body[0].value, 'yield x')
2974 self._check_content(s, fdef.body[1].value, 'await y')
2975
2976 def test_source_segment_multi(self):
2977 s_orig = dedent('''
2978 x = (
2979 a, b,
2980 ) + ()
2981 ''').strip()
2982 s_tuple = dedent('''
2983 (
2984 a, b,
2985 )
2986 ''').strip()
2987 binop = self._parse_value(s_orig)
2988 self.assertEqual(ast.get_source_segment(s_orig, binop.left), s_tuple)
2989
2990 def test_source_segment_padded(self):
2991 s_orig = dedent('''
2992 class C:
2993 def fun(self) -> None:
2994 "ЖЖЖЖЖ"
2995 ''').strip()
2996 s_method = ' def fun(self) -> None:\n' \
2997 ' "ЖЖЖЖЖ"'
2998 cdef = ast.parse(s_orig).body[0]
2999 self.assertEqual(ast.get_source_segment(s_orig, cdef.body[0], padded=True),
3000 s_method)
3001
3002 def test_source_segment_endings(self):
3003 s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\rz = 1\r\n'
3004 v, w, x, y, z = ast.parse(s).body
3005 self._check_content(s, v, 'v = 1')
3006 self._check_content(s, w, 'w = 1')
3007 self._check_content(s, x, 'x = 1')
3008 self._check_content(s, y, 'y = 1')
3009 self._check_content(s, z, 'z = 1')
3010
3011 def test_source_segment_tabs(self):
3012 s = dedent('''
3013 class C:
3014 \t\f def fun(self) -> None:
3015 \t\f pass
3016 ''').strip()
3017 s_method = ' \t\f def fun(self) -> None:\n' \
3018 ' \t\f pass'
3019
3020 cdef = ast.parse(s).body[0]
3021 self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method)
3022
3023 def test_source_segment_newlines(self):
3024 s = 'def f():\n pass\ndef g():\r pass\r\ndef h():\r\n pass\r\n'
3025 f, g, h = ast.parse(s).body
3026 self._check_content(s, f, 'def f():\n pass')
3027 self._check_content(s, g, 'def g():\r pass')
3028 self._check_content(s, h, 'def h():\r\n pass')
3029
3030 s = 'def f():\n a = 1\r b = 2\r\n c = 3\n'
3031 f = ast.parse(s).body[0]
3032 self._check_content(s, f, s.rstrip())
3033
3034 def test_source_segment_missing_info(self):
3035 s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\r\n'
3036 v, w, x, y = ast.parse(s).body
3037 del v.lineno
3038 del w.end_lineno
3039 del x.col_offset
3040 del y.end_col_offset
3041 self.assertIsNone(ast.get_source_segment(s, v))
3042 self.assertIsNone(ast.get_source_segment(s, w))
3043 self.assertIsNone(ast.get_source_segment(s, x))
3044 self.assertIsNone(ast.get_source_segment(s, y))
3045
3046
3047 class NodeTransformerTests(ASTTestMixin, unittest.TestCase):
3048 def assertASTTransformation(self, transformer_class,
3049 initial_code, expected_code):
3050 initial_ast = ast.parse(dedent(initial_code))
3051 expected_ast = ast.parse(dedent(expected_code))
3052
3053 transformer = transformer_class()
3054 result_ast = ast.fix_missing_locations(transformer.visit(initial_ast))
3055
3056 self.assertASTEqual(result_ast, expected_ast)
3057
3058 def test_node_remove_single(self):
3059 code = 'def func(arg) -> SomeType: ...'
3060 expected = 'def func(arg): ...'
3061
3062 # Since `FunctionDef.returns` is defined as a single value, we test
3063 # the `if isinstance(old_value, AST):` branch here.
3064 class SomeTypeRemover(ast.NodeTransformer):
3065 def visit_Name(self, node: ast.Name):
3066 self.generic_visit(node)
3067 if node.id == 'SomeType':
3068 return None
3069 return node
3070
3071 self.assertASTTransformation(SomeTypeRemover, code, expected)
3072
3073 def test_node_remove_from_list(self):
3074 code = """
3075 def func(arg):
3076 print(arg)
3077 yield arg
3078 """
3079 expected = """
3080 def func(arg):
3081 print(arg)
3082 """
3083
3084 # Since `FunctionDef.body` is defined as a list, we test
3085 # the `if isinstance(old_value, list):` branch here.
3086 class YieldRemover(ast.NodeTransformer):
3087 def visit_Expr(self, node: ast.Expr):
3088 self.generic_visit(node)
3089 if isinstance(node.value, ast.Yield):
3090 return None # Remove `yield` from a function
3091 return node
3092
3093 self.assertASTTransformation(YieldRemover, code, expected)
3094
3095 def test_node_return_list(self):
3096 code = """
3097 class DSL(Base, kw1=True): ...
3098 """
3099 expected = """
3100 class DSL(Base, kw1=True, kw2=True, kw3=False): ...
3101 """
3102
3103 class ExtendKeywords(ast.NodeTransformer):
3104 def visit_keyword(self, node: ast.keyword):
3105 self.generic_visit(node)
3106 if node.arg == 'kw1':
3107 return [
3108 node,
3109 ast.keyword('kw2', ast.Constant(True)),
3110 ast.keyword('kw3', ast.Constant(False)),
3111 ]
3112 return node
3113
3114 self.assertASTTransformation(ExtendKeywords, code, expected)
3115
3116 def test_node_mutate(self):
3117 code = """
3118 def func(arg):
3119 print(arg)
3120 """
3121 expected = """
3122 def func(arg):
3123 log(arg)
3124 """
3125
3126 class PrintToLog(ast.NodeTransformer):
3127 def visit_Call(self, node: ast.Call):
3128 self.generic_visit(node)
3129 if isinstance(node.func, ast.Name) and node.func.id == 'print':
3130 node.func.id = 'log'
3131 return node
3132
3133 self.assertASTTransformation(PrintToLog, code, expected)
3134
3135 def test_node_replace(self):
3136 code = """
3137 def func(arg):
3138 print(arg)
3139 """
3140 expected = """
3141 def func(arg):
3142 logger.log(arg, debug=True)
3143 """
3144
3145 class PrintToLog(ast.NodeTransformer):
3146 def visit_Call(self, node: ast.Call):
3147 self.generic_visit(node)
3148 if isinstance(node.func, ast.Name) and node.func.id == 'print':
3149 return ast.Call(
3150 func=ast.Attribute(
3151 ast.Name('logger', ctx=ast.Load()),
3152 attr='log',
3153 ctx=ast.Load(),
3154 ),
3155 args=node.args,
3156 keywords=[ast.keyword('debug', ast.Constant(True))],
3157 )
3158 return node
3159
3160 self.assertASTTransformation(PrintToLog, code, expected)
3161
3162
3163 class ASTConstructorTests(unittest.TestCase):
3164 """Test the autogenerated constructors for AST nodes."""
3165
3166 def test_FunctionDef(self):
3167 args = ast.arguments()
3168 self.assertEqual(args.args, [])
3169 self.assertEqual(args.posonlyargs, [])
3170 with self.assertWarnsRegex(DeprecationWarning,
3171 r"FunctionDef\.__init__ missing 1 required positional argument: 'name'"):
3172 node = ast.FunctionDef(args=args)
3173 self.assertNotHasAttr(node, "name")
3174 self.assertEqual(node.decorator_list, [])
3175 node = ast.FunctionDef(name='foo', args=args)
3176 self.assertEqual(node.name, 'foo')
3177 self.assertEqual(node.decorator_list, [])
3178
3179 def test_expr_context(self):
3180 name = ast.Name("x")
3181 self.assertEqual(name.id, "x")
3182 self.assertIsInstance(name.ctx, ast.Load)
3183
3184 name2 = ast.Name("x", ast.Store())
3185 self.assertEqual(name2.id, "x")
3186 self.assertIsInstance(name2.ctx, ast.Store)
3187
3188 name3 = ast.Name("x", ctx=ast.Del())
3189 self.assertEqual(name3.id, "x")
3190 self.assertIsInstance(name3.ctx, ast.Del)
3191
3192 with self.assertWarnsRegex(DeprecationWarning,
3193 r"Name\.__init__ missing 1 required positional argument: 'id'"):
3194 name3 = ast.Name()
3195
3196 def test_custom_subclass_with_no_fields(self):
3197 class NoInit(ast.AST):
3198 pass
3199
3200 obj = NoInit()
3201 self.assertIsInstance(obj, NoInit)
3202 self.assertEqual(obj.__dict__, {})
3203
3204 def test_fields_but_no_field_types(self):
3205 class Fields(ast.AST):
3206 _fields = ('a',)
3207
3208 obj = Fields()
3209 with self.assertRaises(AttributeError):
3210 obj.a
3211 obj = Fields(a=1)
3212 self.assertEqual(obj.a, 1)
3213
3214 def test_fields_and_types(self):
3215 class FieldsAndTypes(ast.AST):
3216 _fields = ('a',)
3217 _field_types = {'a': int | None}
3218 a: int | None = None
3219
3220 obj = FieldsAndTypes()
3221 self.assertIs(obj.a, None)
3222 obj = FieldsAndTypes(a=1)
3223 self.assertEqual(obj.a, 1)
3224
3225 def test_custom_attributes(self):
3226 class MyAttrs(ast.AST):
3227 _attributes = ("a", "b")
3228
3229 obj = MyAttrs(a=1, b=2)
3230 self.assertEqual(obj.a, 1)
3231 self.assertEqual(obj.b, 2)
3232
3233 with self.assertWarnsRegex(DeprecationWarning,
3234 r"MyAttrs.__init__ got an unexpected keyword argument 'c'."):
3235 obj = MyAttrs(c=3)
3236
3237 def test_fields_and_types_no_default(self):
3238 class FieldsAndTypesNoDefault(ast.AST):
3239 _fields = ('a',)
3240 _field_types = {'a': int}
3241
3242 with self.assertWarnsRegex(DeprecationWarning,
3243 r"FieldsAndTypesNoDefault\.__init__ missing 1 required positional argument: 'a'\."):
3244 obj = FieldsAndTypesNoDefault()
3245 with self.assertRaises(AttributeError):
3246 obj.a
3247 obj = FieldsAndTypesNoDefault(a=1)
3248 self.assertEqual(obj.a, 1)
3249
3250 def test_incomplete_field_types(self):
3251 class MoreFieldsThanTypes(ast.AST):
3252 _fields = ('a', 'b')
3253 _field_types = {'a': int | None}
3254 a: int | None = None
3255 b: int | None = None
3256
3257 with self.assertWarnsRegex(
3258 DeprecationWarning,
3259 r"Field 'b' is missing from MoreFieldsThanTypes\._field_types"
3260 ):
3261 obj = MoreFieldsThanTypes()
3262 self.assertIs(obj.a, None)
3263 self.assertIs(obj.b, None)
3264
3265 obj = MoreFieldsThanTypes(a=1, b=2)
3266 self.assertEqual(obj.a, 1)
3267 self.assertEqual(obj.b, 2)
3268
3269 def test_malformed_fields_with_bytes(self):
3270 class BadFields(ast.AST):
3271 _fields = (b'\xff'*64,)
3272 _field_types = {'a': int}
3273
3274 # This should not crash
3275 with self.assertWarnsRegex(DeprecationWarning, r"Field b'\\xff\\xff.*' .*"):
3276 obj = BadFields()
3277
3278 def test_complete_field_types(self):
3279 class _AllFieldTypes(ast.AST):
3280 _fields = ('a', 'b')
3281 _field_types = {'a': int | None, 'b': list[str]}
3282 # This must be set explicitly
3283 a: int | None = None
3284 # This will add an implicit empty list default
3285 b: list[str]
3286
3287 obj = _AllFieldTypes()
3288 self.assertIs(obj.a, None)
3289 self.assertEqual(obj.b, [])
3290
3291 def test_non_str_kwarg(self):
3292 warn_msg = "got an unexpected keyword argument <object object"
3293 with (
3294 self.assertRaises(TypeError),
3295 self.assertWarnsRegex(DeprecationWarning, warn_msg),
3296 ):
3297 ast.Name(**{object(): 'y'})
3298
3299 class FakeStr:
3300 def __init__(self, value):
3301 self.value = value
3302
3303 def __hash__(self):
3304 return hash(self.value)
3305
3306 def __eq__(self, other):
3307 return isinstance(other, str) and self.value == other
3308
3309 with self.assertRaisesRegex(TypeError, "got multiple values for argument"):
3310 ast.Name("x", **{FakeStr('id'): 'y'})
3311
3312
3313 @support.cpython_only
3314 class ModuleStateTests(unittest.TestCase):
3315 # bpo-41194, bpo-41261, bpo-41631: The _ast module uses a global state.
3316
3317 def check_ast_module(self):
3318 # Check that the _ast module still works as expected
3319 code = 'x + 1'
3320 filename = '<string>'
3321 mode = 'eval'
3322
3323 # Create _ast.AST subclasses instances
3324 ast_tree = compile(code, filename, mode, flags=ast.PyCF_ONLY_AST)
3325
3326 # Call PyAST_Check()
3327 code = compile(ast_tree, filename, mode)
3328 self.assertIsInstance(code, types.CodeType)
3329
3330 def test_reload_module(self):
3331 # bpo-41194: Importing the _ast module twice must not crash.
3332 with support.swap_item(sys.modules, '_ast', None):
3333 del sys.modules['_ast']
3334 import _ast as ast1
3335
3336 del sys.modules['_ast']
3337 import _ast as ast2
3338
3339 self.check_ast_module()
3340
3341 # Unloading the two _ast module instances must not crash.
3342 del ast1
3343 del ast2
3344 support.gc_collect()
3345
3346 self.check_ast_module()
3347
3348 def test_sys_modules(self):
3349 # bpo-41631: Test reproducing a Mercurial crash when PyAST_Check()
3350 # imported the _ast module internally.
3351 lazy_mod = object()
3352
3353 def my_import(name, *args, **kw):
3354 sys.modules[name] = lazy_mod
3355 return lazy_mod
3356
3357 with support.swap_item(sys.modules, '_ast', None):
3358 del sys.modules['_ast']
3359
3360 with support.swap_attr(builtins, '__import__', my_import):
3361 # Test that compile() does not import the _ast module
3362 self.check_ast_module()
3363 self.assertNotIn('_ast', sys.modules)
3364
3365 # Sanity check of the test itself
3366 import _ast
3367 self.assertIs(_ast, lazy_mod)
3368
3369 def test_subinterpreter(self):
3370 # bpo-41631: Importing and using the _ast module in a subinterpreter
3371 # must not crash.
3372 code = dedent('''
3373 import _ast
3374 import ast
3375 import gc
3376 import sys
3377 import types
3378
3379 # Create _ast.AST subclasses instances and call PyAST_Check()
3380 ast_tree = compile('x+1', '<string>', 'eval',
3381 flags=ast.PyCF_ONLY_AST)
3382 code = compile(ast_tree, 'string', 'eval')
3383 if not isinstance(code, types.CodeType):
3384 raise AssertionError
3385
3386 # Unloading the _ast module must not crash.
3387 del ast, _ast
3388 del sys.modules['ast'], sys.modules['_ast']
3389 gc.collect()
3390 ''')
3391 res = support.run_in_subinterp(code)
3392 self.assertEqual(res, 0)
3393
3394
3395 class CommandLineTests(unittest.TestCase):
3396 def setUp(self):
3397 self.filename = tempfile.mktemp()
3398 self.addCleanup(os_helper.unlink, self.filename)
3399
3400 @staticmethod
3401 def text_normalize(string):
3402 return textwrap.dedent(string).strip()
3403
3404 def set_source(self, content):
3405 Path(self.filename).write_text(self.text_normalize(content))
3406
3407 def invoke_ast(self, *flags):
3408 stderr = StringIO()
3409 stdout = StringIO()
3410 with (
3411 contextlib.redirect_stdout(stdout),
3412 contextlib.redirect_stderr(stderr),
3413 ):
3414 ast.main(args=[*flags, self.filename])
3415 self.assertEqual(stderr.getvalue(), '')
3416 return stdout.getvalue().strip()
3417
3418 def check_output(self, source, expect, *flags):
3419 self.set_source(source)
3420 res = self.invoke_ast(*flags)
3421 expect = self.text_normalize(expect)
3422 self.assertEqual(res, expect)
3423
3424 @support.requires_resource('cpu')
3425 def test_invocation(self):
3426 # test various combinations of parameters
3427 base_flags = (
3428 ('-m=exec', '--mode=exec'),
3429 ('--no-type-comments', '--no-type-comments'),
3430 ('-a', '--include-attributes'),
3431 ('-i=4', '--indent=4'),
3432 ('--feature-version=3.13', '--feature-version=3.13'),
3433 ('-O=-1', '--optimize=-1'),
3434 ('--show-empty', '--show-empty'),
3435 )
3436 self.set_source('''
3437 print(1, 2, 3)
3438 def f(x: int) -> int:
3439 x -= 1
3440 return x
3441 ''')
3442
3443 for r in range(1, len(base_flags) + 1):
3444 for choices in itertools.combinations(base_flags, r=r):
3445 for args in itertools.product(*choices):
3446 with self.subTest(flags=args):
3447 self.invoke_ast(*args)
3448
3449 @support.force_not_colorized
3450 def test_help_message(self):
3451 for flag in ('-h', '--help', '--unknown'):
3452 with self.subTest(flag=flag):
3453 output = StringIO()
3454 with self.assertRaises(SystemExit):
3455 with contextlib.redirect_stderr(output):
3456 ast.main(args=flag)
3457 self.assertStartsWith(output.getvalue(), 'usage: ')
3458
3459 def test_exec_mode_flag(self):
3460 # test 'python -m ast -m/--mode exec'
3461 source = 'x: bool = 1 # type: ignore[assignment]'
3462 expect = '''
3463 Module(
3464 body=[
3465 AnnAssign(
3466 target=Name(id='x', ctx=Store()),
3467 annotation=Name(id='bool', ctx=Load()),
3468 value=Constant(value=1),
3469 simple=1)],
3470 type_ignores=[
3471 TypeIgnore(lineno=1, tag='[assignment]')])
3472 '''
3473 for flag in ('-m=exec', '--mode=exec'):
3474 with self.subTest(flag=flag):
3475 self.check_output(source, expect, flag)
3476
3477 def test_single_mode_flag(self):
3478 # test 'python -m ast -m/--mode single'
3479 source = 'pass'
3480 expect = '''
3481 Interactive(
3482 body=[
3483 Pass()])
3484 '''
3485 for flag in ('-m=single', '--mode=single'):
3486 with self.subTest(flag=flag):
3487 self.check_output(source, expect, flag)
3488
3489 def test_eval_mode_flag(self):
3490 # test 'python -m ast -m/--mode eval'
3491 source = 'print(1, 2, 3)'
3492 expect = '''
3493 Expression(
3494 body=Call(
3495 func=Name(id='print', ctx=Load()),
3496 args=[
3497 Constant(value=1),
3498 Constant(value=2),
3499 Constant(value=3)]))
3500 '''
3501 for flag in ('-m=eval', '--mode=eval'):
3502 with self.subTest(flag=flag):
3503 self.check_output(source, expect, flag)
3504
3505 def test_func_type_mode_flag(self):
3506 # test 'python -m ast -m/--mode func_type'
3507 source = '(int, str) -> list[int]'
3508 expect = '''
3509 FunctionType(
3510 argtypes=[
3511 Name(id='int', ctx=Load()),
3512 Name(id='str', ctx=Load())],
3513 returns=Subscript(
3514 value=Name(id='list', ctx=Load()),
3515 slice=Name(id='int', ctx=Load()),
3516 ctx=Load()))
3517 '''
3518 for flag in ('-m=func_type', '--mode=func_type'):
3519 with self.subTest(flag=flag):
3520 self.check_output(source, expect, flag)
3521
3522 def test_no_type_comments_flag(self):
3523 # test 'python -m ast --no-type-comments'
3524 source = 'x: bool = 1 # type: ignore[assignment]'
3525 expect = '''
3526 Module(
3527 body=[
3528 AnnAssign(
3529 target=Name(id='x', ctx=Store()),
3530 annotation=Name(id='bool', ctx=Load()),
3531 value=Constant(value=1),
3532 simple=1)])
3533 '''
3534 self.check_output(source, expect, '--no-type-comments')
3535
3536 def test_include_attributes_flag(self):
3537 # test 'python -m ast -a/--include-attributes'
3538 source = 'pass'
3539 expect = '''
3540 Module(
3541 body=[
3542 Pass(
3543 lineno=1,
3544 col_offset=0,
3545 end_lineno=1,
3546 end_col_offset=4)])
3547 '''
3548 for flag in ('-a', '--include-attributes'):
3549 with self.subTest(flag=flag):
3550 self.check_output(source, expect, flag)
3551
3552 def test_indent_flag(self):
3553 # test 'python -m ast -i/--indent 0'
3554 source = 'pass'
3555 expect = '''
3556 Module(
3557 body=[
3558 Pass()])
3559 '''
3560 for flag in ('-i=0', '--indent=0'):
3561 with self.subTest(flag=flag):
3562 self.check_output(source, expect, flag)
3563
3564 def test_feature_version_flag(self):
3565 # test 'python -m ast --feature-version 3.9/3.10'
3566 source = '''
3567 match x:
3568 case 1:
3569 pass
3570 '''
3571 expect = '''
3572 Module(
3573 body=[
3574 Match(
3575 subject=Name(id='x', ctx=Load()),
3576 cases=[
3577 match_case(
3578 pattern=MatchValue(
3579 value=Constant(value=1)),
3580 body=[
3581 Pass()])])])
3582 '''
3583 self.check_output(source, expect, '--feature-version=3.10')
3584 with self.assertRaises(SyntaxError):
3585 self.invoke_ast('--feature-version=3.9')
3586
3587 def test_no_optimize_flag(self):
3588 # test 'python -m ast -O/--optimize -1/0'
3589 source = '''
3590 match a:
3591 case 1+2j:
3592 pass
3593 '''
3594 expect = '''
3595 Module(
3596 body=[
3597 Match(
3598 subject=Name(id='a', ctx=Load()),
3599 cases=[
3600 match_case(
3601 pattern=MatchValue(
3602 value=BinOp(
3603 left=Constant(value=1),
3604 op=Add(),
3605 right=Constant(value=2j))),
3606 body=[
3607 Pass()])])])
3608 '''
3609 for flag in ('-O=-1', '--optimize=-1', '-O=0', '--optimize=0'):
3610 with self.subTest(flag=flag):
3611 self.check_output(source, expect, flag)
3612
3613 def test_optimize_flag(self):
3614 # test 'python -m ast -O/--optimize 1/2'
3615 source = '''
3616 match a:
3617 case 1+2j:
3618 pass
3619 '''
3620 expect = '''
3621 Module(
3622 body=[
3623 Match(
3624 subject=Name(id='a', ctx=Load()),
3625 cases=[
3626 match_case(
3627 pattern=MatchValue(
3628 value=Constant(value=(1+2j))),
3629 body=[
3630 Pass()])])])
3631 '''
3632 for flag in ('-O=1', '--optimize=1', '-O=2', '--optimize=2'):
3633 with self.subTest(flag=flag):
3634 self.check_output(source, expect, flag)
3635
3636 def test_show_empty_flag(self):
3637 # test 'python -m ast --show-empty'
3638 source = 'print(1, 2, 3)'
3639 expect = '''
3640 Module(
3641 body=[
3642 Expr(
3643 value=Call(
3644 func=Name(id='print', ctx=Load()),
3645 args=[
3646 Constant(value=1),
3647 Constant(value=2),
3648 Constant(value=3)],
3649 keywords=[]))],
3650 type_ignores=[])
3651 '''
3652 self.check_output(source, expect, '--show-empty')
3653
3654
3655 class ASTOptimizationTests(unittest.TestCase):
3656 def wrap_expr(self, expr):
3657 return ast.Module(body=[ast.Expr(value=expr)])
3658
3659 def wrap_statement(self, statement):
3660 return ast.Module(body=[statement])
3661
3662 def assert_ast(self, code, non_optimized_target, optimized_target):
3663 non_optimized_tree = ast.parse(code, optimize=-1)
3664 optimized_tree = ast.parse(code, optimize=1)
3665
3666 # Is a non-optimized tree equal to a non-optimized target?
3667 self.assertTrue(
3668 ast.compare(non_optimized_tree, non_optimized_target),
3669 f"{ast.dump(non_optimized_target)} must equal "
3670 f"{ast.dump(non_optimized_tree)}",
3671 )
3672
3673 # Is a optimized tree equal to a non-optimized target?
3674 self.assertFalse(
3675 ast.compare(optimized_tree, non_optimized_target),
3676 f"{ast.dump(non_optimized_target)} must not equal "
3677 f"{ast.dump(non_optimized_tree)}"
3678 )
3679
3680 # Is a optimized tree is equal to an optimized target?
3681 self.assertTrue(
3682 ast.compare(optimized_tree, optimized_target),
3683 f"{ast.dump(optimized_target)} must equal "
3684 f"{ast.dump(optimized_tree)}",
3685 )
3686
3687 def test_folding_format(self):
3688 code = "'%s' % (a,)"
3689
3690 non_optimized_target = self.wrap_expr(
3691 ast.BinOp(
3692 left=ast.Constant(value="%s"),
3693 op=ast.Mod(),
3694 right=ast.Tuple(elts=[ast.Name(id='a')]))
3695 )
3696 optimized_target = self.wrap_expr(
3697 ast.JoinedStr(
3698 values=[
3699 ast.FormattedValue(value=ast.Name(id='a'), conversion=115)
3700 ]
3701 )
3702 )
3703
3704 self.assert_ast(code, non_optimized_target, optimized_target)
3705
3706 def test_folding_match_case_allowed_expressions(self):
3707 def get_match_case_values(node):
3708 result = []
3709 if isinstance(node, ast.Constant):
3710 result.append(node.value)
3711 elif isinstance(node, ast.MatchValue):
3712 result.extend(get_match_case_values(node.value))
3713 elif isinstance(node, ast.MatchMapping):
3714 for key in node.keys:
3715 result.extend(get_match_case_values(key))
3716 elif isinstance(node, ast.MatchSequence):
3717 for pat in node.patterns:
3718 result.extend(get_match_case_values(pat))
3719 else:
3720 self.fail(f"Unexpected node {node}")
3721 return result
3722
3723 tests = [
3724 ("-0", [0]),
3725 ("-0.1", [-0.1]),
3726 ("-0j", [complex(0, 0)]),
3727 ("-0.1j", [complex(0, -0.1)]),
3728 ("1 + 2j", [complex(1, 2)]),
3729 ("1 - 2j", [complex(1, -2)]),
3730 ("1.1 + 2.1j", [complex(1.1, 2.1)]),
3731 ("1.1 - 2.1j", [complex(1.1, -2.1)]),
3732 ("-0 + 1j", [complex(0, 1)]),
3733 ("-0 - 1j", [complex(0, -1)]),
3734 ("-0.1 + 1.1j", [complex(-0.1, 1.1)]),
3735 ("-0.1 - 1.1j", [complex(-0.1, -1.1)]),
3736 ("{-0: 0}", [0]),
3737 ("{-0.1: 0}", [-0.1]),
3738 ("{-0j: 0}", [complex(0, 0)]),
3739 ("{-0.1j: 0}", [complex(0, -0.1)]),
3740 ("{1 + 2j: 0}", [complex(1, 2)]),
3741 ("{1 - 2j: 0}", [complex(1, -2)]),
3742 ("{1.1 + 2.1j: 0}", [complex(1.1, 2.1)]),
3743 ("{1.1 - 2.1j: 0}", [complex(1.1, -2.1)]),
3744 ("{-0 + 1j: 0}", [complex(0, 1)]),
3745 ("{-0 - 1j: 0}", [complex(0, -1)]),
3746 ("{-0.1 + 1.1j: 0}", [complex(-0.1, 1.1)]),
3747 ("{-0.1 - 1.1j: 0}", [complex(-0.1, -1.1)]),
3748 ("{-0: 0, 0 + 1j: 0, 0.1 + 1j: 0}", [0, complex(0, 1), complex(0.1, 1)]),
3749 ("[-0, -0.1, -0j, -0.1j]", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3750 ("[[[[-0, -0.1, -0j, -0.1j]]]]", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3751 ("[[-0, -0.1], -0j, -0.1j]", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3752 ("[[-0, -0.1], [-0j, -0.1j]]", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3753 ("(-0, -0.1, -0j, -0.1j)", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3754 ("((((-0, -0.1, -0j, -0.1j))))", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3755 ("((-0, -0.1), -0j, -0.1j)", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3756 ("((-0, -0.1), (-0j, -0.1j))", [0, -0.1, complex(0, 0), complex(0, -0.1)]),
3757 ]
3758 for match_expr, constants in tests:
3759 with self.subTest(match_expr):
3760 src = f"match 0:\n\t case {match_expr}: pass"
3761 tree = ast.parse(src, optimize=1)
3762 match_stmt = tree.body[0]
3763 case = match_stmt.cases[0]
3764 values = get_match_case_values(case.pattern)
3765 self.assertListEqual(constants, values)
3766
3767 def test_match_case_not_folded_in_unoptimized_ast(self):
3768 src = textwrap.dedent("""
3769 match a:
3770 case 1+2j:
3771 pass
3772 """)
3773
3774 unfolded = "MatchValue(value=BinOp(left=Constant(value=1), op=Add(), right=Constant(value=2j))"
3775 folded = "MatchValue(value=Constant(value=(1+2j)))"
3776 for optval in (0, 1, 2):
3777 self.assertIn(folded if optval else unfolded, ast.dump(ast.parse(src, optimize=optval)))
3778
3779
3780 if __name__ == '__main__':
3781 if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
3782 ast_repr_update_snapshots()
3783 sys.exit(0)
3784 unittest.main()