diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 3acafd02bbdd7c..13fbff4eb65cb2 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -63,6 +63,7 @@ extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val); extern PyObject * _PyImport_InitModules(PyInterpreterState *interp); extern PyObject * _PyImport_GetModules(PyInterpreterState *interp); +extern PyObject * _PyImport_GetModulesRef(PyInterpreterState *interp); extern void _PyImport_ClearModules(PyInterpreterState *interp); extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp); diff --git a/Python/import.c b/Python/import.c index 8742b6ec767c9b..a671a08daeb50f 100644 --- a/Python/import.c +++ b/Python/import.c @@ -153,6 +153,20 @@ _PyImport_GetModules(PyInterpreterState *interp) return MODULES(interp); } +PyObject * +_PyImport_GetModulesRef(PyInterpreterState *interp) +{ + _PyImport_AcquireLock(interp); + PyObject *modules = MODULES(interp); + if (modules == NULL) { + /* The interpreter hasn't been initialized yet. */ + modules = Py_None; + } + Py_INCREF(modules); + _PyImport_ReleaseLock(interp); + return modules; +} + void _PyImport_ClearModules(PyInterpreterState *interp) {