gh-132775: Drop PyUnstable_InterpreterState_GetMainModule() (gh-132978) · wrongnull/cpython@fe462f5 · GitHub
Skip to content

Commit fe462f5

Browse files
pythongh-132775: Drop PyUnstable_InterpreterState_GetMainModule() (pythongh-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__.
1 parent c172382 commit fe462f5

7 files changed

Lines changed: 53 additions & 20 deletions

File tree

Doc/c-api/init.rst

Lines changed: 0 additions & 10 deletions

Include/cpython/pystate.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
99
PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
1010

11-
PyAPI_FUNC(PyObject *) PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *);
12-
1311
/* State unique per thread */
1412

1513
/* Py_tracefunc return -1 when raising an exception, or 0 for success. */

Include/internal/pycore_import.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val);
6363

6464
extern PyObject * _PyImport_InitModules(PyInterpreterState *interp);
6565
extern PyObject * _PyImport_GetModules(PyInterpreterState *interp);
66+
extern PyObject * _PyImport_GetModulesRef(PyInterpreterState *interp);
6667
extern void _PyImport_ClearModules(PyInterpreterState *interp);
6768

6869
extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp);

Include/internal/pycore_pystate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
283283
// See also PyInterpreterState_Get() and _PyInterpreterState_GET().
284284
extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
285285

286+
extern PyObject * _Py_GetMainModule(PyThreadState *);
287+
extern int _Py_CheckMainModule(PyObject *module);
288+
286289
#ifndef NDEBUG
287290
/* Modern equivalent of assert(PyGILState_Check()) */
288291
static inline void

Python/crossinterp.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,7 @@ _PyXI_Enter(_PyXI_session *session,
17381738

17391739
// Switch to the requested interpreter (if necessary).
17401740
_enter_session(session, interp);
1741+
PyThreadState *session_tstate = session->init_tstate;
17411742
_PyXI_errcode errcode = _PyXI_ERR_UNCAUGHT_EXCEPTION;
17421743

17431744
// Ensure this thread owns __main__.
@@ -1751,8 +1752,8 @@ _PyXI_Enter(_PyXI_session *session,
17511752
session->running = 1;
17521753

17531754
// Cache __main__.__dict__.
1754-
PyObject *main_mod = PyUnstable_InterpreterState_GetMainModule(interp);
1755-
if (main_mod == NULL) {
1755+
PyObject *main_mod = _Py_GetMainModule(session_tstate);
1756+
if (_Py_CheckMainModule(main_mod) < 0) {
17561757
errcode = _PyXI_ERR_MAIN_NS_FAILURE;
17571758
goto error;
17581759
}

Python/import.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ _PyImport_GetModules(PyInterpreterState *interp)
153153
return MODULES(interp);
154154
}
155155

156+
PyObject *
157+
_PyImport_GetModulesRef(PyInterpreterState *interp)
158+
{
159+
_PyImport_AcquireLock(interp);
160+
PyObject *modules = MODULES(interp);
161+
if (modules == NULL) {
162+
/* The interpreter hasn't been initialized yet. */
163+
modules = Py_None;
164+
}
165+
Py_INCREF(modules);
166+
_PyImport_ReleaseLock(interp);
167+
return modules;
168+
}
169+
156170
void
157171
_PyImport_ClearModules(PyInterpreterState *interp)
158172
{

Python/pystate.c

Lines changed: 32 additions & 6 deletions

0 commit comments

Comments
 (0)