diff options
author | shadchin <[email protected]> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Python/import.c | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Python/import.c')
-rw-r--r-- | contrib/tools/python3/src/Python/import.c | 968 |
1 files changed, 484 insertions, 484 deletions
diff --git a/contrib/tools/python3/src/Python/import.c b/contrib/tools/python3/src/Python/import.c index e4dbf4b0d8c..a6e0841b720 100644 --- a/contrib/tools/python3/src/Python/import.c +++ b/contrib/tools/python3/src/Python/import.c @@ -3,15 +3,15 @@ #include "Python.h" #include "Python-ast.h" -#undef Yield /* undefine macro conflicting with <winbase.h> */ -#include "pycore_initconfig.h" -#include "pycore_pyerrors.h" -#include "pycore_pyhash.h" -#include "pycore_pylifecycle.h" -#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() -#include "pycore_interp.h" // _PyInterpreterState_ClearModules() -#include "pycore_pystate.h" // _PyInterpreterState_GET() -#include "pycore_sysmodule.h" +#undef Yield /* undefine macro conflicting with <winbase.h> */ +#include "pycore_initconfig.h" +#include "pycore_pyerrors.h" +#include "pycore_pyhash.h" +#include "pycore_pylifecycle.h" +#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() +#include "pycore_interp.h" // _PyInterpreterState_ClearModules() +#include "pycore_pystate.h" // _PyInterpreterState_GET() +#include "pycore_sysmodule.h" #include "errcode.h" #include "marshal.h" #include "code.h" @@ -27,9 +27,9 @@ extern "C" { #define CACHEDIR "__pycache__" -/* Forward references */ -static PyObject *import_add_module(PyThreadState *tstate, PyObject *name); - +/* Forward references */ +static PyObject *import_add_module(PyThreadState *tstate, PyObject *name); + /* See _PyImport_FixupExtensionObject() below */ static PyObject *extensions = NULL; @@ -39,9 +39,9 @@ extern struct _inittab _PyImport_Inittab[]; struct _inittab *PyImport_Inittab = _PyImport_Inittab; static struct _inittab *inittab_copy = NULL; -_Py_IDENTIFIER(__path__); -_Py_IDENTIFIER(__spec__); - +_Py_IDENTIFIER(__path__); +_Py_IDENTIFIER(__spec__); + /*[clinic input] module _imp [clinic start generated code]*/ @@ -51,8 +51,8 @@ module _imp /* Initialize things */ -PyStatus -_PyImportHooks_Init(PyThreadState *tstate) +PyStatus +_PyImportHooks_Init(PyThreadState *tstate) { PyObject *v, *path_hooks = NULL; int err = 0; @@ -80,49 +80,49 @@ _PyImportHooks_Init(PyThreadState *tstate) goto error; } Py_DECREF(path_hooks); - return _PyStatus_OK(); + return _PyStatus_OK(); error: - _PyErr_Print(tstate); - return _PyStatus_ERR("initializing sys.meta_path, sys.path_hooks, " + _PyErr_Print(tstate); + return _PyStatus_ERR("initializing sys.meta_path, sys.path_hooks, " "or path_importer_cache failed"); } -PyStatus -_PyImportZip_Init(PyThreadState *tstate) +PyStatus +_PyImportZip_Init(PyThreadState *tstate) { - PyObject *path_hooks, *zipimport; + PyObject *path_hooks, *zipimport; int err = 0; path_hooks = PySys_GetObject("path_hooks"); if (path_hooks == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "unable to get sys.path_hooks"); + _PyErr_SetString(tstate, PyExc_RuntimeError, + "unable to get sys.path_hooks"); goto error; } - int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; - if (verbose) { + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + if (verbose) { PySys_WriteStderr("# installing zipimport hook\n"); - } + } - zipimport = PyImport_ImportModule("zipimport"); - if (zipimport == NULL) { - _PyErr_Clear(tstate); /* No zip import module -- okay */ - if (verbose) { + zipimport = PyImport_ImportModule("zipimport"); + if (zipimport == NULL) { + _PyErr_Clear(tstate); /* No zip import module -- okay */ + if (verbose) { PySys_WriteStderr("# can't import zipimport\n"); - } + } } else { _Py_IDENTIFIER(zipimporter); - PyObject *zipimporter = _PyObject_GetAttrId(zipimport, + PyObject *zipimporter = _PyObject_GetAttrId(zipimport, &PyId_zipimporter); - Py_DECREF(zipimport); + Py_DECREF(zipimport); if (zipimporter == NULL) { - _PyErr_Clear(tstate); /* No zipimporter object -- okay */ - if (verbose) { - PySys_WriteStderr("# can't import zipimport.zipimporter\n"); - } + _PyErr_Clear(tstate); /* No zipimporter object -- okay */ + if (verbose) { + PySys_WriteStderr("# can't import zipimport.zipimporter\n"); + } } else { /* sys.path_hooks.insert(0, zipimporter) */ @@ -131,17 +131,17 @@ _PyImportZip_Init(PyThreadState *tstate) if (err < 0) { goto error; } - if (verbose) { - PySys_WriteStderr("# installed zipimport hook\n"); - } + if (verbose) { + PySys_WriteStderr("# installed zipimport hook\n"); + } } } - return _PyStatus_OK(); + return _PyStatus_OK(); error: PyErr_Print(); - return _PyStatus_ERR("initializing zipimport failed"); + return _PyStatus_ERR("initializing zipimport failed"); } /* Locking primitives to prevent parallel imports of the same module @@ -196,7 +196,7 @@ _PyImport_ReleaseLock(void) return 1; } -#ifdef HAVE_FORK +#ifdef HAVE_FORK /* This function is called from PyOS_AfterFork_Child to ensure that newly created child processes do not share locks with the parent. We now acquire the import lock around fork() calls but on some platforms @@ -206,8 +206,8 @@ void _PyImport_ReInitLock(void) { if (import_lock != NULL) { - if (_PyThread_at_fork_reinit(&import_lock) < 0) { - _Py_FatalErrorFunc(__func__, "failed to create a new lock"); + if (_PyThread_at_fork_reinit(&import_lock) < 0) { + _Py_FatalErrorFunc(__func__, "failed to create a new lock"); } } if (import_lock_level > 1) { @@ -225,7 +225,7 @@ _PyImport_ReInitLock(void) import_lock_level = 0; } } -#endif +#endif /*[clinic input] _imp.lock_held @@ -296,12 +296,12 @@ _PyImport_Fini2(void) PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - // Reset PyImport_Inittab - PyImport_Inittab = _PyImport_Inittab; - + // Reset PyImport_Inittab + PyImport_Inittab = _PyImport_Inittab; + /* Free memory allocated by PyImport_ExtendInittab() */ PyMem_RawFree(inittab_copy); - inittab_copy = NULL; + inittab_copy = NULL; PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } @@ -311,9 +311,9 @@ _PyImport_Fini2(void) PyObject * PyImport_GetModuleDict(void) { - PyInterpreterState *interp = _PyInterpreterState_GET(); + PyInterpreterState *interp = _PyInterpreterState_GET(); if (interp->modules == NULL) { - Py_FatalError("interpreter has no modules dictionary"); + Py_FatalError("interpreter has no modules dictionary"); } return interp->modules; } @@ -343,30 +343,30 @@ _PyImport_GetModuleId(struct _Py_Identifier *nameid) int _PyImport_SetModule(PyObject *name, PyObject *m) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *modules = tstate->interp->modules; + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *modules = tstate->interp->modules; return PyObject_SetItem(modules, name, m); } int _PyImport_SetModuleString(const char *name, PyObject *m) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *modules = tstate->interp->modules; + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *modules = tstate->interp->modules; return PyMapping_SetItemString(modules, name, m); } -static PyObject * -import_get_module(PyThreadState *tstate, PyObject *name) +static PyObject * +import_get_module(PyThreadState *tstate, PyObject *name) { - PyObject *modules = tstate->interp->modules; + PyObject *modules = tstate->interp->modules; if (modules == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "unable to get sys.modules"); + _PyErr_SetString(tstate, PyExc_RuntimeError, + "unable to get sys.modules"); return NULL; } - - PyObject *m; + + PyObject *m; Py_INCREF(modules); if (PyDict_CheckExact(modules)) { m = PyDict_GetItemWithError(modules, name); /* borrowed */ @@ -374,8 +374,8 @@ import_get_module(PyThreadState *tstate, PyObject *name) } else { m = PyObject_GetItem(modules, name); - if (m == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - _PyErr_Clear(tstate); + if (m == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + _PyErr_Clear(tstate); } } Py_DECREF(modules); @@ -383,35 +383,35 @@ import_get_module(PyThreadState *tstate, PyObject *name) } -static int -import_ensure_initialized(PyThreadState *tstate, PyObject *mod, PyObject *name) -{ - PyInterpreterState *interp = tstate->interp; - PyObject *spec; - - _Py_IDENTIFIER(_lock_unlock_module); - - /* 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_GetAttrId(mod, &PyId___spec__); - int busy = _PyModuleSpec_IsInitializing(spec); - Py_XDECREF(spec); - if (busy) { - /* Wait until module is done importing. */ - PyObject *value = _PyObject_CallMethodIdOneArg( - interp->importlib, &PyId__lock_unlock_module, name); - if (value == NULL) { - return -1; - } - Py_DECREF(value); - } - return 0; -} - - +static int +import_ensure_initialized(PyThreadState *tstate, PyObject *mod, PyObject *name) +{ + PyInterpreterState *interp = tstate->interp; + PyObject *spec; + + _Py_IDENTIFIER(_lock_unlock_module); + + /* 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_GetAttrId(mod, &PyId___spec__); + int busy = _PyModuleSpec_IsInitializing(spec); + Py_XDECREF(spec); + if (busy) { + /* Wait until module is done importing. */ + PyObject *value = _PyObject_CallMethodIdOneArg( + interp->importlib, &PyId__lock_unlock_module, name); + if (value == NULL) { + return -1; + } + Py_DECREF(value); + } + return 0; +} + + /* List of names to clear in sys */ static const char * const sys_deletes[] = { "path", "argv", "ps1", "ps2", @@ -431,14 +431,14 @@ static const char * const sys_files[] = { /* Un-initialize things, as good as we can */ void -_PyImport_Cleanup(PyThreadState *tstate) +_PyImport_Cleanup(PyThreadState *tstate) { - PyInterpreterState *interp = tstate->interp; - PyObject *modules = interp->modules; - if (modules == NULL) { - /* Already done */ - return; - } + PyInterpreterState *interp = tstate->interp; + PyObject *modules = interp->modules; + if (modules == NULL) { + /* Already done */ + return; + } /* Delete some special variables first. These are common places where user values hide and people complain when their @@ -448,37 +448,37 @@ _PyImport_Cleanup(PyThreadState *tstate) /* XXX Perhaps these precautions are obsolete. Who knows? */ - int verbose = _PyInterpreterState_GetConfig(interp)->verbose; - if (verbose) { + int verbose = _PyInterpreterState_GetConfig(interp)->verbose; + if (verbose) { PySys_WriteStderr("# clear builtins._\n"); - } + } if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) { - PyErr_WriteUnraisable(NULL); + PyErr_WriteUnraisable(NULL); } - const char * const *p; + const char * const *p; for (p = sys_deletes; *p != NULL; p++) { - if (verbose) { + if (verbose) { PySys_WriteStderr("# clear sys.%s\n", *p); - } + } if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) { - PyErr_WriteUnraisable(NULL); + PyErr_WriteUnraisable(NULL); } } for (p = sys_files; *p != NULL; p+=2) { - if (verbose) { + if (verbose) { PySys_WriteStderr("# restore sys.%s\n", *p); - } - PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict, - *(p+1)); - if (value == NULL) { - if (_PyErr_Occurred(tstate)) { - PyErr_WriteUnraisable(NULL); - } + } + PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict, + *(p+1)); + if (value == NULL) { + if (_PyErr_Occurred(tstate)) { + PyErr_WriteUnraisable(NULL); + } value = Py_None; - } + } if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) { - PyErr_WriteUnraisable(NULL); + PyErr_WriteUnraisable(NULL); } } @@ -486,10 +486,10 @@ _PyImport_Cleanup(PyThreadState *tstate) modules when they are removed from sys.modules. The name is used for diagnosis messages (in verbose mode), while the weakref helps detect those modules which have been held alive. */ - PyObject *weaklist = PyList_New(0); - if (weaklist == NULL) { - PyErr_WriteUnraisable(NULL); - } + PyObject *weaklist = PyList_New(0); + if (weaklist == NULL) { + PyErr_WriteUnraisable(NULL); + } #define STORE_MODULE_WEAKREF(name, mod) \ if (weaklist != NULL) { \ @@ -497,31 +497,31 @@ _PyImport_Cleanup(PyThreadState *tstate) if (wr) { \ PyObject *tup = PyTuple_Pack(2, name, wr); \ if (!tup || PyList_Append(weaklist, tup) < 0) { \ - PyErr_WriteUnraisable(NULL); \ + PyErr_WriteUnraisable(NULL); \ } \ Py_XDECREF(tup); \ Py_DECREF(wr); \ } \ else { \ - PyErr_WriteUnraisable(NULL); \ + PyErr_WriteUnraisable(NULL); \ } \ } #define CLEAR_MODULE(name, mod) \ if (PyModule_Check(mod)) { \ - if (verbose && PyUnicode_Check(name)) { \ + if (verbose && PyUnicode_Check(name)) { \ PySys_FormatStderr("# cleanup[2] removing %U\n", name); \ - } \ + } \ STORE_MODULE_WEAKREF(name, mod); \ if (PyObject_SetItem(modules, name, Py_None) < 0) { \ - PyErr_WriteUnraisable(NULL); \ + PyErr_WriteUnraisable(NULL); \ } \ } /* Remove all modules from sys.modules, hoping that garbage collection can reclaim most of them. */ if (PyDict_CheckExact(modules)) { - Py_ssize_t pos = 0; - PyObject *key, *value; + Py_ssize_t pos = 0; + PyObject *key, *value; while (PyDict_Next(modules, &pos, &key, &value)) { CLEAR_MODULE(key, value); } @@ -529,14 +529,14 @@ _PyImport_Cleanup(PyThreadState *tstate) else { PyObject *iterator = PyObject_GetIter(modules); if (iterator == NULL) { - PyErr_WriteUnraisable(NULL); + PyErr_WriteUnraisable(NULL); } else { - PyObject *key; + PyObject *key; while ((key = PyIter_Next(iterator))) { - PyObject *value = PyObject_GetItem(modules, key); + PyObject *value = PyObject_GetItem(modules, key); if (value == NULL) { - PyErr_WriteUnraisable(NULL); + PyErr_WriteUnraisable(NULL); continue; } CLEAR_MODULE(key, value); @@ -544,7 +544,7 @@ _PyImport_Cleanup(PyThreadState *tstate) Py_DECREF(key); } if (PyErr_Occurred()) { - PyErr_WriteUnraisable(NULL); + PyErr_WriteUnraisable(NULL); } Py_DECREF(iterator); } @@ -556,26 +556,26 @@ _PyImport_Cleanup(PyThreadState *tstate) } else { _Py_IDENTIFIER(clear); - if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) { - PyErr_WriteUnraisable(NULL); - } + if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) { + PyErr_WriteUnraisable(NULL); + } } /* Restore the original builtins dict, to ensure that any user data gets cleared. */ - PyObject *dict = PyDict_Copy(interp->builtins); - if (dict == NULL) { - PyErr_WriteUnraisable(NULL); - } + PyObject *dict = PyDict_Copy(interp->builtins); + if (dict == NULL) { + PyErr_WriteUnraisable(NULL); + } PyDict_Clear(interp->builtins); - if (PyDict_Update(interp->builtins, interp->builtins_copy)) { - _PyErr_Clear(tstate); - } + if (PyDict_Update(interp->builtins, interp->builtins_copy)) { + _PyErr_Clear(tstate); + } Py_XDECREF(dict); /* Collect references */ _PyGC_CollectNoFail(); /* Dump GC stats before it's too late, since it uses the warnings machinery. */ - _PyGC_DumpShutdownStats(tstate); + _PyGC_DumpShutdownStats(tstate); /* Now, if there are any modules left alive, clear their globals to minimize potential leaks. All C extension modules actually end @@ -589,10 +589,10 @@ _PyImport_Cleanup(PyThreadState *tstate) module last. Likewise, we don't delete sys until the very end because it is implicitly referenced (e.g. by print). */ if (weaklist != NULL) { - Py_ssize_t i; - /* Since dict is ordered in CPython 3.6+, modules are saved in - importing order. First clear modules imported later. */ - for (i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) { + Py_ssize_t i; + /* Since dict is ordered in CPython 3.6+, modules are saved in + importing order. First clear modules imported later. */ + for (i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) { PyObject *tup = PyList_GET_ITEM(weaklist, i); PyObject *name = PyTuple_GET_ITEM(tup, 0); PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1)); @@ -603,9 +603,9 @@ _PyImport_Cleanup(PyThreadState *tstate) if (dict == interp->builtins || dict == interp->sysdict) continue; Py_INCREF(mod); - if (verbose && PyUnicode_Check(name)) { + if (verbose && PyUnicode_Check(name)) { PySys_FormatStderr("# cleanup[3] wiping %U\n", name); - } + } _PyModule_Clear(mod); Py_DECREF(mod); } @@ -613,18 +613,18 @@ _PyImport_Cleanup(PyThreadState *tstate) } /* Next, delete sys and builtins (in that order) */ - if (verbose) { + if (verbose) { PySys_FormatStderr("# cleanup[3] wiping sys\n"); - } + } _PyModule_ClearDict(interp->sysdict); - if (verbose) { + if (verbose) { PySys_FormatStderr("# cleanup[3] wiping builtins\n"); - } + } _PyModule_ClearDict(interp->builtins); - /* Clear module dict copies stored in the interpreter state */ - _PyInterpreterState_ClearModules(interp); - + /* Clear module dict copies stored in the interpreter state */ + _PyInterpreterState_ClearModules(interp); + /* Clear and delete the modules directory. Actual modules will still be there only if imported during the execution of some destructor. */ @@ -645,7 +645,7 @@ long PyImport_GetMagicNumber(void) { long res; - PyInterpreterState *interp = _PyInterpreterState_GET(); + PyInterpreterState *interp = _PyInterpreterState_GET(); PyObject *external, *pyc_magic; external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); @@ -688,66 +688,66 @@ PyImport_GetMagicTag(void) int _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, - PyObject *filename, PyObject *modules) + PyObject *filename, PyObject *modules) { if (mod == NULL || !PyModule_Check(mod)) { PyErr_BadInternalCall(); return -1; } - - struct PyModuleDef *def = PyModule_GetDef(mod); + + struct PyModuleDef *def = PyModule_GetDef(mod); if (!def) { PyErr_BadInternalCall(); return -1; } - - PyThreadState *tstate = _PyThreadState_GET(); - if (PyObject_SetItem(modules, name, mod) < 0) { + + PyThreadState *tstate = _PyThreadState_GET(); + if (PyObject_SetItem(modules, name, mod) < 0) { return -1; - } - if (_PyState_AddModule(tstate, mod, def) < 0) { + } + if (_PyState_AddModule(tstate, mod, def) < 0) { PyMapping_DelItem(modules, name); return -1; } - - // bpo-44050: Extensions and def->m_base.m_copy can be updated - // when the extension module doesn't support sub-interpreters. - if (_Py_IsMainInterpreter(tstate) || 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; - } + + // bpo-44050: Extensions and def->m_base.m_copy can be updated + // when the extension module doesn't support sub-interpreters. + if (_Py_IsMainInterpreter(tstate) || 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; + } } - - if (extensions == NULL) { - extensions = PyDict_New(); - if (extensions == NULL) { - return -1; - } - } - - PyObject *key = PyTuple_Pack(2, filename, name); - if (key == NULL) { + + if (extensions == NULL) { + extensions = PyDict_New(); + if (extensions == NULL) { + return -1; + } + } + + 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) { + } + int res = PyDict_SetItem(extensions, key, (PyObject *)def); + Py_DECREF(key); + if (res < 0) { return -1; - } + } } - + return 0; } @@ -764,32 +764,32 @@ _PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules) return res; } -static PyObject * -import_find_extension(PyThreadState *tstate, PyObject *name, - PyObject *filename) +static PyObject * +import_find_extension(PyThreadState *tstate, PyObject *name, + PyObject *filename) { - if (extensions == NULL) { - return NULL; - } + if (extensions == NULL) { + return NULL; + } - PyObject *key = PyTuple_Pack(2, filename, name); - if (key == NULL) { + PyObject *key = PyTuple_Pack(2, filename, name); + if (key == NULL) { return NULL; - } - PyModuleDef* def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); + } + PyModuleDef* def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); Py_DECREF(key); - if (def == NULL) { + if (def == NULL) { return NULL; - } - - PyObject *mod, *mdict; - PyObject *modules = tstate->interp->modules; - + } + + 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); + mod = import_add_module(tstate, name); if (mod == NULL) return NULL; mdict = PyModule_GetDict(mod); @@ -810,35 +810,35 @@ import_find_extension(PyThreadState *tstate, PyObject *name, } Py_DECREF(mod); } - if (_PyState_AddModule(tstate, mod, def) < 0) { + if (_PyState_AddModule(tstate, mod, def) < 0) { PyMapping_DelItem(modules, name); return NULL; } - - int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; - if (verbose) { + + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; + if (verbose) { PySys_FormatStderr("import %U # previously loaded (%R)\n", - name, filename); - } + name, filename); + } return mod; -} +} -PyObject * -_PyImport_FindExtensionObject(PyObject *name, PyObject *filename) -{ - PyThreadState *tstate = _PyThreadState_GET(); - return import_find_extension(tstate, name, filename); +PyObject * +_PyImport_FindExtensionObject(PyObject *name, PyObject *filename) +{ + PyThreadState *tstate = _PyThreadState_GET(); + return import_find_extension(tstate, name, filename); } - + PyObject * -_PyImport_FindBuiltin(PyThreadState *tstate, const char *name) +_PyImport_FindBuiltin(PyThreadState *tstate, const char *name) { PyObject *res, *nameobj; nameobj = PyUnicode_InternFromString(name); if (nameobj == NULL) return NULL; - res = import_find_extension(tstate, nameobj, nameobj); + res = import_find_extension(tstate, nameobj, nameobj); Py_DECREF(nameobj); return res; } @@ -849,15 +849,15 @@ _PyImport_FindBuiltin(PyThreadState *tstate, const char *name) Because the former action is most common, THIS DOES NOT RETURN A 'NEW' REFERENCE! */ -static PyObject * -import_add_module(PyThreadState *tstate, PyObject *name) +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; - } + PyObject *modules = tstate->interp->modules; + if (modules == NULL) { + _PyErr_SetString(tstate, PyExc_RuntimeError, + "no import module dictionary"); + return NULL; + } PyObject *m; if (PyDict_CheckExact(modules)) { @@ -865,13 +865,13 @@ import_add_module(PyThreadState *tstate, PyObject *name) } else { m = PyObject_GetItem(modules, name); - // For backward-compatibility we copy the behavior + // For backward-compatibility we copy the behavior // of PyDict_GetItemWithError(). - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - _PyErr_Clear(tstate); + if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + _PyErr_Clear(tstate); } } - if (_PyErr_Occurred(tstate)) { + if (_PyErr_Occurred(tstate)) { return NULL; } if (m != NULL && PyModule_Check(m)) { @@ -890,49 +890,49 @@ import_add_module(PyThreadState *tstate, PyObject *name) } PyObject * -PyImport_AddModuleObject(PyObject *name) -{ - PyThreadState *tstate = _PyThreadState_GET(); - return import_add_module(tstate, name); -} - - -PyObject * +PyImport_AddModuleObject(PyObject *name) +{ + PyThreadState *tstate = _PyThreadState_GET(); + return import_add_module(tstate, name); +} + + +PyObject * PyImport_AddModule(const char *name) { - PyObject *nameobj = PyUnicode_FromString(name); - if (nameobj == NULL) { + PyObject *nameobj = PyUnicode_FromString(name); + if (nameobj == NULL) { return NULL; - } - PyObject *module = PyImport_AddModuleObject(nameobj); + } + PyObject *module = PyImport_AddModuleObject(nameobj); Py_DECREF(nameobj); return module; } -/* 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. - */ +/* 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) +remove_module(PyThreadState *tstate, PyObject *name) { - 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); + 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); + } } - else if (PyMapping_DelItem(modules, name) < 0) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - _PyErr_Clear(tstate); - } - } - - _PyErr_ChainExceptions(type, value, traceback); + + _PyErr_ChainExceptions(type, value, traceback); } @@ -986,18 +986,18 @@ PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, goto error; } else if (cpathobj != NULL) { - PyInterpreterState *interp = _PyInterpreterState_GET(); + PyInterpreterState *interp = _PyInterpreterState_GET(); _Py_IDENTIFIER(_get_sourcefile); if (interp == NULL) { - Py_FatalError("no current interpreter"); + Py_FatalError("no current interpreter"); } external= PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); if (external != NULL) { - pathobj = _PyObject_CallMethodIdOneArg( - external, &PyId__get_sourcefile, cpathobj); + pathobj = _PyObject_CallMethodIdOneArg( + external, &PyId__get_sourcefile, cpathobj); Py_DECREF(external); } if (pathobj == NULL) @@ -1015,23 +1015,23 @@ error: } static PyObject * -module_dict_for_exec(PyThreadState *tstate, PyObject *name) +module_dict_for_exec(PyThreadState *tstate, PyObject *name) { - _Py_IDENTIFIER(__builtins__); + _Py_IDENTIFIER(__builtins__); PyObject *m, *d = NULL; - m = import_add_module(tstate, name); + 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); - if (_PyDict_GetItemIdWithError(d, &PyId___builtins__) == NULL) { - if (_PyErr_Occurred(tstate) || - _PyDict_SetItemId(d, &PyId___builtins__, - PyEval_GetBuiltins()) != 0) - { - remove_module(tstate, name); + if (_PyDict_GetItemIdWithError(d, &PyId___builtins__) == NULL) { + if (_PyErr_Occurred(tstate) || + _PyDict_SetItemId(d, &PyId___builtins__, + PyEval_GetBuiltins()) != 0) + { + remove_module(tstate, name); return NULL; } } @@ -1040,23 +1040,23 @@ module_dict_for_exec(PyThreadState *tstate, PyObject *name) } static PyObject * -exec_code_in_module(PyThreadState *tstate, PyObject *name, - PyObject *module_dict, PyObject *code_object) +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); + 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); + 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; @@ -1066,11 +1066,11 @@ PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname) { - PyThreadState *tstate = _PyThreadState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); PyObject *d, *external, *res; _Py_IDENTIFIER(_fix_up_module); - d = module_dict_for_exec(tstate, name); + d = module_dict_for_exec(tstate, name); if (d == NULL) { return NULL; } @@ -1078,8 +1078,8 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, if (pathname == NULL) { pathname = ((PyCodeObject *)co)->co_filename; } - external = PyObject_GetAttrString(tstate->interp->importlib, - "_bootstrap_external"); + external = PyObject_GetAttrString(tstate->interp->importlib, + "_bootstrap_external"); if (external == NULL) return NULL; res = _PyObject_CallMethodIdObjArgs(external, @@ -1088,7 +1088,7 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, Py_DECREF(external); if (res != NULL) { Py_DECREF(res); - res = exec_code_in_module(tstate, name, d, co); + res = exec_code_in_module(tstate, name, d, co); } return res; } @@ -1187,8 +1187,8 @@ is_builtin(PyObject *name) Returns a borrowed reference. */ static PyObject * -get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, - PyObject *path_hooks, PyObject *p) +get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, + PyObject *path_hooks, PyObject *p) { PyObject *importer; Py_ssize_t j, nhooks; @@ -1201,8 +1201,8 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, if (nhooks < 0) return NULL; /* Shouldn't happen */ - importer = PyDict_GetItemWithError(path_importer_cache, p); - if (importer != NULL || _PyErr_Occurred(tstate)) + importer = PyDict_GetItemWithError(path_importer_cache, p); + if (importer != NULL || _PyErr_Occurred(tstate)) return importer; /* set path_importer_cache[p] to None to avoid recursion */ @@ -1213,14 +1213,14 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, PyObject *hook = PyList_GetItem(path_hooks, j); if (hook == NULL) return NULL; - importer = PyObject_CallOneArg(hook, p); + importer = PyObject_CallOneArg(hook, p); if (importer != NULL) break; - if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) { + if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) { return NULL; } - _PyErr_Clear(tstate); + _PyErr_Clear(tstate); } if (importer == NULL) { return Py_None; @@ -1234,16 +1234,16 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache, return importer; } -PyObject * -PyImport_GetImporter(PyObject *path) -{ - PyThreadState *tstate = _PyThreadState_GET(); +PyObject * +PyImport_GetImporter(PyObject *path) +{ + PyThreadState *tstate = _PyThreadState_GET(); PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; path_importer_cache = PySys_GetObject("path_importer_cache"); path_hooks = PySys_GetObject("path_hooks"); if (path_importer_cache != NULL && path_hooks != NULL) { - importer = get_path_importer(tstate, path_importer_cache, + importer = get_path_importer(tstate, path_importer_cache, path_hooks, path); } Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ @@ -1263,7 +1263,7 @@ static PyObject * _imp_create_builtin(PyObject *module, PyObject *spec) /*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/ { - PyThreadState *tstate = _PyThreadState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); struct _inittab *p; PyObject *name; const char *namestr; @@ -1275,7 +1275,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec) } mod = _PyImport_FindExtensionObject(name, name); - if (mod || _PyErr_Occurred(tstate)) { + if (mod || _PyErr_Occurred(tstate)) { Py_DECREF(name); Py_XINCREF(mod); return mod; @@ -1287,7 +1287,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec) return NULL; } - PyObject *modules = tstate->interp->modules; + PyObject *modules = tstate->interp->modules; for (p = PyImport_Inittab; p->name != NULL; p++) { PyModuleDef *def; if (_PyUnicode_EqualToASCIIString(name, p->name)) { @@ -1401,7 +1401,7 @@ is_frozen_package(PyObject *name) int PyImport_ImportFrozenModuleObject(PyObject *name) { - PyThreadState *tstate = _PyThreadState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); const struct _frozen *p; PyObject *co, *m, *d; int ispackage; @@ -1412,9 +1412,9 @@ PyImport_ImportFrozenModuleObject(PyObject *name) if (p == NULL) return 0; if (p->code == NULL) { - _PyErr_Format(tstate, PyExc_ImportError, - "Excluded frozen object named %R", - name); + _PyErr_Format(tstate, PyExc_ImportError, + "Excluded frozen object named %R", + name); return -1; } size = p->size; @@ -1425,16 +1425,16 @@ PyImport_ImportFrozenModuleObject(PyObject *name) if (co == NULL) return -1; if (!PyCode_Check(co)) { - _PyErr_Format(tstate, PyExc_TypeError, - "frozen object %R is not a code object", - name); + _PyErr_Format(tstate, PyExc_TypeError, + "frozen object %R is not a code object", + name); goto err_return; } if (ispackage) { /* Set __path__ to the empty list */ PyObject *l; int err; - m = import_add_module(tstate, name); + m = import_add_module(tstate, name); if (m == NULL) goto err_return; d = PyModule_GetDict(m); @@ -1447,18 +1447,18 @@ PyImport_ImportFrozenModuleObject(PyObject *name) if (err != 0) goto err_return; } - d = module_dict_for_exec(tstate, name); + 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) { + m = exec_code_in_module(tstate, name, d, co); + if (m == NULL) { goto err_return; - } + } Py_DECREF(co); Py_DECREF(m); return 1; - + err_return: Py_DECREF(co); return -1; @@ -1495,7 +1495,7 @@ PyImport_ImportModule(const char *name) return result; } - + /* Import a module without blocking * * At first it tries to fetch the module from sys.modules. If the module was @@ -1515,7 +1515,7 @@ PyImport_ImportModuleNoBlock(const char *name) /* Remove importlib frames from the traceback, * except in Verbose mode. */ static void -remove_importlib_frames(PyThreadState *tstate) +remove_importlib_frames(PyThreadState *tstate) { const char *importlib_filename = "<frozen importlib._bootstrap>"; const char *external_filename = "<frozen importlib._bootstrap_external>"; @@ -1530,11 +1530,11 @@ remove_importlib_frames(PyThreadState *tstate) from the traceback. We always trim chunks which end with a call to "_call_with_frames_removed". */ - _PyErr_Fetch(tstate, &exception, &value, &base_tb); - if (!exception || _PyInterpreterState_GetConfig(tstate->interp)->verbose) { + _PyErr_Fetch(tstate, &exception, &value, &base_tb); + if (!exception || _PyInterpreterState_GetConfig(tstate->interp)->verbose) { goto done; - } - + } + if (PyType_IsSubtype((PyTypeObject *) exception, (PyTypeObject *) PyExc_ImportError)) always_trim = 1; @@ -1545,7 +1545,7 @@ remove_importlib_frames(PyThreadState *tstate) PyTracebackObject *traceback = (PyTracebackObject *)tb; PyObject *next = (PyObject *) traceback->tb_next; PyFrameObject *frame = traceback->tb_frame; - PyCodeObject *code = PyFrame_GetCode(frame); + PyCodeObject *code = PyFrame_GetCode(frame); int now_in_importlib; assert(PyTraceBack_Check(tb)); @@ -1568,16 +1568,16 @@ remove_importlib_frames(PyThreadState *tstate) else { prev_link = (PyObject **) &traceback->tb_next; } - Py_DECREF(code); + Py_DECREF(code); tb = next; } done: - _PyErr_Restore(tstate, exception, value, base_tb); + _PyErr_Restore(tstate, exception, value, base_tb); } static PyObject * -resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level) +resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level) { _Py_IDENTIFIER(__package__); _Py_IDENTIFIER(__name__); @@ -1590,30 +1590,30 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level int level_up; if (globals == NULL) { - _PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals"); + _PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals"); goto error; } if (!PyDict_Check(globals)) { - _PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict"); + _PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict"); goto error; } - package = _PyDict_GetItemIdWithError(globals, &PyId___package__); + package = _PyDict_GetItemIdWithError(globals, &PyId___package__); if (package == Py_None) { package = NULL; } - else if (package == NULL && _PyErr_Occurred(tstate)) { - goto error; - } - spec = _PyDict_GetItemIdWithError(globals, &PyId___spec__); - if (spec == NULL && _PyErr_Occurred(tstate)) { - goto error; - } + else if (package == NULL && _PyErr_Occurred(tstate)) { + goto error; + } + spec = _PyDict_GetItemIdWithError(globals, &PyId___spec__); + if (spec == NULL && _PyErr_Occurred(tstate)) { + goto error; + } if (package != NULL) { Py_INCREF(package); if (!PyUnicode_Check(package)) { - _PyErr_SetString(tstate, PyExc_TypeError, - "package must be a string"); + _PyErr_SetString(tstate, PyExc_TypeError, + "package must be a string"); goto error; } else if (spec != NULL && spec != Py_None) { @@ -1642,8 +1642,8 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level goto error; } else if (!PyUnicode_Check(package)) { - _PyErr_SetString(tstate, PyExc_TypeError, - "__spec__.parent must be a string"); + _PyErr_SetString(tstate, PyExc_TypeError, + "__spec__.parent must be a string"); goto error; } } @@ -1654,26 +1654,26 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level goto error; } - package = _PyDict_GetItemIdWithError(globals, &PyId___name__); + package = _PyDict_GetItemIdWithError(globals, &PyId___name__); if (package == NULL) { - if (!_PyErr_Occurred(tstate)) { - _PyErr_SetString(tstate, PyExc_KeyError, - "'__name__' not in globals"); - } + if (!_PyErr_Occurred(tstate)) { + _PyErr_SetString(tstate, PyExc_KeyError, + "'__name__' not in globals"); + } goto error; } Py_INCREF(package); if (!PyUnicode_Check(package)) { - _PyErr_SetString(tstate, PyExc_TypeError, - "__name__ must be a string"); + _PyErr_SetString(tstate, PyExc_TypeError, + "__name__ must be a string"); goto error; } - if (_PyDict_GetItemIdWithError(globals, &PyId___path__) == NULL) { + if (_PyDict_GetItemIdWithError(globals, &PyId___path__) == NULL) { Py_ssize_t dot; - if (_PyErr_Occurred(tstate) || PyUnicode_READY(package) < 0) { + if (_PyErr_Occurred(tstate) || PyUnicode_READY(package) < 0) { goto error; } @@ -1682,20 +1682,20 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level if (dot == -2) { goto error; } - else if (dot == -1) { - goto no_parent_error; - } - PyObject *substr = PyUnicode_Substring(package, 0, dot); - if (substr == NULL) { - goto error; + else if (dot == -1) { + goto no_parent_error; } - Py_SETREF(package, substr); + PyObject *substr = PyUnicode_Substring(package, 0, dot); + if (substr == NULL) { + goto error; + } + Py_SETREF(package, substr); } } last_dot = PyUnicode_GET_LENGTH(package); if (last_dot == 0) { - goto no_parent_error; + goto no_parent_error; } for (level_up = 1; level_up < level; level_up += 1) { @@ -1704,9 +1704,9 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level goto error; } else if (last_dot == -1) { - _PyErr_SetString(tstate, PyExc_ImportError, - "attempted relative import beyond top-level " - "package"); + _PyErr_SetString(tstate, PyExc_ImportError, + "attempted relative import beyond top-level " + "package"); goto error; } } @@ -1721,39 +1721,39 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level Py_DECREF(base); return abs_name; - no_parent_error: - _PyErr_SetString(tstate, PyExc_ImportError, - "attempted relative import " - "with no known parent package"); - + no_parent_error: + _PyErr_SetString(tstate, PyExc_ImportError, + "attempted relative import " + "with no known parent package"); + error: Py_XDECREF(package); return NULL; } static PyObject * -import_find_and_load(PyThreadState *tstate, PyObject *abs_name) +import_find_and_load(PyThreadState *tstate, PyObject *abs_name) { _Py_IDENTIFIER(_find_and_load); PyObject *mod = NULL; - PyInterpreterState *interp = tstate->interp; - int import_time = _PyInterpreterState_GetConfig(interp)->import_time; + PyInterpreterState *interp = tstate->interp; + int import_time = _PyInterpreterState_GetConfig(interp)->import_time; static int import_level; static _PyTime_t accumulated; _PyTime_t t1 = 0, accumulated_copy = accumulated; - PyObject *sys_path = PySys_GetObject("path"); - PyObject *sys_meta_path = PySys_GetObject("meta_path"); - PyObject *sys_path_hooks = PySys_GetObject("path_hooks"); - if (_PySys_Audit(tstate, "import", "OOOOO", - abs_name, Py_None, sys_path ? sys_path : Py_None, - sys_meta_path ? sys_meta_path : Py_None, - sys_path_hooks ? sys_path_hooks : Py_None) < 0) { - return NULL; - } - - + PyObject *sys_path = PySys_GetObject("path"); + PyObject *sys_meta_path = PySys_GetObject("meta_path"); + PyObject *sys_path_hooks = PySys_GetObject("path_hooks"); + if (_PySys_Audit(tstate, "import", "OOOOO", + abs_name, Py_None, sys_path ? sys_path : Py_None, + sys_meta_path ? sys_meta_path : Py_None, + sys_path_hooks ? sys_path_hooks : Py_None) < 0) { + return NULL; + } + + /* XOptions is initialized after first some imports. * So we can't have negative cache before completed initialization. * Anyway, importlib._find_and_load is much slower than @@ -1799,38 +1799,38 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) } 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, mod, name) < 0) { - Py_DECREF(mod); - remove_importlib_frames(tstate); - return NULL; - } - } - return mod; -} - -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, 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, int level) { - PyThreadState *tstate = _PyThreadState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); _Py_IDENTIFIER(_handle_fromlist); PyObject *abs_name = NULL; PyObject *final_mod = NULL; PyObject *mod = NULL; PyObject *package = NULL; - PyInterpreterState *interp = tstate->interp; + PyInterpreterState *interp = tstate->interp; int has_from; if (name == NULL) { - _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); + _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); goto error; } @@ -1838,45 +1838,45 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, for added performance. */ if (!PyUnicode_Check(name)) { - _PyErr_SetString(tstate, PyExc_TypeError, - "module name must be a string"); + _PyErr_SetString(tstate, PyExc_TypeError, + "module name must be a string"); goto error; } if (PyUnicode_READY(name) < 0) { goto error; } if (level < 0) { - _PyErr_SetString(tstate, PyExc_ValueError, "level must be >= 0"); + _PyErr_SetString(tstate, PyExc_ValueError, "level must be >= 0"); goto error; } if (level > 0) { - abs_name = resolve_name(tstate, name, globals, level); + abs_name = resolve_name(tstate, name, globals, level); if (abs_name == NULL) goto error; } else { /* level == 0 */ if (PyUnicode_GET_LENGTH(name) == 0) { - _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); + _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); goto error; } abs_name = name; Py_INCREF(abs_name); } - mod = import_get_module(tstate, abs_name); - if (mod == NULL && _PyErr_Occurred(tstate)) { - goto error; - } - + mod = import_get_module(tstate, abs_name); + if (mod == NULL && _PyErr_Occurred(tstate)) { + goto error; + } + if (mod != NULL && mod != Py_None) { - if (import_ensure_initialized(tstate, mod, abs_name) < 0) { - goto error; + if (import_ensure_initialized(tstate, mod, abs_name) < 0) { + goto error; } } else { Py_XDECREF(mod); - mod = import_find_and_load(tstate, abs_name); + mod = import_find_and_load(tstate, abs_name); if (mod == NULL) { goto error; } @@ -1923,14 +1923,14 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, goto error; } - final_mod = import_get_module(tstate, to_return); + final_mod = import_get_module(tstate, to_return); Py_DECREF(to_return); if (final_mod == NULL) { - if (!_PyErr_Occurred(tstate)) { - _PyErr_Format(tstate, PyExc_KeyError, - "%R not in sys.modules as expected", - to_return); - } + if (!_PyErr_Occurred(tstate)) { + _PyErr_Format(tstate, PyExc_KeyError, + "%R not in sys.modules as expected", + to_return); + } goto error; } } @@ -1941,29 +1941,29 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, } } else { - PyObject *path; - if (_PyObject_LookupAttrId(mod, &PyId___path__, &path) < 0) { - goto error; - } - if (path) { - Py_DECREF(path); - final_mod = _PyObject_CallMethodIdObjArgs( - interp->importlib, &PyId__handle_fromlist, - mod, fromlist, interp->import_func, NULL); - } - else { - final_mod = mod; - Py_INCREF(mod); - } + PyObject *path; + if (_PyObject_LookupAttrId(mod, &PyId___path__, &path) < 0) { + goto error; + } + if (path) { + Py_DECREF(path); + final_mod = _PyObject_CallMethodIdObjArgs( + interp->importlib, &PyId__handle_fromlist, + mod, fromlist, interp->import_func, NULL); + } + else { + final_mod = mod; + Py_INCREF(mod); + } } error: Py_XDECREF(abs_name); Py_XDECREF(mod); Py_XDECREF(package); - if (final_mod == NULL) { - remove_importlib_frames(tstate); - } + if (final_mod == NULL) { + remove_importlib_frames(tstate); + } return final_mod; } @@ -1988,23 +1988,23 @@ PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals PyObject * PyImport_ReloadModule(PyObject *m) { - _Py_IDENTIFIER(importlib); + _Py_IDENTIFIER(importlib); _Py_IDENTIFIER(reload); PyObject *reloaded_module = NULL; - PyObject *importlib = _PyImport_GetModuleId(&PyId_importlib); - if (importlib == NULL) { - if (PyErr_Occurred()) { - return NULL; - } - - importlib = PyImport_ImportModule("importlib"); - if (importlib == NULL) { + PyObject *importlib = _PyImport_GetModuleId(&PyId_importlib); + if (importlib == NULL) { + if (PyErr_Occurred()) { + return NULL; + } + + importlib = PyImport_ImportModule("importlib"); + if (importlib == NULL) { return NULL; } } - reloaded_module = _PyObject_CallMethodIdOneArg(importlib, &PyId_reload, m); - Py_DECREF(importlib); + reloaded_module = _PyObject_CallMethodIdOneArg(importlib, &PyId_reload, m); + Py_DECREF(importlib); return reloaded_module; } @@ -2021,7 +2021,7 @@ PyImport_ReloadModule(PyObject *m) PyObject * PyImport_Import(PyObject *module_name) { - PyThreadState *tstate = _PyThreadState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); static PyObject *silly_list = NULL; static PyObject *builtins_str = NULL; static PyObject *import_str = NULL; @@ -2065,9 +2065,9 @@ PyImport_Import(PyObject *module_name) /* Get the __import__ function from the builtins */ if (PyDict_Check(builtins)) { import = PyObject_GetItem(builtins, import_str); - if (import == NULL) { - _PyErr_SetObject(tstate, PyExc_KeyError, import_str); - } + if (import == NULL) { + _PyErr_SetObject(tstate, PyExc_KeyError, import_str); + } } else import = PyObject_GetAttr(builtins, import_str); @@ -2083,9 +2083,9 @@ PyImport_Import(PyObject *module_name) 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); + r = import_get_module(tstate, module_name); + if (r == NULL && !_PyErr_Occurred(tstate)) { + _PyErr_SetObject(tstate, PyExc_KeyError, module_name); } err: @@ -2112,9 +2112,9 @@ _imp_extension_suffixes_impl(PyObject *module) if (list == NULL) return NULL; #ifdef HAVE_DYNAMIC_LOADING - const char *suffix; - unsigned int index = 0; - + const char *suffix; + unsigned int index = 0; + while ((suffix = _PyImport_DynLoadFiletab[index])) { PyObject *item = PyUnicode_FromString(suffix); if (item == NULL) { @@ -2146,7 +2146,7 @@ static PyObject * _imp_init_frozen_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/ { - PyThreadState *tstate = _PyThreadState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); int ret; PyObject *m; @@ -2156,7 +2156,7 @@ _imp_init_frozen_impl(PyObject *module, PyObject *name) if (ret == 0) { Py_RETURN_NONE; } - m = import_add_module(tstate, name); + m = import_add_module(tstate, name); Py_XINCREF(m); return m; } @@ -2415,16 +2415,16 @@ PyInit__imp(void) PyObject *m, *d; m = PyModule_Create(&impmodule); - if (m == NULL) { + if (m == NULL) { goto failure; - } + } d = PyModule_GetDict(m); - if (d == NULL) { + if (d == NULL) { goto failure; - } - - const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode; - PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1); + } + + const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode; + PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1); if (pyc_mode == NULL) { goto failure; } |