summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2025-11-13 11:21:32 (GMT)
committerGitHub <noreply@github.com>2025-11-13 11:21:32 (GMT)
commitd8e6bdc0d083f4e76ac49574544555ad91257592 (patch)
tree28add209953f7d2454cf0ebc262812922b0672d2 /Python/pythonrun.c
parent63548b36998e7f7cd5c7c28b53b348a93f836737 (diff)
downloadcpython-d8e6bdc0d083f4e76ac49574544555ad91257592.zip
cpython-d8e6bdc0d083f4e76ac49574544555ad91257592.tar.gz
cpython-d8e6bdc0d083f4e76ac49574544555ad91257592.tar.bz2
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.
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 45211e1..49ce0a9 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -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);
@@ -1508,20 +1524,30 @@ 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;
PyArena *arena = _PyArena_New();
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;
}