Skip to content

Commit 03017a8

Browse files
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 <kirill.bast9@mail.ru>
1 parent 28937d3 commit 03017a8

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_snippets(self):
131131
(eval_tests, eval_results, "eval")):
132132
for i, o in zip(input, output):
133133
with self.subTest(action="parsing", input=i):
134-
ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
134+
ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST, optimize=False)
135135
self.assertEqual(to_tuple(ast_tree), o)
136136
self._assertTrueorder(ast_tree, (0, 0))
137137
with self.subTest(action="compiling", input=i, kind=kind):
@@ -141,7 +141,7 @@ def test_ast_validation(self):
141141
# compile() is the only function that calls PyAST_Validate
142142
snippets_to_validate = exec_tests + single_tests + eval_tests
143143
for snippet in snippets_to_validate:
144-
tree = ast.parse(snippet)
144+
tree = ast.parse(snippet, optimize=False)
145145
compile(tree, '<string>', 'exec')
146146

147147
def test_parse_invalid_ast(self):
@@ -923,7 +923,7 @@ def test_repr(self) -> None:
923923
snapshots = AST_REPR_DATA_FILE.read_text().split("\n")
924924
for test, snapshot in zip(ast_repr_get_test_cases(), snapshots, strict=True):
925925
with self.subTest(test_input=test):
926-
self.assertEqual(repr(ast.parse(test)), snapshot)
926+
self.assertEqual(repr(ast.parse(test, optimize=False)), snapshot)
927927

928928
def test_repr_large_input_crash(self):
929929
# gh-125010: Fix use-after-free in ast repr()
@@ -1698,22 +1698,22 @@ def test_iter_child_nodes(self):
16981698
)
16991699

17001700
def test_get_docstring(self):
1701-
node = ast.parse('"""line one\n line two"""')
1701+
node = ast.parse('"""line one\n line two"""', optimize=False)
17021702
self.assertEqual(ast.get_docstring(node),
17031703
'line one\nline two')
17041704

1705-
node = ast.parse('class foo:\n """line one\n line two"""')
1705+
node = ast.parse('class foo:\n """line one\n line two"""', optimize=False)
17061706
self.assertEqual(ast.get_docstring(node.body[0]),
17071707
'line one\nline two')
17081708

1709-
node = ast.parse('def foo():\n """line one\n line two"""')
1709+
node = ast.parse('def foo():\n """line one\n line two"""', optimize=False)
17101710
self.assertEqual(ast.get_docstring(node.body[0]),
17111711
'line one\nline two')
17121712

1713-
node = ast.parse('async def foo():\n """spam\n ham"""')
1713+
node = ast.parse('async def foo():\n """spam\n ham"""', optimize=False)
17141714
self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
17151715

1716-
node = ast.parse('async def foo():\n """spam\n ham"""')
1716+
node = ast.parse('async def foo():\n """spam\n ham"""', optimize=False)
17171717
self.assertEqual(ast.get_docstring(node.body[0], clean=False), 'spam\n ham')
17181718

17191719
node = ast.parse('x')
@@ -1752,7 +1752,8 @@ def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
17521752
'def foo():\n """line one\n line two"""\n\n'
17531753
' def bar():\n """line one\n line two"""\n'
17541754
' """line one\n line two"""\n'
1755-
'"""line one\nline two"""\n\n'
1755+
'"""line one\nline two"""\n\n',
1756+
optimize=False
17561757
)
17571758
self.assertEqual(node.body[0].col_offset, 0)
17581759
self.assertEqual(node.body[0].lineno, 1)
@@ -2321,9 +2322,9 @@ def test_stdlib_validates(self):
23212322
fn = os.path.join(STDLIB, module)
23222323
with open(fn, "r", encoding="utf-8") as fp:
23232324
source = fp.read()
2324-
mod = ast.parse(source, fn)
2325+
mod = ast.parse(source, fn, optimize=False)
23252326
compile(mod, fn, "exec")
2326-
mod2 = ast.parse(source, fn)
2327+
mod2 = ast.parse(source, fn, optimize=False)
23272328
self.assertTrue(ast.compare(mod, mod2))
23282329

23292330
constant_1 = ast.Constant(1)
@@ -2537,7 +2538,7 @@ def test_assign_to_constant(self):
25372538
"to in Store context")
25382539

25392540
def test_get_docstring(self):
2540-
tree = ast.parse("'docstring'\nx = 1")
2541+
tree = ast.parse("'docstring'\nx = 1", optimize=False)
25412542
self.assertEqual(ast.get_docstring(tree), 'docstring')
25422543

25432544
def get_load_const(self, tree):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy