gh-135801: Add the module parameter to compile() etc (GH-139652)

Many functions related to compiling or parsing Python code, such as
compile(), ast.parse(), symtable.symtable(),
and importlib.abc.InspectLoader.source_to_code() now allow to pass
the module name used when filtering syntax warnings.
This commit is contained in:
Serhiy Storchaka 2025-11-13 13:21:32 +02:00 committed by GitHub
parent 63548b3699
commit d8e6bdc0d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 390 additions and 115 deletions

View file

@ -1252,12 +1252,19 @@ _PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
} else {
name = &_Py_STR(anon_string);
}
PyObject *module = NULL;
if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
goto done;
}
mod = _PyParser_ASTFromString(str, name, start, flags, arena);
mod = _PyParser_ASTFromString(str, name, start, flags, arena, module);
Py_XDECREF(module);
if (mod != NULL) {
if (mod != NULL) {
ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
}
done:
Py_XDECREF(source);
_PyArena_Free(arena);
return ret;
@ -1407,8 +1414,17 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
return NULL;
}
}
PyObject *module = NULL;
if (globals && PyDict_GetItemStringRef(globals, "__name__", &module) < 0) {
if (interactive_src) {
Py_DECREF(interactive_filename);
}
return NULL;
}
PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1, arena);
PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1,
arena, module);
Py_XDECREF(module);
if (co == NULL) {
if (interactive_src) {
Py_DECREF(interactive_filename);
@ -1507,6 +1523,14 @@ run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
PyObject *
Py_CompileStringObject(const char *str, PyObject *filename, int start,
PyCompilerFlags *flags, int optimize)
{
return _Py_CompileStringObjectWithModule(str, filename, start,
flags, optimize, NULL);
}
PyObject *
_Py_CompileStringObjectWithModule(const char *str, PyObject *filename, int start,
PyCompilerFlags *flags, int optimize, PyObject *module)
{
PyCodeObject *co;
mod_ty mod;
@ -1514,14 +1538,16 @@ Py_CompileStringObject(const char *str, PyObject *filename, int start,
if (arena == NULL)
return NULL;
mod = _PyParser_ASTFromString(str, filename, start, flags, arena);
mod = _PyParser_ASTFromString(str, filename, start, flags, arena, module);
if (mod == NULL) {
_PyArena_Free(arena);
return NULL;
}
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena, syntax_check_only) < 0) {
if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena,
syntax_check_only, module) < 0)
{
_PyArena_Free(arena);
return NULL;
}
@ -1529,7 +1555,7 @@ Py_CompileStringObject(const char *str, PyObject *filename, int start,
_PyArena_Free(arena);
return result;
}
co = _PyAST_Compile(mod, filename, flags, optimize, arena);
co = _PyAST_Compile(mod, filename, flags, optimize, arena, module);
_PyArena_Free(arena);
return (PyObject *)co;
}