summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_ast.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-08-21 16:31:30 (GMT)
committerGitHub <noreply@github.com>2023-08-21 16:31:30 (GMT)
commit10a91d7e98d847b05292eab828ff9ae51308d3ee (patch)
treea81f95a971c03710f13217cf89e0f12532341af8 /Lib/test/test_ast.py
parent47022a079eb9d2a2af781abae3de4a71f80247c2 (diff)
downloadcpython-10a91d7e98d847b05292eab828ff9ae51308d3ee.zip
cpython-10a91d7e98d847b05292eab828ff9ae51308d3ee.tar.gz
cpython-10a91d7e98d847b05292eab828ff9ae51308d3ee.tar.bz2
gh-108113: Make it possible to create an optimized AST (#108154)
Diffstat (limited to 'Lib/test/test_ast.py')
-rw-r--r--Lib/test/test_ast.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 5346b39..f3c7229 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -357,6 +357,34 @@ class AST_Tests(unittest.TestCase):
tree = ast.parse(snippet)
compile(tree, '<string>', 'exec')
+ def test_optimization_levels__debug__(self):
+ 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)
+
+ def test_optimization_levels_const_folding(self):
+ folded = ('Expr', (1, 0, 1, 5), ('Constant', (1, 0, 1, 5), 3, None))
+ not_folded = ('Expr', (1, 0, 1, 5),
+ ('BinOp', (1, 0, 1, 5),
+ ('Constant', (1, 0, 1, 1), 1, None),
+ ('Add',),
+ ('Constant', (1, 4, 1, 5), 2, None)))
+
+ 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)
+
def test_invalid_position_information(self):
invalid_linenos = [
(10, 1), (-10, -11), (10, -11), (-5, -2), (-5, 1)