mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
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:
parent
63548b3699
commit
d8e6bdc0d0
47 changed files with 390 additions and 115 deletions
|
|
@ -751,6 +751,7 @@ compile as builtin_compile
|
|||
dont_inherit: bool = False
|
||||
optimize: int = -1
|
||||
*
|
||||
module as modname: object = None
|
||||
_feature_version as feature_version: int = -1
|
||||
|
||||
Compile source into a code object that can be executed by exec() or eval().
|
||||
|
|
@ -770,8 +771,8 @@ in addition to any features explicitly specified.
|
|||
static PyObject *
|
||||
builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
||||
const char *mode, int flags, int dont_inherit,
|
||||
int optimize, int feature_version)
|
||||
/*[clinic end generated code: output=b0c09c84f116d3d7 input=8f0069edbdac381b]*/
|
||||
int optimize, PyObject *modname, int feature_version)
|
||||
/*[clinic end generated code: output=9a0dce1945917a86 input=ddeae1e0253459dc]*/
|
||||
{
|
||||
PyObject *source_copy;
|
||||
const char *str;
|
||||
|
|
@ -800,6 +801,15 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
"compile(): invalid optimize value");
|
||||
goto error;
|
||||
}
|
||||
if (modname == Py_None) {
|
||||
modname = NULL;
|
||||
}
|
||||
else if (!PyUnicode_Check(modname)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"compile() argument 'module' must be str or None, not %T",
|
||||
modname);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!dont_inherit) {
|
||||
PyEval_MergeCompilerFlags(&cf);
|
||||
|
|
@ -845,8 +855,9 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
goto error;
|
||||
}
|
||||
int syntax_check_only = ((flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
|
||||
if (_PyCompile_AstPreprocess(mod, filename, &cf, optimize,
|
||||
arena, syntax_check_only) < 0) {
|
||||
if (_PyCompile_AstPreprocess(mod, filename, &cf, optimize, arena,
|
||||
syntax_check_only, modname) < 0)
|
||||
{
|
||||
_PyArena_Free(arena);
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -859,7 +870,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
goto error;
|
||||
}
|
||||
result = (PyObject*)_PyAST_Compile(mod, filename,
|
||||
&cf, optimize, arena);
|
||||
&cf, optimize, arena, modname);
|
||||
}
|
||||
_PyArena_Free(arena);
|
||||
goto finally;
|
||||
|
|
@ -877,7 +888,9 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
tstate->suppress_co_const_immortalization++;
|
||||
#endif
|
||||
|
||||
result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
|
||||
result = _Py_CompileStringObjectWithModule(str, filename,
|
||||
start[compile_mode], &cf,
|
||||
optimize, modname);
|
||||
|
||||
#ifdef Py_GIL_DISABLED
|
||||
tstate->suppress_co_const_immortalization--;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue