@@ -2772,6 +2772,63 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
27722772 return 0 ;
27732773}
27742774
2775+ /*[clinic input]
2776+ sys.set_lazy_imports_filter
2777+
2778+ filter: object
2779+
2780+ Set the lazy imports filter callback.
2781+
2782+ The filter is a callable which disables lazy imports when they
2783+ would otherwise be enabled. Returns True if the import is still enabled
2784+ or False to disable it. The callable is called with:
2785+
2786+ (importing_module_name, imported_module_name, [fromlist])
2787+
2788+ Pass None to clear the filter.
2789+ [clinic start generated code]*/
2790+
2791+ static PyObject *
2792+ sys_set_lazy_imports_filter_impl (PyObject * module , PyObject * filter )
2793+ /*[clinic end generated code: output=10251d49469c278c input=2eb48786bdd4ee42]*/
2794+ {
2795+ PyObject * current_filter = NULL ;
2796+ if (filter == Py_None ) {
2797+ current_filter = NULL ;
2798+ }
2799+ else if (!PyCallable_Check (filter )) {
2800+ PyErr_SetString (PyExc_TypeError , "filter must be callable or None" );
2801+ return NULL ;
2802+ }
2803+ else {
2804+ current_filter = filter ;
2805+ }
2806+
2807+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2808+ Py_XSETREF (interp -> imports .lazy_imports_filter , Py_XNewRef (current_filter ));
2809+ Py_RETURN_NONE ;
2810+ }
2811+
2812+ /*[clinic input]
2813+ sys.get_lazy_imports_filter
2814+
2815+ Get the current lazy imports filter callback.
2816+
2817+ Returns the filter callable or None if no filter is set.
2818+ [clinic start generated code]*/
2819+
2820+ static PyObject *
2821+ sys_get_lazy_imports_filter_impl (PyObject * module )
2822+ /*[clinic end generated code: output=3bf73022892165af input=cf1e07cb8e203c94]*/
2823+ {
2824+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2825+ PyObject * filter = interp -> imports .lazy_imports_filter ;
2826+ if (filter == NULL ) {
2827+ Py_RETURN_NONE ;
2828+ }
2829+ return Py_NewRef (filter );
2830+ }
2831+
27752832
27762833static PyMethodDef sys_methods [] = {
27772834 /* Might as well keep this in alphabetic order */
@@ -2837,6 +2894,8 @@ static PyMethodDef sys_methods[] = {
28372894 SYS_UNRAISABLEHOOK_METHODDEF
28382895 SYS_GET_INT_MAX_STR_DIGITS_METHODDEF
28392896 SYS_SET_INT_MAX_STR_DIGITS_METHODDEF
2897+ SYS_GET_LAZY_IMPORTS_FILTER_METHODDEF
2898+ SYS_SET_LAZY_IMPORTS_FILTER_METHODDEF
28402899 SYS__BASEREPL_METHODDEF
28412900#ifdef Py_STATS
28422901 SYS__STATS_ON_METHODDEF
0 commit comments