Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=0)
self.assertEqual(to_tuple(ast_tree), o)
self._assertTrueorder(ast_tree, (0, 0))
with self.subTest(action="compiling", input=i, kind=kind):
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
Loading