gh-132775: Drop PyUnstable_InterpreterState_GetMainModule() (gh-132978)

We replace it with _Py_GetMainModule(), and add _Py_CheckMainModule(), but both in the internal-only C-API.  We also add _PyImport_GetModulesRef(), which is the equivalent of _PyImport_GetModules(), but which increfs before the lock is released.

This is used by a later change related to pickle and handling __main__.
This commit is contained in:
Eric Snow 2025-04-28 12:46:22 -06:00 committed by GitHub
parent c17238251f
commit fe462f5a91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 53 additions and 20 deletions

View file

@ -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)
{