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

@ -1738,6 +1738,7 @@ _PyXI_Enter(_PyXI_session *session,
// Switch to the requested interpreter (if necessary).
_enter_session(session, interp);
PyThreadState *session_tstate = session->init_tstate;
_PyXI_errcode errcode = _PyXI_ERR_UNCAUGHT_EXCEPTION;
// Ensure this thread owns __main__.
@ -1751,8 +1752,8 @@ _PyXI_Enter(_PyXI_session *session,
session->running = 1;
// Cache __main__.__dict__.
PyObject *main_mod = PyUnstable_InterpreterState_GetMainModule(interp);
if (main_mod == NULL) {
PyObject *main_mod = _Py_GetMainModule(session_tstate);
if (_Py_CheckMainModule(main_mod) < 0) {
errcode = _PyXI_ERR_MAIN_NS_FAILURE;
goto error;
}