From 4433124bbc42785eb1ba27bd069132d4e7bd07d2 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 17:13:07 -0700 Subject: [PATCH 01/22] Group code in import.c. --- Python/import.c | 2038 ++++++++++++++++++++++++----------------------- 1 file changed, 1058 insertions(+), 980 deletions(-) diff --git a/Python/import.c b/Python/import.c index 63ed2443657b29..10ac46782707b3 100644 --- a/Python/import.c +++ b/Python/import.c @@ -24,8 +24,18 @@ extern "C" { #endif -/* Forward references */ -static PyObject *import_add_module(PyThreadState *tstate, PyObject *name); + +/*[clinic input] +module _imp +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/ + +#include "clinic/import.c.h" + + +/*******************************/ +/* process-global import state */ +/*******************************/ /* This table is defined in config.c: */ extern struct _inittab _PyImport_Inittab[]; @@ -37,14 +47,159 @@ struct _inittab *PyImport_Inittab = _PyImport_Inittab; // we track the pointer here so we can deallocate it during finalization. static struct _inittab *inittab_copy = NULL; -/*[clinic input] -module _imp -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/ -#include "clinic/import.c.h" +/*******************************/ +/* runtime-global import state */ +/*******************************/ + +#define import_lock _PyRuntime.imports.lock.mutex +#define import_lock_thread _PyRuntime.imports.lock.thread +#define import_lock_level _PyRuntime.imports.lock.level + + +/*********************/ +/* runtime lifecycle */ +/*********************/ + +PyStatus +_PyImport_Init(void) +{ + if (_PyRuntime.imports.inittab != NULL) { + return _PyStatus_ERR("global import state already initialized"); + } + PyStatus status = _PyStatus_OK(); + + size_t size; + for (size = 0; PyImport_Inittab[size].name != NULL; size++) + ; + size++; + + /* Force default raw memory allocator to get a known allocator to be able + to release the memory in _PyImport_Fini() */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + /* Make the copy. */ + struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab)); + if (copied == NULL) { + status = PyStatus_NoMemory(); + goto done; + } + memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab)); + _PyRuntime.imports.inittab = copied; + +done: + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return status; +} + +static inline void _extensions_cache_clear(void); + +void +_PyImport_Fini(void) +{ + _extensions_cache_clear(); + if (import_lock != NULL) { + PyThread_free_lock(import_lock); + import_lock = NULL; + } + + /* Use the same memory allocator as _PyImport_Init(). */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + /* Free memory allocated by _PyImport_Init() */ + struct _inittab *inittab = _PyRuntime.imports.inittab; + _PyRuntime.imports.inittab = NULL; + PyMem_RawFree(inittab); + + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); +} + +void +_PyImport_Fini2(void) +{ + /* Use the same memory allocator than PyImport_ExtendInittab(). */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + // Reset PyImport_Inittab + PyImport_Inittab = _PyImport_Inittab; + + /* Free memory allocated by PyImport_ExtendInittab() */ + PyMem_RawFree(inittab_copy); + inittab_copy = NULL; + + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); +} + + +/*************************/ +/* interpreter lifecycle */ +/*************************/ + +/* In some corner cases it is important to be sure that the import + machinery has been initialized (or not cleaned up yet). For + example, see issue #4236 and PyModule_Create2(). */ + +int +_PyImport_IsInitialized(PyInterpreterState *interp) +{ + if (interp->modules == NULL) + return 0; + return 1; +} + +static PyObject* create_builtin(PyThreadState *tstate, + PyObject *name, PyObject *spec); +static int exec_builtin_or_dynamic(PyObject *mod); + +// Import the _imp extension by calling manually _imp.create_builtin() and +// _imp.exec_builtin() since importlib is not initialized yet. Initializing +// importlib requires the _imp module: this function fix the bootstrap issue. +PyObject* +_PyImport_BootstrapImp(PyThreadState *tstate) +{ + PyObject *name = PyUnicode_FromString("_imp"); + if (name == NULL) { + return NULL; + } + + // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec(): + // an object with just a name attribute. + // + // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway. + PyObject *attrs = Py_BuildValue("{sO}", "name", name); + if (attrs == NULL) { + goto error; + } + PyObject *spec = _PyNamespace_New(attrs); + Py_DECREF(attrs); + if (spec == NULL) { + goto error; + } + + // Create the _imp module from its definition. + PyObject *mod = create_builtin(tstate, name, spec); + Py_CLEAR(name); + Py_DECREF(spec); + if (mod == NULL) { + goto error; + } + assert(mod != Py_None); // not found + + // Execute the _imp module: call imp_module_exec(). + if (exec_builtin_or_dynamic(mod) < 0) { + Py_DECREF(mod); + goto error; + } + return mod; + +error: + Py_XDECREF(name); + return NULL; +} -/* Initialize things */ PyStatus _PyImportZip_Init(PyThreadState *tstate) @@ -90,14 +245,15 @@ _PyImportZip_Init(PyThreadState *tstate) return _PyStatus_ERR("initializing zipimport failed"); } + +/*******************/ +/* the import lock */ +/*******************/ + /* Locking primitives to prevent parallel imports of the same module in different threads to return with a partially loaded module. These calls are serialized by the global interpreter lock. */ -#define import_lock _PyRuntime.imports.lock.mutex -#define import_lock_thread _PyRuntime.imports.lock.thread -#define import_lock_level _PyRuntime.imports.lock.level - void _PyImport_AcquireLock(void) { @@ -170,264 +326,223 @@ _PyImport_ReInitLock(void) } #endif -/*[clinic input] -_imp.lock_held - -Return True if the import lock is currently held, else False. -On platforms without threads, return False. -[clinic start generated code]*/ +/***************/ +/* sys.modules */ +/***************/ -static PyObject * -_imp_lock_held_impl(PyObject *module) -/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/ +PyObject * +PyImport_GetModuleDict(void) { - return PyBool_FromLong(import_lock_thread != PYTHREAD_INVALID_THREAD_ID); + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->modules == NULL) { + Py_FatalError("interpreter has no modules dictionary"); + } + return interp->modules; } -/*[clinic input] -_imp.acquire_lock - -Acquires the interpreter's import lock for the current thread. - -This lock should be used by import hooks to ensure thread-safety when importing -modules. On platforms without threads, this function does nothing. -[clinic start generated code]*/ - -static PyObject * -_imp_acquire_lock_impl(PyObject *module) -/*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/ +// This is only kept around for extensions that use _Py_IDENTIFIER. +PyObject * +_PyImport_GetModuleId(_Py_Identifier *nameid) { - _PyImport_AcquireLock(); - Py_RETURN_NONE; + PyObject *name = _PyUnicode_FromId(nameid); /* borrowed */ + if (name == NULL) { + return NULL; + } + return PyImport_GetModule(name); } -/*[clinic input] -_imp.release_lock - -Release the interpreter's import lock. +int +_PyImport_SetModule(PyObject *name, PyObject *m) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + PyObject *modules = interp->modules; + return PyObject_SetItem(modules, name, m); +} -On platforms without threads, this function does nothing. -[clinic start generated code]*/ +int +_PyImport_SetModuleString(const char *name, PyObject *m) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + PyObject *modules = interp->modules; + return PyMapping_SetItemString(modules, name, m); +} static PyObject * -_imp_release_lock_impl(PyObject *module) -/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/ +import_get_module(PyThreadState *tstate, PyObject *name) { - if (_PyImport_ReleaseLock() < 0) { - PyErr_SetString(PyExc_RuntimeError, - "not holding the import lock"); + PyObject *modules = tstate->interp->modules; + if (modules == NULL) { + _PyErr_SetString(tstate, PyExc_RuntimeError, + "unable to get sys.modules"); return NULL; } - Py_RETURN_NONE; + + PyObject *m; + Py_INCREF(modules); + if (PyDict_CheckExact(modules)) { + m = PyDict_GetItemWithError(modules, name); /* borrowed */ + Py_XINCREF(m); + } + else { + m = PyObject_GetItem(modules, name); + if (m == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + _PyErr_Clear(tstate); + } + } + Py_DECREF(modules); + return m; } -PyStatus -_PyImport_Init(void) +static int +import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name) { - if (_PyRuntime.imports.inittab != NULL) { - return _PyStatus_ERR("global import state already initialized"); - } - PyStatus status = _PyStatus_OK(); + PyObject *spec; - size_t size; - for (size = 0; PyImport_Inittab[size].name != NULL; size++) - ; - size++; - - /* Force default raw memory allocator to get a known allocator to be able - to release the memory in _PyImport_Fini() */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - /* Make the copy. */ - struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab)); - if (copied == NULL) { - status = PyStatus_NoMemory(); - goto done; - } - memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab)); - _PyRuntime.imports.inittab = copied; - -done: - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - return status; -} - -static inline void _extensions_cache_clear(void); - -void -_PyImport_Fini(void) -{ - _extensions_cache_clear(); - if (import_lock != NULL) { - PyThread_free_lock(import_lock); - import_lock = NULL; + /* Optimization: only call _bootstrap._lock_unlock_module() if + __spec__._initializing is true. + NOTE: because of this, initializing must be set *before* + stuffing the new module in sys.modules. + */ + spec = PyObject_GetAttr(mod, &_Py_ID(__spec__)); + int busy = _PyModuleSpec_IsInitializing(spec); + Py_XDECREF(spec); + if (busy) { + /* Wait until module is done importing. */ + PyObject *value = _PyObject_CallMethodOneArg( + interp->importlib, &_Py_ID(_lock_unlock_module), name); + if (value == NULL) { + return -1; + } + Py_DECREF(value); } - - /* Use the same memory allocator as _PyImport_Init(). */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - /* Free memory allocated by _PyImport_Init() */ - struct _inittab *inittab = _PyRuntime.imports.inittab; - _PyRuntime.imports.inittab = NULL; - PyMem_RawFree(inittab); - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); -} - -void -_PyImport_Fini2(void) -{ - /* Use the same memory allocator than PyImport_ExtendInittab(). */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - // Reset PyImport_Inittab - PyImport_Inittab = _PyImport_Inittab; - - /* Free memory allocated by PyImport_ExtendInittab() */ - PyMem_RawFree(inittab_copy); - inittab_copy = NULL; - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return 0; } -/* Helper for sys */ +static void remove_importlib_frames(PyThreadState *tstate); PyObject * -PyImport_GetModuleDict(void) -{ - PyInterpreterState *interp = _PyInterpreterState_GET(); - if (interp->modules == NULL) { - Py_FatalError("interpreter has no modules dictionary"); - } - return interp->modules; -} - -/* In some corner cases it is important to be sure that the import - machinery has been initialized (or not cleaned up yet). For - example, see issue #4236 and PyModule_Create2(). */ - -int -_PyImport_IsInitialized(PyInterpreterState *interp) +PyImport_GetModule(PyObject *name) { - if (interp->modules == NULL) - return 0; - return 1; -} + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *mod; -PyObject * -_PyImport_GetModuleId(_Py_Identifier *nameid) -{ - PyObject *name = _PyUnicode_FromId(nameid); /* borrowed */ - if (name == NULL) { - return NULL; + mod = import_get_module(tstate, name); + if (mod != NULL && mod != Py_None) { + if (import_ensure_initialized(tstate->interp, mod, name) < 0) { + Py_DECREF(mod); + remove_importlib_frames(tstate); + return NULL; + } } - return PyImport_GetModule(name); -} - -int -_PyImport_SetModule(PyObject *name, PyObject *m) -{ - PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *modules = interp->modules; - return PyObject_SetItem(modules, name, m); + return mod; } -int -_PyImport_SetModuleString(const char *name, PyObject *m) -{ - PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *modules = interp->modules; - return PyMapping_SetItemString(modules, name, m); -} +/* Get the module object corresponding to a module name. + First check the modules dictionary if there's one there, + if not, create a new one and insert it in the modules dictionary. */ static PyObject * -import_get_module(PyThreadState *tstate, PyObject *name) +import_add_module(PyThreadState *tstate, PyObject *name) { PyObject *modules = tstate->interp->modules; if (modules == NULL) { _PyErr_SetString(tstate, PyExc_RuntimeError, - "unable to get sys.modules"); + "no import module dictionary"); return NULL; } PyObject *m; - Py_INCREF(modules); if (PyDict_CheckExact(modules)) { - m = PyDict_GetItemWithError(modules, name); /* borrowed */ - Py_XINCREF(m); + m = Py_XNewRef(PyDict_GetItemWithError(modules, name)); } else { m = PyObject_GetItem(modules, name); - if (m == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + // For backward-compatibility we copy the behavior + // of PyDict_GetItemWithError(). + if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { _PyErr_Clear(tstate); } } - Py_DECREF(modules); + if (_PyErr_Occurred(tstate)) { + return NULL; + } + if (m != NULL && PyModule_Check(m)) { + return m; + } + Py_XDECREF(m); + m = PyModule_NewObject(name); + if (m == NULL) + return NULL; + if (PyObject_SetItem(modules, name, m) != 0) { + Py_DECREF(m); + return NULL; + } + return m; } - -static int -import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name) +PyObject * +PyImport_AddModuleObject(PyObject *name) { - PyObject *spec; - - /* Optimization: only call _bootstrap._lock_unlock_module() if - __spec__._initializing is true. - NOTE: because of this, initializing must be set *before* - stuffing the new module in sys.modules. - */ - spec = PyObject_GetAttr(mod, &_Py_ID(__spec__)); - int busy = _PyModuleSpec_IsInitializing(spec); - Py_XDECREF(spec); - if (busy) { - /* Wait until module is done importing. */ - PyObject *value = _PyObject_CallMethodOneArg( - interp->importlib, &_Py_ID(_lock_unlock_module), name); - if (value == NULL) { - return -1; + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *mod = import_add_module(tstate, name); + if (mod) { + PyObject *ref = PyWeakref_NewRef(mod, NULL); + Py_DECREF(mod); + if (ref == NULL) { + return NULL; } - Py_DECREF(value); + mod = PyWeakref_GetObject(ref); + Py_DECREF(ref); } - return 0; + return mod; /* borrowed reference */ } -/* Helper for pythonrun.c -- return magic number and tag. */ - -long -PyImport_GetMagicNumber(void) +PyObject * +PyImport_AddModule(const char *name) { - long res; - PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *external, *pyc_magic; - - external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); - if (external == NULL) - return -1; - pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); - Py_DECREF(external); - if (pyc_magic == NULL) - return -1; - res = PyLong_AsLong(pyc_magic); - Py_DECREF(pyc_magic); - return res; + PyObject *nameobj = PyUnicode_FromString(name); + if (nameobj == NULL) { + return NULL; + } + PyObject *module = PyImport_AddModuleObject(nameobj); + Py_DECREF(nameobj); + return module; } -extern const char * _PySys_ImplCacheTag; - -const char * -PyImport_GetMagicTag(void) +/* Remove name from sys.modules, if it's there. + * Can be called with an exception raised. + * If fail to remove name a new exception will be chained with the old + * exception, otherwise the old exception is preserved. + */ +static void +remove_module(PyThreadState *tstate, PyObject *name) { - return _PySys_ImplCacheTag; + PyObject *type, *value, *traceback; + _PyErr_Fetch(tstate, &type, &value, &traceback); + + PyObject *modules = tstate->interp->modules; + if (PyDict_CheckExact(modules)) { + PyObject *mod = _PyDict_Pop(modules, name, Py_None); + Py_XDECREF(mod); + } + else if (PyMapping_DelItem(modules, name) < 0) { + if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + _PyErr_Clear(tstate); + } + } + + _PyErr_ChainExceptions(type, value, traceback); } +/*****************************/ +/* single-phase init modules */ +/*****************************/ + /* We support a number of kinds of single-phase init builtin/extension modules: @@ -616,33 +731,12 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, return 0; } -int -_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) -{ - int res = -1; - PyObject *nameobj; - nameobj = PyUnicode_InternFromString(name); - if (nameobj == NULL) { - return -1; - } - if (PyObject_SetItem(modules, nameobj, mod) < 0) { - goto finally; - } - if (fix_up_extension(mod, nameobj, nameobj) < 0) { - PyMapping_DelItem(modules, nameobj); - goto finally; - } - res = 0; - -finally: - Py_DECREF(nameobj); - return res; -} - -static PyObject * -import_find_extension(PyThreadState *tstate, PyObject *name, - PyObject *filename) + +static PyObject * +import_find_extension(PyThreadState *tstate, PyObject *name, + PyObject *filename) { + /* Only single-phase init modules will be in the cache. */ PyModuleDef *def = _extensions_cache_get(filename, name); if (def == NULL) { return NULL; @@ -694,546 +788,233 @@ import_find_extension(PyThreadState *tstate, PyObject *name, } -/* Get the module object corresponding to a module name. - First check the modules dictionary if there's one there, - if not, create a new one and insert it in the modules dictionary. */ +/*******************/ -static PyObject * -import_add_module(PyThreadState *tstate, PyObject *name) -{ - PyObject *modules = tstate->interp->modules; - if (modules == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "no import module dictionary"); - return NULL; - } +#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) +#include +EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { + return wasmTable.get(func)(); +}); +#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE - PyObject *m; - if (PyDict_CheckExact(modules)) { - m = Py_XNewRef(PyDict_GetItemWithError(modules, name)); - } - else { - m = PyObject_GetItem(modules, name); - // For backward-compatibility we copy the behavior - // of PyDict_GetItemWithError(). - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - _PyErr_Clear(tstate); - } - } - if (_PyErr_Occurred(tstate)) { - return NULL; + +/*******************/ +/* builtin modules */ +/*******************/ + +int +_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) +{ + int res = -1; + PyObject *nameobj; + nameobj = PyUnicode_InternFromString(name); + if (nameobj == NULL) { + return -1; } - if (m != NULL && PyModule_Check(m)) { - return m; + if (PyObject_SetItem(modules, nameobj, mod) < 0) { + goto finally; } - Py_XDECREF(m); - m = PyModule_NewObject(name); - if (m == NULL) - return NULL; - if (PyObject_SetItem(modules, name, m) != 0) { - Py_DECREF(m); - return NULL; + if (fix_up_extension(mod, nameobj, nameobj) < 0) { + PyMapping_DelItem(modules, nameobj); + goto finally; } + res = 0; - return m; +finally: + Py_DECREF(nameobj); + return res; } -PyObject * -PyImport_AddModuleObject(PyObject *name) +/* Helper to test for built-in module */ + +static int +is_builtin(PyObject *name) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *mod = import_add_module(tstate, name); - if (mod) { - PyObject *ref = PyWeakref_NewRef(mod, NULL); - Py_DECREF(mod); - if (ref == NULL) { - return NULL; + int i; + struct _inittab *inittab = _PyRuntime.imports.inittab; + for (i = 0; inittab[i].name != NULL; i++) { + if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { + if (inittab[i].initfunc == NULL) + return -1; + else + return 1; } - mod = PyWeakref_GetObject(ref); - Py_DECREF(ref); } - return mod; /* borrowed reference */ + return 0; } - -PyObject * -PyImport_AddModule(const char *name) +static PyObject* +create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) { - PyObject *nameobj = PyUnicode_FromString(name); - if (nameobj == NULL) { - return NULL; + PyObject *mod = import_find_extension(tstate, name, name); + if (mod || _PyErr_Occurred(tstate)) { + return mod; } - PyObject *module = PyImport_AddModuleObject(nameobj); - Py_DECREF(nameobj); - return module; -} + PyObject *modules = tstate->interp->modules; + for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { + if (_PyUnicode_EqualToASCIIString(name, p->name)) { + if (p->initfunc == NULL) { + /* Cannot re-init internal module ("sys" or "builtins") */ + mod = PyImport_AddModuleObject(name); + return Py_XNewRef(mod); + } + mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); + if (mod == NULL) { + return NULL; + } -/* Remove name from sys.modules, if it's there. - * Can be called with an exception raised. - * If fail to remove name a new exception will be chained with the old - * exception, otherwise the old exception is preserved. - */ -static void -remove_module(PyThreadState *tstate, PyObject *name) -{ - PyObject *type, *value, *traceback; - _PyErr_Fetch(tstate, &type, &value, &traceback); + if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { + return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); + } + else { + /* Remember pointer to module init function. */ + PyModuleDef *def = PyModule_GetDef(mod); + if (def == NULL) { + return NULL; + } - PyObject *modules = tstate->interp->modules; - if (PyDict_CheckExact(modules)) { - PyObject *mod = _PyDict_Pop(modules, name, Py_None); - Py_XDECREF(mod); - } - else if (PyMapping_DelItem(modules, name) < 0) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - _PyErr_Clear(tstate); + def->m_base.m_init = p->initfunc; + if (_PyImport_FixupExtensionObject(mod, name, name, + modules) < 0) { + return NULL; + } + return mod; + } } } - _PyErr_ChainExceptions(type, value, traceback); + // not found + Py_RETURN_NONE; } -/* Execute a code object in a module and return the module object - * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is - * removed from sys.modules, to avoid leaving damaged module objects - * in sys.modules. The caller may wish to restore the original - * module object (if any) in this case; PyImport_ReloadModule is an - * example. - * - * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer - * interface. The other two exist primarily for backward compatibility. - */ -PyObject * -PyImport_ExecCodeModule(const char *name, PyObject *co) -{ - return PyImport_ExecCodeModuleWithPathnames( - name, co, (char *)NULL, (char *)NULL); -} +/*****************************/ +/* the builtin modules table */ +/*****************************/ -PyObject * -PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) -{ - return PyImport_ExecCodeModuleWithPathnames( - name, co, pathname, (char *)NULL); -} +/* API for embedding applications that want to add their own entries + to the table of built-in modules. This should normally be called + *before* Py_Initialize(). When the table resize fails, -1 is + returned and the existing table is unchanged. -PyObject * -PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, - const char *pathname, - const char *cpathname) -{ - PyObject *m = NULL; - PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; + After a similar function by Just van Rossum. */ - nameobj = PyUnicode_FromString(name); - if (nameobj == NULL) - return NULL; +int +PyImport_ExtendInittab(struct _inittab *newtab) +{ + struct _inittab *p; + size_t i, n; + int res = 0; - if (cpathname != NULL) { - cpathobj = PyUnicode_DecodeFSDefault(cpathname); - if (cpathobj == NULL) - goto error; + if (_PyRuntime.imports.inittab != NULL) { + Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); } - else - cpathobj = NULL; - if (pathname != NULL) { - pathobj = PyUnicode_DecodeFSDefault(pathname); - if (pathobj == NULL) - goto error; - } - else if (cpathobj != NULL) { - PyInterpreterState *interp = _PyInterpreterState_GET(); + /* Count the number of entries in both tables */ + for (n = 0; newtab[n].name != NULL; n++) + ; + if (n == 0) + return 0; /* Nothing to do */ + for (i = 0; PyImport_Inittab[i].name != NULL; i++) + ; - if (interp == NULL) { - Py_FatalError("no current interpreter"); - } + /* Force default raw memory allocator to get a known allocator to be able + to release the memory in _PyImport_Fini2() */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - external= PyObject_GetAttrString(interp->importlib, - "_bootstrap_external"); - if (external != NULL) { - pathobj = _PyObject_CallMethodOneArg( - external, &_Py_ID(_get_sourcefile), cpathobj); - Py_DECREF(external); - } - if (pathobj == NULL) - PyErr_Clear(); + /* Allocate new memory for the combined table */ + p = NULL; + if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { + size_t size = sizeof(struct _inittab) * (i + n + 1); + p = PyMem_RawRealloc(inittab_copy, size); } - else - pathobj = NULL; - - m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); -error: - Py_DECREF(nameobj); - Py_XDECREF(pathobj); - Py_XDECREF(cpathobj); - return m; -} - -static PyObject * -module_dict_for_exec(PyThreadState *tstate, PyObject *name) -{ - PyObject *m, *d; - - m = import_add_module(tstate, name); - if (m == NULL) - return NULL; - /* If the module is being reloaded, we get the old module back - and re-use its dict to exec the new code. */ - d = PyModule_GetDict(m); - int r = PyDict_Contains(d, &_Py_ID(__builtins__)); - if (r == 0) { - r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins()); - } - if (r < 0) { - remove_module(tstate, name); - Py_DECREF(m); - return NULL; - } - - Py_INCREF(d); - Py_DECREF(m); - return d; -} - -static PyObject * -exec_code_in_module(PyThreadState *tstate, PyObject *name, - PyObject *module_dict, PyObject *code_object) -{ - PyObject *v, *m; - - v = PyEval_EvalCode(code_object, module_dict, module_dict); - if (v == NULL) { - remove_module(tstate, name); - return NULL; - } - Py_DECREF(v); - - m = import_get_module(tstate, name); - if (m == NULL && !_PyErr_Occurred(tstate)) { - _PyErr_Format(tstate, PyExc_ImportError, - "Loaded module %R not found in sys.modules", - name); + if (p == NULL) { + res = -1; + goto done; } - return m; -} - -PyObject* -PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, - PyObject *cpathname) -{ - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *d, *external, *res; - - d = module_dict_for_exec(tstate, name); - if (d == NULL) { - return NULL; + /* Copy the tables into the new memory at the first call + to PyImport_ExtendInittab(). */ + if (inittab_copy != PyImport_Inittab) { + memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); } + memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); + PyImport_Inittab = inittab_copy = p; - if (pathname == NULL) { - pathname = ((PyCodeObject *)co)->co_filename; - } - external = PyObject_GetAttrString(tstate->interp->importlib, - "_bootstrap_external"); - if (external == NULL) { - Py_DECREF(d); - return NULL; - } - res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module), - d, name, pathname, cpathname, NULL); - Py_DECREF(external); - if (res != NULL) { - Py_DECREF(res); - res = exec_code_in_module(tstate, name, d, co); - } - Py_DECREF(d); +done: + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); return res; } +/* Shorthand to add a single entry given a name and a function */ -static void -update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) +int +PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) { - PyObject *constants, *tmp; - Py_ssize_t i, n; - - if (PyUnicode_Compare(co->co_filename, oldname)) - return; - - Py_XSETREF(co->co_filename, Py_NewRef(newname)); + struct _inittab newtab[2]; - constants = co->co_consts; - n = PyTuple_GET_SIZE(constants); - for (i = 0; i < n; i++) { - tmp = PyTuple_GET_ITEM(constants, i); - if (PyCode_Check(tmp)) - update_code_filenames((PyCodeObject *)tmp, - oldname, newname); + if (_PyRuntime.imports.inittab != NULL) { + Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); } -} - -static void -update_compiled_module(PyCodeObject *co, PyObject *newname) -{ - PyObject *oldname; - - if (PyUnicode_Compare(co->co_filename, newname) == 0) - return; - - oldname = co->co_filename; - Py_INCREF(oldname); - update_code_filenames(co, oldname, newname); - Py_DECREF(oldname); -} - -/*[clinic input] -_imp._fix_co_filename - - code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") - Code object to change. - - path: unicode - File path to use. - / - -Changes code.co_filename to specify the passed-in file path. -[clinic start generated code]*/ -static PyObject * -_imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code, - PyObject *path) -/*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/ + memset(newtab, '\0', sizeof newtab); -{ - update_compiled_module(code, path); + newtab[0].name = name; + newtab[0].initfunc = initfunc; - Py_RETURN_NONE; + return PyImport_ExtendInittab(newtab); } -/* Helper to test for built-in module */ +/******************/ +/* frozen modules */ +/******************/ -static int -is_builtin(PyObject *name) +/* Return true if the name is an alias. In that case, "alias" is set + to the original module name. If it is an alias but the original + module isn't known then "alias" is set to NULL while true is returned. */ +static bool +resolve_module_alias(const char *name, const struct _module_alias *aliases, + const char **alias) { - int i; - struct _inittab *inittab = _PyRuntime.imports.inittab; - for (i = 0; inittab[i].name != NULL; i++) { - if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { - if (inittab[i].initfunc == NULL) - return -1; - else - return 1; + const struct _module_alias *entry; + for (entry = aliases; ; entry++) { + if (entry->name == NULL) { + /* It isn't an alias. */ + return false; + } + if (strcmp(name, entry->name) == 0) { + if (alias != NULL) { + *alias = entry->orig; + } + return true; } } - return 0; } - -/* Return a finder object for a sys.path/pkg.__path__ item 'p', - possibly by fetching it from the path_importer_cache dict. If it - wasn't yet cached, traverse path_hooks until a hook is found - that can handle the path item. Return None if no hook could; - this tells our caller that the path based finder could not find - a finder for this path item. Cache the result in - path_importer_cache. */ - -static PyObject * -get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, - PyObject *path_hooks, PyObject *p) +static bool +use_frozen(void) { - PyObject *importer; - Py_ssize_t j, nhooks; - - /* These conditions are the caller's responsibility: */ - assert(PyList_Check(path_hooks)); - assert(PyDict_Check(path_importer_cache)); - - nhooks = PyList_Size(path_hooks); - if (nhooks < 0) - return NULL; /* Shouldn't happen */ - - importer = PyDict_GetItemWithError(path_importer_cache, p); - if (importer != NULL || _PyErr_Occurred(tstate)) { - return Py_XNewRef(importer); - } - - /* set path_importer_cache[p] to None to avoid recursion */ - if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) - return NULL; - - for (j = 0; j < nhooks; j++) { - PyObject *hook = PyList_GetItem(path_hooks, j); - if (hook == NULL) - return NULL; - importer = PyObject_CallOneArg(hook, p); - if (importer != NULL) - break; - - if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) { - return NULL; - } - _PyErr_Clear(tstate); + PyInterpreterState *interp = _PyInterpreterState_GET(); + int override = interp->override_frozen_modules; + if (override > 0) { + return true; } - if (importer == NULL) { - Py_RETURN_NONE; + else if (override < 0) { + return false; } - if (PyDict_SetItem(path_importer_cache, p, importer) < 0) { - Py_DECREF(importer); - return NULL; + else { + return interp->config.use_frozen_modules; } - return importer; } -PyObject * -PyImport_GetImporter(PyObject *path) +static PyObject * +list_frozen_module_names(void) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *path_importer_cache = PySys_GetObject("path_importer_cache"); - PyObject *path_hooks = PySys_GetObject("path_hooks"); - if (path_importer_cache == NULL || path_hooks == NULL) { - return NULL; - } - return get_path_importer(tstate, path_importer_cache, path_hooks, path); -} - -#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) -#include -EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { - return wasmTable.get(func)(); -}); -#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE - -static PyObject* -create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) -{ - PyObject *mod = import_find_extension(tstate, name, name); - if (mod || _PyErr_Occurred(tstate)) { - return mod; - } - - PyObject *modules = tstate->interp->modules; - for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { - if (_PyUnicode_EqualToASCIIString(name, p->name)) { - if (p->initfunc == NULL) { - /* Cannot re-init internal module ("sys" or "builtins") */ - mod = PyImport_AddModuleObject(name); - return Py_XNewRef(mod); - } - mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); - if (mod == NULL) { - return NULL; - } - - if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { - return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); - } - else { - /* Remember pointer to module init function. */ - PyModuleDef *def = PyModule_GetDef(mod); - if (def == NULL) { - return NULL; - } - - def->m_base.m_init = p->initfunc; - if (_PyImport_FixupExtensionObject(mod, name, name, - modules) < 0) { - return NULL; - } - return mod; - } - } - } - - // not found - Py_RETURN_NONE; -} - - - -/*[clinic input] -_imp.create_builtin - - spec: object - / - -Create an extension module. -[clinic start generated code]*/ - -static PyObject * -_imp_create_builtin(PyObject *module, PyObject *spec) -/*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/ -{ - PyThreadState *tstate = _PyThreadState_GET(); - - PyObject *name = PyObject_GetAttrString(spec, "name"); - if (name == NULL) { - return NULL; - } - - if (!PyUnicode_Check(name)) { - PyErr_Format(PyExc_TypeError, - "name must be string, not %.200s", - Py_TYPE(name)->tp_name); - Py_DECREF(name); - return NULL; - } - - PyObject *mod = create_builtin(tstate, name, spec); - Py_DECREF(name); - return mod; -} - - -/* Return true if the name is an alias. In that case, "alias" is set - to the original module name. If it is an alias but the original - module isn't known then "alias" is set to NULL while true is returned. */ -static bool -resolve_module_alias(const char *name, const struct _module_alias *aliases, - const char **alias) -{ - const struct _module_alias *entry; - for (entry = aliases; ; entry++) { - if (entry->name == NULL) { - /* It isn't an alias. */ - return false; - } - if (strcmp(name, entry->name) == 0) { - if (alias != NULL) { - *alias = entry->orig; - } - return true; - } - } -} - - -/* Frozen modules */ - -static bool -use_frozen(void) -{ - PyInterpreterState *interp = _PyInterpreterState_GET(); - int override = interp->override_frozen_modules; - if (override > 0) { - return true; - } - else if (override < 0) { - return false; - } - else { - return interp->config.use_frozen_modules; - } -} - -static PyObject * -list_frozen_module_names(void) -{ - PyObject *names = PyList_New(0); - if (names == NULL) { + PyObject *names = PyList_New(0); + if (names == NULL) { return NULL; } bool enabled = use_frozen(); @@ -1492,6 +1273,11 @@ unmarshal_frozen_code(struct frozen_info *info) } +static PyObject * module_dict_for_exec(PyThreadState *tstate, PyObject *name); +static PyObject * exec_code_in_module(PyThreadState *tstate, + PyObject *name, PyObject *module_dict, + PyObject *code_object); + /* Initialize a frozen module. Return 1 for success, 0 if the module is not found, and -1 with an exception set if the initialization failed. @@ -1555,38 +1341,347 @@ PyImport_ImportFrozenModuleObject(PyObject *name) goto err_return; } } - else { - origname = Py_NewRef(Py_None); + else { + origname = Py_NewRef(Py_None); + } + err = PyDict_SetItemString(d, "__origname__", origname); + Py_DECREF(origname); + if (err != 0) { + goto err_return; + } + Py_DECREF(d); + Py_DECREF(co); + return 1; + +err_return: + Py_XDECREF(d); + Py_DECREF(co); + return -1; +} + +int +PyImport_ImportFrozenModule(const char *name) +{ + PyObject *nameobj; + int ret; + nameobj = PyUnicode_InternFromString(name); + if (nameobj == NULL) + return -1; + ret = PyImport_ImportFrozenModuleObject(nameobj); + Py_DECREF(nameobj); + return ret; +} + + +/**************************/ +/* a module's code object */ +/**************************/ + +/* Execute a code object in a module and return the module object + * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is + * removed from sys.modules, to avoid leaving damaged module objects + * in sys.modules. The caller may wish to restore the original + * module object (if any) in this case; PyImport_ReloadModule is an + * example. + * + * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer + * interface. The other two exist primarily for backward compatibility. + */ +PyObject * +PyImport_ExecCodeModule(const char *name, PyObject *co) +{ + return PyImport_ExecCodeModuleWithPathnames( + name, co, (char *)NULL, (char *)NULL); +} + +PyObject * +PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) +{ + return PyImport_ExecCodeModuleWithPathnames( + name, co, pathname, (char *)NULL); +} + +PyObject * +PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, + const char *pathname, + const char *cpathname) +{ + PyObject *m = NULL; + PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; + + nameobj = PyUnicode_FromString(name); + if (nameobj == NULL) + return NULL; + + if (cpathname != NULL) { + cpathobj = PyUnicode_DecodeFSDefault(cpathname); + if (cpathobj == NULL) + goto error; + } + else + cpathobj = NULL; + + if (pathname != NULL) { + pathobj = PyUnicode_DecodeFSDefault(pathname); + if (pathobj == NULL) + goto error; + } + else if (cpathobj != NULL) { + PyInterpreterState *interp = _PyInterpreterState_GET(); + + if (interp == NULL) { + Py_FatalError("no current interpreter"); + } + + external= PyObject_GetAttrString(interp->importlib, + "_bootstrap_external"); + if (external != NULL) { + pathobj = _PyObject_CallMethodOneArg( + external, &_Py_ID(_get_sourcefile), cpathobj); + Py_DECREF(external); + } + if (pathobj == NULL) + PyErr_Clear(); + } + else + pathobj = NULL; + + m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); +error: + Py_DECREF(nameobj); + Py_XDECREF(pathobj); + Py_XDECREF(cpathobj); + return m; +} + +static PyObject * +module_dict_for_exec(PyThreadState *tstate, PyObject *name) +{ + PyObject *m, *d; + + m = import_add_module(tstate, name); + if (m == NULL) + return NULL; + /* If the module is being reloaded, we get the old module back + and re-use its dict to exec the new code. */ + d = PyModule_GetDict(m); + int r = PyDict_Contains(d, &_Py_ID(__builtins__)); + if (r == 0) { + r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins()); + } + if (r < 0) { + remove_module(tstate, name); + Py_DECREF(m); + return NULL; + } + + Py_INCREF(d); + Py_DECREF(m); + return d; +} + +static PyObject * +exec_code_in_module(PyThreadState *tstate, PyObject *name, + PyObject *module_dict, PyObject *code_object) +{ + PyObject *v, *m; + + v = PyEval_EvalCode(code_object, module_dict, module_dict); + if (v == NULL) { + remove_module(tstate, name); + return NULL; + } + Py_DECREF(v); + + m = import_get_module(tstate, name); + if (m == NULL && !_PyErr_Occurred(tstate)) { + _PyErr_Format(tstate, PyExc_ImportError, + "Loaded module %R not found in sys.modules", + name); + } + + return m; +} + +PyObject* +PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, + PyObject *cpathname) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *d, *external, *res; + + d = module_dict_for_exec(tstate, name); + if (d == NULL) { + return NULL; + } + + if (pathname == NULL) { + pathname = ((PyCodeObject *)co)->co_filename; + } + external = PyObject_GetAttrString(tstate->interp->importlib, + "_bootstrap_external"); + if (external == NULL) { + Py_DECREF(d); + return NULL; + } + res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module), + d, name, pathname, cpathname, NULL); + Py_DECREF(external); + if (res != NULL) { + Py_DECREF(res); + res = exec_code_in_module(tstate, name, d, co); + } + Py_DECREF(d); + return res; +} + + +static void +update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) +{ + PyObject *constants, *tmp; + Py_ssize_t i, n; + + if (PyUnicode_Compare(co->co_filename, oldname)) + return; + + Py_XSETREF(co->co_filename, Py_NewRef(newname)); + + constants = co->co_consts; + n = PyTuple_GET_SIZE(constants); + for (i = 0; i < n; i++) { + tmp = PyTuple_GET_ITEM(constants, i); + if (PyCode_Check(tmp)) + update_code_filenames((PyCodeObject *)tmp, + oldname, newname); + } +} + +static void +update_compiled_module(PyCodeObject *co, PyObject *newname) +{ + PyObject *oldname; + + if (PyUnicode_Compare(co->co_filename, newname) == 0) + return; + + oldname = co->co_filename; + Py_INCREF(oldname); + update_code_filenames(co, oldname, newname); + Py_DECREF(oldname); +} + + +/********************/ +/* the magic number */ +/********************/ + +/* Helper for pythonrun.c -- return magic number and tag. */ + +long +PyImport_GetMagicNumber(void) +{ + long res; + PyInterpreterState *interp = _PyInterpreterState_GET(); + PyObject *external, *pyc_magic; + + external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); + if (external == NULL) + return -1; + pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); + Py_DECREF(external); + if (pyc_magic == NULL) + return -1; + res = PyLong_AsLong(pyc_magic); + Py_DECREF(pyc_magic); + return res; +} + + +extern const char * _PySys_ImplCacheTag; + +const char * +PyImport_GetMagicTag(void) +{ + return _PySys_ImplCacheTag; +} + + +/*******************/ + +/* Return a finder object for a sys.path/pkg.__path__ item 'p', + possibly by fetching it from the path_importer_cache dict. If it + wasn't yet cached, traverse path_hooks until a hook is found + that can handle the path item. Return None if no hook could; + this tells our caller that the path based finder could not find + a finder for this path item. Cache the result in + path_importer_cache. */ + +static PyObject * +get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, + PyObject *path_hooks, PyObject *p) +{ + PyObject *importer; + Py_ssize_t j, nhooks; + + /* These conditions are the caller's responsibility: */ + assert(PyList_Check(path_hooks)); + assert(PyDict_Check(path_importer_cache)); + + nhooks = PyList_Size(path_hooks); + if (nhooks < 0) + return NULL; /* Shouldn't happen */ + + importer = PyDict_GetItemWithError(path_importer_cache, p); + if (importer != NULL || _PyErr_Occurred(tstate)) { + return Py_XNewRef(importer); + } + + /* set path_importer_cache[p] to None to avoid recursion */ + if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) + return NULL; + + for (j = 0; j < nhooks; j++) { + PyObject *hook = PyList_GetItem(path_hooks, j); + if (hook == NULL) + return NULL; + importer = PyObject_CallOneArg(hook, p); + if (importer != NULL) + break; + + if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) { + return NULL; + } + _PyErr_Clear(tstate); + } + if (importer == NULL) { + Py_RETURN_NONE; } - err = PyDict_SetItemString(d, "__origname__", origname); - Py_DECREF(origname); - if (err != 0) { - goto err_return; + if (PyDict_SetItem(path_importer_cache, p, importer) < 0) { + Py_DECREF(importer); + return NULL; } - Py_DECREF(d); - Py_DECREF(co); - return 1; - -err_return: - Py_XDECREF(d); - Py_DECREF(co); - return -1; + return importer; } -int -PyImport_ImportFrozenModule(const char *name) +PyObject * +PyImport_GetImporter(PyObject *path) { - PyObject *nameobj; - int ret; - nameobj = PyUnicode_InternFromString(name); - if (nameobj == NULL) - return -1; - ret = PyImport_ImportFrozenModuleObject(nameobj); - Py_DECREF(nameobj); - return ret; + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *path_importer_cache = PySys_GetObject("path_importer_cache"); + PyObject *path_hooks = PySys_GetObject("path_hooks"); + if (path_importer_cache == NULL || path_hooks == NULL) { + return NULL; + } + return get_path_importer(tstate, path_importer_cache, path_hooks, path); } +/*********************/ +/* importing modules */ +/*********************/ + + /* Import a module, either built-in, frozen, or external, and return its module object WITH INCREMENTED REFERENCE COUNT */ @@ -1913,23 +2008,6 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) #undef accumulated } -PyObject * -PyImport_GetModule(PyObject *name) -{ - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *mod; - - mod = import_get_module(tstate, name); - if (mod != NULL && mod != Py_None) { - if (import_ensure_initialized(tstate->interp, mod, name) < 0) { - Py_DECREF(mod); - remove_importlib_frames(tstate); - return NULL; - } - } - return mod; -} - PyObject * PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, @@ -2105,96 +2183,248 @@ PyImport_ReloadModule(PyObject *m) return NULL; } - importlib = PyImport_ImportModule("importlib"); - if (importlib == NULL) { - return NULL; - } - } + importlib = PyImport_ImportModule("importlib"); + if (importlib == NULL) { + return NULL; + } + } + + reloaded_module = PyObject_CallMethodOneArg(importlib, &_Py_ID(reload), m); + Py_DECREF(importlib); + return reloaded_module; +} + + +/* Higher-level import emulator which emulates the "import" statement + more accurately -- it invokes the __import__() function from the + builtins of the current globals. This means that the import is + done using whatever import hooks are installed in the current + environment. + A dummy list ["__doc__"] is passed as the 4th argument so that + e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) + will return instead of . */ + +PyObject * +PyImport_Import(PyObject *module_name) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *globals = NULL; + PyObject *import = NULL; + PyObject *builtins = NULL; + PyObject *r = NULL; + + PyObject *from_list = PyList_New(0); + if (from_list == NULL) { + goto err; + } + + /* Get the builtins from current globals */ + globals = PyEval_GetGlobals(); + if (globals != NULL) { + Py_INCREF(globals); + builtins = PyObject_GetItem(globals, &_Py_ID(__builtins__)); + if (builtins == NULL) + goto err; + } + else { + /* No globals -- use standard builtins, and fake globals */ + builtins = PyImport_ImportModuleLevel("builtins", + NULL, NULL, NULL, 0); + if (builtins == NULL) { + goto err; + } + globals = Py_BuildValue("{OO}", &_Py_ID(__builtins__), builtins); + if (globals == NULL) + goto err; + } + + /* Get the __import__ function from the builtins */ + if (PyDict_Check(builtins)) { + import = PyObject_GetItem(builtins, &_Py_ID(__import__)); + if (import == NULL) { + _PyErr_SetObject(tstate, PyExc_KeyError, &_Py_ID(__import__)); + } + } + else + import = PyObject_GetAttr(builtins, &_Py_ID(__import__)); + if (import == NULL) + goto err; + + /* Call the __import__ function with the proper argument list + Always use absolute import here. + Calling for side-effect of import. */ + r = PyObject_CallFunction(import, "OOOOi", module_name, globals, + globals, from_list, 0, NULL); + if (r == NULL) + goto err; + Py_DECREF(r); + + r = import_get_module(tstate, module_name); + if (r == NULL && !_PyErr_Occurred(tstate)) { + _PyErr_SetObject(tstate, PyExc_KeyError, module_name); + } + + err: + Py_XDECREF(globals); + Py_XDECREF(builtins); + Py_XDECREF(import); + Py_XDECREF(from_list); + + return r; +} + + +/******************/ +/* module helpers */ +/******************/ + +PyObject * +_PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname) +{ + PyObject *mod = PyImport_Import(modname); + if (mod == NULL) { + return NULL; + } + PyObject *result = PyObject_GetAttr(mod, attrname); + Py_DECREF(mod); + return result; +} + +PyObject * +_PyImport_GetModuleAttrString(const char *modname, const char *attrname) +{ + PyObject *pmodname = PyUnicode_FromString(modname); + if (pmodname == NULL) { + return NULL; + } + PyObject *pattrname = PyUnicode_FromString(attrname); + if (pattrname == NULL) { + Py_DECREF(pmodname); + return NULL; + } + PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname); + Py_DECREF(pattrname); + Py_DECREF(pmodname); + return result; +} + + +/**************/ +/* the module */ +/**************/ + +/*[clinic input] +_imp.lock_held + +Return True if the import lock is currently held, else False. + +On platforms without threads, return False. +[clinic start generated code]*/ + +static PyObject * +_imp_lock_held_impl(PyObject *module) +/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/ +{ + return PyBool_FromLong(import_lock_thread != PYTHREAD_INVALID_THREAD_ID); +} + +/*[clinic input] +_imp.acquire_lock + +Acquires the interpreter's import lock for the current thread. + +This lock should be used by import hooks to ensure thread-safety when importing +modules. On platforms without threads, this function does nothing. +[clinic start generated code]*/ + +static PyObject * +_imp_acquire_lock_impl(PyObject *module) +/*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/ +{ + _PyImport_AcquireLock(); + Py_RETURN_NONE; +} + +/*[clinic input] +_imp.release_lock + +Release the interpreter's import lock. + +On platforms without threads, this function does nothing. +[clinic start generated code]*/ + +static PyObject * +_imp_release_lock_impl(PyObject *module) +/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/ +{ + if (_PyImport_ReleaseLock() < 0) { + PyErr_SetString(PyExc_RuntimeError, + "not holding the import lock"); + return NULL; + } + Py_RETURN_NONE; +} + + +/*[clinic input] +_imp._fix_co_filename + + code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") + Code object to change. + + path: unicode + File path to use. + / + +Changes code.co_filename to specify the passed-in file path. +[clinic start generated code]*/ + +static PyObject * +_imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code, + PyObject *path) +/*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/ + +{ + update_compiled_module(code, path); - reloaded_module = PyObject_CallMethodOneArg(importlib, &_Py_ID(reload), m); - Py_DECREF(importlib); - return reloaded_module; + Py_RETURN_NONE; } -/* Higher-level import emulator which emulates the "import" statement - more accurately -- it invokes the __import__() function from the - builtins of the current globals. This means that the import is - done using whatever import hooks are installed in the current - environment. - A dummy list ["__doc__"] is passed as the 4th argument so that - e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) - will return instead of . */ +/*[clinic input] +_imp.create_builtin -PyObject * -PyImport_Import(PyObject *module_name) -{ - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *globals = NULL; - PyObject *import = NULL; - PyObject *builtins = NULL; - PyObject *r = NULL; + spec: object + / - PyObject *from_list = PyList_New(0); - if (from_list == NULL) { - goto err; - } +Create an extension module. +[clinic start generated code]*/ - /* Get the builtins from current globals */ - globals = PyEval_GetGlobals(); - if (globals != NULL) { - Py_INCREF(globals); - builtins = PyObject_GetItem(globals, &_Py_ID(__builtins__)); - if (builtins == NULL) - goto err; - } - else { - /* No globals -- use standard builtins, and fake globals */ - builtins = PyImport_ImportModuleLevel("builtins", - NULL, NULL, NULL, 0); - if (builtins == NULL) { - goto err; - } - globals = Py_BuildValue("{OO}", &_Py_ID(__builtins__), builtins); - if (globals == NULL) - goto err; - } +static PyObject * +_imp_create_builtin(PyObject *module, PyObject *spec) +/*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/ +{ + PyThreadState *tstate = _PyThreadState_GET(); - /* Get the __import__ function from the builtins */ - if (PyDict_Check(builtins)) { - import = PyObject_GetItem(builtins, &_Py_ID(__import__)); - if (import == NULL) { - _PyErr_SetObject(tstate, PyExc_KeyError, &_Py_ID(__import__)); - } + PyObject *name = PyObject_GetAttrString(spec, "name"); + if (name == NULL) { + return NULL; } - else - import = PyObject_GetAttr(builtins, &_Py_ID(__import__)); - if (import == NULL) - goto err; - - /* Call the __import__ function with the proper argument list - Always use absolute import here. - Calling for side-effect of import. */ - r = PyObject_CallFunction(import, "OOOOi", module_name, globals, - globals, from_list, 0, NULL); - if (r == NULL) - goto err; - Py_DECREF(r); - r = import_get_module(tstate, module_name); - if (r == NULL && !_PyErr_Occurred(tstate)) { - _PyErr_SetObject(tstate, PyExc_KeyError, module_name); + if (!PyUnicode_Check(name)) { + PyErr_Format(PyExc_TypeError, + "name must be string, not %.200s", + Py_TYPE(name)->tp_name); + Py_DECREF(name); + return NULL; } - err: - Py_XDECREF(globals); - Py_XDECREF(builtins); - Py_XDECREF(import); - Py_XDECREF(from_list); - - return r; + PyObject *mod = create_builtin(tstate, name, spec); + Py_DECREF(name); + return mod; } + /*[clinic input] _imp.extension_suffixes @@ -2674,158 +2904,6 @@ PyInit__imp(void) } -// Import the _imp extension by calling manually _imp.create_builtin() and -// _imp.exec_builtin() since importlib is not initialized yet. Initializing -// importlib requires the _imp module: this function fix the bootstrap issue. -PyObject* -_PyImport_BootstrapImp(PyThreadState *tstate) -{ - PyObject *name = PyUnicode_FromString("_imp"); - if (name == NULL) { - return NULL; - } - - // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec(): - // an object with just a name attribute. - // - // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway. - PyObject *attrs = Py_BuildValue("{sO}", "name", name); - if (attrs == NULL) { - goto error; - } - PyObject *spec = _PyNamespace_New(attrs); - Py_DECREF(attrs); - if (spec == NULL) { - goto error; - } - - // Create the _imp module from its definition. - PyObject *mod = create_builtin(tstate, name, spec); - Py_CLEAR(name); - Py_DECREF(spec); - if (mod == NULL) { - goto error; - } - assert(mod != Py_None); // not found - - // Execute the _imp module: call imp_module_exec(). - if (exec_builtin_or_dynamic(mod) < 0) { - Py_DECREF(mod); - goto error; - } - return mod; - -error: - Py_XDECREF(name); - return NULL; -} - - -/* API for embedding applications that want to add their own entries - to the table of built-in modules. This should normally be called - *before* Py_Initialize(). When the table resize fails, -1 is - returned and the existing table is unchanged. - - After a similar function by Just van Rossum. */ - -int -PyImport_ExtendInittab(struct _inittab *newtab) -{ - struct _inittab *p; - size_t i, n; - int res = 0; - - if (_PyRuntime.imports.inittab != NULL) { - Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); - } - - /* Count the number of entries in both tables */ - for (n = 0; newtab[n].name != NULL; n++) - ; - if (n == 0) - return 0; /* Nothing to do */ - for (i = 0; PyImport_Inittab[i].name != NULL; i++) - ; - - /* Force default raw memory allocator to get a known allocator to be able - to release the memory in _PyImport_Fini2() */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - /* Allocate new memory for the combined table */ - p = NULL; - if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { - size_t size = sizeof(struct _inittab) * (i + n + 1); - p = PyMem_RawRealloc(inittab_copy, size); - } - if (p == NULL) { - res = -1; - goto done; - } - - /* Copy the tables into the new memory at the first call - to PyImport_ExtendInittab(). */ - if (inittab_copy != PyImport_Inittab) { - memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); - } - memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); - PyImport_Inittab = inittab_copy = p; - -done: - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - return res; -} - -/* Shorthand to add a single entry given a name and a function */ - -int -PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) -{ - struct _inittab newtab[2]; - - if (_PyRuntime.imports.inittab != NULL) { - Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); - } - - memset(newtab, '\0', sizeof newtab); - - newtab[0].name = name; - newtab[0].initfunc = initfunc; - - return PyImport_ExtendInittab(newtab); -} - - -PyObject * -_PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname) -{ - PyObject *mod = PyImport_Import(modname); - if (mod == NULL) { - return NULL; - } - PyObject *result = PyObject_GetAttr(mod, attrname); - Py_DECREF(mod); - return result; -} - -PyObject * -_PyImport_GetModuleAttrString(const char *modname, const char *attrname) -{ - PyObject *pmodname = PyUnicode_FromString(modname); - if (pmodname == NULL) { - return NULL; - } - PyObject *pattrname = PyUnicode_FromString(attrname); - if (pattrname == NULL) { - Py_DECREF(pmodname); - return NULL; - } - PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname); - Py_DECREF(pattrname); - Py_DECREF(pmodname); - return result; -} - #ifdef __cplusplus } #endif From 7626bab66ff2ee0e74a3618ebaa996c5afc0eb02 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:01:02 -0700 Subject: [PATCH 02/22] Move PyState_*() to import.c. --- Python/import.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++ Python/pystate.c | 104 ----------------------------------------------- 2 files changed, 104 insertions(+), 104 deletions(-) diff --git a/Python/import.c b/Python/import.c index 10ac46782707b3..90026401efa438 100644 --- a/Python/import.c +++ b/Python/import.c @@ -539,6 +539,110 @@ remove_module(PyThreadState *tstate, PyObject *name) } +/************************************/ +/* per-interpreter modules-by-index */ +/************************************/ + +PyObject* +PyState_FindModule(PyModuleDef* module) +{ + Py_ssize_t index = module->m_base.m_index; + PyInterpreterState *state = _PyInterpreterState_GET(); + PyObject *res; + if (module->m_slots) { + return NULL; + } + if (index == 0) + return NULL; + if (state->modules_by_index == NULL) + return NULL; + if (index >= PyList_GET_SIZE(state->modules_by_index)) + return NULL; + res = PyList_GET_ITEM(state->modules_by_index, index); + return res==Py_None ? NULL : res; +} + +int +_PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def) +{ + if (!def) { + assert(_PyErr_Occurred(tstate)); + return -1; + } + if (def->m_slots) { + _PyErr_SetString(tstate, + PyExc_SystemError, + "PyState_AddModule called on module with slots"); + return -1; + } + + PyInterpreterState *interp = tstate->interp; + if (!interp->modules_by_index) { + interp->modules_by_index = PyList_New(0); + if (!interp->modules_by_index) { + return -1; + } + } + + while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) { + if (PyList_Append(interp->modules_by_index, Py_None) < 0) { + return -1; + } + } + + return PyList_SetItem(interp->modules_by_index, + def->m_base.m_index, Py_NewRef(module)); +} + +int +PyState_AddModule(PyObject* module, PyModuleDef* def) +{ + if (!def) { + Py_FatalError("module definition is NULL"); + return -1; + } + + PyThreadState *tstate = _PyThreadState_GET(); + PyInterpreterState *interp = tstate->interp; + Py_ssize_t index = def->m_base.m_index; + if (interp->modules_by_index && + index < PyList_GET_SIZE(interp->modules_by_index) && + module == PyList_GET_ITEM(interp->modules_by_index, index)) + { + _Py_FatalErrorFormat(__func__, "module %p already added", module); + return -1; + } + return _PyState_AddModule(tstate, module, def); +} + +int +PyState_RemoveModule(PyModuleDef* def) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyInterpreterState *interp = tstate->interp; + + if (def->m_slots) { + _PyErr_SetString(tstate, + PyExc_SystemError, + "PyState_RemoveModule called on module with slots"); + return -1; + } + + Py_ssize_t index = def->m_base.m_index; + if (index == 0) { + Py_FatalError("invalid module index"); + } + if (interp->modules_by_index == NULL) { + Py_FatalError("Interpreters module-list not accessible."); + } + if (index > PyList_GET_SIZE(interp->modules_by_index)) { + Py_FatalError("Module index out of bounds."); + } + + return PyList_SetItem(interp->modules_by_index, index, Py_NewRef(Py_None)); +} + + /*****************************/ /* single-phase init modules */ /*****************************/ diff --git a/Python/pystate.c b/Python/pystate.c index 1261092d1435fa..f0f88ad0637c9c 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1922,110 +1922,6 @@ _PyThread_CurrentExceptions(void) } -/****************/ -/* module state */ -/****************/ - -PyObject* -PyState_FindModule(PyModuleDef* module) -{ - Py_ssize_t index = module->m_base.m_index; - PyInterpreterState *state = _PyInterpreterState_GET(); - PyObject *res; - if (module->m_slots) { - return NULL; - } - if (index == 0) - return NULL; - if (state->modules_by_index == NULL) - return NULL; - if (index >= PyList_GET_SIZE(state->modules_by_index)) - return NULL; - res = PyList_GET_ITEM(state->modules_by_index, index); - return res==Py_None ? NULL : res; -} - -int -_PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def) -{ - if (!def) { - assert(_PyErr_Occurred(tstate)); - return -1; - } - if (def->m_slots) { - _PyErr_SetString(tstate, - PyExc_SystemError, - "PyState_AddModule called on module with slots"); - return -1; - } - - PyInterpreterState *interp = tstate->interp; - if (!interp->modules_by_index) { - interp->modules_by_index = PyList_New(0); - if (!interp->modules_by_index) { - return -1; - } - } - - while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) { - if (PyList_Append(interp->modules_by_index, Py_None) < 0) { - return -1; - } - } - - return PyList_SetItem(interp->modules_by_index, - def->m_base.m_index, Py_NewRef(module)); -} - -int -PyState_AddModule(PyObject* module, PyModuleDef* def) -{ - if (!def) { - Py_FatalError("module definition is NULL"); - return -1; - } - - PyThreadState *tstate = current_fast_get(&_PyRuntime); - PyInterpreterState *interp = tstate->interp; - Py_ssize_t index = def->m_base.m_index; - if (interp->modules_by_index && - index < PyList_GET_SIZE(interp->modules_by_index) && - module == PyList_GET_ITEM(interp->modules_by_index, index)) - { - _Py_FatalErrorFormat(__func__, "module %p already added", module); - return -1; - } - return _PyState_AddModule(tstate, module, def); -} - -int -PyState_RemoveModule(PyModuleDef* def) -{ - PyThreadState *tstate = current_fast_get(&_PyRuntime); - PyInterpreterState *interp = tstate->interp; - - if (def->m_slots) { - _PyErr_SetString(tstate, - PyExc_SystemError, - "PyState_RemoveModule called on module with slots"); - return -1; - } - - Py_ssize_t index = def->m_base.m_index; - if (index == 0) { - Py_FatalError("invalid module index"); - } - if (interp->modules_by_index == NULL) { - Py_FatalError("Interpreters module-list not accessible."); - } - if (index > PyList_GET_SIZE(interp->modules_by_index)) { - Py_FatalError("Module index out of bounds."); - } - - return PyList_SetItem(interp->modules_by_index, index, Py_NewRef(Py_None)); -} - - /***********************************/ /* Python "auto thread state" API. */ /***********************************/ From 5836e93b8574547fc29f6f5712e8357462395e90 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:05:00 -0700 Subject: [PATCH 03/22] Add _PyImport_GetNextModuleIndex(). --- Include/internal/pycore_import.h | 2 ++ Objects/moduleobject.c | 3 +-- Python/import.c | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 9036dff6725330..9afd202b7cf083 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -36,6 +36,8 @@ struct _import_runtime_state { const char * pkgcontext; }; +extern Py_ssize_t _PyImport_GetNextModuleIndex(void); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 24190e320ee6d6..f7bdbb045960c4 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -42,10 +42,9 @@ PyModuleDef_Init(PyModuleDef* def) { assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY); if (def->m_base.m_index == 0) { - _PyRuntime.imports.last_module_index++; Py_SET_REFCNT(def, 1); Py_SET_TYPE(def, &PyModuleDef_Type); - def->m_base.m_index = _PyRuntime.imports.last_module_index; + def->m_base.m_index = _PyImport_GetNextModuleIndex(); } return (PyObject*)def; } diff --git a/Python/import.c b/Python/import.c index 90026401efa438..27ddd4f9810275 100644 --- a/Python/import.c +++ b/Python/import.c @@ -543,6 +543,13 @@ remove_module(PyThreadState *tstate, PyObject *name) /* per-interpreter modules-by-index */ /************************************/ +Py_ssize_t +_PyImport_GetNextModuleIndex(void) +{ + _PyRuntime.imports.last_module_index++; + return _PyRuntime.imports.last_module_index; +} + PyObject* PyState_FindModule(PyModuleDef* module) { From bc031c17f78245a482fc344daf1be79698ea7c5e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:20:23 -0700 Subject: [PATCH 04/22] Add _PyImport_SwapPackageContext() and _PyImport_ResolveNameWithPackageContext(). --- Include/internal/pycore_import.h | 2 ++ Objects/moduleobject.c | 19 +----------------- Python/import.c | 33 ++++++++++++++++++++++++++++++++ Python/importdl.c | 17 +++++++--------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 9afd202b7cf083..96e671a35290f9 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -37,6 +37,8 @@ struct _import_runtime_state { }; extern Py_ssize_t _PyImport_GetNextModuleIndex(void); +extern const char * _PyImport_ResolveNameWithPackageContext(const char *name); +extern const char * _PyImport_SwapPackageContext(const char *newcontext); #ifdef HAVE_FORK diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index f7bdbb045960c4..32d5a8c3092dfb 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -208,24 +208,7 @@ _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version) "module %s: PyModule_Create is incompatible with m_slots", name); return NULL; } - /* Make sure name is fully qualified. - - This is a bit of a hack: when the shared library is loaded, - the module name is "package.module", but the module calls - PyModule_Create*() with just "module" for the name. The shared - library loader squirrels away the true name of the module in - _Py_PackageContext, and PyModule_Create*() will substitute this - (if the name actually matches). - */ -#define _Py_PackageContext (_PyRuntime.imports.pkgcontext) - if (_Py_PackageContext != NULL) { - const char *p = strrchr(_Py_PackageContext, '.'); - if (p != NULL && strcmp(module->m_name, p+1) == 0) { - name = _Py_PackageContext; - _Py_PackageContext = NULL; - } - } -#undef _Py_PackageContext + name = _PyImport_ResolveNameWithPackageContext(name); if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) return NULL; diff --git a/Python/import.c b/Python/import.c index 27ddd4f9810275..730e79359ba6f8 100644 --- a/Python/import.c +++ b/Python/import.c @@ -56,6 +56,8 @@ static struct _inittab *inittab_copy = NULL; #define import_lock_thread _PyRuntime.imports.lock.thread #define import_lock_level _PyRuntime.imports.lock.level +#define _Py_PackageContext (_PyRuntime.imports.pkgcontext) + /*********************/ /* runtime lifecycle */ @@ -719,6 +721,37 @@ Generally, when multiple interpreters are involved, some of the above gets even messier. */ +/* Make sure name is fully qualified. + + This is a bit of a hack: when the shared library is loaded, + the module name is "package.module", but the module calls + PyModule_Create*() with just "module" for the name. The shared + library loader squirrels away the true name of the module in + _Py_PackageContext, and PyModule_Create*() will substitute this + (if the name actually matches). +*/ +const char * +_PyImport_ResolveNameWithPackageContext(const char *name) +{ + if (_Py_PackageContext != NULL) { + const char *p = strrchr(_Py_PackageContext, '.'); + if (p != NULL && strcmp(name, p+1) == 0) { + name = _Py_PackageContext; + _Py_PackageContext = NULL; + } + } + return name; +} + +const char * +_PyImport_SwapPackageContext(const char *newcontext) +{ + const char *oldcontext = _Py_PackageContext; + _Py_PackageContext = newcontext; + return oldcontext; +} + + /* Magic for extension modules (built-in as well as dynamically loaded). To prevent initializing an extension module more than once, we keep a static dictionary 'extensions' keyed by the tuple diff --git a/Python/importdl.c b/Python/importdl.c index 91fa06f49c2897..6dafb4541486e9 100644 --- a/Python/importdl.c +++ b/Python/importdl.c @@ -99,7 +99,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) #endif PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL; const char *name_buf, *hook_prefix; - const char *oldcontext; + const char *oldcontext, *newcontext; dl_funcptr exportfunc; PyModuleDef *def; PyModInitFunction p0; @@ -113,6 +113,10 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) "spec.name must be a string"); goto error; } + newcontext = PyUnicode_AsUTF8(name_unicode); + if (newcontext == NULL) { + goto error; + } name = get_encoded_name(name_unicode, &hook_prefix); if (name == NULL) { @@ -160,16 +164,9 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) p0 = (PyModInitFunction)exportfunc; /* Package context is needed for single-phase init */ -#define _Py_PackageContext (_PyRuntime.imports.pkgcontext) - oldcontext = _Py_PackageContext; - _Py_PackageContext = PyUnicode_AsUTF8(name_unicode); - if (_Py_PackageContext == NULL) { - _Py_PackageContext = oldcontext; - goto error; - } + oldcontext = _PyImport_SwapPackageContext(newcontext); m = _PyImport_InitFunc_TrampolineCall(p0); - _Py_PackageContext = oldcontext; -#undef _Py_PackageContext + _PyImport_SwapPackageContext(oldcontext); if (m == NULL) { if (!PyErr_Occurred()) { From f67c2990f46569e9b5dfa3ff296673f07b0468fa Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:24:19 -0700 Subject: [PATCH 05/22] Add _PyImport_GetBuiltinModuleNames(). --- Include/internal/pycore_import.h | 2 ++ Python/import.c | 25 +++++++++++++++++++++++++ Python/sysmodule.c | 14 +------------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 96e671a35290f9..b9075e15e03c57 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -46,6 +46,8 @@ extern PyStatus _PyImport_ReInitLock(void); #endif extern PyObject* _PyImport_BootstrapImp(PyThreadState *tstate); +extern PyObject* _PyImport_GetBuiltinModuleNames(void); + struct _module_alias { const char *name; /* ASCII encoded string */ const char *orig; /* ASCII encoded string */ diff --git a/Python/import.c b/Python/import.c index 730e79359ba6f8..10518516e10bac 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1112,6 +1112,31 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) } +PyObject * +_PyImport_GetBuiltinModuleNames(void) +{ + PyObject *list = PyList_New(0); + if (list == NULL) { + return NULL; + } + struct _inittab *inittab = _PyRuntime.imports.inittab; + for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { + PyObject *name = PyUnicode_FromString(inittab[i].name); + if (name == NULL) { + Py_DECREF(list); + return NULL; + } + if (PyList_Append(list, name) < 0) { + Py_DECREF(name); + Py_DECREF(list); + return NULL; + } + Py_DECREF(name); + } + return list; +} + + /******************/ /* frozen modules */ /******************/ diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6e81ef92b67f70..ba2207d4d22986 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2279,22 +2279,10 @@ static PyMethodDef sys_methods[] = { static PyObject * list_builtin_module_names(void) { - PyObject *list = PyList_New(0); + PyObject *list = _PyImport_GetBuiltinModuleNames(); if (list == NULL) { return NULL; } - struct _inittab *inittab = _PyRuntime.imports.inittab; - for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { - PyObject *name = PyUnicode_FromString(inittab[i].name); - if (name == NULL) { - goto error; - } - if (PyList_Append(list, name) < 0) { - Py_DECREF(name); - goto error; - } - Py_DECREF(name); - } if (PyList_Sort(list) != 0) { goto error; } From 9b312e13dfbb4cde87c310c816bd6ebcd0ee40f9 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:29:30 -0700 Subject: [PATCH 06/22] Add an "extension modules" section. --- Python/import.c | 86 ++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/Python/import.c b/Python/import.c index 10518516e10bac..e25aaebfa2027e 100644 --- a/Python/import.c +++ b/Python/import.c @@ -652,6 +652,51 @@ PyState_RemoveModule(PyModuleDef* def) } +/*********************/ +/* extension modules */ +/*********************/ + +/* Make sure name is fully qualified. + + This is a bit of a hack: when the shared library is loaded, + the module name is "package.module", but the module calls + PyModule_Create*() with just "module" for the name. The shared + library loader squirrels away the true name of the module in + _Py_PackageContext, and PyModule_Create*() will substitute this + (if the name actually matches). +*/ +const char * +_PyImport_ResolveNameWithPackageContext(const char *name) +{ + if (_Py_PackageContext != NULL) { + const char *p = strrchr(_Py_PackageContext, '.'); + if (p != NULL && strcmp(name, p+1) == 0) { + name = _Py_PackageContext; + _Py_PackageContext = NULL; + } + } + return name; +} + +const char * +_PyImport_SwapPackageContext(const char *newcontext) +{ + const char *oldcontext = _Py_PackageContext; + _Py_PackageContext = newcontext; + return oldcontext; +} + + +/*******************/ + +#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) +#include +EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { + return wasmTable.get(func)(); +}); +#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE + + /*****************************/ /* single-phase init modules */ /*****************************/ @@ -721,37 +766,6 @@ Generally, when multiple interpreters are involved, some of the above gets even messier. */ -/* Make sure name is fully qualified. - - This is a bit of a hack: when the shared library is loaded, - the module name is "package.module", but the module calls - PyModule_Create*() with just "module" for the name. The shared - library loader squirrels away the true name of the module in - _Py_PackageContext, and PyModule_Create*() will substitute this - (if the name actually matches). -*/ -const char * -_PyImport_ResolveNameWithPackageContext(const char *name) -{ - if (_Py_PackageContext != NULL) { - const char *p = strrchr(_Py_PackageContext, '.'); - if (p != NULL && strcmp(name, p+1) == 0) { - name = _Py_PackageContext; - _Py_PackageContext = NULL; - } - } - return name; -} - -const char * -_PyImport_SwapPackageContext(const char *newcontext) -{ - const char *oldcontext = _Py_PackageContext; - _Py_PackageContext = newcontext; - return oldcontext; -} - - /* Magic for extension modules (built-in as well as dynamically loaded). To prevent initializing an extension module more than once, we keep a static dictionary 'extensions' keyed by the tuple @@ -932,16 +946,6 @@ import_find_extension(PyThreadState *tstate, PyObject *name, } -/*******************/ - -#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) -#include -EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { - return wasmTable.get(func)(); -}); -#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE - - /*******************/ /* builtin modules */ /*******************/ From 24ec4dec3b1eeed3ce7ad78f805c87a58be678d6 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:33:08 -0700 Subject: [PATCH 07/22] Move the "extension modules" section down. --- Python/import.c | 1491 ++++++++++++++++++++++++----------------------- 1 file changed, 748 insertions(+), 743 deletions(-) diff --git a/Python/import.c b/Python/import.c index e25aaebfa2027e..055a86cd0ed61e 100644 --- a/Python/import.c +++ b/Python/import.c @@ -652,903 +652,908 @@ PyState_RemoveModule(PyModuleDef* def) } -/*********************/ -/* extension modules */ -/*********************/ +/*******************/ +/* builtin modules */ +/*******************/ -/* Make sure name is fully qualified. +static int fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename); - This is a bit of a hack: when the shared library is loaded, - the module name is "package.module", but the module calls - PyModule_Create*() with just "module" for the name. The shared - library loader squirrels away the true name of the module in - _Py_PackageContext, and PyModule_Create*() will substitute this - (if the name actually matches). -*/ -const char * -_PyImport_ResolveNameWithPackageContext(const char *name) +int +_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) { - if (_Py_PackageContext != NULL) { - const char *p = strrchr(_Py_PackageContext, '.'); - if (p != NULL && strcmp(name, p+1) == 0) { - name = _Py_PackageContext; - _Py_PackageContext = NULL; - } + int res = -1; + PyObject *nameobj; + nameobj = PyUnicode_InternFromString(name); + if (nameobj == NULL) { + return -1; } - return name; + if (PyObject_SetItem(modules, nameobj, mod) < 0) { + goto finally; + } + if (fix_up_extension(mod, nameobj, nameobj) < 0) { + PyMapping_DelItem(modules, nameobj); + goto finally; + } + res = 0; + +finally: + Py_DECREF(nameobj); + return res; } -const char * -_PyImport_SwapPackageContext(const char *newcontext) +/* Helper to test for built-in module */ + +static int +is_builtin(PyObject *name) { - const char *oldcontext = _Py_PackageContext; - _Py_PackageContext = newcontext; - return oldcontext; + int i; + struct _inittab *inittab = _PyRuntime.imports.inittab; + for (i = 0; inittab[i].name != NULL; i++) { + if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { + if (inittab[i].initfunc == NULL) + return -1; + else + return 1; + } + } + return 0; } +static PyObject * import_find_extension(PyThreadState *tstate, + PyObject *name, PyObject *filename); -/*******************/ +static PyObject* +create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) +{ + PyObject *mod = import_find_extension(tstate, name, name); + if (mod || _PyErr_Occurred(tstate)) { + return mod; + } -#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) -#include -EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { - return wasmTable.get(func)(); -}); -#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE + PyObject *modules = tstate->interp->modules; + for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { + if (_PyUnicode_EqualToASCIIString(name, p->name)) { + if (p->initfunc == NULL) { + /* Cannot re-init internal module ("sys" or "builtins") */ + mod = PyImport_AddModuleObject(name); + return Py_XNewRef(mod); + } + mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); + if (mod == NULL) { + return NULL; + } + + if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { + return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); + } + else { + /* Remember pointer to module init function. */ + PyModuleDef *def = PyModule_GetDef(mod); + if (def == NULL) { + return NULL; + } + + def->m_base.m_init = p->initfunc; + if (_PyImport_FixupExtensionObject(mod, name, name, + modules) < 0) { + return NULL; + } + return mod; + } + } + } + + // not found + Py_RETURN_NONE; +} /*****************************/ -/* single-phase init modules */ +/* the builtin modules table */ /*****************************/ -/* -We support a number of kinds of single-phase init builtin/extension modules: +/* API for embedding applications that want to add their own entries + to the table of built-in modules. This should normally be called + *before* Py_Initialize(). When the table resize fails, -1 is + returned and the existing table is unchanged. -* "basic" - * no module state (PyModuleDef.m_size == -1) - * does not support repeated init (we use PyModuleDef.m_base.m_copy) - * may have process-global state - * the module's def is cached in _PyRuntime.imports.extensions, - by (name, filename) -* "reinit" - * no module state (PyModuleDef.m_size == 0) - * supports repeated init (m_copy is never used) - * should not have any process-global state - * its def is never cached in _PyRuntime.imports.extensions - (except, currently, under the main interpreter, for some reason) -* "with state" (almost the same as reinit) - * has module state (PyModuleDef.m_size > 0) - * supports repeated init (m_copy is never used) - * should not have any process-global state - * its def is never cached in _PyRuntime.imports.extensions - (except, currently, under the main interpreter, for some reason) + After a similar function by Just van Rossum. */ -There are also variants within those classes: +int +PyImport_ExtendInittab(struct _inittab *newtab) +{ + struct _inittab *p; + size_t i, n; + int res = 0; -* two or more modules share a PyModuleDef - * a module's init func uses another module's PyModuleDef - * a module's init func calls another's module's init func - * a module's init "func" is actually a variable statically initialized - to another module's init func -* two or modules share "methods" - * a module's init func copies another module's PyModuleDef - (with a different name) -* (basic-only) two or modules share process-global state + if (_PyRuntime.imports.inittab != NULL) { + Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); + } -In the first case, where modules share a PyModuleDef, the following -notable weirdness happens: + /* Count the number of entries in both tables */ + for (n = 0; newtab[n].name != NULL; n++) + ; + if (n == 0) + return 0; /* Nothing to do */ + for (i = 0; PyImport_Inittab[i].name != NULL; i++) + ; -* the module's __name__ matches the def, not the requested name -* the last module (with the same def) to be imported for the first time wins - * returned by PyState_Find_Module() (via interp->modules_by_index) - * (non-basic-only) its init func is used when re-loading any of them - (via the def's m_init) - * (basic-only) the copy of its __dict__ is used when re-loading any of them - (via the def's m_copy) + /* Force default raw memory allocator to get a known allocator to be able + to release the memory in _PyImport_Fini2() */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); -However, the following happens as expected: + /* Allocate new memory for the combined table */ + p = NULL; + if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { + size_t size = sizeof(struct _inittab) * (i + n + 1); + p = PyMem_RawRealloc(inittab_copy, size); + } + if (p == NULL) { + res = -1; + goto done; + } -* a new module object (with its own __dict__) is created for each request -* the module's __spec__ has the requested name -* the loaded module is cached in sys.modules under the requested name -* the m_index field of the shared def is not changed, - so at least PyState_FindModule() will always look in the same place + /* Copy the tables into the new memory at the first call + to PyImport_ExtendInittab(). */ + if (inittab_copy != PyImport_Inittab) { + memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); + } + memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); + PyImport_Inittab = inittab_copy = p; -For "basic" modules there are other quirks: +done: + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return res; +} -* (whether sharing a def or not) when loaded the first time, - m_copy is set before _init_module_attrs() is called - in importlib._bootstrap.module_from_spec(), - so when the module is re-loaded, the previous value - for __wpec__ (and others) is reset, possibly unexpectedly. +/* Shorthand to add a single entry given a name and a function */ -Generally, when multiple interpreters are involved, some of the above -gets even messier. -*/ +int +PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) +{ + struct _inittab newtab[2]; -/* Magic for extension modules (built-in as well as dynamically - loaded). To prevent initializing an extension module more than - once, we keep a static dictionary 'extensions' keyed by the tuple - (module name, module name) (for built-in modules) or by - (filename, module name) (for dynamically loaded modules), containing these - modules. A copy of the module's dictionary is stored by calling - _PyImport_FixupExtensionObject() immediately after the module initialization - function succeeds. A copy can be retrieved from there by calling - import_find_extension(). + if (_PyRuntime.imports.inittab != NULL) { + Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); + } - Modules which do support multiple initialization set their m_size - field to a non-negative number (indicating the size of the - module-specific state). They are still recorded in the extensions - dictionary, to avoid loading shared libraries twice. -*/ + memset(newtab, '\0', sizeof newtab); -static PyModuleDef * -_extensions_cache_get(PyObject *filename, PyObject *name) + newtab[0].name = name; + newtab[0].initfunc = initfunc; + + return PyImport_ExtendInittab(newtab); +} + + +PyObject * +_PyImport_GetBuiltinModuleNames(void) { - PyObject *extensions = _PyRuntime.imports.extensions; - if (extensions == NULL) { - return NULL; - } - PyObject *key = PyTuple_Pack(2, filename, name); - if (key == NULL) { + PyObject *list = PyList_New(0); + if (list == NULL) { return NULL; } - PyModuleDef *def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); - Py_DECREF(key); - return def; -} - -static int -_extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def) -{ - PyObject *extensions = _PyRuntime.imports.extensions; - if (extensions == NULL) { - extensions = PyDict_New(); - if (extensions == NULL) { - return -1; + struct _inittab *inittab = _PyRuntime.imports.inittab; + for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { + PyObject *name = PyUnicode_FromString(inittab[i].name); + if (name == NULL) { + Py_DECREF(list); + return NULL; } - _PyRuntime.imports.extensions = extensions; - } - PyObject *key = PyTuple_Pack(2, filename, name); - if (key == NULL) { - return -1; - } - int res = PyDict_SetItem(extensions, key, (PyObject *)def); - Py_DECREF(key); - if (res < 0) { - return -1; + if (PyList_Append(list, name) < 0) { + Py_DECREF(name); + Py_DECREF(list); + return NULL; + } + Py_DECREF(name); } - return 0; -} - -static void -_extensions_cache_clear(void) -{ - Py_CLEAR(_PyRuntime.imports.extensions); + return list; } -static int -fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename) -{ - if (mod == NULL || !PyModule_Check(mod)) { - PyErr_BadInternalCall(); - return -1; - } - - struct PyModuleDef *def = PyModule_GetDef(mod); - if (!def) { - PyErr_BadInternalCall(); - return -1; - } - PyThreadState *tstate = _PyThreadState_GET(); - if (_PyState_AddModule(tstate, mod, def) < 0) { - return -1; - } +/******************/ +/* frozen modules */ +/******************/ - // bpo-44050: Extensions and def->m_base.m_copy can be updated - // when the extension module doesn't support sub-interpreters. - // XXX Why special-case the main interpreter? - if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) { - if (def->m_size == -1) { - if (def->m_base.m_copy) { - /* Somebody already imported the module, - likely under a different name. - XXX this should really not happen. */ - Py_CLEAR(def->m_base.m_copy); - } - PyObject *dict = PyModule_GetDict(mod); - if (dict == NULL) { - return -1; - } - def->m_base.m_copy = PyDict_Copy(dict); - if (def->m_base.m_copy == NULL) { - return -1; - } +/* Return true if the name is an alias. In that case, "alias" is set + to the original module name. If it is an alias but the original + module isn't known then "alias" is set to NULL while true is returned. */ +static bool +resolve_module_alias(const char *name, const struct _module_alias *aliases, + const char **alias) +{ + const struct _module_alias *entry; + for (entry = aliases; ; entry++) { + if (entry->name == NULL) { + /* It isn't an alias. */ + return false; } - - if (_extensions_cache_set(filename, name, def) < 0) { - return -1; + if (strcmp(name, entry->name) == 0) { + if (alias != NULL) { + *alias = entry->orig; + } + return true; } } - - return 0; } -int -_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, - PyObject *filename, PyObject *modules) +static bool +use_frozen(void) { - if (PyObject_SetItem(modules, name, mod) < 0) { - return -1; + PyInterpreterState *interp = _PyInterpreterState_GET(); + int override = interp->override_frozen_modules; + if (override > 0) { + return true; } - if (fix_up_extension(mod, name, filename) < 0) { - PyMapping_DelItem(modules, name); - return -1; + else if (override < 0) { + return false; + } + else { + return interp->config.use_frozen_modules; } - return 0; } - static PyObject * -import_find_extension(PyThreadState *tstate, PyObject *name, - PyObject *filename) +list_frozen_module_names(void) { - /* Only single-phase init modules will be in the cache. */ - PyModuleDef *def = _extensions_cache_get(filename, name); - if (def == NULL) { + PyObject *names = PyList_New(0); + if (names == NULL) { return NULL; } - - PyObject *mod, *mdict; - PyObject *modules = tstate->interp->modules; - - if (def->m_size == -1) { - /* Module does not support repeated initialization */ - if (def->m_base.m_copy == NULL) - return NULL; - mod = import_add_module(tstate, name); - if (mod == NULL) - return NULL; - mdict = PyModule_GetDict(mod); - if (mdict == NULL) { - Py_DECREF(mod); - return NULL; + bool enabled = use_frozen(); + const struct _frozen *p; +#define ADD_MODULE(name) \ + do { \ + PyObject *nameobj = PyUnicode_FromString(name); \ + if (nameobj == NULL) { \ + goto error; \ + } \ + int res = PyList_Append(names, nameobj); \ + Py_DECREF(nameobj); \ + if (res != 0) { \ + goto error; \ + } \ + } while(0) + // We always use the bootstrap modules. + for (p = _PyImport_FrozenBootstrap; ; p++) { + if (p->name == NULL) { + break; } - if (PyDict_Update(mdict, def->m_base.m_copy)) { - Py_DECREF(mod); - return NULL; + ADD_MODULE(p->name); + } + // Frozen stdlib modules may be disabled. + for (p = _PyImport_FrozenStdlib; ; p++) { + if (p->name == NULL) { + break; + } + if (enabled) { + ADD_MODULE(p->name); } } - else { - if (def->m_base.m_init == NULL) - return NULL; - mod = _PyImport_InitFunc_TrampolineCall(def->m_base.m_init); - if (mod == NULL) - return NULL; - if (PyObject_SetItem(modules, name, mod) == -1) { - Py_DECREF(mod); - return NULL; + for (p = _PyImport_FrozenTest; ; p++) { + if (p->name == NULL) { + break; + } + if (enabled) { + ADD_MODULE(p->name); } } - if (_PyState_AddModule(tstate, mod, def) < 0) { - PyMapping_DelItem(modules, name); - Py_DECREF(mod); - return NULL; +#undef ADD_MODULE + // Add any custom modules. + if (PyImport_FrozenModules != NULL) { + for (p = PyImport_FrozenModules; ; p++) { + if (p->name == NULL) { + break; + } + PyObject *nameobj = PyUnicode_FromString(p->name); + if (nameobj == NULL) { + goto error; + } + int found = PySequence_Contains(names, nameobj); + if (found < 0) { + Py_DECREF(nameobj); + goto error; + } + else if (found) { + Py_DECREF(nameobj); + } + else { + int res = PyList_Append(names, nameobj); + Py_DECREF(nameobj); + if (res != 0) { + goto error; + } + } + } } + return names; - int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; - if (verbose) { - PySys_FormatStderr("import %U # previously loaded (%R)\n", - name, filename); - } - return mod; +error: + Py_DECREF(names); + return NULL; } +typedef enum { + FROZEN_OKAY, + FROZEN_BAD_NAME, // The given module name wasn't valid. + FROZEN_NOT_FOUND, // It wasn't in PyImport_FrozenModules. + FROZEN_DISABLED, // -X frozen_modules=off (and not essential) + FROZEN_EXCLUDED, /* The PyImport_FrozenModules entry has NULL "code" + (module is present but marked as unimportable, stops search). */ + FROZEN_INVALID, /* The PyImport_FrozenModules entry is bogus + (eg. does not contain executable code). */ +} frozen_status; -/*******************/ -/* builtin modules */ -/*******************/ - -int -_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) +static inline void +set_frozen_error(frozen_status status, PyObject *modname) { - int res = -1; - PyObject *nameobj; - nameobj = PyUnicode_InternFromString(name); - if (nameobj == NULL) { - return -1; - } - if (PyObject_SetItem(modules, nameobj, mod) < 0) { - goto finally; + const char *err = NULL; + switch (status) { + case FROZEN_BAD_NAME: + case FROZEN_NOT_FOUND: + err = "No such frozen object named %R"; + break; + case FROZEN_DISABLED: + err = "Frozen modules are disabled and the frozen object named %R is not essential"; + break; + case FROZEN_EXCLUDED: + err = "Excluded frozen object named %R"; + break; + case FROZEN_INVALID: + err = "Frozen object named %R is invalid"; + break; + case FROZEN_OKAY: + // There was no error. + break; + default: + Py_UNREACHABLE(); } - if (fix_up_extension(mod, nameobj, nameobj) < 0) { - PyMapping_DelItem(modules, nameobj); - goto finally; + if (err != NULL) { + PyObject *msg = PyUnicode_FromFormat(err, modname); + if (msg == NULL) { + PyErr_Clear(); + } + PyErr_SetImportError(msg, modname, NULL); + Py_XDECREF(msg); } - res = 0; - -finally: - Py_DECREF(nameobj); - return res; } -/* Helper to test for built-in module */ - -static int -is_builtin(PyObject *name) +static const struct _frozen * +look_up_frozen(const char *name) { - int i; - struct _inittab *inittab = _PyRuntime.imports.inittab; - for (i = 0; inittab[i].name != NULL; i++) { - if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { - if (inittab[i].initfunc == NULL) - return -1; - else - return 1; + const struct _frozen *p; + // We always use the bootstrap modules. + for (p = _PyImport_FrozenBootstrap; ; p++) { + if (p->name == NULL) { + // We hit the end-of-list sentinel value. + break; + } + if (strcmp(name, p->name) == 0) { + return p; } } - return 0; -} - -static PyObject* -create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) -{ - PyObject *mod = import_find_extension(tstate, name, name); - if (mod || _PyErr_Occurred(tstate)) { - return mod; + // Prefer custom modules, if any. Frozen stdlib modules can be + // disabled here by setting "code" to NULL in the array entry. + if (PyImport_FrozenModules != NULL) { + for (p = PyImport_FrozenModules; ; p++) { + if (p->name == NULL) { + break; + } + if (strcmp(name, p->name) == 0) { + return p; + } + } } - - PyObject *modules = tstate->interp->modules; - for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { - if (_PyUnicode_EqualToASCIIString(name, p->name)) { - if (p->initfunc == NULL) { - /* Cannot re-init internal module ("sys" or "builtins") */ - mod = PyImport_AddModuleObject(name); - return Py_XNewRef(mod); + // Frozen stdlib modules may be disabled. + if (use_frozen()) { + for (p = _PyImport_FrozenStdlib; ; p++) { + if (p->name == NULL) { + break; } - mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); - if (mod == NULL) { - return NULL; + if (strcmp(name, p->name) == 0) { + return p; } - - if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { - return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); + } + for (p = _PyImport_FrozenTest; ; p++) { + if (p->name == NULL) { + break; } - else { - /* Remember pointer to module init function. */ - PyModuleDef *def = PyModule_GetDef(mod); - if (def == NULL) { - return NULL; - } - - def->m_base.m_init = p->initfunc; - if (_PyImport_FixupExtensionObject(mod, name, name, - modules) < 0) { - return NULL; - } - return mod; + if (strcmp(name, p->name) == 0) { + return p; } } } - - // not found - Py_RETURN_NONE; + return NULL; } +struct frozen_info { + PyObject *nameobj; + const char *data; + PyObject *(*get_code)(void); + Py_ssize_t size; + bool is_package; + bool is_alias; + const char *origname; +}; -/*****************************/ -/* the builtin modules table */ -/*****************************/ - -/* API for embedding applications that want to add their own entries - to the table of built-in modules. This should normally be called - *before* Py_Initialize(). When the table resize fails, -1 is - returned and the existing table is unchanged. - - After a similar function by Just van Rossum. */ - -int -PyImport_ExtendInittab(struct _inittab *newtab) +static frozen_status +find_frozen(PyObject *nameobj, struct frozen_info *info) { - struct _inittab *p; - size_t i, n; - int res = 0; - - if (_PyRuntime.imports.inittab != NULL) { - Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); + if (info != NULL) { + memset(info, 0, sizeof(*info)); } - /* Count the number of entries in both tables */ - for (n = 0; newtab[n].name != NULL; n++) - ; - if (n == 0) - return 0; /* Nothing to do */ - for (i = 0; PyImport_Inittab[i].name != NULL; i++) - ; - - /* Force default raw memory allocator to get a known allocator to be able - to release the memory in _PyImport_Fini2() */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - /* Allocate new memory for the combined table */ - p = NULL; - if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { - size_t size = sizeof(struct _inittab) * (i + n + 1); - p = PyMem_RawRealloc(inittab_copy, size); + if (nameobj == NULL || nameobj == Py_None) { + return FROZEN_BAD_NAME; } - if (p == NULL) { - res = -1; - goto done; + const char *name = PyUnicode_AsUTF8(nameobj); + if (name == NULL) { + // Note that this function previously used + // _PyUnicode_EqualToASCIIString(). We clear the error here + // (instead of propagating it) to match the earlier behavior + // more closely. + PyErr_Clear(); + return FROZEN_BAD_NAME; } - /* Copy the tables into the new memory at the first call - to PyImport_ExtendInittab(). */ - if (inittab_copy != PyImport_Inittab) { - memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); + const struct _frozen *p = look_up_frozen(name); + if (p == NULL) { + return FROZEN_NOT_FOUND; } - memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); - PyImport_Inittab = inittab_copy = p; - -done: - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - return res; -} - -/* Shorthand to add a single entry given a name and a function */ - -int -PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) -{ - struct _inittab newtab[2]; - - if (_PyRuntime.imports.inittab != NULL) { - Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); + if (info != NULL) { + info->nameobj = nameobj; // borrowed + info->data = (const char *)p->code; + info->get_code = p->get_code; + info->size = p->size; + info->is_package = p->is_package; + if (p->size < 0) { + // backward compatibility with negative size values + info->size = -(p->size); + info->is_package = true; + } + info->origname = name; + info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases, + &info->origname); } - - memset(newtab, '\0', sizeof newtab); - - newtab[0].name = name; - newtab[0].initfunc = initfunc; - - return PyImport_ExtendInittab(newtab); + if (p->code == NULL && p->size == 0 && p->get_code != NULL) { + /* It is only deepfrozen. */ + return FROZEN_OKAY; + } + if (p->code == NULL) { + /* It is frozen but marked as un-importable. */ + return FROZEN_EXCLUDED; + } + if (p->code[0] == '\0' || p->size == 0) { + /* Does not contain executable code. */ + return FROZEN_INVALID; + } + return FROZEN_OKAY; } - -PyObject * -_PyImport_GetBuiltinModuleNames(void) +static PyObject * +unmarshal_frozen_code(struct frozen_info *info) { - PyObject *list = PyList_New(0); - if (list == NULL) { - return NULL; - } - struct _inittab *inittab = _PyRuntime.imports.inittab; - for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { - PyObject *name = PyUnicode_FromString(inittab[i].name); - if (name == NULL) { - Py_DECREF(list); - return NULL; - } - if (PyList_Append(list, name) < 0) { - Py_DECREF(name); - Py_DECREF(list); - return NULL; - } - Py_DECREF(name); + if (info->get_code) { + PyObject *code = info->get_code(); + assert(code != NULL); + return code; } - return list; + PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size); + if (co == NULL) { + /* Does not contain executable code. */ + set_frozen_error(FROZEN_INVALID, info->nameobj); + return NULL; + } + if (!PyCode_Check(co)) { + // We stick with TypeError for backward compatibility. + PyErr_Format(PyExc_TypeError, + "frozen object %R is not a code object", + info->nameobj); + Py_DECREF(co); + return NULL; + } + return co; } -/******************/ -/* frozen modules */ -/******************/ +static PyObject * module_dict_for_exec(PyThreadState *tstate, PyObject *name); +static PyObject * exec_code_in_module(PyThreadState *tstate, + PyObject *name, PyObject *module_dict, + PyObject *code_object); -/* Return true if the name is an alias. In that case, "alias" is set - to the original module name. If it is an alias but the original - module isn't known then "alias" is set to NULL while true is returned. */ -static bool -resolve_module_alias(const char *name, const struct _module_alias *aliases, - const char **alias) -{ - const struct _module_alias *entry; - for (entry = aliases; ; entry++) { - if (entry->name == NULL) { - /* It isn't an alias. */ - return false; - } - if (strcmp(name, entry->name) == 0) { - if (alias != NULL) { - *alias = entry->orig; - } - return true; - } - } -} +/* Initialize a frozen module. + Return 1 for success, 0 if the module is not found, and -1 with + an exception set if the initialization failed. + This function is also used from frozenmain.c */ -static bool -use_frozen(void) +int +PyImport_ImportFrozenModuleObject(PyObject *name) { - PyInterpreterState *interp = _PyInterpreterState_GET(); - int override = interp->override_frozen_modules; - if (override > 0) { - return true; + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *co, *m, *d = NULL; + int err; + + struct frozen_info info; + frozen_status status = find_frozen(name, &info); + if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) { + return 0; } - else if (override < 0) { - return false; + else if (status == FROZEN_BAD_NAME) { + return 0; } - else { - return interp->config.use_frozen_modules; + else if (status != FROZEN_OKAY) { + set_frozen_error(status, name); + return -1; } -} - -static PyObject * -list_frozen_module_names(void) -{ - PyObject *names = PyList_New(0); - if (names == NULL) { - return NULL; + co = unmarshal_frozen_code(&info); + if (co == NULL) { + return -1; } - bool enabled = use_frozen(); - const struct _frozen *p; -#define ADD_MODULE(name) \ - do { \ - PyObject *nameobj = PyUnicode_FromString(name); \ - if (nameobj == NULL) { \ - goto error; \ - } \ - int res = PyList_Append(names, nameobj); \ - Py_DECREF(nameobj); \ - if (res != 0) { \ - goto error; \ - } \ - } while(0) - // We always use the bootstrap modules. - for (p = _PyImport_FrozenBootstrap; ; p++) { - if (p->name == NULL) { - break; + if (info.is_package) { + /* Set __path__ to the empty list */ + PyObject *l; + m = import_add_module(tstate, name); + if (m == NULL) + goto err_return; + d = PyModule_GetDict(m); + l = PyList_New(0); + if (l == NULL) { + Py_DECREF(m); + goto err_return; } - ADD_MODULE(p->name); + err = PyDict_SetItemString(d, "__path__", l); + Py_DECREF(l); + Py_DECREF(m); + if (err != 0) + goto err_return; } - // Frozen stdlib modules may be disabled. - for (p = _PyImport_FrozenStdlib; ; p++) { - if (p->name == NULL) { - break; - } - if (enabled) { - ADD_MODULE(p->name); - } + d = module_dict_for_exec(tstate, name); + if (d == NULL) { + goto err_return; } - for (p = _PyImport_FrozenTest; ; p++) { - if (p->name == NULL) { - break; - } - if (enabled) { - ADD_MODULE(p->name); - } + m = exec_code_in_module(tstate, name, d, co); + if (m == NULL) { + goto err_return; } -#undef ADD_MODULE - // Add any custom modules. - if (PyImport_FrozenModules != NULL) { - for (p = PyImport_FrozenModules; ; p++) { - if (p->name == NULL) { - break; - } - PyObject *nameobj = PyUnicode_FromString(p->name); - if (nameobj == NULL) { - goto error; - } - int found = PySequence_Contains(names, nameobj); - if (found < 0) { - Py_DECREF(nameobj); - goto error; - } - else if (found) { - Py_DECREF(nameobj); - } - else { - int res = PyList_Append(names, nameobj); - Py_DECREF(nameobj); - if (res != 0) { - goto error; - } - } + Py_DECREF(m); + /* Set __origname__ (consumed in FrozenImporter._setup_module()). */ + PyObject *origname; + if (info.origname) { + origname = PyUnicode_FromString(info.origname); + if (origname == NULL) { + goto err_return; } } - return names; + else { + origname = Py_NewRef(Py_None); + } + err = PyDict_SetItemString(d, "__origname__", origname); + Py_DECREF(origname); + if (err != 0) { + goto err_return; + } + Py_DECREF(d); + Py_DECREF(co); + return 1; -error: - Py_DECREF(names); - return NULL; +err_return: + Py_XDECREF(d); + Py_DECREF(co); + return -1; } -typedef enum { - FROZEN_OKAY, - FROZEN_BAD_NAME, // The given module name wasn't valid. - FROZEN_NOT_FOUND, // It wasn't in PyImport_FrozenModules. - FROZEN_DISABLED, // -X frozen_modules=off (and not essential) - FROZEN_EXCLUDED, /* The PyImport_FrozenModules entry has NULL "code" - (module is present but marked as unimportable, stops search). */ - FROZEN_INVALID, /* The PyImport_FrozenModules entry is bogus - (eg. does not contain executable code). */ -} frozen_status; +int +PyImport_ImportFrozenModule(const char *name) +{ + PyObject *nameobj; + int ret; + nameobj = PyUnicode_InternFromString(name); + if (nameobj == NULL) + return -1; + ret = PyImport_ImportFrozenModuleObject(nameobj); + Py_DECREF(nameobj); + return ret; +} -static inline void -set_frozen_error(frozen_status status, PyObject *modname) + +/*********************/ +/* extension modules */ +/*********************/ + +/* Make sure name is fully qualified. + + This is a bit of a hack: when the shared library is loaded, + the module name is "package.module", but the module calls + PyModule_Create*() with just "module" for the name. The shared + library loader squirrels away the true name of the module in + _Py_PackageContext, and PyModule_Create*() will substitute this + (if the name actually matches). +*/ +const char * +_PyImport_ResolveNameWithPackageContext(const char *name) { - const char *err = NULL; - switch (status) { - case FROZEN_BAD_NAME: - case FROZEN_NOT_FOUND: - err = "No such frozen object named %R"; - break; - case FROZEN_DISABLED: - err = "Frozen modules are disabled and the frozen object named %R is not essential"; - break; - case FROZEN_EXCLUDED: - err = "Excluded frozen object named %R"; - break; - case FROZEN_INVALID: - err = "Frozen object named %R is invalid"; - break; - case FROZEN_OKAY: - // There was no error. - break; - default: - Py_UNREACHABLE(); - } - if (err != NULL) { - PyObject *msg = PyUnicode_FromFormat(err, modname); - if (msg == NULL) { - PyErr_Clear(); + if (_Py_PackageContext != NULL) { + const char *p = strrchr(_Py_PackageContext, '.'); + if (p != NULL && strcmp(name, p+1) == 0) { + name = _Py_PackageContext; + _Py_PackageContext = NULL; } - PyErr_SetImportError(msg, modname, NULL); - Py_XDECREF(msg); } + return name; } -static const struct _frozen * -look_up_frozen(const char *name) +const char * +_PyImport_SwapPackageContext(const char *newcontext) { - const struct _frozen *p; - // We always use the bootstrap modules. - for (p = _PyImport_FrozenBootstrap; ; p++) { - if (p->name == NULL) { - // We hit the end-of-list sentinel value. - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } - } - // Prefer custom modules, if any. Frozen stdlib modules can be - // disabled here by setting "code" to NULL in the array entry. - if (PyImport_FrozenModules != NULL) { - for (p = PyImport_FrozenModules; ; p++) { - if (p->name == NULL) { - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } - } - } - // Frozen stdlib modules may be disabled. - if (use_frozen()) { - for (p = _PyImport_FrozenStdlib; ; p++) { - if (p->name == NULL) { - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } - } - for (p = _PyImport_FrozenTest; ; p++) { - if (p->name == NULL) { - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } - } - } - return NULL; + const char *oldcontext = _Py_PackageContext; + _Py_PackageContext = newcontext; + return oldcontext; } -struct frozen_info { - PyObject *nameobj; - const char *data; - PyObject *(*get_code)(void); - Py_ssize_t size; - bool is_package; - bool is_alias; - const char *origname; -}; -static frozen_status -find_frozen(PyObject *nameobj, struct frozen_info *info) -{ - if (info != NULL) { - memset(info, 0, sizeof(*info)); - } +/*******************/ - if (nameobj == NULL || nameobj == Py_None) { - return FROZEN_BAD_NAME; - } - const char *name = PyUnicode_AsUTF8(nameobj); - if (name == NULL) { - // Note that this function previously used - // _PyUnicode_EqualToASCIIString(). We clear the error here - // (instead of propagating it) to match the earlier behavior - // more closely. - PyErr_Clear(); - return FROZEN_BAD_NAME; +#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) +#include +EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { + return wasmTable.get(func)(); +}); +#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE + + +/*****************************/ +/* single-phase init modules */ +/*****************************/ + +/* +We support a number of kinds of single-phase init builtin/extension modules: + +* "basic" + * no module state (PyModuleDef.m_size == -1) + * does not support repeated init (we use PyModuleDef.m_base.m_copy) + * may have process-global state + * the module's def is cached in _PyRuntime.imports.extensions, + by (name, filename) +* "reinit" + * no module state (PyModuleDef.m_size == 0) + * supports repeated init (m_copy is never used) + * should not have any process-global state + * its def is never cached in _PyRuntime.imports.extensions + (except, currently, under the main interpreter, for some reason) +* "with state" (almost the same as reinit) + * has module state (PyModuleDef.m_size > 0) + * supports repeated init (m_copy is never used) + * should not have any process-global state + * its def is never cached in _PyRuntime.imports.extensions + (except, currently, under the main interpreter, for some reason) + +There are also variants within those classes: + +* two or more modules share a PyModuleDef + * a module's init func uses another module's PyModuleDef + * a module's init func calls another's module's init func + * a module's init "func" is actually a variable statically initialized + to another module's init func +* two or modules share "methods" + * a module's init func copies another module's PyModuleDef + (with a different name) +* (basic-only) two or modules share process-global state + +In the first case, where modules share a PyModuleDef, the following +notable weirdness happens: + +* the module's __name__ matches the def, not the requested name +* the last module (with the same def) to be imported for the first time wins + * returned by PyState_Find_Module() (via interp->modules_by_index) + * (non-basic-only) its init func is used when re-loading any of them + (via the def's m_init) + * (basic-only) the copy of its __dict__ is used when re-loading any of them + (via the def's m_copy) + +However, the following happens as expected: + +* a new module object (with its own __dict__) is created for each request +* the module's __spec__ has the requested name +* the loaded module is cached in sys.modules under the requested name +* the m_index field of the shared def is not changed, + so at least PyState_FindModule() will always look in the same place + +For "basic" modules there are other quirks: + +* (whether sharing a def or not) when loaded the first time, + m_copy is set before _init_module_attrs() is called + in importlib._bootstrap.module_from_spec(), + so when the module is re-loaded, the previous value + for __wpec__ (and others) is reset, possibly unexpectedly. + +Generally, when multiple interpreters are involved, some of the above +gets even messier. +*/ + +/* Magic for extension modules (built-in as well as dynamically + loaded). To prevent initializing an extension module more than + once, we keep a static dictionary 'extensions' keyed by the tuple + (module name, module name) (for built-in modules) or by + (filename, module name) (for dynamically loaded modules), containing these + modules. A copy of the module's dictionary is stored by calling + _PyImport_FixupExtensionObject() immediately after the module initialization + function succeeds. A copy can be retrieved from there by calling + import_find_extension(). + + Modules which do support multiple initialization set their m_size + field to a non-negative number (indicating the size of the + module-specific state). They are still recorded in the extensions + dictionary, to avoid loading shared libraries twice. +*/ + +static PyModuleDef * +_extensions_cache_get(PyObject *filename, PyObject *name) +{ + PyObject *extensions = _PyRuntime.imports.extensions; + if (extensions == NULL) { + return NULL; } - - const struct _frozen *p = look_up_frozen(name); - if (p == NULL) { - return FROZEN_NOT_FOUND; + PyObject *key = PyTuple_Pack(2, filename, name); + if (key == NULL) { + return NULL; } - if (info != NULL) { - info->nameobj = nameobj; // borrowed - info->data = (const char *)p->code; - info->get_code = p->get_code; - info->size = p->size; - info->is_package = p->is_package; - if (p->size < 0) { - // backward compatibility with negative size values - info->size = -(p->size); - info->is_package = true; + PyModuleDef *def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); + Py_DECREF(key); + return def; +} + +static int +_extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def) +{ + PyObject *extensions = _PyRuntime.imports.extensions; + if (extensions == NULL) { + extensions = PyDict_New(); + if (extensions == NULL) { + return -1; } - info->origname = name; - info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases, - &info->origname); - } - if (p->code == NULL && p->size == 0 && p->get_code != NULL) { - /* It is only deepfrozen. */ - return FROZEN_OKAY; + _PyRuntime.imports.extensions = extensions; } - if (p->code == NULL) { - /* It is frozen but marked as un-importable. */ - return FROZEN_EXCLUDED; + PyObject *key = PyTuple_Pack(2, filename, name); + if (key == NULL) { + return -1; } - if (p->code[0] == '\0' || p->size == 0) { - /* Does not contain executable code. */ - return FROZEN_INVALID; + int res = PyDict_SetItem(extensions, key, (PyObject *)def); + Py_DECREF(key); + if (res < 0) { + return -1; } - return FROZEN_OKAY; + return 0; } -static PyObject * -unmarshal_frozen_code(struct frozen_info *info) +static void +_extensions_cache_clear(void) { - if (info->get_code) { - PyObject *code = info->get_code(); - assert(code != NULL); - return code; + Py_CLEAR(_PyRuntime.imports.extensions); +} + +static int +fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename) +{ + if (mod == NULL || !PyModule_Check(mod)) { + PyErr_BadInternalCall(); + return -1; } - PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size); - if (co == NULL) { - /* Does not contain executable code. */ - set_frozen_error(FROZEN_INVALID, info->nameobj); - return NULL; + + struct PyModuleDef *def = PyModule_GetDef(mod); + if (!def) { + PyErr_BadInternalCall(); + return -1; } - if (!PyCode_Check(co)) { - // We stick with TypeError for backward compatibility. - PyErr_Format(PyExc_TypeError, - "frozen object %R is not a code object", - info->nameobj); - Py_DECREF(co); - return NULL; + + PyThreadState *tstate = _PyThreadState_GET(); + if (_PyState_AddModule(tstate, mod, def) < 0) { + return -1; } - return co; -} + // bpo-44050: Extensions and def->m_base.m_copy can be updated + // when the extension module doesn't support sub-interpreters. + // XXX Why special-case the main interpreter? + if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) { + if (def->m_size == -1) { + if (def->m_base.m_copy) { + /* Somebody already imported the module, + likely under a different name. + XXX this should really not happen. */ + Py_CLEAR(def->m_base.m_copy); + } + PyObject *dict = PyModule_GetDict(mod); + if (dict == NULL) { + return -1; + } + def->m_base.m_copy = PyDict_Copy(dict); + if (def->m_base.m_copy == NULL) { + return -1; + } + } -static PyObject * module_dict_for_exec(PyThreadState *tstate, PyObject *name); -static PyObject * exec_code_in_module(PyThreadState *tstate, - PyObject *name, PyObject *module_dict, - PyObject *code_object); + if (_extensions_cache_set(filename, name, def) < 0) { + return -1; + } + } -/* Initialize a frozen module. - Return 1 for success, 0 if the module is not found, and -1 with - an exception set if the initialization failed. - This function is also used from frozenmain.c */ + return 0; +} int -PyImport_ImportFrozenModuleObject(PyObject *name) +_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, + PyObject *filename, PyObject *modules) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *co, *m, *d = NULL; - int err; - - struct frozen_info info; - frozen_status status = find_frozen(name, &info); - if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) { - return 0; - } - else if (status == FROZEN_BAD_NAME) { - return 0; - } - else if (status != FROZEN_OKAY) { - set_frozen_error(status, name); + if (PyObject_SetItem(modules, name, mod) < 0) { return -1; } - co = unmarshal_frozen_code(&info); - if (co == NULL) { + if (fix_up_extension(mod, name, filename) < 0) { + PyMapping_DelItem(modules, name); return -1; } - if (info.is_package) { - /* Set __path__ to the empty list */ - PyObject *l; - m = import_add_module(tstate, name); - if (m == NULL) - goto err_return; - d = PyModule_GetDict(m); - l = PyList_New(0); - if (l == NULL) { - Py_DECREF(m); - goto err_return; - } - err = PyDict_SetItemString(d, "__path__", l); - Py_DECREF(l); - Py_DECREF(m); - if (err != 0) - goto err_return; - } - d = module_dict_for_exec(tstate, name); - if (d == NULL) { - goto err_return; - } - m = exec_code_in_module(tstate, name, d, co); - if (m == NULL) { - goto err_return; + return 0; +} + + +static PyObject * +import_find_extension(PyThreadState *tstate, PyObject *name, + PyObject *filename) +{ + /* Only single-phase init modules will be in the cache. */ + PyModuleDef *def = _extensions_cache_get(filename, name); + if (def == NULL) { + return NULL; } - Py_DECREF(m); - /* Set __origname__ (consumed in FrozenImporter._setup_module()). */ - PyObject *origname; - if (info.origname) { - origname = PyUnicode_FromString(info.origname); - if (origname == NULL) { - goto err_return; + + PyObject *mod, *mdict; + PyObject *modules = tstate->interp->modules; + + if (def->m_size == -1) { + /* Module does not support repeated initialization */ + if (def->m_base.m_copy == NULL) + return NULL; + mod = import_add_module(tstate, name); + if (mod == NULL) + return NULL; + mdict = PyModule_GetDict(mod); + if (mdict == NULL) { + Py_DECREF(mod); + return NULL; + } + if (PyDict_Update(mdict, def->m_base.m_copy)) { + Py_DECREF(mod); + return NULL; } } else { - origname = Py_NewRef(Py_None); + if (def->m_base.m_init == NULL) + return NULL; + mod = _PyImport_InitFunc_TrampolineCall(def->m_base.m_init); + if (mod == NULL) + return NULL; + if (PyObject_SetItem(modules, name, mod) == -1) { + Py_DECREF(mod); + return NULL; + } } - err = PyDict_SetItemString(d, "__origname__", origname); - Py_DECREF(origname); - if (err != 0) { - goto err_return; + if (_PyState_AddModule(tstate, mod, def) < 0) { + PyMapping_DelItem(modules, name); + Py_DECREF(mod); + return NULL; } - Py_DECREF(d); - Py_DECREF(co); - return 1; - -err_return: - Py_XDECREF(d); - Py_DECREF(co); - return -1; -} -int -PyImport_ImportFrozenModule(const char *name) -{ - PyObject *nameobj; - int ret; - nameobj = PyUnicode_InternFromString(name); - if (nameobj == NULL) - return -1; - ret = PyImport_ImportFrozenModuleObject(nameobj); - Py_DECREF(nameobj); - return ret; + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + if (verbose) { + PySys_FormatStderr("import %U # previously loaded (%R)\n", + name, filename); + } + return mod; } -/**************************/ -/* a module's code object */ -/**************************/ +/*********************************/ +/* a Python module's code object */ +/*********************************/ /* Execute a code object in a module and return the module object * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is From b2043e4a988ce137eb952fd653ee69629e54d865 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 19:40:46 -0700 Subject: [PATCH 08/22] Add _PyImport_GetDLOpenFlags() and _PyImport_SetDLOpenFlags(). --- Include/internal/pycore_import.h | 3 +++ Python/dynload_shlib.c | 2 +- Python/import.c | 14 ++++++++++++++ Python/sysmodule.c | 5 +++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index b9075e15e03c57..06ae220794c015 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -40,6 +40,9 @@ extern Py_ssize_t _PyImport_GetNextModuleIndex(void); extern const char * _PyImport_ResolveNameWithPackageContext(const char *name); extern const char * _PyImport_SwapPackageContext(const char *newcontext); +extern int _PyImport_GetDLOpenFlags(PyInterpreterState *interp); +extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c index 3c5fd83df584d5..6761bba457983b 100644 --- a/Python/dynload_shlib.c +++ b/Python/dynload_shlib.c @@ -75,7 +75,7 @@ _PyImport_FindSharedFuncptr(const char *prefix, return NULL; } - dlopenflags = _PyInterpreterState_GET()->dlopenflags; + dlopenflags = _PyImport_GetDLOpenFlags(_PyInterpreterState_GET()); handle = dlopen(pathname, dlopenflags); diff --git a/Python/import.c b/Python/import.c index 055a86cd0ed61e..9ee4b94fdeda04 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1291,6 +1291,20 @@ _PyImport_SwapPackageContext(const char *newcontext) return oldcontext; } +#ifdef HAVE_DLOPEN +int +_PyImport_GetDLOpenFlags(PyInterpreterState *interp) +{ + return interp->dlopenflags; +} + +void +_PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val) +{ + interp->dlopenflags = new_val; +} +#endif // HAVE_DLOPEN + /*******************/ diff --git a/Python/sysmodule.c b/Python/sysmodule.c index ba2207d4d22986..4e33481f0c2246 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1650,7 +1650,7 @@ sys_setdlopenflags_impl(PyObject *module, int new_val) /*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/ { PyInterpreterState *interp = _PyInterpreterState_GET(); - interp->dlopenflags = new_val; + _PyImport_SetDLOpenFlags(interp, new_val); Py_RETURN_NONE; } @@ -1668,7 +1668,8 @@ sys_getdlopenflags_impl(PyObject *module) /*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/ { PyInterpreterState *interp = _PyInterpreterState_GET(); - return PyLong_FromLong(interp->dlopenflags); + return PyLong_FromLong( + _PyImport_GetDLOpenFlags(interp)); } #endif /* HAVE_DLOPEN */ From 43d8de7fbbb7c07d8d413edd40380569118ea4aa Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 20:03:30 -0700 Subject: [PATCH 09/22] Hide sys.modules. --- Include/internal/pycore_import.h | 6 ++++++ Python/_warnings.c | 2 +- Python/import.c | 29 +++++++++++++++++++++++++++++ Python/pylifecycle.c | 9 +++++---- Python/pystate.c | 13 +++++++++---- Python/sysmodule.c | 7 +++---- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 06ae220794c015..d7eda4cbabf697 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -36,6 +36,8 @@ struct _import_runtime_state { const char * pkgcontext; }; +extern void _PyImport_ClearCore(PyInterpreterState *interp); + extern Py_ssize_t _PyImport_GetNextModuleIndex(void); extern const char * _PyImport_ResolveNameWithPackageContext(const char *name); extern const char * _PyImport_SwapPackageContext(const char *newcontext); @@ -43,6 +45,10 @@ extern const char * _PyImport_SwapPackageContext(const char *newcontext); extern int _PyImport_GetDLOpenFlags(PyInterpreterState *interp); extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val); +extern PyObject * _PyImport_InitModules(PyInterpreterState *interp); +extern PyObject * _PyImport_GetModules(PyInterpreterState *interp); +extern void _PyImport_ClearModules(PyInterpreterState *interp); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Python/_warnings.c b/Python/_warnings.c index e78f21644f372b..9a70d022e95a57 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -214,7 +214,7 @@ get_warnings_attr(PyInterpreterState *interp, PyObject *attr, int try_import) gone, then we can't even use PyImport_GetModule without triggering an interpreter abort. */ - if (!interp->modules) { + if (!_PyImport_GetModules(interp)) { return NULL; } warnings_module = PyImport_GetModule(&_Py_ID(warnings)); diff --git a/Python/import.c b/Python/import.c index 9ee4b94fdeda04..d18e6de8dd7a3d 100644 --- a/Python/import.c +++ b/Python/import.c @@ -152,6 +152,12 @@ _PyImport_IsInitialized(PyInterpreterState *interp) return 1; } +void +_PyImport_ClearCore(PyInterpreterState *interp) +{ + Py_CLEAR(interp->modules); +} + static PyObject* create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec); static int exec_builtin_or_dynamic(PyObject *mod); @@ -333,6 +339,29 @@ _PyImport_ReInitLock(void) /* sys.modules */ /***************/ +PyObject * +_PyImport_InitModules(PyInterpreterState *interp) +{ + assert(interp->modules == NULL); + interp->modules = PyDict_New(); + if (interp->modules == NULL) { + return NULL; + } + return interp->modules; +} + +PyObject * +_PyImport_GetModules(PyInterpreterState *interp) +{ + return interp->modules; +} + +void +_PyImport_ClearModules(PyInterpreterState *interp) +{ + Py_SETREF(interp->modules, NULL); +} + PyObject * PyImport_GetModuleDict(void) { diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index a8a8e7f3d84f21..3d83f8b1b5619f 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -818,7 +818,8 @@ pycore_init_builtins(PyThreadState *tstate) goto error; } - if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) { + PyObject *modules = _PyImport_GetModules(interp); + if (_PyImport_FixupBuiltin(bimod, "builtins", modules) < 0) { goto error; } @@ -1584,7 +1585,7 @@ static void finalize_modules(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; - PyObject *modules = interp->modules; + PyObject *modules = _PyImport_GetModules(interp); if (modules == NULL) { // Already done return; @@ -1654,7 +1655,7 @@ finalize_modules(PyThreadState *tstate) // Clear and delete the modules directory. Actual modules will // still be there only if imported during the execution of some // destructor. - Py_SETREF(interp->modules, NULL); + _PyImport_ClearModules(interp); // Collect garbage once more _PyGC_CollectNoFail(tstate); @@ -2735,7 +2736,7 @@ _Py_DumpExtensionModules(int fd, PyInterpreterState *interp) if (interp == NULL) { return; } - PyObject *modules = interp->modules; + PyObject *modules = _PyImport_GetModules(interp); if (modules == NULL || !PyDict_Check(modules)) { return; } diff --git a/Python/pystate.c b/Python/pystate.c index f0f88ad0637c9c..02e71c01cee5c8 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -772,11 +772,13 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) Py_CLEAR(interp->codec_search_path); Py_CLEAR(interp->codec_search_cache); Py_CLEAR(interp->codec_error_registry); - Py_CLEAR(interp->modules); + + assert(interp->modules == NULL); Py_CLEAR(interp->modules_by_index); - Py_CLEAR(interp->builtins_copy); Py_CLEAR(interp->importlib); Py_CLEAR(interp->import_func); + + Py_CLEAR(interp->builtins_copy); Py_CLEAR(interp->dict); #ifdef HAVE_FORK Py_CLEAR(interp->before_forkers); @@ -836,6 +838,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp) // garbage. It can be different than the current Python thread state // of 'interp'. PyThreadState *current_tstate = current_fast_get(interp->runtime); + _PyImport_ClearCore(interp); interpreter_clear(interp, current_tstate); } @@ -843,6 +846,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp) void _PyInterpreterState_Clear(PyThreadState *tstate) { + _PyImport_ClearCore(tstate->interp); interpreter_clear(tstate->interp, tstate); } @@ -1058,11 +1062,12 @@ _PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required) PyObject * _PyInterpreterState_GetMainModule(PyInterpreterState *interp) { - if (interp->modules == NULL) { + PyObject *modules = _PyImport_GetModules(interp); + if (modules == NULL) { PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized"); return NULL; } - return PyMapping_GetItemString(interp->modules, "__main__"); + return PyMapping_GetItemString(modules, "__main__"); } PyObject * diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 4e33481f0c2246..b08897eaf9fd2f 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3400,11 +3400,10 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p) PyInterpreterState *interp = tstate->interp; - PyObject *modules = PyDict_New(); + PyObject *modules = _PyImport_InitModules(interp); if (modules == NULL) { goto error; } - interp->modules = modules; PyObject *sysmod = _PyModule_CreateInitialized(&sysmodule, PYTHON_API_VERSION); if (sysmod == NULL) { @@ -3417,7 +3416,7 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p) } interp->sysdict = Py_NewRef(sysdict); - if (PyDict_SetItemString(sysdict, "modules", interp->modules) < 0) { + if (PyDict_SetItemString(sysdict, "modules", modules) < 0) { goto error; } @@ -3431,7 +3430,7 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p) return status; } - if (_PyImport_FixupBuiltin(sysmod, "sys", interp->modules) < 0) { + if (_PyImport_FixupBuiltin(sysmod, "sys", modules) < 0) { goto error; } From b7cabebce6c1f0f1881d5f50ed6b0058a4dea4e8 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 20:08:55 -0700 Subject: [PATCH 10/22] _PyInterpreterState_ClearModules() -> _PyImport_ClearModulesByIndex(). --- Include/internal/pycore_import.h | 2 ++ Include/internal/pycore_interp.h | 1 - Python/import.c | 33 +++++++++++++++++++++++++++++++- Python/pylifecycle.c | 2 +- Python/pystate.c | 32 +------------------------------ 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index d7eda4cbabf697..501a1c58d24f40 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -49,6 +49,8 @@ extern PyObject * _PyImport_InitModules(PyInterpreterState *interp); extern PyObject * _PyImport_GetModules(PyInterpreterState *interp); extern void _PyImport_ClearModules(PyInterpreterState *interp); +extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h index 0e3d46852f2e6d..c11ae5522de76a 100644 --- a/Include/internal/pycore_interp.h +++ b/Include/internal/pycore_interp.h @@ -205,7 +205,6 @@ struct _is { /* other API */ -extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp); extern void _PyInterpreterState_Clear(PyThreadState *tstate); diff --git a/Python/import.c b/Python/import.c index d18e6de8dd7a3d..a6a357cc595d95 100644 --- a/Python/import.c +++ b/Python/import.c @@ -4,7 +4,7 @@ #include "pycore_import.h" // _PyImport_BootstrapImp() #include "pycore_initconfig.h" // _PyStatus_OK() -#include "pycore_interp.h" // _PyInterpreterState_ClearModules() +#include "pycore_interp.h" // struct _import_runtime_state #include "pycore_namespace.h" // _PyNamespace_Type #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pyhash.h" // _Py_KeyedHash() @@ -156,6 +156,7 @@ void _PyImport_ClearCore(PyInterpreterState *interp) { Py_CLEAR(interp->modules); + Py_CLEAR(interp->modules_by_index); } static PyObject* create_builtin(PyThreadState *tstate, @@ -681,6 +682,36 @@ PyState_RemoveModule(PyModuleDef* def) } +// Used by finalize_modules() +void +_PyImport_ClearModulesByIndex(PyInterpreterState *interp) +{ + if (!interp->modules_by_index) { + return; + } + + Py_ssize_t i; + for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) { + PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i); + if (PyModule_Check(m)) { + /* cleanup the saved copy of module dicts */ + PyModuleDef *md = PyModule_GetDef(m); + if (md) { + Py_CLEAR(md->m_base.m_copy); + } + } + } + + /* Setting modules_by_index to NULL could be dangerous, so we + clear the list instead. */ + if (PyList_SetSlice(interp->modules_by_index, + 0, PyList_GET_SIZE(interp->modules_by_index), + NULL)) { + PyErr_WriteUnraisable(interp->modules_by_index); + } +} + + /*******************/ /* builtin modules */ /*******************/ diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 3d83f8b1b5619f..7098886c121aa0 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1650,7 +1650,7 @@ finalize_modules(PyThreadState *tstate) // clear PyInterpreterState.modules_by_index and // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase // initialization API) - _PyInterpreterState_ClearModules(interp); + _PyImport_ClearModulesByIndex(interp); // Clear and delete the modules directory. Actual modules will // still be there only if imported during the execution of some diff --git a/Python/pystate.c b/Python/pystate.c index 02e71c01cee5c8..a3ebe316b65ad7 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -774,7 +774,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) Py_CLEAR(interp->codec_error_registry); assert(interp->modules == NULL); - Py_CLEAR(interp->modules_by_index); + assert(interp->modules_by_index == NULL); Py_CLEAR(interp->importlib); Py_CLEAR(interp->import_func); @@ -949,36 +949,6 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime) #endif -// Used by finalize_modules() -void -_PyInterpreterState_ClearModules(PyInterpreterState *interp) -{ - if (!interp->modules_by_index) { - return; - } - - Py_ssize_t i; - for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) { - PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i); - if (PyModule_Check(m)) { - /* cleanup the saved copy of module dicts */ - PyModuleDef *md = PyModule_GetDef(m); - if (md) { - Py_CLEAR(md->m_base.m_copy); - } - } - } - - /* Setting modules_by_index to NULL could be dangerous, so we - clear the list instead. */ - if (PyList_SetSlice(interp->modules_by_index, - 0, PyList_GET_SIZE(interp->modules_by_index), - NULL)) { - PyErr_WriteUnraisable(interp->modules_by_index); - } -} - - //---------- // accessors //---------- From d4693ea5e91e0d79aae3a37cef5a7ea9fea34bac Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 20:24:37 -0700 Subject: [PATCH 11/22] Hide interp->import_func. --- Include/internal/pycore_import.h | 4 ++++ Python/ceval.c | 2 +- Python/import.c | 20 ++++++++++++++++++++ Python/pylifecycle.c | 6 +----- Python/pystate.c | 2 +- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 501a1c58d24f40..c70eddb59e20fd 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -51,6 +51,10 @@ extern void _PyImport_ClearModules(PyInterpreterState *interp); extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp); +extern int _PyImport_InitDefaultImportFunc(PyInterpreterState *interp); +extern int _PyImport_IsDefaultImportFunc(PyInterpreterState *interp, + PyObject *func); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Python/ceval.c b/Python/ceval.c index 611d62b0eba9af..09fd2f29266c87 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2688,7 +2688,7 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame, } PyObject *locals = frame->f_locals; /* Fast path for not overloaded __import__. */ - if (import_func == tstate->interp->import_func) { + if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) { int ilevel = _PyLong_AsInt(level); if (ilevel == -1 && _PyErr_Occurred(tstate)) { return NULL; diff --git a/Python/import.c b/Python/import.c index a6a357cc595d95..f9ad1946c44a0b 100644 --- a/Python/import.c +++ b/Python/import.c @@ -157,6 +157,7 @@ _PyImport_ClearCore(PyInterpreterState *interp) { Py_CLEAR(interp->modules); Py_CLEAR(interp->modules_by_index); + Py_CLEAR(interp->import_func); } static PyObject* create_builtin(PyThreadState *tstate, @@ -1933,6 +1934,25 @@ PyImport_GetImporter(PyObject *path) /* importing modules */ /*********************/ +int +_PyImport_InitDefaultImportFunc(PyInterpreterState *interp) +{ + // Get the __import__ function + PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins, + "__import__"); + if (import_func == NULL) { + return -1; + } + interp->import_func = Py_NewRef(import_func); + return 0; +} + +int +_PyImport_IsDefaultImportFunc(PyInterpreterState *interp, PyObject *func) +{ + return func == interp->import_func; +} + /* Import a module, either built-in, frozen, or external, and return its module object WITH INCREMENTED REFERENCE COUNT */ diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 7098886c121aa0..26774fe19e97d9 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -855,13 +855,9 @@ pycore_init_builtins(PyThreadState *tstate) } Py_DECREF(bimod); - // Get the __import__ function - PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins, - "__import__"); - if (import_func == NULL) { + if (_PyImport_InitDefaultImportFunc(interp) < 0) { goto error; } - interp->import_func = Py_NewRef(import_func); assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); diff --git a/Python/pystate.c b/Python/pystate.c index a3ebe316b65ad7..971f06dfea94ab 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -776,7 +776,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) assert(interp->modules == NULL); assert(interp->modules_by_index == NULL); Py_CLEAR(interp->importlib); - Py_CLEAR(interp->import_func); + assert(interp->import_func == NULL); Py_CLEAR(interp->builtins_copy); Py_CLEAR(interp->dict); From 7120be2411d7c23946c9bf26ced060b9ad18a7a0 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Feb 2023 20:39:02 -0700 Subject: [PATCH 12/22] Hide interp->importlib. --- Include/internal/pycore_import.h | 13 +++++++++ Objects/moduleobject.c | 5 ++-- Python/_warnings.c | 9 +----- Python/import.c | 49 ++++++++++++++++++++++++++++++++ Python/pylifecycle.c | 4 +-- Python/pystate.c | 2 +- Python/pythonrun.c | 10 ++----- 7 files changed, 70 insertions(+), 22 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index c70eddb59e20fd..1af74eaddcf6ff 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -55,6 +55,19 @@ extern int _PyImport_InitDefaultImportFunc(PyInterpreterState *interp); extern int _PyImport_IsDefaultImportFunc(PyInterpreterState *interp, PyObject *func); +extern PyObject * _PyImport_GetImportlibLoader( + PyInterpreterState *interp, + const char *loader_name); +extern PyObject * _PyImport_GetImportlibExternalLoader( + PyInterpreterState *interp, + const char *loader_name); +extern PyObject * _PyImport_BlessMyLoader( + PyInterpreterState *interp, + PyObject *module_globals); +extern PyObject * _PyImport_ImportlibModuleRepr( + PyInterpreterState *interp, + PyObject *module); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 32d5a8c3092dfb..69edbf30b023f8 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -688,12 +688,11 @@ module_dealloc(PyModuleObject *m) Py_TYPE(m)->tp_free((PyObject *)m); } -static PyObject * +PyObject * module_repr(PyModuleObject *m) { PyInterpreterState *interp = _PyInterpreterState_GET(); - - return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); + return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m); } /* Check if the "_initializing" attribute of the module spec is set to true. diff --git a/Python/_warnings.c b/Python/_warnings.c index 9a70d022e95a57..d510381c365b66 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1050,7 +1050,6 @@ warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category, static PyObject * get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno) { - PyObject *external; PyObject *loader; PyObject *module_name; PyObject *get_source; @@ -1059,13 +1058,7 @@ get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno PyObject *source_line; /* stolen from import.c */ - external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); - if (external == NULL) { - return NULL; - } - - loader = PyObject_CallMethod(external, "_bless_my_loader", "O", module_globals, NULL); - Py_DECREF(external); + loader = _PyImport_BlessMyLoader(interp, module_globals); if (loader == NULL) { return NULL; } diff --git a/Python/import.c b/Python/import.c index f9ad1946c44a0b..21db46af0dec13 100644 --- a/Python/import.c +++ b/Python/import.c @@ -157,6 +157,7 @@ _PyImport_ClearCore(PyInterpreterState *interp) { Py_CLEAR(interp->modules); Py_CLEAR(interp->modules_by_index); + Py_CLEAR(interp->importlib); Py_CLEAR(interp->import_func); } @@ -1860,6 +1861,54 @@ PyImport_GetMagicTag(void) } +/*************/ +/* importlib */ +/*************/ + +PyObject * +_PyImport_GetImportlibLoader(PyInterpreterState *interp, + const char *loader_name) +{ + return PyObject_GetAttrString(interp->importlib, loader_name); +} + +PyObject * +_PyImport_GetImportlibExternalLoader(PyInterpreterState *interp, + const char *loader_name) +{ + PyObject *bootstrap = PyObject_GetAttrString(interp->importlib, + "_bootstrap_external"); + if (bootstrap == NULL) { + return NULL; + } + + PyObject *loader_type = PyObject_GetAttrString(bootstrap, loader_name); + Py_DECREF(bootstrap); + return loader_type; +} + +PyObject * +_PyImport_BlessMyLoader(PyInterpreterState *interp, PyObject *module_globals) +{ + PyObject *external = PyObject_GetAttrString(interp->importlib, + "_bootstrap_external"); + if (external == NULL) { + return NULL; + } + + PyObject *loader = PyObject_CallMethod(external, "_bless_my_loader", + "O", module_globals, NULL); + Py_DECREF(external); + return loader; +} + +PyObject * +_PyImport_ImportlibModuleRepr(PyInterpreterState *interp, PyObject *m) +{ + return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); +} + + /*******************/ /* Return a finder object for a sys.path/pkg.__path__ item 'p', diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 26774fe19e97d9..6f224aa53ac441 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2233,8 +2233,8 @@ add_main_module(PyInterpreterState *interp) if (PyErr_Occurred()) { return _PyStatus_ERR("Failed to test __main__.__loader__"); } - PyObject *loader = PyObject_GetAttrString(interp->importlib, - "BuiltinImporter"); + PyObject *loader = _PyImport_GetImportlibLoader(interp, + "BuiltinImporter"); if (loader == NULL) { return _PyStatus_ERR("Failed to retrieve BuiltinImporter"); } diff --git a/Python/pystate.c b/Python/pystate.c index 971f06dfea94ab..dc171b818b47c3 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -775,7 +775,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) assert(interp->modules == NULL); assert(interp->modules_by_index == NULL); - Py_CLEAR(interp->importlib); + assert(interp->importlib == NULL); assert(interp->import_func == NULL); Py_CLEAR(interp->builtins_copy); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 6a4d593768690a..ce993ea8796cb7 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -350,14 +350,8 @@ static int set_main_loader(PyObject *d, PyObject *filename, const char *loader_name) { PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *bootstrap = PyObject_GetAttrString(interp->importlib, - "_bootstrap_external"); - if (bootstrap == NULL) { - return -1; - } - - PyObject *loader_type = PyObject_GetAttrString(bootstrap, loader_name); - Py_DECREF(bootstrap); + PyObject *loader_type = _PyImport_GetImportlibExternalLoader(interp, + loader_name); if (loader_type == NULL) { return -1; } From 1935ee5f7d61fe790d70f9757fe5ed1a1a91f4bc Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 09:09:42 -0700 Subject: [PATCH 13/22] Add _PyImport_InitCore() and _PyImport_InitExternal(). --- Include/internal/pycore_import.h | 13 +- Include/internal/pycore_pylifecycle.h | 1 - Python/import.c | 273 ++++++++++++++++++-------- Python/pylifecycle.c | 84 +------- 4 files changed, 201 insertions(+), 170 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 1af74eaddcf6ff..2818f428e45d1c 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -52,8 +52,9 @@ extern void _PyImport_ClearModules(PyInterpreterState *interp); extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp); extern int _PyImport_InitDefaultImportFunc(PyInterpreterState *interp); -extern int _PyImport_IsDefaultImportFunc(PyInterpreterState *interp, - PyObject *func); +extern int _PyImport_IsDefaultImportFunc( + PyInterpreterState *interp, + PyObject *func); extern PyObject * _PyImport_GetImportlibLoader( PyInterpreterState *interp, @@ -69,10 +70,16 @@ extern PyObject * _PyImport_ImportlibModuleRepr( PyObject *module); +extern PyStatus _PyImport_InitCore( + PyThreadState *tstate, + PyObject *sysmod, + int importlib); +extern PyStatus _PyImport_InitExternal(PyThreadState *tstate); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); #endif -extern PyObject* _PyImport_BootstrapImp(PyThreadState *tstate); + extern PyObject* _PyImport_GetBuiltinModuleNames(void); diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h index 2d431befd74f99..27ba86c2fe80d9 100644 --- a/Include/internal/pycore_pylifecycle.h +++ b/Include/internal/pycore_pylifecycle.h @@ -45,7 +45,6 @@ extern int _PyBuiltins_AddExceptions(PyObject * bltinmod); extern PyStatus _Py_HashRandomization_Init(const PyConfig *); extern PyStatus _PyTime_Init(void); -extern PyStatus _PyImportZip_Init(PyThreadState *tstate); extern PyStatus _PyGC_Init(PyInterpreterState *interp); extern PyStatus _PyAtExit_Init(PyInterpreterState *interp); extern int _Py_Deepfreeze_Init(void); diff --git a/Python/import.c b/Python/import.c index 21db46af0dec13..f9920fdae38bd2 100644 --- a/Python/import.c +++ b/Python/import.c @@ -140,6 +140,21 @@ _PyImport_Fini2(void) /* interpreter lifecycle */ /*************************/ +static int init_importlib(PyThreadState *tstate, PyObject *sysmod); + +PyStatus +_PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib) +{ + if (importlib) { + /* This call sets up builtin and frozen import support */ + if (init_importlib(tstate, sysmod) < 0) { + return _PyStatus_ERR("failed to initialize importlib"); + } + } + + return _PyStatus_OK(); +} + /* In some corner cases it is important to be sure that the import machinery has been initialized (or not cleaned up yet). For example, see issue #4236 and PyModule_Create2(). */ @@ -161,71 +176,19 @@ _PyImport_ClearCore(PyInterpreterState *interp) Py_CLEAR(interp->import_func); } -static PyObject* create_builtin(PyThreadState *tstate, - PyObject *name, PyObject *spec); -static int exec_builtin_or_dynamic(PyObject *mod); - -// Import the _imp extension by calling manually _imp.create_builtin() and -// _imp.exec_builtin() since importlib is not initialized yet. Initializing -// importlib requires the _imp module: this function fix the bootstrap issue. -PyObject* -_PyImport_BootstrapImp(PyThreadState *tstate) -{ - PyObject *name = PyUnicode_FromString("_imp"); - if (name == NULL) { - return NULL; - } - - // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec(): - // an object with just a name attribute. - // - // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway. - PyObject *attrs = Py_BuildValue("{sO}", "name", name); - if (attrs == NULL) { - goto error; - } - PyObject *spec = _PyNamespace_New(attrs); - Py_DECREF(attrs); - if (spec == NULL) { - goto error; - } - - // Create the _imp module from its definition. - PyObject *mod = create_builtin(tstate, name, spec); - Py_CLEAR(name); - Py_DECREF(spec); - if (mod == NULL) { - goto error; - } - assert(mod != Py_None); // not found - - // Execute the _imp module: call imp_module_exec(). - if (exec_builtin_or_dynamic(mod) < 0) { - Py_DECREF(mod); - goto error; - } - return mod; - -error: - Py_XDECREF(name); - return NULL; -} +/* "external" imports */ -PyStatus -_PyImportZip_Init(PyThreadState *tstate) +static int +init_zipimport(PyThreadState *tstate, int verbose) { - PyObject *path_hooks; - int err = 0; - - path_hooks = PySys_GetObject("path_hooks"); + PyObject *path_hooks = PySys_GetObject("path_hooks"); if (path_hooks == NULL) { _PyErr_SetString(tstate, PyExc_RuntimeError, "unable to get sys.path_hooks"); - goto error; + return -1; } - int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; if (verbose) { PySys_WriteStderr("# installing zipimport hook\n"); } @@ -239,21 +202,37 @@ _PyImportZip_Init(PyThreadState *tstate) } else { /* sys.path_hooks.insert(0, zipimporter) */ - err = PyList_Insert(path_hooks, 0, zipimporter); + int err = PyList_Insert(path_hooks, 0, zipimporter); Py_DECREF(zipimporter); if (err < 0) { - goto error; + return -1; } if (verbose) { PySys_WriteStderr("# installed zipimport hook\n"); } } - return _PyStatus_OK(); + return 0; +} - error: - PyErr_Print(); - return _PyStatus_ERR("initializing zipimport failed"); +static int init_importlib_external(PyInterpreterState *interp); + +PyStatus +_PyImport_InitExternal(PyThreadState *tstate) +{ + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + + if (init_importlib_external(tstate->interp) != 0) { + _PyErr_Print(tstate); + return _PyStatus_ERR("external importer setup failed"); + } + + if (init_zipimport(tstate, verbose) != 0) { + PyErr_Print(); + return _PyStatus_ERR("initializing zipimport failed"); + } + + return _PyStatus_OK(); } @@ -1368,6 +1347,31 @@ _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val) #endif // HAVE_DLOPEN +/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ +static int +exec_builtin_or_dynamic(PyObject *mod) { + PyModuleDef *def; + void *state; + + if (!PyModule_Check(mod)) { + return 0; + } + + def = PyModule_GetDef(mod); + if (def == NULL) { + return 0; + } + + state = PyModule_GetState(mod); + if (state) { + /* Already initialized; skip reload */ + return 0; + } + + return PyModule_ExecDef(mod, def); +} + + /*******************/ #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) @@ -1865,6 +1869,125 @@ PyImport_GetMagicTag(void) /* importlib */ /*************/ +/* Import the _imp extension by calling manually _imp.create_builtin() and + _imp.exec_builtin() since importlib is not initialized yet. Initializing + importlib requires the _imp module: this function fix the bootstrap issue. + */ +static PyObject* +bootstrap_imp(PyThreadState *tstate) +{ + PyObject *name = PyUnicode_FromString("_imp"); + if (name == NULL) { + return NULL; + } + + // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec(): + // an object with just a name attribute. + // + // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway. + PyObject *attrs = Py_BuildValue("{sO}", "name", name); + if (attrs == NULL) { + goto error; + } + PyObject *spec = _PyNamespace_New(attrs); + Py_DECREF(attrs); + if (spec == NULL) { + goto error; + } + + // Create the _imp module from its definition. + PyObject *mod = create_builtin(tstate, name, spec); + Py_CLEAR(name); + Py_DECREF(spec); + if (mod == NULL) { + goto error; + } + assert(mod != Py_None); // not found + + // Execute the _imp module: call imp_module_exec(). + if (exec_builtin_or_dynamic(mod) < 0) { + Py_DECREF(mod); + goto error; + } + return mod; + +error: + Py_XDECREF(name); + return NULL; +} + +/* Global initializations. Can be undone by Py_FinalizeEx(). Don't + call this twice without an intervening Py_FinalizeEx() call. When + initializations fail, a fatal error is issued and the function does + not return. On return, the first thread and interpreter state have + been created. + + Locking: you must hold the interpreter lock while calling this. + (If the lock has not yet been initialized, that's equivalent to + having the lock, but you cannot use multiple threads.) + +*/ +static int +init_importlib(PyThreadState *tstate, PyObject *sysmod) +{ + assert(!_PyErr_Occurred(tstate)); + + PyInterpreterState *interp = tstate->interp; + int verbose = _PyInterpreterState_GetConfig(interp)->verbose; + + // Import _importlib through its frozen version, _frozen_importlib. + if (verbose) { + PySys_FormatStderr("import _frozen_importlib # frozen\n"); + } + if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { + return -1; + } + PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed + if (importlib == NULL) { + return -1; + } + interp->importlib = Py_NewRef(importlib); + + // Import the _imp module + if (verbose) { + PySys_FormatStderr("import _imp # builtin\n"); + } + PyObject *imp_mod = bootstrap_imp(tstate); + if (imp_mod == NULL) { + return -1; + } + if (_PyImport_SetModuleString("_imp", imp_mod) < 0) { + Py_DECREF(imp_mod); + return -1; + } + + // Install importlib as the implementation of import + PyObject *value = PyObject_CallMethod(importlib, "_install", + "OO", sysmod, imp_mod); + Py_DECREF(imp_mod); + if (value == NULL) { + return -1; + } + Py_DECREF(value); + + assert(!_PyErr_Occurred(tstate)); + return 0; +} + + +static int +init_importlib_external(PyInterpreterState *interp) +{ + PyObject *value; + value = PyObject_CallMethod(interp->importlib, + "_install_external_importers", ""); + if (value == NULL) { + return -1; + } + Py_DECREF(value); + return 0; +} + PyObject * _PyImport_GetImportlibLoader(PyInterpreterState *interp, const char *loader_name) @@ -3014,30 +3137,6 @@ _imp__override_frozen_modules_for_tests_impl(PyObject *module, int override) Py_RETURN_NONE; } -/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ -static int -exec_builtin_or_dynamic(PyObject *mod) { - PyModuleDef *def; - void *state; - - if (!PyModule_Check(mod)) { - return 0; - } - - def = PyModule_GetDef(mod); - if (def == NULL) { - return 0; - } - - state = PyModule_GetState(mod); - if (state) { - /* Already initialized; skip reload */ - return 0; - } - - return PyModule_ExecDef(mod, def); -} - #ifdef HAVE_DYNAMIC_LOADING /*[clinic input] diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 6f224aa53ac441..6bc385be18c056 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -160,79 +160,6 @@ Py_IsInitialized(void) } -/* Global initializations. Can be undone by Py_FinalizeEx(). Don't - call this twice without an intervening Py_FinalizeEx() call. When - initializations fail, a fatal error is issued and the function does - not return. On return, the first thread and interpreter state have - been created. - - Locking: you must hold the interpreter lock while calling this. - (If the lock has not yet been initialized, that's equivalent to - having the lock, but you cannot use multiple threads.) - -*/ -static int -init_importlib(PyThreadState *tstate, PyObject *sysmod) -{ - assert(!_PyErr_Occurred(tstate)); - - PyInterpreterState *interp = tstate->interp; - int verbose = _PyInterpreterState_GetConfig(interp)->verbose; - - // Import _importlib through its frozen version, _frozen_importlib. - if (verbose) { - PySys_FormatStderr("import _frozen_importlib # frozen\n"); - } - if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { - return -1; - } - PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed - if (importlib == NULL) { - return -1; - } - interp->importlib = Py_NewRef(importlib); - - // Import the _imp module - if (verbose) { - PySys_FormatStderr("import _imp # builtin\n"); - } - PyObject *imp_mod = _PyImport_BootstrapImp(tstate); - if (imp_mod == NULL) { - return -1; - } - if (_PyImport_SetModuleString("_imp", imp_mod) < 0) { - Py_DECREF(imp_mod); - return -1; - } - - // Install importlib as the implementation of import - PyObject *value = PyObject_CallMethod(importlib, "_install", - "OO", sysmod, imp_mod); - Py_DECREF(imp_mod); - if (value == NULL) { - return -1; - } - Py_DECREF(value); - - assert(!_PyErr_Occurred(tstate)); - return 0; -} - - -static PyStatus -init_importlib_external(PyThreadState *tstate) -{ - PyObject *value; - value = PyObject_CallMethod(tstate->interp->importlib, - "_install_external_importers", ""); - if (value == NULL) { - _PyErr_Print(tstate); - return _PyStatus_ERR("external importer setup failed"); - } - Py_DECREF(value); - return _PyImportZip_Init(tstate); -} - /* Helper functions to better handle the legacy C locale * * The legacy C locale assumes ASCII as the default text encoding, which @@ -919,11 +846,10 @@ pycore_interp_init(PyThreadState *tstate) } const PyConfig *config = _PyInterpreterState_GetConfig(interp); - if (config->_install_importlib) { - /* This call sets up builtin and frozen import support */ - if (init_importlib(tstate, sysmod) < 0) { - return _PyStatus_ERR("failed to initialize importlib"); - } + + status = _PyImport_InitCore(tstate, sysmod, config->_install_importlib); + if (_PyStatus_EXCEPTION(status)) { + goto done; } done: @@ -1173,7 +1099,7 @@ init_interp_main(PyThreadState *tstate) return _PyStatus_ERR("failed to update the Python config"); } - status = init_importlib_external(tstate); + status = _PyImport_InitExternal(tstate); if (_PyStatus_EXCEPTION(status)) { return status; } From 7e29b7cfa08f39b52d428cd915edb423e4b38f85 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 09:15:40 -0700 Subject: [PATCH 14/22] Re-order import.c sections. --- Python/import.c | 1884 +++++++++++++++++++++++------------------------ 1 file changed, 937 insertions(+), 947 deletions(-) diff --git a/Python/import.c b/Python/import.c index f9920fdae38bd2..229202d479e153 100644 --- a/Python/import.c +++ b/Python/import.c @@ -693,1175 +693,1165 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp) } -/*******************/ -/* builtin modules */ -/*******************/ +/*********************/ +/* extension modules */ +/*********************/ -static int fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename); +/* Make sure name is fully qualified. -int -_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) + This is a bit of a hack: when the shared library is loaded, + the module name is "package.module", but the module calls + PyModule_Create*() with just "module" for the name. The shared + library loader squirrels away the true name of the module in + _Py_PackageContext, and PyModule_Create*() will substitute this + (if the name actually matches). +*/ +const char * +_PyImport_ResolveNameWithPackageContext(const char *name) { - int res = -1; - PyObject *nameobj; - nameobj = PyUnicode_InternFromString(name); - if (nameobj == NULL) { - return -1; - } - if (PyObject_SetItem(modules, nameobj, mod) < 0) { - goto finally; - } - if (fix_up_extension(mod, nameobj, nameobj) < 0) { - PyMapping_DelItem(modules, nameobj); - goto finally; + if (_Py_PackageContext != NULL) { + const char *p = strrchr(_Py_PackageContext, '.'); + if (p != NULL && strcmp(name, p+1) == 0) { + name = _Py_PackageContext; + _Py_PackageContext = NULL; + } } - res = 0; - -finally: - Py_DECREF(nameobj); - return res; + return name; } -/* Helper to test for built-in module */ - -static int -is_builtin(PyObject *name) +const char * +_PyImport_SwapPackageContext(const char *newcontext) { - int i; - struct _inittab *inittab = _PyRuntime.imports.inittab; - for (i = 0; inittab[i].name != NULL; i++) { - if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { - if (inittab[i].initfunc == NULL) - return -1; - else - return 1; - } - } - return 0; + const char *oldcontext = _Py_PackageContext; + _Py_PackageContext = newcontext; + return oldcontext; } -static PyObject * import_find_extension(PyThreadState *tstate, - PyObject *name, PyObject *filename); +#ifdef HAVE_DLOPEN +int +_PyImport_GetDLOpenFlags(PyInterpreterState *interp) +{ + return interp->dlopenflags; +} -static PyObject* -create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) +void +_PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val) { - PyObject *mod = import_find_extension(tstate, name, name); - if (mod || _PyErr_Occurred(tstate)) { - return mod; - } + interp->dlopenflags = new_val; +} +#endif // HAVE_DLOPEN - PyObject *modules = tstate->interp->modules; - for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { - if (_PyUnicode_EqualToASCIIString(name, p->name)) { - if (p->initfunc == NULL) { - /* Cannot re-init internal module ("sys" or "builtins") */ - mod = PyImport_AddModuleObject(name); - return Py_XNewRef(mod); - } - mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); - if (mod == NULL) { - return NULL; - } - if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { - return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); - } - else { - /* Remember pointer to module init function. */ - PyModuleDef *def = PyModule_GetDef(mod); - if (def == NULL) { - return NULL; - } +/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ +static int +exec_builtin_or_dynamic(PyObject *mod) { + PyModuleDef *def; + void *state; - def->m_base.m_init = p->initfunc; - if (_PyImport_FixupExtensionObject(mod, name, name, - modules) < 0) { - return NULL; - } - return mod; - } - } + if (!PyModule_Check(mod)) { + return 0; } - // not found - Py_RETURN_NONE; -} + def = PyModule_GetDef(mod); + if (def == NULL) { + return 0; + } + state = PyModule_GetState(mod); + if (state) { + /* Already initialized; skip reload */ + return 0; + } -/*****************************/ -/* the builtin modules table */ -/*****************************/ + return PyModule_ExecDef(mod, def); +} -/* API for embedding applications that want to add their own entries - to the table of built-in modules. This should normally be called - *before* Py_Initialize(). When the table resize fails, -1 is - returned and the existing table is unchanged. - After a similar function by Just van Rossum. */ +/*******************/ -int -PyImport_ExtendInittab(struct _inittab *newtab) -{ - struct _inittab *p; - size_t i, n; - int res = 0; +#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) +#include +EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { + return wasmTable.get(func)(); +}); +#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE - if (_PyRuntime.imports.inittab != NULL) { - Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); - } - /* Count the number of entries in both tables */ - for (n = 0; newtab[n].name != NULL; n++) - ; - if (n == 0) - return 0; /* Nothing to do */ - for (i = 0; PyImport_Inittab[i].name != NULL; i++) - ; +/*****************************/ +/* single-phase init modules */ +/*****************************/ - /* Force default raw memory allocator to get a known allocator to be able - to release the memory in _PyImport_Fini2() */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); +/* +We support a number of kinds of single-phase init builtin/extension modules: - /* Allocate new memory for the combined table */ - p = NULL; - if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { - size_t size = sizeof(struct _inittab) * (i + n + 1); - p = PyMem_RawRealloc(inittab_copy, size); - } - if (p == NULL) { - res = -1; - goto done; - } +* "basic" + * no module state (PyModuleDef.m_size == -1) + * does not support repeated init (we use PyModuleDef.m_base.m_copy) + * may have process-global state + * the module's def is cached in _PyRuntime.imports.extensions, + by (name, filename) +* "reinit" + * no module state (PyModuleDef.m_size == 0) + * supports repeated init (m_copy is never used) + * should not have any process-global state + * its def is never cached in _PyRuntime.imports.extensions + (except, currently, under the main interpreter, for some reason) +* "with state" (almost the same as reinit) + * has module state (PyModuleDef.m_size > 0) + * supports repeated init (m_copy is never used) + * should not have any process-global state + * its def is never cached in _PyRuntime.imports.extensions + (except, currently, under the main interpreter, for some reason) - /* Copy the tables into the new memory at the first call - to PyImport_ExtendInittab(). */ - if (inittab_copy != PyImport_Inittab) { - memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); - } - memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); - PyImport_Inittab = inittab_copy = p; +There are also variants within those classes: -done: - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - return res; -} +* two or more modules share a PyModuleDef + * a module's init func uses another module's PyModuleDef + * a module's init func calls another's module's init func + * a module's init "func" is actually a variable statically initialized + to another module's init func +* two or modules share "methods" + * a module's init func copies another module's PyModuleDef + (with a different name) +* (basic-only) two or modules share process-global state -/* Shorthand to add a single entry given a name and a function */ +In the first case, where modules share a PyModuleDef, the following +notable weirdness happens: -int -PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) -{ - struct _inittab newtab[2]; +* the module's __name__ matches the def, not the requested name +* the last module (with the same def) to be imported for the first time wins + * returned by PyState_Find_Module() (via interp->modules_by_index) + * (non-basic-only) its init func is used when re-loading any of them + (via the def's m_init) + * (basic-only) the copy of its __dict__ is used when re-loading any of them + (via the def's m_copy) - if (_PyRuntime.imports.inittab != NULL) { - Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); - } +However, the following happens as expected: - memset(newtab, '\0', sizeof newtab); +* a new module object (with its own __dict__) is created for each request +* the module's __spec__ has the requested name +* the loaded module is cached in sys.modules under the requested name +* the m_index field of the shared def is not changed, + so at least PyState_FindModule() will always look in the same place - newtab[0].name = name; - newtab[0].initfunc = initfunc; +For "basic" modules there are other quirks: - return PyImport_ExtendInittab(newtab); -} +* (whether sharing a def or not) when loaded the first time, + m_copy is set before _init_module_attrs() is called + in importlib._bootstrap.module_from_spec(), + so when the module is re-loaded, the previous value + for __wpec__ (and others) is reset, possibly unexpectedly. +Generally, when multiple interpreters are involved, some of the above +gets even messier. +*/ -PyObject * -_PyImport_GetBuiltinModuleNames(void) +/* Magic for extension modules (built-in as well as dynamically + loaded). To prevent initializing an extension module more than + once, we keep a static dictionary 'extensions' keyed by the tuple + (module name, module name) (for built-in modules) or by + (filename, module name) (for dynamically loaded modules), containing these + modules. A copy of the module's dictionary is stored by calling + _PyImport_FixupExtensionObject() immediately after the module initialization + function succeeds. A copy can be retrieved from there by calling + import_find_extension(). + + Modules which do support multiple initialization set their m_size + field to a non-negative number (indicating the size of the + module-specific state). They are still recorded in the extensions + dictionary, to avoid loading shared libraries twice. +*/ + +static PyModuleDef * +_extensions_cache_get(PyObject *filename, PyObject *name) { - PyObject *list = PyList_New(0); - if (list == NULL) { + PyObject *extensions = _PyRuntime.imports.extensions; + if (extensions == NULL) { return NULL; } - struct _inittab *inittab = _PyRuntime.imports.inittab; - for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { - PyObject *name = PyUnicode_FromString(inittab[i].name); - if (name == NULL) { - Py_DECREF(list); - return NULL; - } - if (PyList_Append(list, name) < 0) { - Py_DECREF(name); - Py_DECREF(list); - return NULL; - } - Py_DECREF(name); + PyObject *key = PyTuple_Pack(2, filename, name); + if (key == NULL) { + return NULL; } - return list; + PyModuleDef *def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); + Py_DECREF(key); + return def; } - -/******************/ -/* frozen modules */ -/******************/ - -/* Return true if the name is an alias. In that case, "alias" is set - to the original module name. If it is an alias but the original - module isn't known then "alias" is set to NULL while true is returned. */ -static bool -resolve_module_alias(const char *name, const struct _module_alias *aliases, - const char **alias) +static int +_extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def) { - const struct _module_alias *entry; - for (entry = aliases; ; entry++) { - if (entry->name == NULL) { - /* It isn't an alias. */ - return false; - } - if (strcmp(name, entry->name) == 0) { - if (alias != NULL) { - *alias = entry->orig; - } - return true; + PyObject *extensions = _PyRuntime.imports.extensions; + if (extensions == NULL) { + extensions = PyDict_New(); + if (extensions == NULL) { + return -1; } + _PyRuntime.imports.extensions = extensions; + } + PyObject *key = PyTuple_Pack(2, filename, name); + if (key == NULL) { + return -1; + } + int res = PyDict_SetItem(extensions, key, (PyObject *)def); + Py_DECREF(key); + if (res < 0) { + return -1; } + return 0; } -static bool -use_frozen(void) +static void +_extensions_cache_clear(void) { - PyInterpreterState *interp = _PyInterpreterState_GET(); - int override = interp->override_frozen_modules; - if (override > 0) { - return true; - } - else if (override < 0) { - return false; - } - else { - return interp->config.use_frozen_modules; - } + Py_CLEAR(_PyRuntime.imports.extensions); } -static PyObject * -list_frozen_module_names(void) +static int +fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename) { - PyObject *names = PyList_New(0); - if (names == NULL) { - return NULL; - } - bool enabled = use_frozen(); - const struct _frozen *p; -#define ADD_MODULE(name) \ - do { \ - PyObject *nameobj = PyUnicode_FromString(name); \ - if (nameobj == NULL) { \ - goto error; \ - } \ - int res = PyList_Append(names, nameobj); \ - Py_DECREF(nameobj); \ - if (res != 0) { \ - goto error; \ - } \ - } while(0) - // We always use the bootstrap modules. - for (p = _PyImport_FrozenBootstrap; ; p++) { - if (p->name == NULL) { - break; - } - ADD_MODULE(p->name); + if (mod == NULL || !PyModule_Check(mod)) { + PyErr_BadInternalCall(); + return -1; } - // Frozen stdlib modules may be disabled. - for (p = _PyImport_FrozenStdlib; ; p++) { - if (p->name == NULL) { - break; - } - if (enabled) { - ADD_MODULE(p->name); - } + + struct PyModuleDef *def = PyModule_GetDef(mod); + if (!def) { + PyErr_BadInternalCall(); + return -1; } - for (p = _PyImport_FrozenTest; ; p++) { - if (p->name == NULL) { - break; - } - if (enabled) { - ADD_MODULE(p->name); - } + + PyThreadState *tstate = _PyThreadState_GET(); + if (_PyState_AddModule(tstate, mod, def) < 0) { + return -1; } -#undef ADD_MODULE - // Add any custom modules. - if (PyImport_FrozenModules != NULL) { - for (p = PyImport_FrozenModules; ; p++) { - if (p->name == NULL) { - break; - } - PyObject *nameobj = PyUnicode_FromString(p->name); - if (nameobj == NULL) { - goto error; - } - int found = PySequence_Contains(names, nameobj); - if (found < 0) { - Py_DECREF(nameobj); - goto error; + + // bpo-44050: Extensions and def->m_base.m_copy can be updated + // when the extension module doesn't support sub-interpreters. + // XXX Why special-case the main interpreter? + if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) { + if (def->m_size == -1) { + if (def->m_base.m_copy) { + /* Somebody already imported the module, + likely under a different name. + XXX this should really not happen. */ + Py_CLEAR(def->m_base.m_copy); } - else if (found) { - Py_DECREF(nameobj); + PyObject *dict = PyModule_GetDict(mod); + if (dict == NULL) { + return -1; } - else { - int res = PyList_Append(names, nameobj); - Py_DECREF(nameobj); - if (res != 0) { - goto error; - } + def->m_base.m_copy = PyDict_Copy(dict); + if (def->m_base.m_copy == NULL) { + return -1; } } + + if (_extensions_cache_set(filename, name, def) < 0) { + return -1; + } } - return names; -error: - Py_DECREF(names); - return NULL; + return 0; } -typedef enum { - FROZEN_OKAY, - FROZEN_BAD_NAME, // The given module name wasn't valid. - FROZEN_NOT_FOUND, // It wasn't in PyImport_FrozenModules. - FROZEN_DISABLED, // -X frozen_modules=off (and not essential) - FROZEN_EXCLUDED, /* The PyImport_FrozenModules entry has NULL "code" - (module is present but marked as unimportable, stops search). */ - FROZEN_INVALID, /* The PyImport_FrozenModules entry is bogus - (eg. does not contain executable code). */ -} frozen_status; - -static inline void -set_frozen_error(frozen_status status, PyObject *modname) +int +_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, + PyObject *filename, PyObject *modules) { - const char *err = NULL; - switch (status) { - case FROZEN_BAD_NAME: - case FROZEN_NOT_FOUND: - err = "No such frozen object named %R"; - break; - case FROZEN_DISABLED: - err = "Frozen modules are disabled and the frozen object named %R is not essential"; - break; - case FROZEN_EXCLUDED: - err = "Excluded frozen object named %R"; - break; - case FROZEN_INVALID: - err = "Frozen object named %R is invalid"; - break; - case FROZEN_OKAY: - // There was no error. - break; - default: - Py_UNREACHABLE(); + if (PyObject_SetItem(modules, name, mod) < 0) { + return -1; } - if (err != NULL) { - PyObject *msg = PyUnicode_FromFormat(err, modname); - if (msg == NULL) { - PyErr_Clear(); - } - PyErr_SetImportError(msg, modname, NULL); - Py_XDECREF(msg); + if (fix_up_extension(mod, name, filename) < 0) { + PyMapping_DelItem(modules, name); + return -1; } + return 0; } -static const struct _frozen * -look_up_frozen(const char *name) -{ - const struct _frozen *p; - // We always use the bootstrap modules. - for (p = _PyImport_FrozenBootstrap; ; p++) { - if (p->name == NULL) { - // We hit the end-of-list sentinel value. - break; + +static PyObject * +import_find_extension(PyThreadState *tstate, PyObject *name, + PyObject *filename) +{ + /* Only single-phase init modules will be in the cache. */ + PyModuleDef *def = _extensions_cache_get(filename, name); + if (def == NULL) { + return NULL; + } + + PyObject *mod, *mdict; + PyObject *modules = tstate->interp->modules; + + if (def->m_size == -1) { + /* Module does not support repeated initialization */ + if (def->m_base.m_copy == NULL) + return NULL; + mod = import_add_module(tstate, name); + if (mod == NULL) + return NULL; + mdict = PyModule_GetDict(mod); + if (mdict == NULL) { + Py_DECREF(mod); + return NULL; } - if (strcmp(name, p->name) == 0) { - return p; + if (PyDict_Update(mdict, def->m_base.m_copy)) { + Py_DECREF(mod); + return NULL; } } - // Prefer custom modules, if any. Frozen stdlib modules can be - // disabled here by setting "code" to NULL in the array entry. - if (PyImport_FrozenModules != NULL) { - for (p = PyImport_FrozenModules; ; p++) { - if (p->name == NULL) { - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } + else { + if (def->m_base.m_init == NULL) + return NULL; + mod = _PyImport_InitFunc_TrampolineCall(def->m_base.m_init); + if (mod == NULL) + return NULL; + if (PyObject_SetItem(modules, name, mod) == -1) { + Py_DECREF(mod); + return NULL; } } - // Frozen stdlib modules may be disabled. - if (use_frozen()) { - for (p = _PyImport_FrozenStdlib; ; p++) { - if (p->name == NULL) { - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } - } - for (p = _PyImport_FrozenTest; ; p++) { - if (p->name == NULL) { - break; - } - if (strcmp(name, p->name) == 0) { - return p; - } - } + if (_PyState_AddModule(tstate, mod, def) < 0) { + PyMapping_DelItem(modules, name); + Py_DECREF(mod); + return NULL; } - return NULL; + + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + if (verbose) { + PySys_FormatStderr("import %U # previously loaded (%R)\n", + name, filename); + } + return mod; } -struct frozen_info { - PyObject *nameobj; - const char *data; - PyObject *(*get_code)(void); - Py_ssize_t size; - bool is_package; - bool is_alias; - const char *origname; -}; -static frozen_status -find_frozen(PyObject *nameobj, struct frozen_info *info) +/*******************/ +/* builtin modules */ +/*******************/ + +int +_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) { - if (info != NULL) { - memset(info, 0, sizeof(*info)); + int res = -1; + PyObject *nameobj; + nameobj = PyUnicode_InternFromString(name); + if (nameobj == NULL) { + return -1; } - - if (nameobj == NULL || nameobj == Py_None) { - return FROZEN_BAD_NAME; + if (PyObject_SetItem(modules, nameobj, mod) < 0) { + goto finally; } - const char *name = PyUnicode_AsUTF8(nameobj); - if (name == NULL) { - // Note that this function previously used - // _PyUnicode_EqualToASCIIString(). We clear the error here - // (instead of propagating it) to match the earlier behavior - // more closely. - PyErr_Clear(); - return FROZEN_BAD_NAME; + if (fix_up_extension(mod, nameobj, nameobj) < 0) { + PyMapping_DelItem(modules, nameobj); + goto finally; } + res = 0; - const struct _frozen *p = look_up_frozen(name); - if (p == NULL) { - return FROZEN_NOT_FOUND; - } - if (info != NULL) { - info->nameobj = nameobj; // borrowed - info->data = (const char *)p->code; - info->get_code = p->get_code; - info->size = p->size; - info->is_package = p->is_package; - if (p->size < 0) { - // backward compatibility with negative size values - info->size = -(p->size); - info->is_package = true; +finally: + Py_DECREF(nameobj); + return res; +} + +/* Helper to test for built-in module */ + +static int +is_builtin(PyObject *name) +{ + int i; + struct _inittab *inittab = _PyRuntime.imports.inittab; + for (i = 0; inittab[i].name != NULL; i++) { + if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { + if (inittab[i].initfunc == NULL) + return -1; + else + return 1; } - info->origname = name; - info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases, - &info->origname); } - if (p->code == NULL && p->size == 0 && p->get_code != NULL) { - /* It is only deepfrozen. */ - return FROZEN_OKAY; - } - if (p->code == NULL) { - /* It is frozen but marked as un-importable. */ - return FROZEN_EXCLUDED; - } - if (p->code[0] == '\0' || p->size == 0) { - /* Does not contain executable code. */ - return FROZEN_INVALID; - } - return FROZEN_OKAY; + return 0; } -static PyObject * -unmarshal_frozen_code(struct frozen_info *info) +static PyObject* +create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) { - if (info->get_code) { - PyObject *code = info->get_code(); - assert(code != NULL); - return code; - } - PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size); - if (co == NULL) { - /* Does not contain executable code. */ - set_frozen_error(FROZEN_INVALID, info->nameobj); - return NULL; + PyObject *mod = import_find_extension(tstate, name, name); + if (mod || _PyErr_Occurred(tstate)) { + return mod; } - if (!PyCode_Check(co)) { - // We stick with TypeError for backward compatibility. - PyErr_Format(PyExc_TypeError, - "frozen object %R is not a code object", - info->nameobj); - Py_DECREF(co); - return NULL; + + PyObject *modules = tstate->interp->modules; + for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { + if (_PyUnicode_EqualToASCIIString(name, p->name)) { + if (p->initfunc == NULL) { + /* Cannot re-init internal module ("sys" or "builtins") */ + mod = PyImport_AddModuleObject(name); + return Py_XNewRef(mod); + } + mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); + if (mod == NULL) { + return NULL; + } + + if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { + return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); + } + else { + /* Remember pointer to module init function. */ + PyModuleDef *def = PyModule_GetDef(mod); + if (def == NULL) { + return NULL; + } + + def->m_base.m_init = p->initfunc; + if (_PyImport_FixupExtensionObject(mod, name, name, + modules) < 0) { + return NULL; + } + return mod; + } + } } - return co; + + // not found + Py_RETURN_NONE; } -static PyObject * module_dict_for_exec(PyThreadState *tstate, PyObject *name); -static PyObject * exec_code_in_module(PyThreadState *tstate, - PyObject *name, PyObject *module_dict, - PyObject *code_object); +/*****************************/ +/* the builtin modules table */ +/*****************************/ -/* Initialize a frozen module. - Return 1 for success, 0 if the module is not found, and -1 with - an exception set if the initialization failed. - This function is also used from frozenmain.c */ +/* API for embedding applications that want to add their own entries + to the table of built-in modules. This should normally be called + *before* Py_Initialize(). When the table resize fails, -1 is + returned and the existing table is unchanged. + + After a similar function by Just van Rossum. */ int -PyImport_ImportFrozenModuleObject(PyObject *name) +PyImport_ExtendInittab(struct _inittab *newtab) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *co, *m, *d = NULL; - int err; + struct _inittab *p; + size_t i, n; + int res = 0; - struct frozen_info info; - frozen_status status = find_frozen(name, &info); - if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) { - return 0; - } - else if (status == FROZEN_BAD_NAME) { - return 0; - } - else if (status != FROZEN_OKAY) { - set_frozen_error(status, name); - return -1; - } - co = unmarshal_frozen_code(&info); - if (co == NULL) { - return -1; - } - if (info.is_package) { - /* Set __path__ to the empty list */ - PyObject *l; - m = import_add_module(tstate, name); - if (m == NULL) - goto err_return; - d = PyModule_GetDict(m); - l = PyList_New(0); - if (l == NULL) { - Py_DECREF(m); - goto err_return; - } - err = PyDict_SetItemString(d, "__path__", l); - Py_DECREF(l); - Py_DECREF(m); - if (err != 0) - goto err_return; - } - d = module_dict_for_exec(tstate, name); - if (d == NULL) { - goto err_return; - } - m = exec_code_in_module(tstate, name, d, co); - if (m == NULL) { - goto err_return; + if (_PyRuntime.imports.inittab != NULL) { + Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); } - Py_DECREF(m); - /* Set __origname__ (consumed in FrozenImporter._setup_module()). */ - PyObject *origname; - if (info.origname) { - origname = PyUnicode_FromString(info.origname); - if (origname == NULL) { - goto err_return; - } + + /* Count the number of entries in both tables */ + for (n = 0; newtab[n].name != NULL; n++) + ; + if (n == 0) + return 0; /* Nothing to do */ + for (i = 0; PyImport_Inittab[i].name != NULL; i++) + ; + + /* Force default raw memory allocator to get a known allocator to be able + to release the memory in _PyImport_Fini2() */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + /* Allocate new memory for the combined table */ + p = NULL; + if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) { + size_t size = sizeof(struct _inittab) * (i + n + 1); + p = PyMem_RawRealloc(inittab_copy, size); } - else { - origname = Py_NewRef(Py_None); + if (p == NULL) { + res = -1; + goto done; } - err = PyDict_SetItemString(d, "__origname__", origname); - Py_DECREF(origname); - if (err != 0) { - goto err_return; + + /* Copy the tables into the new memory at the first call + to PyImport_ExtendInittab(). */ + if (inittab_copy != PyImport_Inittab) { + memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); } - Py_DECREF(d); - Py_DECREF(co); - return 1; + memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab)); + PyImport_Inittab = inittab_copy = p; -err_return: - Py_XDECREF(d); - Py_DECREF(co); - return -1; +done: + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return res; } +/* Shorthand to add a single entry given a name and a function */ + int -PyImport_ImportFrozenModule(const char *name) +PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) { - PyObject *nameobj; - int ret; - nameobj = PyUnicode_InternFromString(name); - if (nameobj == NULL) - return -1; - ret = PyImport_ImportFrozenModuleObject(nameobj); - Py_DECREF(nameobj); - return ret; -} + struct _inittab newtab[2]; + if (_PyRuntime.imports.inittab != NULL) { + Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); + } -/*********************/ -/* extension modules */ -/*********************/ + memset(newtab, '\0', sizeof newtab); -/* Make sure name is fully qualified. + newtab[0].name = name; + newtab[0].initfunc = initfunc; - This is a bit of a hack: when the shared library is loaded, - the module name is "package.module", but the module calls - PyModule_Create*() with just "module" for the name. The shared - library loader squirrels away the true name of the module in - _Py_PackageContext, and PyModule_Create*() will substitute this - (if the name actually matches). -*/ -const char * -_PyImport_ResolveNameWithPackageContext(const char *name) + return PyImport_ExtendInittab(newtab); +} + + +PyObject * +_PyImport_GetBuiltinModuleNames(void) { - if (_Py_PackageContext != NULL) { - const char *p = strrchr(_Py_PackageContext, '.'); - if (p != NULL && strcmp(name, p+1) == 0) { - name = _Py_PackageContext; - _Py_PackageContext = NULL; + PyObject *list = PyList_New(0); + if (list == NULL) { + return NULL; + } + struct _inittab *inittab = _PyRuntime.imports.inittab; + for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { + PyObject *name = PyUnicode_FromString(inittab[i].name); + if (name == NULL) { + Py_DECREF(list); + return NULL; + } + if (PyList_Append(list, name) < 0) { + Py_DECREF(name); + Py_DECREF(list); + return NULL; } + Py_DECREF(name); } - return name; + return list; +} + + +/********************/ +/* the magic number */ +/********************/ + +/* Helper for pythonrun.c -- return magic number and tag. */ + +long +PyImport_GetMagicNumber(void) +{ + long res; + PyInterpreterState *interp = _PyInterpreterState_GET(); + PyObject *external, *pyc_magic; + + external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); + if (external == NULL) + return -1; + pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); + Py_DECREF(external); + if (pyc_magic == NULL) + return -1; + res = PyLong_AsLong(pyc_magic); + Py_DECREF(pyc_magic); + return res; } + +extern const char * _PySys_ImplCacheTag; + const char * -_PyImport_SwapPackageContext(const char *newcontext) +PyImport_GetMagicTag(void) { - const char *oldcontext = _Py_PackageContext; - _Py_PackageContext = newcontext; - return oldcontext; + return _PySys_ImplCacheTag; } -#ifdef HAVE_DLOPEN -int -_PyImport_GetDLOpenFlags(PyInterpreterState *interp) + +/*********************************/ +/* a Python module's code object */ +/*********************************/ + +/* Execute a code object in a module and return the module object + * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is + * removed from sys.modules, to avoid leaving damaged module objects + * in sys.modules. The caller may wish to restore the original + * module object (if any) in this case; PyImport_ReloadModule is an + * example. + * + * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer + * interface. The other two exist primarily for backward compatibility. + */ +PyObject * +PyImport_ExecCodeModule(const char *name, PyObject *co) { - return interp->dlopenflags; + return PyImport_ExecCodeModuleWithPathnames( + name, co, (char *)NULL, (char *)NULL); } -void -_PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val) +PyObject * +PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) { - interp->dlopenflags = new_val; + return PyImport_ExecCodeModuleWithPathnames( + name, co, pathname, (char *)NULL); } -#endif // HAVE_DLOPEN +PyObject * +PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, + const char *pathname, + const char *cpathname) +{ + PyObject *m = NULL; + PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; -/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ -static int -exec_builtin_or_dynamic(PyObject *mod) { - PyModuleDef *def; - void *state; + nameobj = PyUnicode_FromString(name); + if (nameobj == NULL) + return NULL; - if (!PyModule_Check(mod)) { - return 0; + if (cpathname != NULL) { + cpathobj = PyUnicode_DecodeFSDefault(cpathname); + if (cpathobj == NULL) + goto error; } + else + cpathobj = NULL; - def = PyModule_GetDef(mod); - if (def == NULL) { - return 0; + if (pathname != NULL) { + pathobj = PyUnicode_DecodeFSDefault(pathname); + if (pathobj == NULL) + goto error; } + else if (cpathobj != NULL) { + PyInterpreterState *interp = _PyInterpreterState_GET(); - state = PyModule_GetState(mod); - if (state) { - /* Already initialized; skip reload */ - return 0; + if (interp == NULL) { + Py_FatalError("no current interpreter"); + } + + external= PyObject_GetAttrString(interp->importlib, + "_bootstrap_external"); + if (external != NULL) { + pathobj = _PyObject_CallMethodOneArg( + external, &_Py_ID(_get_sourcefile), cpathobj); + Py_DECREF(external); + } + if (pathobj == NULL) + PyErr_Clear(); } + else + pathobj = NULL; - return PyModule_ExecDef(mod, def); + m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); +error: + Py_DECREF(nameobj); + Py_XDECREF(pathobj); + Py_XDECREF(cpathobj); + return m; } +static PyObject * +module_dict_for_exec(PyThreadState *tstate, PyObject *name) +{ + PyObject *m, *d; -/*******************/ + m = import_add_module(tstate, name); + if (m == NULL) + return NULL; + /* If the module is being reloaded, we get the old module back + and re-use its dict to exec the new code. */ + d = PyModule_GetDict(m); + int r = PyDict_Contains(d, &_Py_ID(__builtins__)); + if (r == 0) { + r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins()); + } + if (r < 0) { + remove_module(tstate, name); + Py_DECREF(m); + return NULL; + } -#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) -#include -EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), { - return wasmTable.get(func)(); -}); -#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE + Py_INCREF(d); + Py_DECREF(m); + return d; +} +static PyObject * +exec_code_in_module(PyThreadState *tstate, PyObject *name, + PyObject *module_dict, PyObject *code_object) +{ + PyObject *v, *m; -/*****************************/ -/* single-phase init modules */ -/*****************************/ + v = PyEval_EvalCode(code_object, module_dict, module_dict); + if (v == NULL) { + remove_module(tstate, name); + return NULL; + } + Py_DECREF(v); -/* -We support a number of kinds of single-phase init builtin/extension modules: + m = import_get_module(tstate, name); + if (m == NULL && !_PyErr_Occurred(tstate)) { + _PyErr_Format(tstate, PyExc_ImportError, + "Loaded module %R not found in sys.modules", + name); + } -* "basic" - * no module state (PyModuleDef.m_size == -1) - * does not support repeated init (we use PyModuleDef.m_base.m_copy) - * may have process-global state - * the module's def is cached in _PyRuntime.imports.extensions, - by (name, filename) -* "reinit" - * no module state (PyModuleDef.m_size == 0) - * supports repeated init (m_copy is never used) - * should not have any process-global state - * its def is never cached in _PyRuntime.imports.extensions - (except, currently, under the main interpreter, for some reason) -* "with state" (almost the same as reinit) - * has module state (PyModuleDef.m_size > 0) - * supports repeated init (m_copy is never used) - * should not have any process-global state - * its def is never cached in _PyRuntime.imports.extensions - (except, currently, under the main interpreter, for some reason) + return m; +} -There are also variants within those classes: +PyObject* +PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, + PyObject *cpathname) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *d, *external, *res; -* two or more modules share a PyModuleDef - * a module's init func uses another module's PyModuleDef - * a module's init func calls another's module's init func - * a module's init "func" is actually a variable statically initialized - to another module's init func -* two or modules share "methods" - * a module's init func copies another module's PyModuleDef - (with a different name) -* (basic-only) two or modules share process-global state + d = module_dict_for_exec(tstate, name); + if (d == NULL) { + return NULL; + } -In the first case, where modules share a PyModuleDef, the following -notable weirdness happens: + if (pathname == NULL) { + pathname = ((PyCodeObject *)co)->co_filename; + } + external = PyObject_GetAttrString(tstate->interp->importlib, + "_bootstrap_external"); + if (external == NULL) { + Py_DECREF(d); + return NULL; + } + res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module), + d, name, pathname, cpathname, NULL); + Py_DECREF(external); + if (res != NULL) { + Py_DECREF(res); + res = exec_code_in_module(tstate, name, d, co); + } + Py_DECREF(d); + return res; +} -* the module's __name__ matches the def, not the requested name -* the last module (with the same def) to be imported for the first time wins - * returned by PyState_Find_Module() (via interp->modules_by_index) - * (non-basic-only) its init func is used when re-loading any of them - (via the def's m_init) - * (basic-only) the copy of its __dict__ is used when re-loading any of them - (via the def's m_copy) -However, the following happens as expected: +static void +update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) +{ + PyObject *constants, *tmp; + Py_ssize_t i, n; -* a new module object (with its own __dict__) is created for each request -* the module's __spec__ has the requested name -* the loaded module is cached in sys.modules under the requested name -* the m_index field of the shared def is not changed, - so at least PyState_FindModule() will always look in the same place + if (PyUnicode_Compare(co->co_filename, oldname)) + return; -For "basic" modules there are other quirks: + Py_XSETREF(co->co_filename, Py_NewRef(newname)); -* (whether sharing a def or not) when loaded the first time, - m_copy is set before _init_module_attrs() is called - in importlib._bootstrap.module_from_spec(), - so when the module is re-loaded, the previous value - for __wpec__ (and others) is reset, possibly unexpectedly. + constants = co->co_consts; + n = PyTuple_GET_SIZE(constants); + for (i = 0; i < n; i++) { + tmp = PyTuple_GET_ITEM(constants, i); + if (PyCode_Check(tmp)) + update_code_filenames((PyCodeObject *)tmp, + oldname, newname); + } +} -Generally, when multiple interpreters are involved, some of the above -gets even messier. -*/ +static void +update_compiled_module(PyCodeObject *co, PyObject *newname) +{ + PyObject *oldname; -/* Magic for extension modules (built-in as well as dynamically - loaded). To prevent initializing an extension module more than - once, we keep a static dictionary 'extensions' keyed by the tuple - (module name, module name) (for built-in modules) or by - (filename, module name) (for dynamically loaded modules), containing these - modules. A copy of the module's dictionary is stored by calling - _PyImport_FixupExtensionObject() immediately after the module initialization - function succeeds. A copy can be retrieved from there by calling - import_find_extension(). + if (PyUnicode_Compare(co->co_filename, newname) == 0) + return; - Modules which do support multiple initialization set their m_size - field to a non-negative number (indicating the size of the - module-specific state). They are still recorded in the extensions - dictionary, to avoid loading shared libraries twice. -*/ + oldname = co->co_filename; + Py_INCREF(oldname); + update_code_filenames(co, oldname, newname); + Py_DECREF(oldname); +} -static PyModuleDef * -_extensions_cache_get(PyObject *filename, PyObject *name) + +/******************/ +/* frozen modules */ +/******************/ + +/* Return true if the name is an alias. In that case, "alias" is set + to the original module name. If it is an alias but the original + module isn't known then "alias" is set to NULL while true is returned. */ +static bool +resolve_module_alias(const char *name, const struct _module_alias *aliases, + const char **alias) { - PyObject *extensions = _PyRuntime.imports.extensions; - if (extensions == NULL) { - return NULL; - } - PyObject *key = PyTuple_Pack(2, filename, name); - if (key == NULL) { - return NULL; + const struct _module_alias *entry; + for (entry = aliases; ; entry++) { + if (entry->name == NULL) { + /* It isn't an alias. */ + return false; + } + if (strcmp(name, entry->name) == 0) { + if (alias != NULL) { + *alias = entry->orig; + } + return true; + } } - PyModuleDef *def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); - Py_DECREF(key); - return def; } -static int -_extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def) +static bool +use_frozen(void) { - PyObject *extensions = _PyRuntime.imports.extensions; - if (extensions == NULL) { - extensions = PyDict_New(); - if (extensions == NULL) { - return -1; - } - _PyRuntime.imports.extensions = extensions; + PyInterpreterState *interp = _PyInterpreterState_GET(); + int override = interp->override_frozen_modules; + if (override > 0) { + return true; } - PyObject *key = PyTuple_Pack(2, filename, name); - if (key == NULL) { - return -1; + else if (override < 0) { + return false; } - int res = PyDict_SetItem(extensions, key, (PyObject *)def); - Py_DECREF(key); - if (res < 0) { - return -1; + else { + return interp->config.use_frozen_modules; } - return 0; -} - -static void -_extensions_cache_clear(void) -{ - Py_CLEAR(_PyRuntime.imports.extensions); } -static int -fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename) +static PyObject * +list_frozen_module_names(void) { - if (mod == NULL || !PyModule_Check(mod)) { - PyErr_BadInternalCall(); - return -1; + PyObject *names = PyList_New(0); + if (names == NULL) { + return NULL; } - - struct PyModuleDef *def = PyModule_GetDef(mod); - if (!def) { - PyErr_BadInternalCall(); - return -1; + bool enabled = use_frozen(); + const struct _frozen *p; +#define ADD_MODULE(name) \ + do { \ + PyObject *nameobj = PyUnicode_FromString(name); \ + if (nameobj == NULL) { \ + goto error; \ + } \ + int res = PyList_Append(names, nameobj); \ + Py_DECREF(nameobj); \ + if (res != 0) { \ + goto error; \ + } \ + } while(0) + // We always use the bootstrap modules. + for (p = _PyImport_FrozenBootstrap; ; p++) { + if (p->name == NULL) { + break; + } + ADD_MODULE(p->name); } - - PyThreadState *tstate = _PyThreadState_GET(); - if (_PyState_AddModule(tstate, mod, def) < 0) { - return -1; + // Frozen stdlib modules may be disabled. + for (p = _PyImport_FrozenStdlib; ; p++) { + if (p->name == NULL) { + break; + } + if (enabled) { + ADD_MODULE(p->name); + } } - - // bpo-44050: Extensions and def->m_base.m_copy can be updated - // when the extension module doesn't support sub-interpreters. - // XXX Why special-case the main interpreter? - if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) { - if (def->m_size == -1) { - if (def->m_base.m_copy) { - /* Somebody already imported the module, - likely under a different name. - XXX this should really not happen. */ - Py_CLEAR(def->m_base.m_copy); + for (p = _PyImport_FrozenTest; ; p++) { + if (p->name == NULL) { + break; + } + if (enabled) { + ADD_MODULE(p->name); + } + } +#undef ADD_MODULE + // Add any custom modules. + if (PyImport_FrozenModules != NULL) { + for (p = PyImport_FrozenModules; ; p++) { + if (p->name == NULL) { + break; } - PyObject *dict = PyModule_GetDict(mod); - if (dict == NULL) { - return -1; + PyObject *nameobj = PyUnicode_FromString(p->name); + if (nameobj == NULL) { + goto error; } - def->m_base.m_copy = PyDict_Copy(dict); - if (def->m_base.m_copy == NULL) { - return -1; + int found = PySequence_Contains(names, nameobj); + if (found < 0) { + Py_DECREF(nameobj); + goto error; + } + else if (found) { + Py_DECREF(nameobj); + } + else { + int res = PyList_Append(names, nameobj); + Py_DECREF(nameobj); + if (res != 0) { + goto error; + } } - } - - if (_extensions_cache_set(filename, name, def) < 0) { - return -1; } } + return names; - return 0; +error: + Py_DECREF(names); + return NULL; } -int -_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, - PyObject *filename, PyObject *modules) +typedef enum { + FROZEN_OKAY, + FROZEN_BAD_NAME, // The given module name wasn't valid. + FROZEN_NOT_FOUND, // It wasn't in PyImport_FrozenModules. + FROZEN_DISABLED, // -X frozen_modules=off (and not essential) + FROZEN_EXCLUDED, /* The PyImport_FrozenModules entry has NULL "code" + (module is present but marked as unimportable, stops search). */ + FROZEN_INVALID, /* The PyImport_FrozenModules entry is bogus + (eg. does not contain executable code). */ +} frozen_status; + +static inline void +set_frozen_error(frozen_status status, PyObject *modname) { - if (PyObject_SetItem(modules, name, mod) < 0) { - return -1; + const char *err = NULL; + switch (status) { + case FROZEN_BAD_NAME: + case FROZEN_NOT_FOUND: + err = "No such frozen object named %R"; + break; + case FROZEN_DISABLED: + err = "Frozen modules are disabled and the frozen object named %R is not essential"; + break; + case FROZEN_EXCLUDED: + err = "Excluded frozen object named %R"; + break; + case FROZEN_INVALID: + err = "Frozen object named %R is invalid"; + break; + case FROZEN_OKAY: + // There was no error. + break; + default: + Py_UNREACHABLE(); } - if (fix_up_extension(mod, name, filename) < 0) { - PyMapping_DelItem(modules, name); - return -1; + if (err != NULL) { + PyObject *msg = PyUnicode_FromFormat(err, modname); + if (msg == NULL) { + PyErr_Clear(); + } + PyErr_SetImportError(msg, modname, NULL); + Py_XDECREF(msg); } - return 0; } - -static PyObject * -import_find_extension(PyThreadState *tstate, PyObject *name, - PyObject *filename) +static const struct _frozen * +look_up_frozen(const char *name) { - /* Only single-phase init modules will be in the cache. */ - PyModuleDef *def = _extensions_cache_get(filename, name); - if (def == NULL) { - return NULL; - } - - PyObject *mod, *mdict; - PyObject *modules = tstate->interp->modules; - - if (def->m_size == -1) { - /* Module does not support repeated initialization */ - if (def->m_base.m_copy == NULL) - return NULL; - mod = import_add_module(tstate, name); - if (mod == NULL) - return NULL; - mdict = PyModule_GetDict(mod); - if (mdict == NULL) { - Py_DECREF(mod); - return NULL; + const struct _frozen *p; + // We always use the bootstrap modules. + for (p = _PyImport_FrozenBootstrap; ; p++) { + if (p->name == NULL) { + // We hit the end-of-list sentinel value. + break; } - if (PyDict_Update(mdict, def->m_base.m_copy)) { - Py_DECREF(mod); - return NULL; + if (strcmp(name, p->name) == 0) { + return p; } } - else { - if (def->m_base.m_init == NULL) - return NULL; - mod = _PyImport_InitFunc_TrampolineCall(def->m_base.m_init); - if (mod == NULL) - return NULL; - if (PyObject_SetItem(modules, name, mod) == -1) { - Py_DECREF(mod); - return NULL; + // Prefer custom modules, if any. Frozen stdlib modules can be + // disabled here by setting "code" to NULL in the array entry. + if (PyImport_FrozenModules != NULL) { + for (p = PyImport_FrozenModules; ; p++) { + if (p->name == NULL) { + break; + } + if (strcmp(name, p->name) == 0) { + return p; + } } } - if (_PyState_AddModule(tstate, mod, def) < 0) { - PyMapping_DelItem(modules, name); - Py_DECREF(mod); - return NULL; - } - - int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; - if (verbose) { - PySys_FormatStderr("import %U # previously loaded (%R)\n", - name, filename); + // Frozen stdlib modules may be disabled. + if (use_frozen()) { + for (p = _PyImport_FrozenStdlib; ; p++) { + if (p->name == NULL) { + break; + } + if (strcmp(name, p->name) == 0) { + return p; + } + } + for (p = _PyImport_FrozenTest; ; p++) { + if (p->name == NULL) { + break; + } + if (strcmp(name, p->name) == 0) { + return p; + } + } } - return mod; -} - - -/*********************************/ -/* a Python module's code object */ -/*********************************/ - -/* Execute a code object in a module and return the module object - * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is - * removed from sys.modules, to avoid leaving damaged module objects - * in sys.modules. The caller may wish to restore the original - * module object (if any) in this case; PyImport_ReloadModule is an - * example. - * - * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer - * interface. The other two exist primarily for backward compatibility. - */ -PyObject * -PyImport_ExecCodeModule(const char *name, PyObject *co) -{ - return PyImport_ExecCodeModuleWithPathnames( - name, co, (char *)NULL, (char *)NULL); + return NULL; } -PyObject * -PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) -{ - return PyImport_ExecCodeModuleWithPathnames( - name, co, pathname, (char *)NULL); -} +struct frozen_info { + PyObject *nameobj; + const char *data; + PyObject *(*get_code)(void); + Py_ssize_t size; + bool is_package; + bool is_alias; + const char *origname; +}; -PyObject * -PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, - const char *pathname, - const char *cpathname) +static frozen_status +find_frozen(PyObject *nameobj, struct frozen_info *info) { - PyObject *m = NULL; - PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; - - nameobj = PyUnicode_FromString(name); - if (nameobj == NULL) - return NULL; - - if (cpathname != NULL) { - cpathobj = PyUnicode_DecodeFSDefault(cpathname); - if (cpathobj == NULL) - goto error; + if (info != NULL) { + memset(info, 0, sizeof(*info)); } - else - cpathobj = NULL; - if (pathname != NULL) { - pathobj = PyUnicode_DecodeFSDefault(pathname); - if (pathobj == NULL) - goto error; + if (nameobj == NULL || nameobj == Py_None) { + return FROZEN_BAD_NAME; + } + const char *name = PyUnicode_AsUTF8(nameobj); + if (name == NULL) { + // Note that this function previously used + // _PyUnicode_EqualToASCIIString(). We clear the error here + // (instead of propagating it) to match the earlier behavior + // more closely. + PyErr_Clear(); + return FROZEN_BAD_NAME; } - else if (cpathobj != NULL) { - PyInterpreterState *interp = _PyInterpreterState_GET(); - - if (interp == NULL) { - Py_FatalError("no current interpreter"); - } - external= PyObject_GetAttrString(interp->importlib, - "_bootstrap_external"); - if (external != NULL) { - pathobj = _PyObject_CallMethodOneArg( - external, &_Py_ID(_get_sourcefile), cpathobj); - Py_DECREF(external); + const struct _frozen *p = look_up_frozen(name); + if (p == NULL) { + return FROZEN_NOT_FOUND; + } + if (info != NULL) { + info->nameobj = nameobj; // borrowed + info->data = (const char *)p->code; + info->get_code = p->get_code; + info->size = p->size; + info->is_package = p->is_package; + if (p->size < 0) { + // backward compatibility with negative size values + info->size = -(p->size); + info->is_package = true; } - if (pathobj == NULL) - PyErr_Clear(); + info->origname = name; + info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases, + &info->origname); } - else - pathobj = NULL; - - m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); -error: - Py_DECREF(nameobj); - Py_XDECREF(pathobj); - Py_XDECREF(cpathobj); - return m; + if (p->code == NULL && p->size == 0 && p->get_code != NULL) { + /* It is only deepfrozen. */ + return FROZEN_OKAY; + } + if (p->code == NULL) { + /* It is frozen but marked as un-importable. */ + return FROZEN_EXCLUDED; + } + if (p->code[0] == '\0' || p->size == 0) { + /* Does not contain executable code. */ + return FROZEN_INVALID; + } + return FROZEN_OKAY; } static PyObject * -module_dict_for_exec(PyThreadState *tstate, PyObject *name) +unmarshal_frozen_code(struct frozen_info *info) { - PyObject *m, *d; - - m = import_add_module(tstate, name); - if (m == NULL) - return NULL; - /* If the module is being reloaded, we get the old module back - and re-use its dict to exec the new code. */ - d = PyModule_GetDict(m); - int r = PyDict_Contains(d, &_Py_ID(__builtins__)); - if (r == 0) { - r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins()); + if (info->get_code) { + PyObject *code = info->get_code(); + assert(code != NULL); + return code; } - if (r < 0) { - remove_module(tstate, name); - Py_DECREF(m); + PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size); + if (co == NULL) { + /* Does not contain executable code. */ + set_frozen_error(FROZEN_INVALID, info->nameobj); return NULL; } - - Py_INCREF(d); - Py_DECREF(m); - return d; -} - -static PyObject * -exec_code_in_module(PyThreadState *tstate, PyObject *name, - PyObject *module_dict, PyObject *code_object) -{ - PyObject *v, *m; - - v = PyEval_EvalCode(code_object, module_dict, module_dict); - if (v == NULL) { - remove_module(tstate, name); + if (!PyCode_Check(co)) { + // We stick with TypeError for backward compatibility. + PyErr_Format(PyExc_TypeError, + "frozen object %R is not a code object", + info->nameobj); + Py_DECREF(co); return NULL; } - Py_DECREF(v); + return co; +} - m = import_get_module(tstate, name); - if (m == NULL && !_PyErr_Occurred(tstate)) { - _PyErr_Format(tstate, PyExc_ImportError, - "Loaded module %R not found in sys.modules", - name); - } - return m; -} +/* Initialize a frozen module. + Return 1 for success, 0 if the module is not found, and -1 with + an exception set if the initialization failed. + This function is also used from frozenmain.c */ -PyObject* -PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, - PyObject *cpathname) +int +PyImport_ImportFrozenModuleObject(PyObject *name) { PyThreadState *tstate = _PyThreadState_GET(); - PyObject *d, *external, *res; + PyObject *co, *m, *d = NULL; + int err; + struct frozen_info info; + frozen_status status = find_frozen(name, &info); + if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) { + return 0; + } + else if (status == FROZEN_BAD_NAME) { + return 0; + } + else if (status != FROZEN_OKAY) { + set_frozen_error(status, name); + return -1; + } + co = unmarshal_frozen_code(&info); + if (co == NULL) { + return -1; + } + if (info.is_package) { + /* Set __path__ to the empty list */ + PyObject *l; + m = import_add_module(tstate, name); + if (m == NULL) + goto err_return; + d = PyModule_GetDict(m); + l = PyList_New(0); + if (l == NULL) { + Py_DECREF(m); + goto err_return; + } + err = PyDict_SetItemString(d, "__path__", l); + Py_DECREF(l); + Py_DECREF(m); + if (err != 0) + goto err_return; + } d = module_dict_for_exec(tstate, name); if (d == NULL) { - return NULL; + goto err_return; } - - if (pathname == NULL) { - pathname = ((PyCodeObject *)co)->co_filename; + m = exec_code_in_module(tstate, name, d, co); + if (m == NULL) { + goto err_return; } - external = PyObject_GetAttrString(tstate->interp->importlib, - "_bootstrap_external"); - if (external == NULL) { - Py_DECREF(d); - return NULL; + Py_DECREF(m); + /* Set __origname__ (consumed in FrozenImporter._setup_module()). */ + PyObject *origname; + if (info.origname) { + origname = PyUnicode_FromString(info.origname); + if (origname == NULL) { + goto err_return; + } } - res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module), - d, name, pathname, cpathname, NULL); - Py_DECREF(external); - if (res != NULL) { - Py_DECREF(res); - res = exec_code_in_module(tstate, name, d, co); + else { + origname = Py_NewRef(Py_None); } - Py_DECREF(d); - return res; -} - - -static void -update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) -{ - PyObject *constants, *tmp; - Py_ssize_t i, n; - - if (PyUnicode_Compare(co->co_filename, oldname)) - return; - - Py_XSETREF(co->co_filename, Py_NewRef(newname)); - - constants = co->co_consts; - n = PyTuple_GET_SIZE(constants); - for (i = 0; i < n; i++) { - tmp = PyTuple_GET_ITEM(constants, i); - if (PyCode_Check(tmp)) - update_code_filenames((PyCodeObject *)tmp, - oldname, newname); + err = PyDict_SetItemString(d, "__origname__", origname); + Py_DECREF(origname); + if (err != 0) { + goto err_return; } -} - -static void -update_compiled_module(PyCodeObject *co, PyObject *newname) -{ - PyObject *oldname; - - if (PyUnicode_Compare(co->co_filename, newname) == 0) - return; + Py_DECREF(d); + Py_DECREF(co); + return 1; - oldname = co->co_filename; - Py_INCREF(oldname); - update_code_filenames(co, oldname, newname); - Py_DECREF(oldname); +err_return: + Py_XDECREF(d); + Py_DECREF(co); + return -1; } - -/********************/ -/* the magic number */ -/********************/ - -/* Helper for pythonrun.c -- return magic number and tag. */ - -long -PyImport_GetMagicNumber(void) +int +PyImport_ImportFrozenModule(const char *name) { - long res; - PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *external, *pyc_magic; - - external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); - if (external == NULL) - return -1; - pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); - Py_DECREF(external); - if (pyc_magic == NULL) + PyObject *nameobj; + int ret; + nameobj = PyUnicode_InternFromString(name); + if (nameobj == NULL) return -1; - res = PyLong_AsLong(pyc_magic); - Py_DECREF(pyc_magic); - return res; -} - - -extern const char * _PySys_ImplCacheTag; - -const char * -PyImport_GetMagicTag(void) -{ - return _PySys_ImplCacheTag; + ret = PyImport_ImportFrozenModuleObject(nameobj); + Py_DECREF(nameobj); + return ret; } From 66de41d8f326358a1d0b5a86d6fcfe3905d33a33 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 10:28:34 -0700 Subject: [PATCH 15/22] Add _PyImport_FiniExternal() and _PyImport_FiniCore(). --- Include/internal/pycore_import.h | 7 +++++ Include/internal/pycore_pylifecycle.h | 3 -- Include/internal/pycore_sysmodule.h | 3 ++ Python/import.c | 43 +++++++++++++++++++++++++++ Python/pylifecycle.c | 22 ++++++++++---- Python/sysmodule.c | 14 +++++++++ 6 files changed, 83 insertions(+), 9 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 2818f428e45d1c..b83ce0f40b4d60 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -70,11 +70,18 @@ extern PyObject * _PyImport_ImportlibModuleRepr( PyObject *module); +extern PyStatus _PyImport_Init(void); +extern void _PyImport_Fini(void); +extern void _PyImport_Fini2(void); + extern PyStatus _PyImport_InitCore( PyThreadState *tstate, PyObject *sysmod, int importlib); extern PyStatus _PyImport_InitExternal(PyThreadState *tstate); +extern void _PyImport_FiniCore(PyInterpreterState *interp); +extern void _PyImport_FiniExternal(PyInterpreterState *interp); + #ifdef HAVE_FORK extern PyStatus _PyImport_ReInitLock(void); diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h index 27ba86c2fe80d9..e7a31807205254 100644 --- a/Include/internal/pycore_pylifecycle.h +++ b/Include/internal/pycore_pylifecycle.h @@ -30,7 +30,6 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc); /* Various one-time initializers */ extern void _Py_InitVersion(void); -extern PyStatus _PyImport_Init(void); extern PyStatus _PyFaulthandler_Init(int enable); extern int _PyTraceMalloc_Init(int enable); extern PyObject * _PyBuiltin_Init(PyInterpreterState *interp); @@ -54,8 +53,6 @@ extern int _Py_Deepfreeze_Init(void); extern int _PySignal_Init(int install_signal_handlers); extern void _PySignal_Fini(void); -extern void _PyImport_Fini(void); -extern void _PyImport_Fini2(void); extern void _PyGC_Fini(PyInterpreterState *interp); extern void _Py_HashRandomization_Fini(void); extern void _PyFaulthandler_Fini(void); diff --git a/Include/internal/pycore_sysmodule.h b/Include/internal/pycore_sysmodule.h index 10d092cdc30a2c..b4b1febafa4479 100644 --- a/Include/internal/pycore_sysmodule.h +++ b/Include/internal/pycore_sysmodule.h @@ -20,6 +20,9 @@ extern void _PySys_ClearAuditHooks(PyThreadState *tstate); PyAPI_FUNC(int) _PySys_SetAttr(PyObject *, PyObject *); +extern int _PySys_ClearAttrString(PyInterpreterState *interp, + const char *name, int verbose); + #ifdef __cplusplus } #endif diff --git a/Python/import.c b/Python/import.c index 229202d479e153..f6f9b244a1f58b 100644 --- a/Python/import.c +++ b/Python/import.c @@ -100,6 +100,7 @@ static inline void _extensions_cache_clear(void); void _PyImport_Fini(void) { + /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ _extensions_cache_clear(); if (import_lock != NULL) { PyThread_free_lock(import_lock); @@ -145,6 +146,9 @@ static int init_importlib(PyThreadState *tstate, PyObject *sysmod); PyStatus _PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib) { + // XXX Initialize here: interp->modules and interp->import_func. + // XXX Initialize here: sys.modules and sys.meta_path. + if (importlib) { /* This call sets up builtin and frozen import support */ if (init_importlib(tstate, sysmod) < 0) { @@ -167,15 +171,38 @@ _PyImport_IsInitialized(PyInterpreterState *interp) return 1; } +/* Clear the direct per-interpreter import state, if not cleared already. */ void _PyImport_ClearCore(PyInterpreterState *interp) { + /* interp->modules should have been cleaned up and cleared already + by _PyImport_FiniCore(). */ Py_CLEAR(interp->modules); Py_CLEAR(interp->modules_by_index); Py_CLEAR(interp->importlib); Py_CLEAR(interp->import_func); } +void +_PyImport_FiniCore(PyInterpreterState *interp) +{ + int verbose = _PyInterpreterState_GetConfig(interp)->verbose; + + if (_PySys_ClearAttrString(interp, "meta_path", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } + + // XXX Pull in most of finalize_modules() in pylifecycle.c. + + if (_PySys_ClearAttrString(interp, "modules", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } + + _PyImport_ClearCore(interp); +} + +// XXX Add something like _PyImport_Disable() for use early in interp fini? + /* "external" imports */ @@ -222,6 +249,8 @@ _PyImport_InitExternal(PyThreadState *tstate) { int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + // XXX Initialize here: sys.path_hooks and sys.path_importer_cache. + if (init_importlib_external(tstate->interp) != 0) { _PyErr_Print(tstate); return _PyStatus_ERR("external importer setup failed"); @@ -235,6 +264,20 @@ _PyImport_InitExternal(PyThreadState *tstate) return _PyStatus_OK(); } +void +_PyImport_FiniExternal(PyInterpreterState *interp) +{ + int verbose = _PyInterpreterState_GetConfig(interp)->verbose; + + // XXX Uninstall importlib metapath importers here? + + if (_PySys_ClearAttrString(interp, "path_importer_cache", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } + if (_PySys_ClearAttrString(interp, "path_hooks", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } +} /*******************/ /* the import lock */ diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 6bc385be18c056..5a539be56a9a1b 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1306,8 +1306,11 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose) static const char * const sys_deletes[] = { "path", "argv", "ps1", "ps2", "last_type", "last_value", "last_traceback", - "path_hooks", "path_importer_cache", "meta_path", "__interactivehook__", + // path_hooks and path_importer_cache are cleared + // by _PyImport_FiniExternal(). + // XXX Clear meta_path in _PyImport_FiniCore(). + "meta_path", NULL }; @@ -1328,10 +1331,7 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose) const char * const *p; for (p = sys_deletes; *p != NULL; p++) { - if (verbose) { - PySys_WriteStderr("# clear sys.%s\n", *p); - } - if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) { + if (_PySys_ClearAttrString(interp, *p, verbose) < 0) { PyErr_WriteUnraisable(NULL); } } @@ -1503,6 +1503,7 @@ finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose) /* Clear modules, as good as we can */ +// XXX Move most of this to import.c. static void finalize_modules(PyThreadState *tstate) { @@ -1788,6 +1789,8 @@ Py_FinalizeEx(void) runtime->initialized = 0; runtime->core_initialized = 0; + // XXX Call something like _PyImport_Disable() here? + /* Destroy the state of all threads of the interpreter, except of the current thread. In practice, only daemon threads should still be alive, except if wait_for_thread_shutdown() has been cancelled by CTRL+C. @@ -1837,6 +1840,7 @@ Py_FinalizeEx(void) PyGC_Collect(); /* Destroy all modules */ + _PyImport_FiniExternal(tstate->interp); finalize_modules(tstate); /* Print debug stats if any */ @@ -1870,7 +1874,9 @@ Py_FinalizeEx(void) so it is possible to use tracemalloc in objects destructor. */ _PyTraceMalloc_Fini(); - /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ + /* Finalize any remaining import state */ + // XXX Move these up to where finalize_modules() is currently. + _PyImport_FiniCore(tstate->interp); _PyImport_Fini(); /* unload faulthandler module */ @@ -2110,6 +2116,10 @@ Py_EndInterpreter(PyThreadState *tstate) Py_FatalError("not the last thread"); } + // XXX Call something like _PyImport_Disable() here? + + _PyImport_FiniExternal(tstate->interp); + _PyImport_FiniCore(tstate->interp); finalize_modules(tstate); finalize_interp_clear(tstate); diff --git a/Python/sysmodule.c b/Python/sysmodule.c index b08897eaf9fd2f..b69b803560924c 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -142,6 +142,20 @@ PySys_SetObject(const char *name, PyObject *v) return sys_set_object_str(interp, name, v); } +int +_PySys_ClearAttrString(PyInterpreterState *interp, + const char *name, int verbose) +{ + if (verbose) { + PySys_WriteStderr("# clear sys.%s\n", name); + } + /* To play it safe, we set the attr to None instead of deleting it. */ + if (PyDict_SetItemString(interp->sysdict, name, Py_None) < 0) { + return -1; + } + return 0; +} + static int should_audit(PyInterpreterState *interp) From 384bd693d808342bc79a6b0ab4fa022cab4688ac Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 10:44:25 -0700 Subject: [PATCH 16/22] Factor out init_builtin_modules_table() and fini_builtin_builtins_table(). --- Python/import.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Python/import.c b/Python/import.c index f6f9b244a1f58b..c8cdbc50de27c5 100644 --- a/Python/import.c +++ b/Python/import.c @@ -63,32 +63,26 @@ static struct _inittab *inittab_copy = NULL; /* runtime lifecycle */ /*********************/ +static int init_builtin_modules_table(void); + PyStatus _PyImport_Init(void) { if (_PyRuntime.imports.inittab != NULL) { return _PyStatus_ERR("global import state already initialized"); } - PyStatus status = _PyStatus_OK(); - size_t size; - for (size = 0; PyImport_Inittab[size].name != NULL; size++) - ; - size++; + PyStatus status = _PyStatus_OK(); /* Force default raw memory allocator to get a known allocator to be able to release the memory in _PyImport_Fini() */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - /* Make the copy. */ - struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab)); - if (copied == NULL) { + if (init_builtin_modules_table() != 0) { status = PyStatus_NoMemory(); goto done; } - memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab)); - _PyRuntime.imports.inittab = copied; done: PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); @@ -96,6 +90,7 @@ _PyImport_Init(void) } static inline void _extensions_cache_clear(void); +static void fini_builtin_modules_table(void); void _PyImport_Fini(void) @@ -112,9 +107,7 @@ _PyImport_Fini(void) _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); /* Free memory allocated by _PyImport_Init() */ - struct _inittab *inittab = _PyRuntime.imports.inittab; - _PyRuntime.imports.inittab = NULL; - PyMem_RawFree(inittab); + fini_builtin_modules_table(); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } @@ -1239,6 +1232,34 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) } +/* the internal table */ + +static int +init_builtin_modules_table(void) +{ + size_t size; + for (size = 0; PyImport_Inittab[size].name != NULL; size++) + ; + size++; + + /* Make the copy. */ + struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab)); + if (copied == NULL) { + return -1; + } + memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab)); + _PyRuntime.imports.inittab = copied; + return 0; +} + +static void +fini_builtin_modules_table(void) +{ + struct _inittab *inittab = _PyRuntime.imports.inittab; + _PyRuntime.imports.inittab = NULL; + PyMem_RawFree(inittab); +} + PyObject * _PyImport_GetBuiltinModuleNames(void) { From e4777b7c1c5706444b00edae67891f5a36863d8e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 12:24:20 -0700 Subject: [PATCH 17/22] Add PyInterpreterState.imports. --- Include/internal/pycore_import.h | 53 +++++++++ Include/internal/pycore_interp.h | 34 +----- Include/internal/pycore_runtime_init.h | 15 +-- Python/import.c | 144 ++++++++++++++----------- Python/pystate.c | 8 +- 5 files changed, 143 insertions(+), 111 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index b83ce0f40b4d60..c7c3ceaa020341 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -36,6 +36,59 @@ struct _import_runtime_state { const char * pkgcontext; }; +struct _import_state { + /* cached sys.modules dictionary */ + PyObject *modules; + /* This is the list of module objects for all legacy (single-phase init) + extension modules ever loaded in this process (i.e. imported + in this interpreter or in any other). Py_None stands in for + modules that haven't actually been imported in this interpreter. + + A module's index (PyModuleDef.m_base.m_index) is used to look up + the corresponding module object for this interpreter, if any. + (See PyState_FindModule().) When any extension module + is initialized during import, its moduledef gets initialized by + PyModuleDef_Init(), and the first time that happens for each + PyModuleDef, its index gets set to the current value of + a global counter (see _PyRuntimeState.imports.last_module_index). + The entry for that index in this interpreter remains unset until + the module is actually imported here. (Py_None is used as + a placeholder.) Note that multi-phase init modules always get + an index for which there will never be a module set. + + This is initialized lazily in _PyState_AddModule(), which is also + where modules get added. */ + PyObject *modules_by_index; + /* importlib module._bootstrap */ + PyObject *importlib; + /* override for config->use_frozen_modules (for tests) + (-1: "off", 1: "on", 0: no override) */ + int override_frozen_modules; +#ifdef HAVE_DLOPEN + int dlopenflags; +#endif + PyObject *import_func; +}; + +#ifdef HAVE_DLOPEN +# include +# if HAVE_DECL_RTLD_NOW +# define _Py_DLOPEN_FLAGS RTLD_NOW +# else +# define _Py_DLOPEN_FLAGS RTLD_LAZY +# endif +# define DLOPENFLAGS_INIT .dlopenflags = _Py_DLOPEN_FLAGS, +#else +# define _Py_DLOPEN_FLAGS 0 +# define DLOPENFLAGS_INIT +#endif + +#define IMPORTS_INIT \ + { \ + .override_frozen_modules = 0, \ + DLOPENFLAGS_INIT \ + } + extern void _PyImport_ClearCore(PyInterpreterState *interp); extern Py_ssize_t _PyImport_GetNextModuleIndex(void); diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h index c11ae5522de76a..60de31b336f613 100644 --- a/Include/internal/pycore_interp.h +++ b/Include/internal/pycore_interp.h @@ -21,6 +21,7 @@ extern "C" { #include "pycore_function.h" // FUNC_MAX_WATCHERS #include "pycore_genobject.h" // struct _Py_async_gen_state #include "pycore_gc.h" // struct _gc_runtime_state +#include "pycore_import.h" // struct _import_state #include "pycore_list.h" // struct _Py_list_state #include "pycore_global_objects.h" // struct _Py_interp_static_objects #include "pycore_tuple.h" // struct _Py_tuple_state @@ -92,37 +93,12 @@ struct _is { struct _ceval_state ceval; struct _gc_runtime_state gc; - // sys.modules dictionary - PyObject *modules; - /* This is the list of module objects for all legacy (single-phase init) - extension modules ever loaded in this process (i.e. imported - in this interpreter or in any other). Py_None stands in for - modules that haven't actually been imported in this interpreter. - - A module's index (PyModuleDef.m_base.m_index) is used to look up - the corresponding module object for this interpreter, if any. - (See PyState_FindModule().) When any extension module - is initialized during import, its moduledef gets initialized by - PyModuleDef_Init(), and the first time that happens for each - PyModuleDef, its index gets set to the current value of - a global counter (see _PyRuntimeState.imports.last_module_index). - The entry for that index in this interpreter remains unset until - the module is actually imported here. (Py_None is used as - a placeholder.) Note that multi-phase init modules always get - an index for which there will never be a module set. - - This is initialized lazily in _PyState_AddModule(), which is also - where modules get added. */ - PyObject *modules_by_index; + struct _import_state imports; + // Dictionary of the sys module PyObject *sysdict; // Dictionary of the builtins module PyObject *builtins; - // importlib module - PyObject *importlib; - // override for config->use_frozen_modules (for tests) - // (-1: "off", 1: "on", 0: no override) - int override_frozen_modules; PyObject *codec_search_path; PyObject *codec_search_cache; @@ -130,15 +106,11 @@ struct _is { int codecs_initialized; PyConfig config; -#ifdef HAVE_DLOPEN - int dlopenflags; -#endif unsigned long feature_flags; PyObject *dict; /* Stores per-interpreter state */ PyObject *builtins_copy; - PyObject *import_func; // Initialized to _PyEval_EvalFrameDefault(). _PyFrameEvalFunction eval_frame; diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index c6a27d076eae2d..a8d5953ff98b0b 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -97,23 +97,10 @@ extern "C" { ._main_interpreter = _PyInterpreterState_INIT, \ } -#ifdef HAVE_DLOPEN -# include -# if HAVE_DECL_RTLD_NOW -# define _Py_DLOPEN_FLAGS RTLD_NOW -# else -# define _Py_DLOPEN_FLAGS RTLD_LAZY -# endif -# define DLOPENFLAGS_INIT .dlopenflags = _Py_DLOPEN_FLAGS, -#else -# define _Py_DLOPEN_FLAGS 0 -# define DLOPENFLAGS_INIT -#endif - #define _PyInterpreterState_INIT \ { \ .id_refcount = -1, \ - DLOPENFLAGS_INIT \ + .imports = IMPORTS_INIT, \ .ceval = { \ .recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \ }, \ diff --git a/Python/import.c b/Python/import.c index c8cdbc50de27c5..a464466d9a00be 100644 --- a/Python/import.c +++ b/Python/import.c @@ -59,6 +59,26 @@ static struct _inittab *inittab_copy = NULL; #define _Py_PackageContext (_PyRuntime.imports.pkgcontext) +/*******************************/ +/* interpreter import state */ +/*******************************/ + +#define MODULES(interp) \ + (interp)->imports.modules +#define MODULES_BY_INDEX(interp) \ + (interp)->imports.modules_by_index +#define IMPORTLIB(interp) \ + (interp)->imports.importlib +#define OVERRIDE_FROZEN_MODULES(interp) \ + (interp)->imports.override_frozen_modules +#ifdef HAVE_DLOPEN +# define DLOPENFLAGS(interp) \ + (interp)->imports.dlopenflags +#endif +#define IMPORT_FUNC(interp) \ + (interp)->imports.import_func + + /*********************/ /* runtime lifecycle */ /*********************/ @@ -159,7 +179,7 @@ _PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib) int _PyImport_IsInitialized(PyInterpreterState *interp) { - if (interp->modules == NULL) + if (MODULES(interp) == NULL) return 0; return 1; } @@ -170,10 +190,10 @@ _PyImport_ClearCore(PyInterpreterState *interp) { /* interp->modules should have been cleaned up and cleared already by _PyImport_FiniCore(). */ - Py_CLEAR(interp->modules); - Py_CLEAR(interp->modules_by_index); - Py_CLEAR(interp->importlib); - Py_CLEAR(interp->import_func); + Py_CLEAR(MODULES(interp)); + Py_CLEAR(MODULES_BY_INDEX(interp)); + Py_CLEAR(IMPORTLIB(interp)); + Py_CLEAR(IMPORT_FUNC(interp)); } void @@ -360,34 +380,34 @@ _PyImport_ReInitLock(void) PyObject * _PyImport_InitModules(PyInterpreterState *interp) { - assert(interp->modules == NULL); - interp->modules = PyDict_New(); - if (interp->modules == NULL) { + assert(MODULES(interp) == NULL); + MODULES(interp) = PyDict_New(); + if (MODULES(interp) == NULL) { return NULL; } - return interp->modules; + return MODULES(interp); } PyObject * _PyImport_GetModules(PyInterpreterState *interp) { - return interp->modules; + return MODULES(interp); } void _PyImport_ClearModules(PyInterpreterState *interp) { - Py_SETREF(interp->modules, NULL); + Py_SETREF(MODULES(interp), NULL); } PyObject * PyImport_GetModuleDict(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); - if (interp->modules == NULL) { + if (MODULES(interp) == NULL) { Py_FatalError("interpreter has no modules dictionary"); } - return interp->modules; + return MODULES(interp); } // This is only kept around for extensions that use _Py_IDENTIFIER. @@ -405,7 +425,7 @@ int _PyImport_SetModule(PyObject *name, PyObject *m) { PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *modules = interp->modules; + PyObject *modules = MODULES(interp); return PyObject_SetItem(modules, name, m); } @@ -413,14 +433,14 @@ int _PyImport_SetModuleString(const char *name, PyObject *m) { PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *modules = interp->modules; + PyObject *modules = MODULES(interp); return PyMapping_SetItemString(modules, name, m); } static PyObject * import_get_module(PyThreadState *tstate, PyObject *name) { - PyObject *modules = tstate->interp->modules; + PyObject *modules = MODULES(tstate->interp); if (modules == NULL) { _PyErr_SetString(tstate, PyExc_RuntimeError, "unable to get sys.modules"); @@ -459,7 +479,7 @@ import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *n if (busy) { /* Wait until module is done importing. */ PyObject *value = _PyObject_CallMethodOneArg( - interp->importlib, &_Py_ID(_lock_unlock_module), name); + IMPORTLIB(interp), &_Py_ID(_lock_unlock_module), name); if (value == NULL) { return -1; } @@ -494,7 +514,7 @@ PyImport_GetModule(PyObject *name) static PyObject * import_add_module(PyThreadState *tstate, PyObject *name) { - PyObject *modules = tstate->interp->modules; + PyObject *modules = MODULES(tstate->interp); if (modules == NULL) { _PyErr_SetString(tstate, PyExc_RuntimeError, "no import module dictionary"); @@ -573,7 +593,7 @@ remove_module(PyThreadState *tstate, PyObject *name) PyObject *type, *value, *traceback; _PyErr_Fetch(tstate, &type, &value, &traceback); - PyObject *modules = tstate->interp->modules; + PyObject *modules = MODULES(tstate->interp); if (PyDict_CheckExact(modules)) { PyObject *mod = _PyDict_Pop(modules, name, Py_None); Py_XDECREF(mod); @@ -603,18 +623,18 @@ PyObject* PyState_FindModule(PyModuleDef* module) { Py_ssize_t index = module->m_base.m_index; - PyInterpreterState *state = _PyInterpreterState_GET(); + PyInterpreterState *interp = _PyInterpreterState_GET(); PyObject *res; if (module->m_slots) { return NULL; } if (index == 0) return NULL; - if (state->modules_by_index == NULL) + if (MODULES_BY_INDEX(interp) == NULL) return NULL; - if (index >= PyList_GET_SIZE(state->modules_by_index)) + if (index >= PyList_GET_SIZE(MODULES_BY_INDEX(interp))) return NULL; - res = PyList_GET_ITEM(state->modules_by_index, index); + res = PyList_GET_ITEM(MODULES_BY_INDEX(interp), index); return res==Py_None ? NULL : res; } @@ -633,20 +653,20 @@ _PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def) } PyInterpreterState *interp = tstate->interp; - if (!interp->modules_by_index) { - interp->modules_by_index = PyList_New(0); - if (!interp->modules_by_index) { + if (!MODULES_BY_INDEX(interp)) { + MODULES_BY_INDEX(interp) = PyList_New(0); + if (!MODULES_BY_INDEX(interp)) { return -1; } } - while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) { - if (PyList_Append(interp->modules_by_index, Py_None) < 0) { + while (PyList_GET_SIZE(MODULES_BY_INDEX(interp)) <= def->m_base.m_index) { + if (PyList_Append(MODULES_BY_INDEX(interp), Py_None) < 0) { return -1; } } - return PyList_SetItem(interp->modules_by_index, + return PyList_SetItem(MODULES_BY_INDEX(interp), def->m_base.m_index, Py_NewRef(module)); } @@ -661,9 +681,9 @@ PyState_AddModule(PyObject* module, PyModuleDef* def) PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = tstate->interp; Py_ssize_t index = def->m_base.m_index; - if (interp->modules_by_index && - index < PyList_GET_SIZE(interp->modules_by_index) && - module == PyList_GET_ITEM(interp->modules_by_index, index)) + if (MODULES_BY_INDEX(interp) && + index < PyList_GET_SIZE(MODULES_BY_INDEX(interp)) && + module == PyList_GET_ITEM(MODULES_BY_INDEX(interp), index)) { _Py_FatalErrorFormat(__func__, "module %p already added", module); return -1; @@ -688,14 +708,14 @@ PyState_RemoveModule(PyModuleDef* def) if (index == 0) { Py_FatalError("invalid module index"); } - if (interp->modules_by_index == NULL) { + if (MODULES_BY_INDEX(interp) == NULL) { Py_FatalError("Interpreters module-list not accessible."); } - if (index > PyList_GET_SIZE(interp->modules_by_index)) { + if (index > PyList_GET_SIZE(MODULES_BY_INDEX(interp))) { Py_FatalError("Module index out of bounds."); } - return PyList_SetItem(interp->modules_by_index, index, Py_NewRef(Py_None)); + return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(Py_None)); } @@ -703,13 +723,13 @@ PyState_RemoveModule(PyModuleDef* def) void _PyImport_ClearModulesByIndex(PyInterpreterState *interp) { - if (!interp->modules_by_index) { + if (!MODULES_BY_INDEX(interp)) { return; } Py_ssize_t i; - for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) { - PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i); + for (i = 0; i < PyList_GET_SIZE(MODULES_BY_INDEX(interp)); i++) { + PyObject *m = PyList_GET_ITEM(MODULES_BY_INDEX(interp), i); if (PyModule_Check(m)) { /* cleanup the saved copy of module dicts */ PyModuleDef *md = PyModule_GetDef(m); @@ -721,10 +741,10 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp) /* Setting modules_by_index to NULL could be dangerous, so we clear the list instead. */ - if (PyList_SetSlice(interp->modules_by_index, - 0, PyList_GET_SIZE(interp->modules_by_index), + if (PyList_SetSlice(MODULES_BY_INDEX(interp), + 0, PyList_GET_SIZE(MODULES_BY_INDEX(interp)), NULL)) { - PyErr_WriteUnraisable(interp->modules_by_index); + PyErr_WriteUnraisable(MODULES_BY_INDEX(interp)); } } @@ -767,13 +787,13 @@ _PyImport_SwapPackageContext(const char *newcontext) int _PyImport_GetDLOpenFlags(PyInterpreterState *interp) { - return interp->dlopenflags; + return DLOPENFLAGS(interp); } void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val) { - interp->dlopenflags = new_val; + DLOPENFLAGS(interp) = new_val; } #endif // HAVE_DLOPEN @@ -1017,7 +1037,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name, } PyObject *mod, *mdict; - PyObject *modules = tstate->interp->modules; + PyObject *modules = MODULES(tstate->interp); if (def->m_size == -1) { /* Module does not support repeated initialization */ @@ -1115,7 +1135,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) return mod; } - PyObject *modules = tstate->interp->modules; + PyObject *modules = MODULES(tstate->interp); for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { if (_PyUnicode_EqualToASCIIString(name, p->name)) { if (p->initfunc == NULL) { @@ -1298,7 +1318,7 @@ PyImport_GetMagicNumber(void) PyInterpreterState *interp = _PyInterpreterState_GET(); PyObject *external, *pyc_magic; - external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); + external = PyObject_GetAttrString(IMPORTLIB(interp), "_bootstrap_external"); if (external == NULL) return -1; pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); @@ -1380,7 +1400,7 @@ PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, Py_FatalError("no current interpreter"); } - external= PyObject_GetAttrString(interp->importlib, + external= PyObject_GetAttrString(IMPORTLIB(interp), "_bootstrap_external"); if (external != NULL) { pathobj = _PyObject_CallMethodOneArg( @@ -1465,7 +1485,7 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, if (pathname == NULL) { pathname = ((PyCodeObject *)co)->co_filename; } - external = PyObject_GetAttrString(tstate->interp->importlib, + external = PyObject_GetAttrString(IMPORTLIB(tstate->interp), "_bootstrap_external"); if (external == NULL) { Py_DECREF(d); @@ -1549,7 +1569,7 @@ static bool use_frozen(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); - int override = interp->override_frozen_modules; + int override = OVERRIDE_FROZEN_MODULES(interp); if (override > 0) { return true; } @@ -2000,7 +2020,7 @@ init_importlib(PyThreadState *tstate, PyObject *sysmod) if (importlib == NULL) { return -1; } - interp->importlib = Py_NewRef(importlib); + IMPORTLIB(interp) = Py_NewRef(importlib); // Import the _imp module if (verbose) { @@ -2033,7 +2053,7 @@ static int init_importlib_external(PyInterpreterState *interp) { PyObject *value; - value = PyObject_CallMethod(interp->importlib, + value = PyObject_CallMethod(IMPORTLIB(interp), "_install_external_importers", ""); if (value == NULL) { return -1; @@ -2046,14 +2066,14 @@ PyObject * _PyImport_GetImportlibLoader(PyInterpreterState *interp, const char *loader_name) { - return PyObject_GetAttrString(interp->importlib, loader_name); + return PyObject_GetAttrString(IMPORTLIB(interp), loader_name); } PyObject * _PyImport_GetImportlibExternalLoader(PyInterpreterState *interp, const char *loader_name) { - PyObject *bootstrap = PyObject_GetAttrString(interp->importlib, + PyObject *bootstrap = PyObject_GetAttrString(IMPORTLIB(interp), "_bootstrap_external"); if (bootstrap == NULL) { return NULL; @@ -2067,7 +2087,7 @@ _PyImport_GetImportlibExternalLoader(PyInterpreterState *interp, PyObject * _PyImport_BlessMyLoader(PyInterpreterState *interp, PyObject *module_globals) { - PyObject *external = PyObject_GetAttrString(interp->importlib, + PyObject *external = PyObject_GetAttrString(IMPORTLIB(interp), "_bootstrap_external"); if (external == NULL) { return NULL; @@ -2082,7 +2102,7 @@ _PyImport_BlessMyLoader(PyInterpreterState *interp, PyObject *module_globals) PyObject * _PyImport_ImportlibModuleRepr(PyInterpreterState *interp, PyObject *m) { - return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); + return PyObject_CallMethod(IMPORTLIB(interp), "_module_repr", "O", m); } @@ -2169,14 +2189,14 @@ _PyImport_InitDefaultImportFunc(PyInterpreterState *interp) if (import_func == NULL) { return -1; } - interp->import_func = Py_NewRef(import_func); + IMPORT_FUNC(interp) = Py_NewRef(import_func); return 0; } int _PyImport_IsDefaultImportFunc(PyInterpreterState *interp, PyObject *func) { - return func == interp->import_func; + return func == IMPORT_FUNC(interp); } @@ -2482,8 +2502,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()) PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name)); - mod = PyObject_CallMethodObjArgs(interp->importlib, &_Py_ID(_find_and_load), - abs_name, interp->import_func, NULL); + mod = PyObject_CallMethodObjArgs(IMPORTLIB(interp), &_Py_ID(_find_and_load), + abs_name, IMPORT_FUNC(interp), NULL); if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED()) PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name), @@ -2635,8 +2655,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, if (path) { Py_DECREF(path); final_mod = PyObject_CallMethodObjArgs( - interp->importlib, &_Py_ID(_handle_fromlist), - mod, fromlist, interp->import_func, NULL); + IMPORTLIB(interp), &_Py_ID(_handle_fromlist), + mod, fromlist, IMPORT_FUNC(interp), NULL); } else { final_mod = Py_NewRef(mod); @@ -3187,7 +3207,7 @@ _imp__override_frozen_modules_for_tests_impl(PyObject *module, int override) /*[clinic end generated code: output=36d5cb1594160811 input=8f1f95a3ef21aec3]*/ { PyInterpreterState *interp = _PyInterpreterState_GET(); - interp->override_frozen_modules = override; + OVERRIDE_FROZEN_MODULES(interp) = override; Py_RETURN_NONE; } diff --git a/Python/pystate.c b/Python/pystate.c index dc171b818b47c3..4770caaed0a363 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -773,10 +773,10 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) Py_CLEAR(interp->codec_search_cache); Py_CLEAR(interp->codec_error_registry); - assert(interp->modules == NULL); - assert(interp->modules_by_index == NULL); - assert(interp->importlib == NULL); - assert(interp->import_func == NULL); + assert(interp->imports.modules == NULL); + assert(interp->imports.modules_by_index == NULL); + assert(interp->imports.importlib == NULL); + assert(interp->imports.import_func == NULL); Py_CLEAR(interp->builtins_copy); Py_CLEAR(interp->dict); From 48fb47c87996e030dfa9d90405417b04bd2057a1 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 12:28:00 -0700 Subject: [PATCH 18/22] Move the lifecycle sections to the bottom. --- Python/import.c | 418 ++++++++++++++++++++++++------------------------ 1 file changed, 205 insertions(+), 213 deletions(-) diff --git a/Python/import.c b/Python/import.c index a464466d9a00be..bc5bbc7e4f6666 100644 --- a/Python/import.c +++ b/Python/import.c @@ -79,219 +79,6 @@ static struct _inittab *inittab_copy = NULL; (interp)->imports.import_func -/*********************/ -/* runtime lifecycle */ -/*********************/ - -static int init_builtin_modules_table(void); - -PyStatus -_PyImport_Init(void) -{ - if (_PyRuntime.imports.inittab != NULL) { - return _PyStatus_ERR("global import state already initialized"); - } - - PyStatus status = _PyStatus_OK(); - - /* Force default raw memory allocator to get a known allocator to be able - to release the memory in _PyImport_Fini() */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - if (init_builtin_modules_table() != 0) { - status = PyStatus_NoMemory(); - goto done; - } - -done: - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - return status; -} - -static inline void _extensions_cache_clear(void); -static void fini_builtin_modules_table(void); - -void -_PyImport_Fini(void) -{ - /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ - _extensions_cache_clear(); - if (import_lock != NULL) { - PyThread_free_lock(import_lock); - import_lock = NULL; - } - - /* Use the same memory allocator as _PyImport_Init(). */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - /* Free memory allocated by _PyImport_Init() */ - fini_builtin_modules_table(); - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); -} - -void -_PyImport_Fini2(void) -{ - /* Use the same memory allocator than PyImport_ExtendInittab(). */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - // Reset PyImport_Inittab - PyImport_Inittab = _PyImport_Inittab; - - /* Free memory allocated by PyImport_ExtendInittab() */ - PyMem_RawFree(inittab_copy); - inittab_copy = NULL; - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); -} - - -/*************************/ -/* interpreter lifecycle */ -/*************************/ - -static int init_importlib(PyThreadState *tstate, PyObject *sysmod); - -PyStatus -_PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib) -{ - // XXX Initialize here: interp->modules and interp->import_func. - // XXX Initialize here: sys.modules and sys.meta_path. - - if (importlib) { - /* This call sets up builtin and frozen import support */ - if (init_importlib(tstate, sysmod) < 0) { - return _PyStatus_ERR("failed to initialize importlib"); - } - } - - return _PyStatus_OK(); -} - -/* In some corner cases it is important to be sure that the import - machinery has been initialized (or not cleaned up yet). For - example, see issue #4236 and PyModule_Create2(). */ - -int -_PyImport_IsInitialized(PyInterpreterState *interp) -{ - if (MODULES(interp) == NULL) - return 0; - return 1; -} - -/* Clear the direct per-interpreter import state, if not cleared already. */ -void -_PyImport_ClearCore(PyInterpreterState *interp) -{ - /* interp->modules should have been cleaned up and cleared already - by _PyImport_FiniCore(). */ - Py_CLEAR(MODULES(interp)); - Py_CLEAR(MODULES_BY_INDEX(interp)); - Py_CLEAR(IMPORTLIB(interp)); - Py_CLEAR(IMPORT_FUNC(interp)); -} - -void -_PyImport_FiniCore(PyInterpreterState *interp) -{ - int verbose = _PyInterpreterState_GetConfig(interp)->verbose; - - if (_PySys_ClearAttrString(interp, "meta_path", verbose) < 0) { - PyErr_WriteUnraisable(NULL); - } - - // XXX Pull in most of finalize_modules() in pylifecycle.c. - - if (_PySys_ClearAttrString(interp, "modules", verbose) < 0) { - PyErr_WriteUnraisable(NULL); - } - - _PyImport_ClearCore(interp); -} - -// XXX Add something like _PyImport_Disable() for use early in interp fini? - - -/* "external" imports */ - -static int -init_zipimport(PyThreadState *tstate, int verbose) -{ - PyObject *path_hooks = PySys_GetObject("path_hooks"); - if (path_hooks == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "unable to get sys.path_hooks"); - return -1; - } - - if (verbose) { - PySys_WriteStderr("# installing zipimport hook\n"); - } - - PyObject *zipimporter = _PyImport_GetModuleAttrString("zipimport", "zipimporter"); - if (zipimporter == NULL) { - _PyErr_Clear(tstate); /* No zipimporter object -- okay */ - if (verbose) { - PySys_WriteStderr("# can't import zipimport.zipimporter\n"); - } - } - else { - /* sys.path_hooks.insert(0, zipimporter) */ - int err = PyList_Insert(path_hooks, 0, zipimporter); - Py_DECREF(zipimporter); - if (err < 0) { - return -1; - } - if (verbose) { - PySys_WriteStderr("# installed zipimport hook\n"); - } - } - - return 0; -} - -static int init_importlib_external(PyInterpreterState *interp); - -PyStatus -_PyImport_InitExternal(PyThreadState *tstate) -{ - int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; - - // XXX Initialize here: sys.path_hooks and sys.path_importer_cache. - - if (init_importlib_external(tstate->interp) != 0) { - _PyErr_Print(tstate); - return _PyStatus_ERR("external importer setup failed"); - } - - if (init_zipimport(tstate, verbose) != 0) { - PyErr_Print(); - return _PyStatus_ERR("initializing zipimport failed"); - } - - return _PyStatus_OK(); -} - -void -_PyImport_FiniExternal(PyInterpreterState *interp) -{ - int verbose = _PyInterpreterState_GetConfig(interp)->verbose; - - // XXX Uninstall importlib metapath importers here? - - if (_PySys_ClearAttrString(interp, "path_importer_cache", verbose) < 0) { - PyErr_WriteUnraisable(NULL); - } - if (_PySys_ClearAttrString(interp, "path_hooks", verbose) < 0) { - PyErr_WriteUnraisable(NULL); - } -} - /*******************/ /* the import lock */ /*******************/ @@ -2792,6 +2579,211 @@ PyImport_Import(PyObject *module_name) } +/*********************/ +/* runtime lifecycle */ +/*********************/ + +PyStatus +_PyImport_Init(void) +{ + if (_PyRuntime.imports.inittab != NULL) { + return _PyStatus_ERR("global import state already initialized"); + } + + PyStatus status = _PyStatus_OK(); + + /* Force default raw memory allocator to get a known allocator to be able + to release the memory in _PyImport_Fini() */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + if (init_builtin_modules_table() != 0) { + status = PyStatus_NoMemory(); + goto done; + } + +done: + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return status; +} + +void +_PyImport_Fini(void) +{ + /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ + _extensions_cache_clear(); + if (import_lock != NULL) { + PyThread_free_lock(import_lock); + import_lock = NULL; + } + + /* Use the same memory allocator as _PyImport_Init(). */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + /* Free memory allocated by _PyImport_Init() */ + fini_builtin_modules_table(); + + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); +} + +void +_PyImport_Fini2(void) +{ + /* Use the same memory allocator than PyImport_ExtendInittab(). */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + // Reset PyImport_Inittab + PyImport_Inittab = _PyImport_Inittab; + + /* Free memory allocated by PyImport_ExtendInittab() */ + PyMem_RawFree(inittab_copy); + inittab_copy = NULL; + + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); +} + + +/*************************/ +/* interpreter lifecycle */ +/*************************/ + +PyStatus +_PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib) +{ + // XXX Initialize here: interp->modules and interp->import_func. + // XXX Initialize here: sys.modules and sys.meta_path. + + if (importlib) { + /* This call sets up builtin and frozen import support */ + if (init_importlib(tstate, sysmod) < 0) { + return _PyStatus_ERR("failed to initialize importlib"); + } + } + + return _PyStatus_OK(); +} + +/* In some corner cases it is important to be sure that the import + machinery has been initialized (or not cleaned up yet). For + example, see issue #4236 and PyModule_Create2(). */ + +int +_PyImport_IsInitialized(PyInterpreterState *interp) +{ + if (MODULES(interp) == NULL) + return 0; + return 1; +} + +/* Clear the direct per-interpreter import state, if not cleared already. */ +void +_PyImport_ClearCore(PyInterpreterState *interp) +{ + /* interp->modules should have been cleaned up and cleared already + by _PyImport_FiniCore(). */ + Py_CLEAR(MODULES(interp)); + Py_CLEAR(MODULES_BY_INDEX(interp)); + Py_CLEAR(IMPORTLIB(interp)); + Py_CLEAR(IMPORT_FUNC(interp)); +} + +void +_PyImport_FiniCore(PyInterpreterState *interp) +{ + int verbose = _PyInterpreterState_GetConfig(interp)->verbose; + + if (_PySys_ClearAttrString(interp, "meta_path", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } + + // XXX Pull in most of finalize_modules() in pylifecycle.c. + + if (_PySys_ClearAttrString(interp, "modules", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } + + _PyImport_ClearCore(interp); +} + +// XXX Add something like _PyImport_Disable() for use early in interp fini? + + +/* "external" imports */ + +static int +init_zipimport(PyThreadState *tstate, int verbose) +{ + PyObject *path_hooks = PySys_GetObject("path_hooks"); + if (path_hooks == NULL) { + _PyErr_SetString(tstate, PyExc_RuntimeError, + "unable to get sys.path_hooks"); + return -1; + } + + if (verbose) { + PySys_WriteStderr("# installing zipimport hook\n"); + } + + PyObject *zipimporter = _PyImport_GetModuleAttrString("zipimport", "zipimporter"); + if (zipimporter == NULL) { + _PyErr_Clear(tstate); /* No zipimporter object -- okay */ + if (verbose) { + PySys_WriteStderr("# can't import zipimport.zipimporter\n"); + } + } + else { + /* sys.path_hooks.insert(0, zipimporter) */ + int err = PyList_Insert(path_hooks, 0, zipimporter); + Py_DECREF(zipimporter); + if (err < 0) { + return -1; + } + if (verbose) { + PySys_WriteStderr("# installed zipimport hook\n"); + } + } + + return 0; +} + +PyStatus +_PyImport_InitExternal(PyThreadState *tstate) +{ + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + + // XXX Initialize here: sys.path_hooks and sys.path_importer_cache. + + if (init_importlib_external(tstate->interp) != 0) { + _PyErr_Print(tstate); + return _PyStatus_ERR("external importer setup failed"); + } + + if (init_zipimport(tstate, verbose) != 0) { + PyErr_Print(); + return _PyStatus_ERR("initializing zipimport failed"); + } + + return _PyStatus_OK(); +} + +void +_PyImport_FiniExternal(PyInterpreterState *interp) +{ + int verbose = _PyInterpreterState_GetConfig(interp)->verbose; + + // XXX Uninstall importlib metapath importers here? + + if (_PySys_ClearAttrString(interp, "path_importer_cache", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } + if (_PySys_ClearAttrString(interp, "path_hooks", verbose) < 0) { + PyErr_WriteUnraisable(NULL); + } +} + + /******************/ /* module helpers */ /******************/ From e8930c86726f15d6cef61e048dc7abe41fe722de Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 12:37:33 -0700 Subject: [PATCH 19/22] Add macros for runtime-global import state. --- Python/import.c | 59 +++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/Python/import.c b/Python/import.c index bc5bbc7e4f6666..042c6d82076934 100644 --- a/Python/import.c +++ b/Python/import.c @@ -52,11 +52,16 @@ static struct _inittab *inittab_copy = NULL; /* runtime-global import state */ /*******************************/ +#define INITTAB _PyRuntime.imports.inittab +#define LAST_MODULE_INDEX _PyRuntime.imports.last_module_index +#define EXTENSIONS _PyRuntime.imports.extensions + #define import_lock _PyRuntime.imports.lock.mutex #define import_lock_thread _PyRuntime.imports.lock.thread #define import_lock_level _PyRuntime.imports.lock.level -#define _Py_PackageContext (_PyRuntime.imports.pkgcontext) +#define FIND_AND_LOAD _PyRuntime.imports.find_and_load +#define PKGCONTEXT (_PyRuntime.imports.pkgcontext) /*******************************/ @@ -402,8 +407,8 @@ remove_module(PyThreadState *tstate, PyObject *name) Py_ssize_t _PyImport_GetNextModuleIndex(void) { - _PyRuntime.imports.last_module_index++; - return _PyRuntime.imports.last_module_index; + LAST_MODULE_INDEX++; + return LAST_MODULE_INDEX; } PyObject* @@ -546,17 +551,17 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp) the module name is "package.module", but the module calls PyModule_Create*() with just "module" for the name. The shared library loader squirrels away the true name of the module in - _Py_PackageContext, and PyModule_Create*() will substitute this - (if the name actually matches). + _PyRuntime.imports.pkgcontext, and PyModule_Create*() will + substitute this (if the name actually matches). */ const char * _PyImport_ResolveNameWithPackageContext(const char *name) { - if (_Py_PackageContext != NULL) { - const char *p = strrchr(_Py_PackageContext, '.'); + if (PKGCONTEXT != NULL) { + const char *p = strrchr(PKGCONTEXT, '.'); if (p != NULL && strcmp(name, p+1) == 0) { - name = _Py_PackageContext; - _Py_PackageContext = NULL; + name = PKGCONTEXT; + PKGCONTEXT = NULL; } } return name; @@ -565,8 +570,8 @@ _PyImport_ResolveNameWithPackageContext(const char *name) const char * _PyImport_SwapPackageContext(const char *newcontext) { - const char *oldcontext = _Py_PackageContext; - _Py_PackageContext = newcontext; + const char *oldcontext = PKGCONTEXT; + PKGCONTEXT = newcontext; return oldcontext; } @@ -708,7 +713,7 @@ gets even messier. static PyModuleDef * _extensions_cache_get(PyObject *filename, PyObject *name) { - PyObject *extensions = _PyRuntime.imports.extensions; + PyObject *extensions = EXTENSIONS; if (extensions == NULL) { return NULL; } @@ -724,13 +729,13 @@ _extensions_cache_get(PyObject *filename, PyObject *name) static int _extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def) { - PyObject *extensions = _PyRuntime.imports.extensions; + PyObject *extensions = EXTENSIONS; if (extensions == NULL) { extensions = PyDict_New(); if (extensions == NULL) { return -1; } - _PyRuntime.imports.extensions = extensions; + EXTENSIONS = extensions; } PyObject *key = PyTuple_Pack(2, filename, name); if (key == NULL) { @@ -747,7 +752,7 @@ _extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def) static void _extensions_cache_clear(void) { - Py_CLEAR(_PyRuntime.imports.extensions); + Py_CLEAR(EXTENSIONS); } static int @@ -902,7 +907,7 @@ static int is_builtin(PyObject *name) { int i; - struct _inittab *inittab = _PyRuntime.imports.inittab; + struct _inittab *inittab = INITTAB; for (i = 0; inittab[i].name != NULL; i++) { if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) { if (inittab[i].initfunc == NULL) @@ -923,7 +928,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) } PyObject *modules = MODULES(tstate->interp); - for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) { + for (struct _inittab *p = INITTAB; p->name != NULL; p++) { if (_PyUnicode_EqualToASCIIString(name, p->name)) { if (p->initfunc == NULL) { /* Cannot re-init internal module ("sys" or "builtins") */ @@ -978,7 +983,7 @@ PyImport_ExtendInittab(struct _inittab *newtab) size_t i, n; int res = 0; - if (_PyRuntime.imports.inittab != NULL) { + if (INITTAB != NULL) { Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()"); } @@ -1026,7 +1031,7 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) { struct _inittab newtab[2]; - if (_PyRuntime.imports.inittab != NULL) { + if (INITTAB != NULL) { Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()"); } @@ -1055,15 +1060,15 @@ init_builtin_modules_table(void) return -1; } memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab)); - _PyRuntime.imports.inittab = copied; + INITTAB = copied; return 0; } static void fini_builtin_modules_table(void) { - struct _inittab *inittab = _PyRuntime.imports.inittab; - _PyRuntime.imports.inittab = NULL; + struct _inittab *inittab = INITTAB; + INITTAB = NULL; PyMem_RawFree(inittab); } @@ -1074,7 +1079,7 @@ _PyImport_GetBuiltinModuleNames(void) if (list == NULL) { return NULL; } - struct _inittab *inittab = _PyRuntime.imports.inittab; + struct _inittab *inittab = INITTAB; for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) { PyObject *name = PyUnicode_FromString(inittab[i].name); if (name == NULL) { @@ -2251,8 +2256,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) PyObject *mod = NULL; PyInterpreterState *interp = tstate->interp; int import_time = _PyInterpreterState_GetConfig(interp)->import_time; -#define import_level _PyRuntime.imports.find_and_load.import_level -#define accumulated _PyRuntime.imports.find_and_load.accumulated +#define import_level FIND_AND_LOAD.import_level +#define accumulated FIND_AND_LOAD.accumulated _PyTime_t t1 = 0, accumulated_copy = accumulated; @@ -2273,7 +2278,7 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) * _PyDict_GetItemIdWithError(). */ if (import_time) { -#define header _PyRuntime.imports.find_and_load.header +#define header FIND_AND_LOAD.header if (header) { fputs("import time: self [us] | cumulative | imported package\n", stderr); @@ -2586,7 +2591,7 @@ PyImport_Import(PyObject *module_name) PyStatus _PyImport_Init(void) { - if (_PyRuntime.imports.inittab != NULL) { + if (INITTAB != NULL) { return _PyStatus_ERR("global import state already initialized"); } From 79a72c4a7e1b355d5dbfc93a7ab22106d368b2fd Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 14 Feb 2023 15:21:52 -0700 Subject: [PATCH 20/22] minor fixes --- Objects/moduleobject.c | 2 +- Python/pylifecycle.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 69edbf30b023f8..a0be19a3ca8ac8 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -688,7 +688,7 @@ module_dealloc(PyModuleObject *m) Py_TYPE(m)->tp_free((PyObject *)m); } -PyObject * +static PyObject * module_repr(PyModuleObject *m) { PyInterpreterState *interp = _PyInterpreterState_GET(); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 5a539be56a9a1b..70be65200dfa89 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2119,8 +2119,8 @@ Py_EndInterpreter(PyThreadState *tstate) // XXX Call something like _PyImport_Disable() here? _PyImport_FiniExternal(tstate->interp); - _PyImport_FiniCore(tstate->interp); finalize_modules(tstate); + _PyImport_FiniCore(tstate->interp); finalize_interp_clear(tstate); finalize_interp_delete(tstate->interp); From 0527913b14519f1587de282cdba4305e0752b115 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 15 Feb 2023 10:58:23 -0700 Subject: [PATCH 21/22] Drop _PyState_AddModule(). --- Include/internal/pycore_import.h | 2 +- Include/internal/pycore_pystate.h | 6 -- Lib/test/test_imp.py | 2 +- Lib/test/test_stable_abi_ctypes.py | 1 - Misc/stable_abi.toml | 3 - PC/python3dll.c | 1 - Python/import.c | 116 +++++++++++++++++------------ 7 files changed, 69 insertions(+), 62 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index c7c3ceaa020341..da766253ef6b9c 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -56,7 +56,7 @@ struct _import_state { a placeholder.) Note that multi-phase init modules always get an index for which there will never be a module set. - This is initialized lazily in _PyState_AddModule(), which is also + This is initialized lazily in PyState_AddModule(), which is also where modules get added. */ PyObject *modules_by_index; /* importlib module._bootstrap */ diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 7046ec8d9adaaf..638b86253879ea 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -152,12 +152,6 @@ extern void _PySignal_AfterFork(void); #endif -PyAPI_FUNC(int) _PyState_AddModule( - PyThreadState *tstate, - PyObject* module, - PyModuleDef* def); - - PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate); #define HEAD_LOCK(runtime) \ diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py index 31dce21587e2ca..c85ab92307de78 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -387,7 +387,7 @@ def check_with_reinit_reloaded(module, lookedup, initialized, check_basic_reloaded(mod, lookedup, initialized, init_count, before, reloaded) - # Currently _PyState_AddModule() always replaces the cached module. + # Currently PyState_AddModule() always replaces the cached module. self.assertIs(basic.look_up_self(), mod) self.assertEqual(basic.initialized_count(), expected_init_count) diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index e77c1c8409880d..7e50fbda2c07cb 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -864,7 +864,6 @@ def test_windows_feature_macros(self): "_PyObject_GC_Resize", "_PyObject_New", "_PyObject_NewVar", - "_PyState_AddModule", "_PyThreadState_Init", "_PyThreadState_Prealloc", "_PyWeakref_CallableProxyType", diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index 21ff9616133445..c04a3a228caf56 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -1684,9 +1684,6 @@ [function._PyObject_NewVar] added = '3.2' abi_only = true -[function._PyState_AddModule] - added = '3.2' - abi_only = true [function._PyThreadState_Init] added = '3.2' abi_only = true diff --git a/PC/python3dll.c b/PC/python3dll.c index e300819365756e..79f09037282f54 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -34,7 +34,6 @@ EXPORT_FUNC(_PyObject_GC_NewVar) EXPORT_FUNC(_PyObject_GC_Resize) EXPORT_FUNC(_PyObject_New) EXPORT_FUNC(_PyObject_NewVar) -EXPORT_FUNC(_PyState_AddModule) EXPORT_FUNC(_PyThreadState_Init) EXPORT_FUNC(_PyThreadState_Prealloc) EXPORT_FUNC(Py_AddPendingCall) diff --git a/Python/import.c b/Python/import.c index 042c6d82076934..36a0ff8fa22394 100644 --- a/Python/import.c +++ b/Python/import.c @@ -411,55 +411,79 @@ _PyImport_GetNextModuleIndex(void) return LAST_MODULE_INDEX; } -PyObject* -PyState_FindModule(PyModuleDef* module) +static const char * +_modules_by_index_check(PyInterpreterState *interp, Py_ssize_t index) { - Py_ssize_t index = module->m_base.m_index; - PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *res; - if (module->m_slots) { - return NULL; + if (index == 0) { + return "invalid module index"; } - if (index == 0) - return NULL; - if (MODULES_BY_INDEX(interp) == NULL) - return NULL; - if (index >= PyList_GET_SIZE(MODULES_BY_INDEX(interp))) + if (MODULES_BY_INDEX(interp) == NULL) { + return "Interpreters module-list not accessible."; + } + if (index > PyList_GET_SIZE(MODULES_BY_INDEX(interp))) { + return "Module index out of bounds."; + } + return NULL; +} + +static PyObject * +_modules_by_index_get(PyInterpreterState *interp, PyModuleDef *def) +{ + Py_ssize_t index = def->m_base.m_index; + if (_modules_by_index_check(interp, index) != NULL) { return NULL; - res = PyList_GET_ITEM(MODULES_BY_INDEX(interp), index); + } + PyObject *res = PyList_GET_ITEM(MODULES_BY_INDEX(interp), index); return res==Py_None ? NULL : res; } -int -_PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def) +static int +_modules_by_index_set(PyInterpreterState *interp, + PyModuleDef *def, PyObject *module) { - if (!def) { - assert(_PyErr_Occurred(tstate)); - return -1; - } - if (def->m_slots) { - _PyErr_SetString(tstate, - PyExc_SystemError, - "PyState_AddModule called on module with slots"); - return -1; - } + assert(def != NULL); + assert(def == PyModule_GetDef(module)); + assert(def->m_slots == NULL); + assert(def->m_base.m_index > 0); - PyInterpreterState *interp = tstate->interp; - if (!MODULES_BY_INDEX(interp)) { + if (MODULES_BY_INDEX(interp) == NULL) { MODULES_BY_INDEX(interp) = PyList_New(0); - if (!MODULES_BY_INDEX(interp)) { + if (MODULES_BY_INDEX(interp) == NULL) { return -1; } } - while (PyList_GET_SIZE(MODULES_BY_INDEX(interp)) <= def->m_base.m_index) { + Py_ssize_t index = def->m_base.m_index; + while (PyList_GET_SIZE(MODULES_BY_INDEX(interp)) <= index) { if (PyList_Append(MODULES_BY_INDEX(interp), Py_None) < 0) { return -1; } } - return PyList_SetItem(MODULES_BY_INDEX(interp), - def->m_base.m_index, Py_NewRef(module)); + return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(module)); +} + +static int +_modules_by_index_clear(PyInterpreterState *interp, PyModuleDef *def) +{ + Py_ssize_t index = def->m_base.m_index; + const char *err = _modules_by_index_check(interp, index); + if (err != NULL) { + Py_FatalError(err); + return -1; + } + return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(Py_None)); +} + + +PyObject* +PyState_FindModule(PyModuleDef* module) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (module->m_slots) { + return NULL; + } + return _modules_by_index_get(interp, module); } int @@ -471,6 +495,13 @@ PyState_AddModule(PyObject* module, PyModuleDef* def) } PyThreadState *tstate = _PyThreadState_GET(); + if (def->m_slots) { + _PyErr_SetString(tstate, + PyExc_SystemError, + "PyState_AddModule called on module with slots"); + return -1; + } + PyInterpreterState *interp = tstate->interp; Py_ssize_t index = def->m_base.m_index; if (MODULES_BY_INDEX(interp) && @@ -480,34 +511,21 @@ PyState_AddModule(PyObject* module, PyModuleDef* def) _Py_FatalErrorFormat(__func__, "module %p already added", module); return -1; } - return _PyState_AddModule(tstate, module, def); + + return _modules_by_index_set(interp, def, module); } int PyState_RemoveModule(PyModuleDef* def) { PyThreadState *tstate = _PyThreadState_GET(); - PyInterpreterState *interp = tstate->interp; - if (def->m_slots) { _PyErr_SetString(tstate, PyExc_SystemError, "PyState_RemoveModule called on module with slots"); return -1; } - - Py_ssize_t index = def->m_base.m_index; - if (index == 0) { - Py_FatalError("invalid module index"); - } - if (MODULES_BY_INDEX(interp) == NULL) { - Py_FatalError("Interpreters module-list not accessible."); - } - if (index > PyList_GET_SIZE(MODULES_BY_INDEX(interp))) { - Py_FatalError("Module index out of bounds."); - } - - return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(Py_None)); + return _modules_by_index_clear(tstate->interp, def); } @@ -770,7 +788,7 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename) } PyThreadState *tstate = _PyThreadState_GET(); - if (_PyState_AddModule(tstate, mod, def) < 0) { + if (_modules_by_index_set(tstate->interp, def, mod) < 0) { return -1; } @@ -859,7 +877,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name, return NULL; } } - if (_PyState_AddModule(tstate, mod, def) < 0) { + if (_modules_by_index_set(tstate->interp, def, mod) < 0) { PyMapping_DelItem(modules, name); Py_DECREF(mod); return NULL; From a4deb8aaf07d141e0fa62857fef46aff658bf605 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 15 Feb 2023 12:39:23 -0700 Subject: [PATCH 22/22] Drop a wrong assert. --- Python/import.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/import.c b/Python/import.c index 36a0ff8fa22394..ae27aaf56848d6 100644 --- a/Python/import.c +++ b/Python/import.c @@ -442,7 +442,6 @@ _modules_by_index_set(PyInterpreterState *interp, PyModuleDef *def, PyObject *module) { assert(def != NULL); - assert(def == PyModule_GetDef(module)); assert(def->m_slots == NULL); assert(def->m_base.m_index > 0);