]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132775: Drop PyUnstable_InterpreterState_GetMainModule() (gh-132978)
authorEric Snow <ericsnowcurrently@gmail.com>
Mon, 28 Apr 2025 18:46:22 +0000 (12:46 -0600)
committerGitHub <noreply@github.com>
Mon, 28 Apr 2025 18:46:22 +0000 (12:46 -0600)
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__.

Doc/c-api/init.rst
Include/cpython/pystate.h
Include/internal/pycore_import.h
Include/internal/pycore_pystate.h
Python/crossinterp.c
Python/import.c
Python/pystate.c

index 33e9f87a6061df57886170096075b9e328b1dd9d..52f64a61006b74d91abac87722a1c9e0cab86170 100644 (file)
@@ -1517,16 +1517,6 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
    .. versionadded:: 3.8
 
 
-.. c:function:: PyObject* PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *interp)
-
-   Return a :term:`strong reference` to the ``__main__`` :ref:`module object <moduleobjects>`
-   for the given interpreter.
-
-   The caller must have an :term:`attached thread state`.
-
-   .. versionadded:: 3.13
-
-
 .. c:type:: PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
 
    Type of a frame evaluation function.
index c562426767c2cb5154a92ddf71c004151a75d434..97f11fff244a8ba1ba52aa9cd6c972592ee60e3b 100644 (file)
@@ -8,8 +8,6 @@
 PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
 PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
 
-PyAPI_FUNC(PyObject *) PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *);
-
 /* State unique per thread */
 
 /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
index 3acafd02bbdd7c4fb76c9662793c164808b33c4d..13fbff4eb65cb2aa20b4ee9984472bbe9eb9a126 100644 (file)
@@ -63,6 +63,7 @@ extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val);
 
 extern PyObject * _PyImport_InitModules(PyInterpreterState *interp);
 extern PyObject * _PyImport_GetModules(PyInterpreterState *interp);
+extern PyObject * _PyImport_GetModulesRef(PyInterpreterState *interp);
 extern void _PyImport_ClearModules(PyInterpreterState *interp);
 
 extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp);
index 864e0f5d1db28922bc857dedcdb81e9a2bc4c84a..601b9790001a03dcc53909e711352a630297b9be 100644 (file)
@@ -283,6 +283,9 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
 // See also PyInterpreterState_Get() and _PyInterpreterState_GET().
 extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
 
+extern PyObject * _Py_GetMainModule(PyThreadState *);
+extern int _Py_CheckMainModule(PyObject *module);
+
 #ifndef NDEBUG
 /* Modern equivalent of assert(PyGILState_Check()) */
 static inline void
index 8ba88c4b0576865dc3ebaf37079a7095b5519264..a1dd6b5901dcfa0c822eede5341ffdedcb8501d3 100644 (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;
     }
index 8742b6ec767c9bceb946e891b3b504900680fb02..a671a08daeb50ff2841500e8c089fb78808dac89 100644 (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)
 {
index aba558279a657d8f406ad990d8312fc7abd13728..ec19e3ae6a415a450e7782bcaa0203a91a1abc96 100644 (file)
@@ -1210,14 +1210,40 @@ _PyInterpreterState_SetWhence(PyInterpreterState *interp, long whence)
 
 
 PyObject *
-PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *interp)
+_Py_GetMainModule(PyThreadState *tstate)
 {
-    PyObject *modules = _PyImport_GetModules(interp);
-    if (modules == NULL) {
-        PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
-        return NULL;
+    // We return None to indicate "not found" or "bogus".
+    PyObject *modules = _PyImport_GetModulesRef(tstate->interp);
+    if (modules == Py_None) {
+        return modules;
+    }
+    PyObject *module = NULL;
+    (void)PyMapping_GetOptionalItem(modules, &_Py_ID(__main__), &module);
+    Py_DECREF(modules);
+    if (module == NULL && !PyErr_Occurred()) {
+        Py_RETURN_NONE;
     }
-    return PyMapping_GetItemString(modules, "__main__");
+    return module;
+}
+
+int
+_Py_CheckMainModule(PyObject *module)
+{
+    if (module == NULL || module == Py_None) {
+        if (!PyErr_Occurred()) {
+            (void)_PyErr_SetModuleNotFoundError(&_Py_ID(__main__));
+        }
+        return -1;
+    }
+    if (!Py_IS_TYPE(module, &PyModule_Type)) {
+        /* The __main__ module has been tampered with. */
+        PyObject *msg = PyUnicode_FromString("invalid __main__ module");
+        if (msg != NULL) {
+            (void)PyErr_SetImportError(msg, &_Py_ID(__main__), NULL);
+        }
+        return -1;
+    }
+    return 0;
 }