From 80a72f016bc5323d0b4b3d9062f3c17586435a00 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 9 Apr 2025 15:26:16 -0600 Subject: [PATCH] Add _PyImport_GetModulesRef(). --- Include/internal/pycore_import.h | 1 + Python/import.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) 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) {