mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Add sys.set_lazy_imports_filter
This commit is contained in:
parent
9078f571e4
commit
f67310c9b9
2 changed files with 146 additions and 1 deletions
88
Python/clinic/sysmodule.c.h
generated
88
Python/clinic/sysmodule.c.h
generated
|
|
@ -1821,6 +1821,92 @@ exit:
|
|||
return return_value;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sys_set_lazy_imports_filter__doc__,
|
||||
"set_lazy_imports_filter($module, /, filter)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Set the lazy imports filter callback.\n"
|
||||
"\n"
|
||||
"The filter is a callable which disables lazy imports when they\n"
|
||||
"would otherwise be enabled. Returns True if the import is still enabled\n"
|
||||
"or False to disable it. The callable is called with:\n"
|
||||
"\n"
|
||||
"(importing_module_name, imported_module_name, [fromlist])\n"
|
||||
"\n"
|
||||
"Pass None to clear the filter.");
|
||||
|
||||
#define SYS_SET_LAZY_IMPORTS_FILTER_METHODDEF \
|
||||
{"set_lazy_imports_filter", _PyCFunction_CAST(sys_set_lazy_imports_filter), METH_FASTCALL|METH_KEYWORDS, sys_set_lazy_imports_filter__doc__},
|
||||
|
||||
static PyObject *
|
||||
sys_set_lazy_imports_filter_impl(PyObject *module, PyObject *filter);
|
||||
|
||||
static PyObject *
|
||||
sys_set_lazy_imports_filter(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
|
||||
|
||||
#define NUM_KEYWORDS 1
|
||||
static struct {
|
||||
PyGC_Head _this_is_not_used;
|
||||
PyObject_VAR_HEAD
|
||||
Py_hash_t ob_hash;
|
||||
PyObject *ob_item[NUM_KEYWORDS];
|
||||
} _kwtuple = {
|
||||
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
|
||||
.ob_hash = -1,
|
||||
.ob_item = { &_Py_ID(filter), },
|
||||
};
|
||||
#undef NUM_KEYWORDS
|
||||
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
|
||||
|
||||
#else // !Py_BUILD_CORE
|
||||
# define KWTUPLE NULL
|
||||
#endif // !Py_BUILD_CORE
|
||||
|
||||
static const char * const _keywords[] = {"filter", NULL};
|
||||
static _PyArg_Parser _parser = {
|
||||
.keywords = _keywords,
|
||||
.fname = "set_lazy_imports_filter",
|
||||
.kwtuple = KWTUPLE,
|
||||
};
|
||||
#undef KWTUPLE
|
||||
PyObject *argsbuf[1];
|
||||
PyObject *filter;
|
||||
|
||||
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
|
||||
/*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
|
||||
if (!args) {
|
||||
goto exit;
|
||||
}
|
||||
filter = args[0];
|
||||
return_value = sys_set_lazy_imports_filter_impl(module, filter);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sys_get_lazy_imports_filter__doc__,
|
||||
"get_lazy_imports_filter($module, /)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Get the current lazy imports filter callback.\n"
|
||||
"\n"
|
||||
"Returns the filter callable or None if no filter is set.");
|
||||
|
||||
#define SYS_GET_LAZY_IMPORTS_FILTER_METHODDEF \
|
||||
{"get_lazy_imports_filter", (PyCFunction)sys_get_lazy_imports_filter, METH_NOARGS, sys_get_lazy_imports_filter__doc__},
|
||||
|
||||
static PyObject *
|
||||
sys_get_lazy_imports_filter_impl(PyObject *module);
|
||||
|
||||
static PyObject *
|
||||
sys_get_lazy_imports_filter(PyObject *module, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
return sys_get_lazy_imports_filter_impl(module);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(_jit_is_available__doc__,
|
||||
"is_available($module, /)\n"
|
||||
"--\n"
|
||||
|
|
@ -1948,4 +2034,4 @@ exit:
|
|||
#ifndef SYS_GETANDROIDAPILEVEL_METHODDEF
|
||||
#define SYS_GETANDROIDAPILEVEL_METHODDEF
|
||||
#endif /* !defined(SYS_GETANDROIDAPILEVEL_METHODDEF) */
|
||||
/*[clinic end generated code: output=449d16326e69dcf6 input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=64c56362026c8fcf input=a9049054013a1b77]*/
|
||||
|
|
|
|||
|
|
@ -2772,6 +2772,63 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
sys.set_lazy_imports_filter
|
||||
|
||||
filter: object
|
||||
|
||||
Set the lazy imports filter callback.
|
||||
|
||||
The filter is a callable which disables lazy imports when they
|
||||
would otherwise be enabled. Returns True if the import is still enabled
|
||||
or False to disable it. The callable is called with:
|
||||
|
||||
(importing_module_name, imported_module_name, [fromlist])
|
||||
|
||||
Pass None to clear the filter.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
sys_set_lazy_imports_filter_impl(PyObject *module, PyObject *filter)
|
||||
/*[clinic end generated code: output=10251d49469c278c input=2eb48786bdd4ee42]*/
|
||||
{
|
||||
PyObject *current_filter = NULL;
|
||||
if (filter == Py_None) {
|
||||
current_filter = NULL;
|
||||
}
|
||||
else if (!PyCallable_Check(filter)) {
|
||||
PyErr_SetString(PyExc_TypeError, "filter must be callable or None");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
current_filter = filter;
|
||||
}
|
||||
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
Py_XSETREF(interp->imports.lazy_imports_filter, Py_XNewRef(current_filter));
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
sys.get_lazy_imports_filter
|
||||
|
||||
Get the current lazy imports filter callback.
|
||||
|
||||
Returns the filter callable or None if no filter is set.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
sys_get_lazy_imports_filter_impl(PyObject *module)
|
||||
/*[clinic end generated code: output=3bf73022892165af input=cf1e07cb8e203c94]*/
|
||||
{
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
PyObject *filter = interp->imports.lazy_imports_filter;
|
||||
if (filter == NULL) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
return Py_NewRef(filter);
|
||||
}
|
||||
|
||||
|
||||
static PyMethodDef sys_methods[] = {
|
||||
/* Might as well keep this in alphabetic order */
|
||||
|
|
@ -2837,6 +2894,8 @@ static PyMethodDef sys_methods[] = {
|
|||
SYS_UNRAISABLEHOOK_METHODDEF
|
||||
SYS_GET_INT_MAX_STR_DIGITS_METHODDEF
|
||||
SYS_SET_INT_MAX_STR_DIGITS_METHODDEF
|
||||
SYS_GET_LAZY_IMPORTS_FILTER_METHODDEF
|
||||
SYS_SET_LAZY_IMPORTS_FILTER_METHODDEF
|
||||
SYS__BASEREPL_METHODDEF
|
||||
#ifdef Py_STATS
|
||||
SYS__STATS_ON_METHODDEF
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue