Skip to content

bpo-41631: Check that importing _ast returns the right module #21973

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,22 @@ def visit_Ellipsis(self, node):
])


class ModuleReplacementTests(unittest.TestCase):
# bpo-41631
# Check that compile(..., ast.PyCF_ONLY_AST) fails if _ast is tampered with
def test_replaced_ast_module(self):
old_ast = sys.modules['_ast']
try:
for replacement in None, 'a string', ast:
with self.subTest(replacement=replacement):
sys.modules['_ast'] = replacement
with self.assertRaises(SystemError):
compile('1+1', '<string>', 'eval',
flags=ast.PyCF_ONLY_AST)
finally:
sys.modules['_ast'] = old_ast


def main():
if __name__ != '__main__':
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
If the _ast module is unavailable or replaced with an unexpected object,
compiling to AST now raises an exception.
25 changes: 25 additions & 0 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,8 @@ def generate_module_def(f, mod):
return (astmodulestate*)state;
}

static struct PyModuleDef _astmodule;

static astmodulestate*
get_global_ast_state(void)
{
Expand All @@ -1400,6 +1402,29 @@ def generate_module_def(f, mod):
return NULL;
}
}
if (!PyModule_Check(module)) {
PyErr_SetString(
PyExc_SystemError,
"Non-module object imported as _ast");
return NULL;
}

// testing "pegen" requires generating a C extension module, which contains
// a copy of the symbols defined in Python-ast.c and needs to be treated
// as the _ast module.
#ifndef AST_SKIP_MODULE_CHECK

struct PyModuleDef *def = PyModule_GetDef(module);
if (def != &_astmodule) {
if (!PyErr_Occurred()) {
PyErr_SetString(
PyExc_SystemError,
"Unexpected module imported as _ast");
}
return NULL;
}
#endif

astmodulestate *state = get_ast_state(module);
Py_DECREF(module);
return state;
Expand Down
25 changes: 25 additions & 0 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Tools/peg_generator/pegen/ast_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
because testing pegen requires generating a C extension module, which contains
a copy of the symbols defined in Python-ast.c. Thus, the isinstance check would
always fail. We rely on string comparison of the base classes instead.
TODO: Remove the above-described hack.
TODO: Remove the above-described hack, along with AST_SKIP_MODULE_CHECK
elsewhere.
"""


Expand Down
6 changes: 6 additions & 0 deletions Tools/peg_generator/pegen/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def compile_c_extension(
extra_link_args = get_extra_flags("LDFLAGS", "PY_LDFLAGS_NODIST")
if keep_asserts:
extra_compile_args.append("-UNDEBUG")

# testing pegen requires generating C extension modules, which contains
# a copy of the symbols defined in Python-ast.c and needs to be treated
# as the _ast module. See ast_dump.py.
extra_compile_args.append("-DAST_SKIP_MODULE_CHECK")

extension = [
Extension(
extension_name,
Expand Down