summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_ast.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-08-23 08:01:17 (GMT)
committerGitHub <noreply@github.com>2023-08-23 08:01:17 (GMT)
commit2dfbd4f36dd83f88f5df64c33612dd34eff256bb (patch)
treefca70758a85ee8f31122ec1f9cd882b425d5cf6b /Lib/test/test_ast.py
parent79fdacc0059a3959074d2d9d054653eae1dcfe06 (diff)
downloadcpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.zip
cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.tar.gz
cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.tar.bz2
gh-108113: Make it possible to optimize an AST (#108282)
Diffstat (limited to 'Lib/test/test_ast.py')
-rw-r--r--Lib/test/test_ast.py26
1 files changed, 15 insertions, 11 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index f3c7229..68de4d6 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -361,14 +361,16 @@ class AST_Tests(unittest.TestCase):
cases = [(-1, '__debug__'), (0, '__debug__'), (1, False), (2, False)]
for (optval, expected) in cases:
with self.subTest(optval=optval, expected=expected):
- res = ast.parse("__debug__", optimize=optval)
- self.assertIsInstance(res.body[0], ast.Expr)
- if isinstance(expected, bool):
- self.assertIsInstance(res.body[0].value, ast.Constant)
- self.assertEqual(res.body[0].value.value, expected)
- else:
- self.assertIsInstance(res.body[0].value, ast.Name)
- self.assertEqual(res.body[0].value.id, expected)
+ res1 = ast.parse("__debug__", optimize=optval)
+ res2 = ast.parse(ast.parse("__debug__"), optimize=optval)
+ for res in [res1, res2]:
+ self.assertIsInstance(res.body[0], ast.Expr)
+ if isinstance(expected, bool):
+ self.assertIsInstance(res.body[0].value, ast.Constant)
+ self.assertEqual(res.body[0].value.value, expected)
+ else:
+ self.assertIsInstance(res.body[0].value, ast.Name)
+ self.assertEqual(res.body[0].value.id, expected)
def test_optimization_levels_const_folding(self):
folded = ('Expr', (1, 0, 1, 5), ('Constant', (1, 0, 1, 5), 3, None))
@@ -381,9 +383,11 @@ class AST_Tests(unittest.TestCase):
cases = [(-1, not_folded), (0, not_folded), (1, folded), (2, folded)]
for (optval, expected) in cases:
with self.subTest(optval=optval):
- tree = ast.parse("1 + 2", optimize=optval)
- res = to_tuple(tree.body[0])
- self.assertEqual(res, expected)
+ tree1 = ast.parse("1 + 2", optimize=optval)
+ tree2 = ast.parse(ast.parse("1 + 2"), optimize=optval)
+ for tree in [tree1, tree2]:
+ res = to_tuple(tree.body[0])
+ self.assertEqual(res, expected)
def test_invalid_position_information(self):
invalid_linenos = [