Skip to content

Commit fe462f5

Browse files
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__.
1 parent c172382 commit fe462f5

File tree

7 files changed

+53
-20
lines changed

7 files changed

+53
-20
lines changed

Doc/c-api/init.rst

-10
Original file line numberDiff line numberDiff line change
@@ -1517,16 +1517,6 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
15171517
.. versionadded:: 3.8
15181518
15191519
1520-
.. c:function:: PyObject* PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *interp)
1521-
1522-
Return a :term:`strong reference` to the ``__main__`` :ref:`module object <moduleobjects>`
1523-
for the given interpreter.
1524-
1525-
The caller must have an :term:`attached thread state`.
1526-
1527-
.. versionadded:: 3.13
1528-
1529-
15301520
.. c:type:: PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
15311521
15321522
Type of a frame evaluation function.

Include/cpython/pystate.h

-2
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

+1
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

+3
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

+3-2
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

+14
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

+32-6
Original file line numberDiff line numberDiff line change
@@ -1210,14 +1210,40 @@ _PyInterpreterState_SetWhence(PyInterpreterState *interp, long whence)
12101210

12111211

12121212
PyObject *
1213-
PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *interp)
1213+
_Py_GetMainModule(PyThreadState *tstate)
12141214
{
1215-
PyObject *modules = _PyImport_GetModules(interp);
1216-
if (modules == NULL) {
1217-
PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
1218-
return NULL;
1215+
// We return None to indicate "not found" or "bogus".
1216+
PyObject *modules = _PyImport_GetModulesRef(tstate->interp);
1217+
if (modules == Py_None) {
1218+
return modules;
1219+
}
1220+
PyObject *module = NULL;
1221+
(void)PyMapping_GetOptionalItem(modules, &_Py_ID(__main__), &module);
1222+
Py_DECREF(modules);
1223+
if (module == NULL && !PyErr_Occurred()) {
1224+
Py_RETURN_NONE;
12191225
}
1220-
return PyMapping_GetItemString(modules, "__main__");
1226+
return module;
1227+
}
1228+
1229+
int
1230+
_Py_CheckMainModule(PyObject *module)
1231+
{
1232+
if (module == NULL || module == Py_None) {
1233+
if (!PyErr_Occurred()) {
1234+
(void)_PyErr_SetModuleNotFoundError(&_Py_ID(__main__));
1235+
}
1236+
return -1;
1237+
}
1238+
if (!Py_IS_TYPE(module, &PyModule_Type)) {
1239+
/* The __main__ module has been tampered with. */
1240+
PyObject *msg = PyUnicode_FromString("invalid __main__ module");
1241+
if (msg != NULL) {
1242+
(void)PyErr_SetImportError(msg, &_Py_ID(__main__), NULL);
1243+
}
1244+
return -1;
1245+
}
1246+
return 0;
12211247
}
12221248

12231249

0 commit comments

Comments
 (0)