mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
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:
parent
c17238251f
commit
fe462f5a91
7 changed files with 53 additions and 20 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
return PyMapping_GetItemString(modules, "__main__");
|
||||
PyObject *module = NULL;
|
||||
(void)PyMapping_GetOptionalItem(modules, &_Py_ID(__main__), &module);
|
||||
Py_DECREF(modules);
|
||||
if (module == NULL && !PyErr_Occurred()) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue