https://github.com/python/cpython/commit/03017a8cc2242d881a3042b1eb9084c9bae9f85d
commit: 03017a8cc2242d881a3042b1eb9084c9bae9f85d
branch: main
author: Mikhail Efimov <[email protected]>
committer: Eclips4 <[email protected]>
date: 2025-07-18T19:07:46+03:00
summary:

gh-136438: Make sure `test_ast` pass with all optimization levels (#136596)

Explicitly pass an `optimizer` parameter to the calls of `ast.parse/compile`, 
because if it is not provided, the interpreter will use its internal state, 
which can be modified using the `-O` or `-OO` flags.

Co-authored-by: Kirill Podoprigora <[email protected]>

files:
M Lib/test/test_ast/test_ast.py

diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py
index cc46529c0ef105..38d3a5adc5f01f 100644
--- a/Lib/test/test_ast/test_ast.py
+++ b/Lib/test/test_ast/test_ast.py
@@ -131,7 +131,7 @@ def test_snippets(self):
                                     (eval_tests, eval_results, "eval")):
             for i, o in zip(input, output):
                 with self.subTest(action="parsing", input=i):
-                    ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
+                    ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST, 
optimize=False)
                     self.assertEqual(to_tuple(ast_tree), o)
                     self._assertTrueorder(ast_tree, (0, 0))
                 with self.subTest(action="compiling", input=i, kind=kind):
@@ -141,7 +141,7 @@ def test_ast_validation(self):
         # compile() is the only function that calls PyAST_Validate
         snippets_to_validate = exec_tests + single_tests + eval_tests
         for snippet in snippets_to_validate:
-            tree = ast.parse(snippet)
+            tree = ast.parse(snippet, optimize=False)
             compile(tree, '<string>', 'exec')
 
     def test_parse_invalid_ast(self):
@@ -923,7 +923,7 @@ def test_repr(self) -> None:
         snapshots = AST_REPR_DATA_FILE.read_text().split("\n")
         for test, snapshot in zip(ast_repr_get_test_cases(), snapshots, 
strict=True):
             with self.subTest(test_input=test):
-                self.assertEqual(repr(ast.parse(test)), snapshot)
+                self.assertEqual(repr(ast.parse(test, optimize=False)), 
snapshot)
 
     def test_repr_large_input_crash(self):
         # gh-125010: Fix use-after-free in ast repr()
@@ -1698,22 +1698,22 @@ def test_iter_child_nodes(self):
         )
 
     def test_get_docstring(self):
-        node = ast.parse('"""line one\n  line two"""')
+        node = ast.parse('"""line one\n  line two"""', optimize=False)
         self.assertEqual(ast.get_docstring(node),
                          'line one\nline two')
 
-        node = ast.parse('class foo:\n  """line one\n  line two"""')
+        node = ast.parse('class foo:\n  """line one\n  line two"""', 
optimize=False)
         self.assertEqual(ast.get_docstring(node.body[0]),
                          'line one\nline two')
 
-        node = ast.parse('def foo():\n  """line one\n  line two"""')
+        node = ast.parse('def foo():\n  """line one\n  line two"""', 
optimize=False)
         self.assertEqual(ast.get_docstring(node.body[0]),
                          'line one\nline two')
 
-        node = ast.parse('async def foo():\n  """spam\n  ham"""')
+        node = ast.parse('async def foo():\n  """spam\n  ham"""', 
optimize=False)
         self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
 
-        node = ast.parse('async def foo():\n  """spam\n  ham"""')
+        node = ast.parse('async def foo():\n  """spam\n  ham"""', 
optimize=False)
         self.assertEqual(ast.get_docstring(node.body[0], clean=False), 'spam\n 
 ham')
 
         node = ast.parse('x')
@@ -1752,7 +1752,8 @@ def 
test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
             'def foo():\n  """line one\n  line two"""\n\n'
             '  def bar():\n    """line one\n    line two"""\n'
             '  """line one\n  line two"""\n'
-            '"""line one\nline two"""\n\n'
+            '"""line one\nline two"""\n\n',
+            optimize=False
         )
         self.assertEqual(node.body[0].col_offset, 0)
         self.assertEqual(node.body[0].lineno, 1)
@@ -2321,9 +2322,9 @@ def test_stdlib_validates(self):
                 fn = os.path.join(STDLIB, module)
                 with open(fn, "r", encoding="utf-8") as fp:
                     source = fp.read()
-                mod = ast.parse(source, fn)
+                mod = ast.parse(source, fn, optimize=False)
                 compile(mod, fn, "exec")
-                mod2 = ast.parse(source, fn)
+                mod2 = ast.parse(source, fn, optimize=False)
                 self.assertTrue(ast.compare(mod, mod2))
 
     constant_1 = ast.Constant(1)
@@ -2537,7 +2538,7 @@ def test_assign_to_constant(self):
                          "to in Store context")
 
     def test_get_docstring(self):
-        tree = ast.parse("'docstring'\nx = 1")
+        tree = ast.parse("'docstring'\nx = 1", optimize=False)
         self.assertEqual(ast.get_docstring(tree), 'docstring')
 
     def get_load_const(self, tree):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to