Skip to content

Commit ed0d690

Browse files
Replace PyUnstable_InterpreterState_GetMainModule() with _Py_GetMainModule() and _Py_CheckMainModule().
1 parent 8a4d4f3 commit ed0d690

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

Doc/c-api/init.rst

Lines changed: 0 additions & 10 deletions
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

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_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
@@ -1728,6 +1728,7 @@ _PyXI_Enter(_PyXI_session *session,
17281728

17291729
// Switch to the requested interpreter (if necessary).
17301730
_enter_session(session, interp);
1731+
PyThreadState *session_tstate = session->init_tstate;
17311732
_PyXI_errcode errcode = _PyXI_ERR_UNCAUGHT_EXCEPTION;
17321733

17331734
// Ensure this thread owns __main__.
@@ -1741,8 +1742,8 @@ _PyXI_Enter(_PyXI_session *session,
17411742
session->running = 1;
17421743

17431744
// Cache __main__.__dict__.
1744-
PyObject *main_mod = PyUnstable_InterpreterState_GetMainModule(interp);
1745-
if (main_mod == NULL) {
1745+
PyObject *main_mod = _Py_GetMainModule(session_tstate);
1746+
if (_Py_CheckMainModule(main_mod) < 0) {
17461747
errcode = _PyXI_ERR_MAIN_NS_FAILURE;
17471748
goto error;
17481749
}

Python/pystate.c

Lines changed: 32 additions & 6 deletions
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)