Home | History | Annotate | Download | only in Python
      1 /* Module definition and import implementation */
      2 
      3 #include "Python.h"
      4 
      5 #include "Python-ast.h"
      6 #undef Yield /* undefine macro conflicting with winbase.h */
      7 #include "internal/hash.h"
      8 #include "internal/import.h"
      9 #include "internal/pystate.h"
     10 #include "errcode.h"
     11 #include "marshal.h"
     12 #include "code.h"
     13 #include "frameobject.h"
     14 #include "osdefs.h"
     15 #include "importdl.h"
     16 #include "pydtrace.h"
     17 
     18 #ifdef HAVE_FCNTL_H
     19 #include <fcntl.h>
     20 #endif
     21 #ifdef __cplusplus
     22 extern "C" {
     23 #endif
     24 
     25 #define CACHEDIR "__pycache__"
     26 
     27 /* See _PyImport_FixupExtensionObject() below */
     28 static PyObject *extensions = NULL;
     29 
     30 /* This table is defined in config.c: */
     31 extern struct _inittab _PyImport_Inittab[];
     32 
     33 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
     34 static struct _inittab *inittab_copy = NULL;
     35 
     36 /*[clinic input]
     37 module _imp
     38 [clinic start generated code]*/
     39 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/
     40 
     41 #include "clinic/import.c.h"
     42 
     43 /* Initialize things */
     44 
     45 _PyInitError
     46 _PyImport_Init(PyInterpreterState *interp)
     47 {
     48     interp->builtins_copy = PyDict_Copy(interp->builtins);
     49     if (interp->builtins_copy == NULL) {
     50         return _Py_INIT_ERR("Can't backup builtins dict");
     51     }
     52     return _Py_INIT_OK();
     53 }
     54 
     55 _PyInitError
     56 _PyImportHooks_Init(void)
     57 {
     58     PyObject *v, *path_hooks = NULL;
     59     int err = 0;
     60 
     61     /* adding sys.path_hooks and sys.path_importer_cache */
     62     v = PyList_New(0);
     63     if (v == NULL)
     64         goto error;
     65     err = PySys_SetObject("meta_path", v);
     66     Py_DECREF(v);
     67     if (err)
     68         goto error;
     69     v = PyDict_New();
     70     if (v == NULL)
     71         goto error;
     72     err = PySys_SetObject("path_importer_cache", v);
     73     Py_DECREF(v);
     74     if (err)
     75         goto error;
     76     path_hooks = PyList_New(0);
     77     if (path_hooks == NULL)
     78         goto error;
     79     err = PySys_SetObject("path_hooks", path_hooks);
     80     if (err) {
     81         goto error;
     82     }
     83     Py_DECREF(path_hooks);
     84     return _Py_INIT_OK();
     85 
     86   error:
     87     PyErr_Print();
     88     return _Py_INIT_ERR("initializing sys.meta_path, sys.path_hooks, "
     89                         "or path_importer_cache failed");
     90 }
     91 
     92 _PyInitError
     93 _PyImportZip_Init(void)
     94 {
     95     PyObject *path_hooks, *zimpimport;
     96     int err = 0;
     97 
     98     path_hooks = PySys_GetObject("path_hooks");
     99     if (path_hooks == NULL) {
    100         PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
    101         goto error;
    102     }
    103 
    104     if (Py_VerboseFlag)
    105         PySys_WriteStderr("# installing zipimport hook\n");
    106 
    107     zimpimport = PyImport_ImportModule("zipimport");
    108     if (zimpimport == NULL) {
    109         PyErr_Clear(); /* No zip import module -- okay */
    110         if (Py_VerboseFlag)
    111             PySys_WriteStderr("# can't import zipimport\n");
    112     }
    113     else {
    114         _Py_IDENTIFIER(zipimporter);
    115         PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
    116                                                     &PyId_zipimporter);
    117         Py_DECREF(zimpimport);
    118         if (zipimporter == NULL) {
    119             PyErr_Clear(); /* No zipimporter object -- okay */
    120             if (Py_VerboseFlag)
    121                 PySys_WriteStderr(
    122                     "# can't import zipimport.zipimporter\n");
    123         }
    124         else {
    125             /* sys.path_hooks.insert(0, zipimporter) */
    126             err = PyList_Insert(path_hooks, 0, zipimporter);
    127             Py_DECREF(zipimporter);
    128             if (err < 0) {
    129                 goto error;
    130             }
    131             if (Py_VerboseFlag)
    132                 PySys_WriteStderr(
    133                     "# installed zipimport hook\n");
    134         }
    135     }
    136 
    137     return _Py_INIT_OK();
    138 
    139   error:
    140     PyErr_Print();
    141     return _Py_INIT_ERR("initializing zipimport failed");
    142 }
    143 
    144 /* Locking primitives to prevent parallel imports of the same module
    145    in different threads to return with a partially loaded module.
    146    These calls are serialized by the global interpreter lock. */
    147 
    148 #include "pythread.h"
    149 
    150 static PyThread_type_lock import_lock = 0;
    151 static unsigned long import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
    152 static int import_lock_level = 0;
    153 
    154 void
    155 _PyImport_AcquireLock(void)
    156 {
    157     unsigned long me = PyThread_get_thread_ident();
    158     if (me == PYTHREAD_INVALID_THREAD_ID)
    159         return; /* Too bad */
    160     if (import_lock == NULL) {
    161         import_lock = PyThread_allocate_lock();
    162         if (import_lock == NULL)
    163             return;  /* Nothing much we can do. */
    164     }
    165     if (import_lock_thread == me) {
    166         import_lock_level++;
    167         return;
    168     }
    169     if (import_lock_thread != PYTHREAD_INVALID_THREAD_ID ||
    170         !PyThread_acquire_lock(import_lock, 0))
    171     {
    172         PyThreadState *tstate = PyEval_SaveThread();
    173         PyThread_acquire_lock(import_lock, 1);
    174         PyEval_RestoreThread(tstate);
    175     }
    176     assert(import_lock_level == 0);
    177     import_lock_thread = me;
    178     import_lock_level = 1;
    179 }
    180 
    181 int
    182 _PyImport_ReleaseLock(void)
    183 {
    184     unsigned long me = PyThread_get_thread_ident();
    185     if (me == PYTHREAD_INVALID_THREAD_ID || import_lock == NULL)
    186         return 0; /* Too bad */
    187     if (import_lock_thread != me)
    188         return -1;
    189     import_lock_level--;
    190     assert(import_lock_level >= 0);
    191     if (import_lock_level == 0) {
    192         import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
    193         PyThread_release_lock(import_lock);
    194     }
    195     return 1;
    196 }
    197 
    198 /* This function is called from PyOS_AfterFork_Child to ensure that newly
    199    created child processes do not share locks with the parent.
    200    We now acquire the import lock around fork() calls but on some platforms
    201    (Solaris 9 and earlier? see isue7242) that still left us with problems. */
    202 
    203 void
    204 _PyImport_ReInitLock(void)
    205 {
    206     if (import_lock != NULL) {
    207         import_lock = PyThread_allocate_lock();
    208         if (import_lock == NULL) {
    209             Py_FatalError("PyImport_ReInitLock failed to create a new lock");
    210         }
    211     }
    212     if (import_lock_level > 1) {
    213         /* Forked as a side effect of import */
    214         unsigned long me = PyThread_get_thread_ident();
    215         /* The following could fail if the lock is already held, but forking as
    216            a side-effect of an import is a) rare, b) nuts, and c) difficult to
    217            do thanks to the lock only being held when doing individual module
    218            locks per import. */
    219         PyThread_acquire_lock(import_lock, NOWAIT_LOCK);
    220         import_lock_thread = me;
    221         import_lock_level--;
    222     } else {
    223         import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
    224         import_lock_level = 0;
    225     }
    226 }
    227 
    228 /*[clinic input]
    229 _imp.lock_held
    230 
    231 Return True if the import lock is currently held, else False.
    232 
    233 On platforms without threads, return False.
    234 [clinic start generated code]*/
    235 
    236 static PyObject *
    237 _imp_lock_held_impl(PyObject *module)
    238 /*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
    239 {
    240     return PyBool_FromLong(import_lock_thread != PYTHREAD_INVALID_THREAD_ID);
    241 }
    242 
    243 /*[clinic input]
    244 _imp.acquire_lock
    245 
    246 Acquires the interpreter's import lock for the current thread.
    247 
    248 This lock should be used by import hooks to ensure thread-safety when importing
    249 modules. On platforms without threads, this function does nothing.
    250 [clinic start generated code]*/
    251 
    252 static PyObject *
    253 _imp_acquire_lock_impl(PyObject *module)
    254 /*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/
    255 {
    256     _PyImport_AcquireLock();
    257     Py_RETURN_NONE;
    258 }
    259 
    260 /*[clinic input]
    261 _imp.release_lock
    262 
    263 Release the interpreter's import lock.
    264 
    265 On platforms without threads, this function does nothing.
    266 [clinic start generated code]*/
    267 
    268 static PyObject *
    269 _imp_release_lock_impl(PyObject *module)
    270 /*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
    271 {
    272     if (_PyImport_ReleaseLock() < 0) {
    273         PyErr_SetString(PyExc_RuntimeError,
    274                         "not holding the import lock");
    275         return NULL;
    276     }
    277     Py_RETURN_NONE;
    278 }
    279 
    280 void
    281 _PyImport_Fini(void)
    282 {
    283     Py_CLEAR(extensions);
    284     if (import_lock != NULL) {
    285         PyThread_free_lock(import_lock);
    286         import_lock = NULL;
    287     }
    288 }
    289 
    290 void
    291 _PyImport_Fini2(void)
    292 {
    293     /* Use the same memory allocator than PyImport_ExtendInittab(). */
    294     PyMemAllocatorEx old_alloc;
    295     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
    296 
    297     /* Free memory allocated by PyImport_ExtendInittab() */
    298     PyMem_RawFree(inittab_copy);
    299 
    300     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
    301 }
    302 
    303 /* Helper for sys */
    304 
    305 PyObject *
    306 PyImport_GetModuleDict(void)
    307 {
    308     PyInterpreterState *interp = PyThreadState_GET()->interp;
    309     if (interp->modules == NULL) {
    310         Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
    311     }
    312     return interp->modules;
    313 }
    314 
    315 /* In some corner cases it is important to be sure that the import
    316    machinery has been initialized (or not cleaned up yet).  For
    317    example, see issue #4236 and PyModule_Create2(). */
    318 
    319 int
    320 _PyImport_IsInitialized(PyInterpreterState *interp)
    321 {
    322     if (interp->modules == NULL)
    323         return 0;
    324     return 1;
    325 }
    326 
    327 PyObject *
    328 _PyImport_GetModuleId(struct _Py_Identifier *nameid)
    329 {
    330     PyObject *name = _PyUnicode_FromId(nameid); /* borrowed */
    331     if (name == NULL) {
    332         return NULL;
    333     }
    334     return PyImport_GetModule(name);
    335 }
    336 
    337 int
    338 _PyImport_SetModule(PyObject *name, PyObject *m)
    339 {
    340     PyObject *modules = PyImport_GetModuleDict();
    341     return PyObject_SetItem(modules, name, m);
    342 }
    343 
    344 int
    345 _PyImport_SetModuleString(const char *name, PyObject *m)
    346 {
    347     PyObject *modules = PyImport_GetModuleDict();
    348     return PyMapping_SetItemString(modules, name, m);
    349 }
    350 
    351 PyObject *
    352 PyImport_GetModule(PyObject *name)
    353 {
    354     PyObject *m;
    355     PyObject *modules = PyImport_GetModuleDict();
    356     if (modules == NULL) {
    357         PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
    358         return NULL;
    359     }
    360     Py_INCREF(modules);
    361     if (PyDict_CheckExact(modules)) {
    362         m = PyDict_GetItemWithError(modules, name);  /* borrowed */
    363         Py_XINCREF(m);
    364     }
    365     else {
    366         m = PyObject_GetItem(modules, name);
    367         if (m == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) {
    368             PyErr_Clear();
    369         }
    370     }
    371     Py_DECREF(modules);
    372     return m;
    373 }
    374 
    375 
    376 /* List of names to clear in sys */
    377 static const char * const sys_deletes[] = {
    378     "path", "argv", "ps1", "ps2",
    379     "last_type", "last_value", "last_traceback",
    380     "path_hooks", "path_importer_cache", "meta_path",
    381     "__interactivehook__",
    382     /* misc stuff */
    383     "flags", "float_info",
    384     NULL
    385 };
    386 
    387 static const char * const sys_files[] = {
    388     "stdin", "__stdin__",
    389     "stdout", "__stdout__",
    390     "stderr", "__stderr__",
    391     NULL
    392 };
    393 
    394 /* Un-initialize things, as good as we can */
    395 
    396 void
    397 PyImport_Cleanup(void)
    398 {
    399     Py_ssize_t pos;
    400     PyObject *key, *value, *dict;
    401     PyInterpreterState *interp = PyThreadState_GET()->interp;
    402     PyObject *modules = PyImport_GetModuleDict();
    403     PyObject *weaklist = NULL;
    404     const char * const *p;
    405 
    406     if (modules == NULL)
    407         return; /* Already done */
    408 
    409     /* Delete some special variables first.  These are common
    410        places where user values hide and people complain when their
    411        destructors fail.  Since the modules containing them are
    412        deleted *last* of all, they would come too late in the normal
    413        destruction order.  Sigh. */
    414 
    415     /* XXX Perhaps these precautions are obsolete. Who knows? */
    416 
    417     if (Py_VerboseFlag)
    418         PySys_WriteStderr("# clear builtins._\n");
    419     if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
    420         PyErr_Clear();
    421     }
    422 
    423     for (p = sys_deletes; *p != NULL; p++) {
    424         if (Py_VerboseFlag)
    425             PySys_WriteStderr("# clear sys.%s\n", *p);
    426         if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
    427             PyErr_Clear();
    428         }
    429     }
    430     for (p = sys_files; *p != NULL; p+=2) {
    431         if (Py_VerboseFlag)
    432             PySys_WriteStderr("# restore sys.%s\n", *p);
    433         value = PyDict_GetItemString(interp->sysdict, *(p+1));
    434         if (value == NULL)
    435             value = Py_None;
    436         if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) {
    437             PyErr_Clear();
    438         }
    439     }
    440 
    441     /* We prepare a list which will receive (name, weakref) tuples of
    442        modules when they are removed from sys.modules.  The name is used
    443        for diagnosis messages (in verbose mode), while the weakref helps
    444        detect those modules which have been held alive. */
    445     weaklist = PyList_New(0);
    446     if (weaklist == NULL)
    447         PyErr_Clear();
    448 
    449 #define STORE_MODULE_WEAKREF(name, mod) \
    450     if (weaklist != NULL) { \
    451         PyObject *wr = PyWeakref_NewRef(mod, NULL); \
    452         if (wr) { \
    453             PyObject *tup = PyTuple_Pack(2, name, wr); \
    454             if (!tup || PyList_Append(weaklist, tup) < 0) { \
    455                 PyErr_Clear(); \
    456             } \
    457             Py_XDECREF(tup); \
    458             Py_DECREF(wr); \
    459         } \
    460         else { \
    461             PyErr_Clear(); \
    462         } \
    463     }
    464 #define CLEAR_MODULE(name, mod) \
    465     if (PyModule_Check(mod)) { \
    466         if (Py_VerboseFlag && PyUnicode_Check(name)) \
    467             PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
    468         STORE_MODULE_WEAKREF(name, mod); \
    469         if (PyObject_SetItem(modules, name, Py_None) < 0) { \
    470             PyErr_Clear(); \
    471         } \
    472     }
    473 
    474     /* Remove all modules from sys.modules, hoping that garbage collection
    475        can reclaim most of them. */
    476     if (PyDict_CheckExact(modules)) {
    477         pos = 0;
    478         while (PyDict_Next(modules, &pos, &key, &value)) {
    479             CLEAR_MODULE(key, value);
    480         }
    481     }
    482     else {
    483         PyObject *iterator = PyObject_GetIter(modules);
    484         if (iterator == NULL) {
    485             PyErr_Clear();
    486         }
    487         else {
    488             while ((key = PyIter_Next(iterator))) {
    489                 value = PyObject_GetItem(modules, key);
    490                 if (value == NULL) {
    491                     PyErr_Clear();
    492                     continue;
    493                 }
    494                 CLEAR_MODULE(key, value);
    495                 Py_DECREF(value);
    496                 Py_DECREF(key);
    497             }
    498             if (PyErr_Occurred()) {
    499                 PyErr_Clear();
    500             }
    501             Py_DECREF(iterator);
    502         }
    503     }
    504 
    505     /* Clear the modules dict. */
    506     if (PyDict_CheckExact(modules)) {
    507         PyDict_Clear(modules);
    508     }
    509     else {
    510         _Py_IDENTIFIER(clear);
    511         if (_PyObject_CallMethodId(modules, &PyId_clear, "") == NULL)
    512             PyErr_Clear();
    513     }
    514     /* Restore the original builtins dict, to ensure that any
    515        user data gets cleared. */
    516     dict = PyDict_Copy(interp->builtins);
    517     if (dict == NULL)
    518         PyErr_Clear();
    519     PyDict_Clear(interp->builtins);
    520     if (PyDict_Update(interp->builtins, interp->builtins_copy))
    521         PyErr_Clear();
    522     Py_XDECREF(dict);
    523     /* Clear module dict copies stored in the interpreter state */
    524     _PyState_ClearModules();
    525     /* Collect references */
    526     _PyGC_CollectNoFail();
    527     /* Dump GC stats before it's too late, since it uses the warnings
    528        machinery. */
    529     _PyGC_DumpShutdownStats();
    530 
    531     /* Now, if there are any modules left alive, clear their globals to
    532        minimize potential leaks.  All C extension modules actually end
    533        up here, since they are kept alive in the interpreter state.
    534 
    535        The special treatment of "builtins" here is because even
    536        when it's not referenced as a module, its dictionary is
    537        referenced by almost every module's __builtins__.  Since
    538        deleting a module clears its dictionary (even if there are
    539        references left to it), we need to delete the "builtins"
    540        module last.  Likewise, we don't delete sys until the very
    541        end because it is implicitly referenced (e.g. by print). */
    542     if (weaklist != NULL) {
    543         Py_ssize_t i, n;
    544         n = PyList_GET_SIZE(weaklist);
    545         for (i = 0; i < n; i++) {
    546             PyObject *tup = PyList_GET_ITEM(weaklist, i);
    547             PyObject *name = PyTuple_GET_ITEM(tup, 0);
    548             PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
    549             if (mod == Py_None)
    550                 continue;
    551             assert(PyModule_Check(mod));
    552             dict = PyModule_GetDict(mod);
    553             if (dict == interp->builtins || dict == interp->sysdict)
    554                 continue;
    555             Py_INCREF(mod);
    556             if (Py_VerboseFlag && PyUnicode_Check(name))
    557                 PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
    558             _PyModule_Clear(mod);
    559             Py_DECREF(mod);
    560         }
    561         Py_DECREF(weaklist);
    562     }
    563 
    564     /* Next, delete sys and builtins (in that order) */
    565     if (Py_VerboseFlag)
    566         PySys_FormatStderr("# cleanup[3] wiping sys\n");
    567     _PyModule_ClearDict(interp->sysdict);
    568     if (Py_VerboseFlag)
    569         PySys_FormatStderr("# cleanup[3] wiping builtins\n");
    570     _PyModule_ClearDict(interp->builtins);
    571 
    572     /* Clear and delete the modules directory.  Actual modules will
    573        still be there only if imported during the execution of some
    574        destructor. */
    575     interp->modules = NULL;
    576     Py_DECREF(modules);
    577 
    578     /* Once more */
    579     _PyGC_CollectNoFail();
    580 
    581 #undef CLEAR_MODULE
    582 #undef STORE_MODULE_WEAKREF
    583 }
    584 
    585 
    586 /* Helper for pythonrun.c -- return magic number and tag. */
    587 
    588 long
    589 PyImport_GetMagicNumber(void)
    590 {
    591     long res;
    592     PyInterpreterState *interp = PyThreadState_Get()->interp;
    593     PyObject *external, *pyc_magic;
    594 
    595     external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
    596     if (external == NULL)
    597         return -1;
    598     pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER");
    599     Py_DECREF(external);
    600     if (pyc_magic == NULL)
    601         return -1;
    602     res = PyLong_AsLong(pyc_magic);
    603     Py_DECREF(pyc_magic);
    604     return res;
    605 }
    606 
    607 
    608 extern const char * _PySys_ImplCacheTag;
    609 
    610 const char *
    611 PyImport_GetMagicTag(void)
    612 {
    613     return _PySys_ImplCacheTag;
    614 }
    615 
    616 
    617 /* Magic for extension modules (built-in as well as dynamically
    618    loaded).  To prevent initializing an extension module more than
    619    once, we keep a static dictionary 'extensions' keyed by the tuple
    620    (module name, module name)  (for built-in modules) or by
    621    (filename, module name) (for dynamically loaded modules), containing these
    622    modules.  A copy of the module's dictionary is stored by calling
    623    _PyImport_FixupExtensionObject() immediately after the module initialization
    624    function succeeds.  A copy can be retrieved from there by calling
    625    _PyImport_FindExtensionObject().
    626 
    627    Modules which do support multiple initialization set their m_size
    628    field to a non-negative number (indicating the size of the
    629    module-specific state). They are still recorded in the extensions
    630    dictionary, to avoid loading shared libraries twice.
    631 */
    632 
    633 int
    634 _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
    635                                  PyObject *filename, PyObject *modules)
    636 {
    637     PyObject *dict, *key;
    638     struct PyModuleDef *def;
    639     int res;
    640     if (extensions == NULL) {
    641         extensions = PyDict_New();
    642         if (extensions == NULL)
    643             return -1;
    644     }
    645     if (mod == NULL || !PyModule_Check(mod)) {
    646         PyErr_BadInternalCall();
    647         return -1;
    648     }
    649     def = PyModule_GetDef(mod);
    650     if (!def) {
    651         PyErr_BadInternalCall();
    652         return -1;
    653     }
    654     if (PyObject_SetItem(modules, name, mod) < 0)
    655         return -1;
    656     if (_PyState_AddModule(mod, def) < 0) {
    657         PyMapping_DelItem(modules, name);
    658         return -1;
    659     }
    660     if (def->m_size == -1) {
    661         if (def->m_base.m_copy) {
    662             /* Somebody already imported the module,
    663                likely under a different name.
    664                XXX this should really not happen. */
    665             Py_CLEAR(def->m_base.m_copy);
    666         }
    667         dict = PyModule_GetDict(mod);
    668         if (dict == NULL)
    669             return -1;
    670         def->m_base.m_copy = PyDict_Copy(dict);
    671         if (def->m_base.m_copy == NULL)
    672             return -1;
    673     }
    674     key = PyTuple_Pack(2, filename, name);
    675     if (key == NULL)
    676         return -1;
    677     res = PyDict_SetItem(extensions, key, (PyObject *)def);
    678     Py_DECREF(key);
    679     if (res < 0)
    680         return -1;
    681     return 0;
    682 }
    683 
    684 int
    685 _PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules)
    686 {
    687     int res;
    688     PyObject *nameobj;
    689     nameobj = PyUnicode_InternFromString(name);
    690     if (nameobj == NULL)
    691         return -1;
    692     res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj, modules);
    693     Py_DECREF(nameobj);
    694     return res;
    695 }
    696 
    697 PyObject *
    698 _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
    699 {
    700     PyObject *modules = PyImport_GetModuleDict();
    701     return _PyImport_FindExtensionObjectEx(name, filename, modules);
    702 }
    703 
    704 PyObject *
    705 _PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename,
    706                                 PyObject *modules)
    707 {
    708     PyObject *mod, *mdict, *key;
    709     PyModuleDef* def;
    710     if (extensions == NULL)
    711         return NULL;
    712     key = PyTuple_Pack(2, filename, name);
    713     if (key == NULL)
    714         return NULL;
    715     def = (PyModuleDef *)PyDict_GetItem(extensions, key);
    716     Py_DECREF(key);
    717     if (def == NULL)
    718         return NULL;
    719     if (def->m_size == -1) {
    720         /* Module does not support repeated initialization */
    721         if (def->m_base.m_copy == NULL)
    722             return NULL;
    723         mod = _PyImport_AddModuleObject(name, modules);
    724         if (mod == NULL)
    725             return NULL;
    726         mdict = PyModule_GetDict(mod);
    727         if (mdict == NULL)
    728             return NULL;
    729         if (PyDict_Update(mdict, def->m_base.m_copy))
    730             return NULL;
    731     }
    732     else {
    733         if (def->m_base.m_init == NULL)
    734             return NULL;
    735         mod = def->m_base.m_init();
    736         if (mod == NULL)
    737             return NULL;
    738         if (PyObject_SetItem(modules, name, mod) == -1) {
    739             Py_DECREF(mod);
    740             return NULL;
    741         }
    742         Py_DECREF(mod);
    743     }
    744     if (_PyState_AddModule(mod, def) < 0) {
    745         PyMapping_DelItem(modules, name);
    746         return NULL;
    747     }
    748     if (Py_VerboseFlag)
    749         PySys_FormatStderr("import %U # previously loaded (%R)\n",
    750                           name, filename);
    751     return mod;
    752 
    753 }
    754 
    755 PyObject *
    756 _PyImport_FindBuiltin(const char *name, PyObject *modules)
    757 {
    758     PyObject *res, *nameobj;
    759     nameobj = PyUnicode_InternFromString(name);
    760     if (nameobj == NULL)
    761         return NULL;
    762     res = _PyImport_FindExtensionObjectEx(nameobj, nameobj, modules);
    763     Py_DECREF(nameobj);
    764     return res;
    765 }
    766 
    767 /* Get the module object corresponding to a module name.
    768    First check the modules dictionary if there's one there,
    769    if not, create a new one and insert it in the modules dictionary.
    770    Because the former action is most common, THIS DOES NOT RETURN A
    771    'NEW' REFERENCE! */
    772 
    773 PyObject *
    774 PyImport_AddModuleObject(PyObject *name)
    775 {
    776     PyObject *modules = PyImport_GetModuleDict();
    777     return _PyImport_AddModuleObject(name, modules);
    778 }
    779 
    780 PyObject *
    781 _PyImport_AddModuleObject(PyObject *name, PyObject *modules)
    782 {
    783     PyObject *m;
    784     if (PyDict_CheckExact(modules)) {
    785         m = PyDict_GetItemWithError(modules, name);
    786     }
    787     else {
    788         m = PyObject_GetItem(modules, name);
    789         // For backward-comaptibility we copy the behavior
    790         // of PyDict_GetItemWithError().
    791         if (PyErr_ExceptionMatches(PyExc_KeyError)) {
    792             PyErr_Clear();
    793         }
    794     }
    795     if (PyErr_Occurred()) {
    796         return NULL;
    797     }
    798     if (m != NULL && PyModule_Check(m)) {
    799         return m;
    800     }
    801     m = PyModule_NewObject(name);
    802     if (m == NULL)
    803         return NULL;
    804     if (PyObject_SetItem(modules, name, m) != 0) {
    805         Py_DECREF(m);
    806         return NULL;
    807     }
    808     Py_DECREF(m); /* Yes, it still exists, in modules! */
    809 
    810     return m;
    811 }
    812 
    813 PyObject *
    814 PyImport_AddModule(const char *name)
    815 {
    816     PyObject *nameobj, *module;
    817     nameobj = PyUnicode_FromString(name);
    818     if (nameobj == NULL)
    819         return NULL;
    820     module = PyImport_AddModuleObject(nameobj);
    821     Py_DECREF(nameobj);
    822     return module;
    823 }
    824 
    825 
    826 /* Remove name from sys.modules, if it's there. */
    827 static void
    828 remove_module(PyObject *name)
    829 {
    830     PyObject *modules = PyImport_GetModuleDict();
    831     if (PyMapping_DelItem(modules, name) < 0) {
    832         if (!PyMapping_HasKey(modules, name)) {
    833             return;
    834         }
    835         Py_FatalError("import:  deleting existing key in "
    836                       "sys.modules failed");
    837     }
    838 }
    839 
    840 
    841 /* Execute a code object in a module and return the module object
    842  * WITH INCREMENTED REFERENCE COUNT.  If an error occurs, name is
    843  * removed from sys.modules, to avoid leaving damaged module objects
    844  * in sys.modules.  The caller may wish to restore the original
    845  * module object (if any) in this case; PyImport_ReloadModule is an
    846  * example.
    847  *
    848  * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
    849  * interface.  The other two exist primarily for backward compatibility.
    850  */
    851 PyObject *
    852 PyImport_ExecCodeModule(const char *name, PyObject *co)
    853 {
    854     return PyImport_ExecCodeModuleWithPathnames(
    855         name, co, (char *)NULL, (char *)NULL);
    856 }
    857 
    858 PyObject *
    859 PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
    860 {
    861     return PyImport_ExecCodeModuleWithPathnames(
    862         name, co, pathname, (char *)NULL);
    863 }
    864 
    865 PyObject *
    866 PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
    867                                      const char *pathname,
    868                                      const char *cpathname)
    869 {
    870     PyObject *m = NULL;
    871     PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL;
    872 
    873     nameobj = PyUnicode_FromString(name);
    874     if (nameobj == NULL)
    875         return NULL;
    876 
    877     if (cpathname != NULL) {
    878         cpathobj = PyUnicode_DecodeFSDefault(cpathname);
    879         if (cpathobj == NULL)
    880             goto error;
    881     }
    882     else
    883         cpathobj = NULL;
    884 
    885     if (pathname != NULL) {
    886         pathobj = PyUnicode_DecodeFSDefault(pathname);
    887         if (pathobj == NULL)
    888             goto error;
    889     }
    890     else if (cpathobj != NULL) {
    891         PyInterpreterState *interp = PyThreadState_GET()->interp;
    892         _Py_IDENTIFIER(_get_sourcefile);
    893 
    894         if (interp == NULL) {
    895             Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
    896                           "no interpreter!");
    897         }
    898 
    899         external= PyObject_GetAttrString(interp->importlib,
    900                                          "_bootstrap_external");
    901         if (external != NULL) {
    902             pathobj = _PyObject_CallMethodIdObjArgs(external,
    903                                                     &PyId__get_sourcefile, cpathobj,
    904                                                     NULL);
    905             Py_DECREF(external);
    906         }
    907         if (pathobj == NULL)
    908             PyErr_Clear();
    909     }
    910     else
    911         pathobj = NULL;
    912 
    913     m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
    914 error:
    915     Py_DECREF(nameobj);
    916     Py_XDECREF(pathobj);
    917     Py_XDECREF(cpathobj);
    918     return m;
    919 }
    920 
    921 static PyObject *
    922 module_dict_for_exec(PyObject *name)
    923 {
    924     PyObject *m, *d = NULL;
    925 
    926     m = PyImport_AddModuleObject(name);
    927     if (m == NULL)
    928         return NULL;
    929     /* If the module is being reloaded, we get the old module back
    930        and re-use its dict to exec the new code. */
    931     d = PyModule_GetDict(m);
    932     if (PyDict_GetItemString(d, "__builtins__") == NULL) {
    933         if (PyDict_SetItemString(d, "__builtins__",
    934                                  PyEval_GetBuiltins()) != 0) {
    935             remove_module(name);
    936             return NULL;
    937         }
    938     }
    939 
    940     return d;  /* Return a borrowed reference. */
    941 }
    942 
    943 static PyObject *
    944 exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object)
    945 {
    946     PyObject *v, *m;
    947 
    948     v = PyEval_EvalCode(code_object, module_dict, module_dict);
    949     if (v == NULL) {
    950         remove_module(name);
    951         return NULL;
    952     }
    953     Py_DECREF(v);
    954 
    955     m = PyImport_GetModule(name);
    956     if (m == NULL) {
    957         PyErr_Format(PyExc_ImportError,
    958                      "Loaded module %R not found in sys.modules",
    959                      name);
    960         return NULL;
    961     }
    962 
    963     return m;
    964 }
    965 
    966 PyObject*
    967 PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
    968                               PyObject *cpathname)
    969 {
    970     PyObject *d, *external, *res;
    971     PyInterpreterState *interp = PyThreadState_GET()->interp;
    972     _Py_IDENTIFIER(_fix_up_module);
    973 
    974     d = module_dict_for_exec(name);
    975     if (d == NULL) {
    976         return NULL;
    977     }
    978 
    979     if (pathname == NULL) {
    980         pathname = ((PyCodeObject *)co)->co_filename;
    981     }
    982     external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
    983     if (external == NULL)
    984         return NULL;
    985     res = _PyObject_CallMethodIdObjArgs(external,
    986                                         &PyId__fix_up_module,
    987                                         d, name, pathname, cpathname, NULL);
    988     Py_DECREF(external);
    989     if (res != NULL) {
    990         Py_DECREF(res);
    991         res = exec_code_in_module(name, d, co);
    992     }
    993     return res;
    994 }
    995 
    996 
    997 static void
    998 update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
    999 {
   1000     PyObject *constants, *tmp;
   1001     Py_ssize_t i, n;
   1002 
   1003     if (PyUnicode_Compare(co->co_filename, oldname))
   1004         return;
   1005 
   1006     Py_INCREF(newname);
   1007     Py_XSETREF(co->co_filename, newname);
   1008 
   1009     constants = co->co_consts;
   1010     n = PyTuple_GET_SIZE(constants);
   1011     for (i = 0; i < n; i++) {
   1012         tmp = PyTuple_GET_ITEM(constants, i);
   1013         if (PyCode_Check(tmp))
   1014             update_code_filenames((PyCodeObject *)tmp,
   1015                                   oldname, newname);
   1016     }
   1017 }
   1018 
   1019 static void
   1020 update_compiled_module(PyCodeObject *co, PyObject *newname)
   1021 {
   1022     PyObject *oldname;
   1023 
   1024     if (PyUnicode_Compare(co->co_filename, newname) == 0)
   1025         return;
   1026 
   1027     oldname = co->co_filename;
   1028     Py_INCREF(oldname);
   1029     update_code_filenames(co, oldname, newname);
   1030     Py_DECREF(oldname);
   1031 }
   1032 
   1033 /*[clinic input]
   1034 _imp._fix_co_filename
   1035 
   1036     code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
   1037         Code object to change.
   1038 
   1039     path: unicode
   1040         File path to use.
   1041     /
   1042 
   1043 Changes code.co_filename to specify the passed-in file path.
   1044 [clinic start generated code]*/
   1045 
   1046 static PyObject *
   1047 _imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code,
   1048                            PyObject *path)
   1049 /*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/
   1050 
   1051 {
   1052     update_compiled_module(code, path);
   1053 
   1054     Py_RETURN_NONE;
   1055 }
   1056 
   1057 
   1058 /* Forward */
   1059 static const struct _frozen * find_frozen(PyObject *);
   1060 
   1061 
   1062 /* Helper to test for built-in module */
   1063 
   1064 static int
   1065 is_builtin(PyObject *name)
   1066 {
   1067     int i;
   1068     for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
   1069         if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) {
   1070             if (PyImport_Inittab[i].initfunc == NULL)
   1071                 return -1;
   1072             else
   1073                 return 1;
   1074         }
   1075     }
   1076     return 0;
   1077 }
   1078 
   1079 
   1080 /* Return a finder object for a sys.path/pkg.__path__ item 'p',
   1081    possibly by fetching it from the path_importer_cache dict. If it
   1082    wasn't yet cached, traverse path_hooks until a hook is found
   1083    that can handle the path item. Return None if no hook could;
   1084    this tells our caller that the path based finder could not find
   1085    a finder for this path item. Cache the result in
   1086    path_importer_cache.
   1087    Returns a borrowed reference. */
   1088 
   1089 static PyObject *
   1090 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
   1091                   PyObject *p)
   1092 {
   1093     PyObject *importer;
   1094     Py_ssize_t j, nhooks;
   1095 
   1096     /* These conditions are the caller's responsibility: */
   1097     assert(PyList_Check(path_hooks));
   1098     assert(PyDict_Check(path_importer_cache));
   1099 
   1100     nhooks = PyList_Size(path_hooks);
   1101     if (nhooks < 0)
   1102         return NULL; /* Shouldn't happen */
   1103 
   1104     importer = PyDict_GetItem(path_importer_cache, p);
   1105     if (importer != NULL)
   1106         return importer;
   1107 
   1108     /* set path_importer_cache[p] to None to avoid recursion */
   1109     if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
   1110         return NULL;
   1111 
   1112     for (j = 0; j < nhooks; j++) {
   1113         PyObject *hook = PyList_GetItem(path_hooks, j);
   1114         if (hook == NULL)
   1115             return NULL;
   1116         importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
   1117         if (importer != NULL)
   1118             break;
   1119 
   1120         if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
   1121             return NULL;
   1122         }
   1123         PyErr_Clear();
   1124     }
   1125     if (importer == NULL) {
   1126         return Py_None;
   1127     }
   1128     if (importer != NULL) {
   1129         int err = PyDict_SetItem(path_importer_cache, p, importer);
   1130         Py_DECREF(importer);
   1131         if (err != 0)
   1132             return NULL;
   1133     }
   1134     return importer;
   1135 }
   1136 
   1137 PyAPI_FUNC(PyObject *)
   1138 PyImport_GetImporter(PyObject *path) {
   1139     PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
   1140 
   1141     path_importer_cache = PySys_GetObject("path_importer_cache");
   1142     path_hooks = PySys_GetObject("path_hooks");
   1143     if (path_importer_cache != NULL && path_hooks != NULL) {
   1144         importer = get_path_importer(path_importer_cache,
   1145                                      path_hooks, path);
   1146     }
   1147     Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
   1148     return importer;
   1149 }
   1150 
   1151 /*[clinic input]
   1152 _imp.create_builtin
   1153 
   1154     spec: object
   1155     /
   1156 
   1157 Create an extension module.
   1158 [clinic start generated code]*/
   1159 
   1160 static PyObject *
   1161 _imp_create_builtin(PyObject *module, PyObject *spec)
   1162 /*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/
   1163 {
   1164     struct _inittab *p;
   1165     PyObject *name;
   1166     const char *namestr;
   1167     PyObject *mod;
   1168 
   1169     name = PyObject_GetAttrString(spec, "name");
   1170     if (name == NULL) {
   1171         return NULL;
   1172     }
   1173 
   1174     mod = _PyImport_FindExtensionObject(name, name);
   1175     if (mod || PyErr_Occurred()) {
   1176         Py_DECREF(name);
   1177         Py_XINCREF(mod);
   1178         return mod;
   1179     }
   1180 
   1181     namestr = PyUnicode_AsUTF8(name);
   1182     if (namestr == NULL) {
   1183         Py_DECREF(name);
   1184         return NULL;
   1185     }
   1186 
   1187     PyObject *modules = NULL;
   1188     for (p = PyImport_Inittab; p->name != NULL; p++) {
   1189         PyModuleDef *def;
   1190         if (_PyUnicode_EqualToASCIIString(name, p->name)) {
   1191             if (p->initfunc == NULL) {
   1192                 /* Cannot re-init internal module ("sys" or "builtins") */
   1193                 mod = PyImport_AddModule(namestr);
   1194                 Py_DECREF(name);
   1195                 return mod;
   1196             }
   1197             mod = (*p->initfunc)();
   1198             if (mod == NULL) {
   1199                 Py_DECREF(name);
   1200                 return NULL;
   1201             }
   1202             if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
   1203                 Py_DECREF(name);
   1204                 return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
   1205             } else {
   1206                 /* Remember pointer to module init function. */
   1207                 def = PyModule_GetDef(mod);
   1208                 if (def == NULL) {
   1209                     Py_DECREF(name);
   1210                     return NULL;
   1211                 }
   1212                 def->m_base.m_init = p->initfunc;
   1213                 if (modules == NULL) {
   1214                     modules = PyImport_GetModuleDict();
   1215                 }
   1216                 if (_PyImport_FixupExtensionObject(mod, name, name,
   1217                                                    modules) < 0) {
   1218                     Py_DECREF(name);
   1219                     return NULL;
   1220                 }
   1221                 Py_DECREF(name);
   1222                 return mod;
   1223             }
   1224         }
   1225     }
   1226     Py_DECREF(name);
   1227     Py_RETURN_NONE;
   1228 }
   1229 
   1230 
   1231 /* Frozen modules */
   1232 
   1233 static const struct _frozen *
   1234 find_frozen(PyObject *name)
   1235 {
   1236     const struct _frozen *p;
   1237 
   1238     if (name == NULL)
   1239         return NULL;
   1240 
   1241     for (p = PyImport_FrozenModules; ; p++) {
   1242         if (p->name == NULL)
   1243             return NULL;
   1244         if (_PyUnicode_EqualToASCIIString(name, p->name))
   1245             break;
   1246     }
   1247     return p;
   1248 }
   1249 
   1250 static PyObject *
   1251 get_frozen_object(PyObject *name)
   1252 {
   1253     const struct _frozen *p = find_frozen(name);
   1254     int size;
   1255 
   1256     if (p == NULL) {
   1257         PyErr_Format(PyExc_ImportError,
   1258                      "No such frozen object named %R",
   1259                      name);
   1260         return NULL;
   1261     }
   1262     if (p->code == NULL) {
   1263         PyErr_Format(PyExc_ImportError,
   1264                      "Excluded frozen object named %R",
   1265                      name);
   1266         return NULL;
   1267     }
   1268     size = p->size;
   1269     if (size < 0)
   1270         size = -size;
   1271     return PyMarshal_ReadObjectFromString((const char *)p->code, size);
   1272 }
   1273 
   1274 static PyObject *
   1275 is_frozen_package(PyObject *name)
   1276 {
   1277     const struct _frozen *p = find_frozen(name);
   1278     int size;
   1279 
   1280     if (p == NULL) {
   1281         PyErr_Format(PyExc_ImportError,
   1282                      "No such frozen object named %R",
   1283                      name);
   1284         return NULL;
   1285     }
   1286 
   1287     size = p->size;
   1288 
   1289     if (size < 0)
   1290         Py_RETURN_TRUE;
   1291     else
   1292         Py_RETURN_FALSE;
   1293 }
   1294 
   1295 
   1296 /* Initialize a frozen module.
   1297    Return 1 for success, 0 if the module is not found, and -1 with
   1298    an exception set if the initialization failed.
   1299    This function is also used from frozenmain.c */
   1300 
   1301 int
   1302 PyImport_ImportFrozenModuleObject(PyObject *name)
   1303 {
   1304     const struct _frozen *p;
   1305     PyObject *co, *m, *d;
   1306     int ispackage;
   1307     int size;
   1308 
   1309     p = find_frozen(name);
   1310 
   1311     if (p == NULL)
   1312         return 0;
   1313     if (p->code == NULL) {
   1314         PyErr_Format(PyExc_ImportError,
   1315                      "Excluded frozen object named %R",
   1316                      name);
   1317         return -1;
   1318     }
   1319     size = p->size;
   1320     ispackage = (size < 0);
   1321     if (ispackage)
   1322         size = -size;
   1323     co = PyMarshal_ReadObjectFromString((const char *)p->code, size);
   1324     if (co == NULL)
   1325         return -1;
   1326     if (!PyCode_Check(co)) {
   1327         PyErr_Format(PyExc_TypeError,
   1328                      "frozen object %R is not a code object",
   1329                      name);
   1330         goto err_return;
   1331     }
   1332     if (ispackage) {
   1333         /* Set __path__ to the empty list */
   1334         PyObject *l;
   1335         int err;
   1336         m = PyImport_AddModuleObject(name);
   1337         if (m == NULL)
   1338             goto err_return;
   1339         d = PyModule_GetDict(m);
   1340         l = PyList_New(0);
   1341         if (l == NULL) {
   1342             goto err_return;
   1343         }
   1344         err = PyDict_SetItemString(d, "__path__", l);
   1345         Py_DECREF(l);
   1346         if (err != 0)
   1347             goto err_return;
   1348     }
   1349     d = module_dict_for_exec(name);
   1350     if (d == NULL) {
   1351         goto err_return;
   1352     }
   1353     m = exec_code_in_module(name, d, co);
   1354     if (m == NULL)
   1355         goto err_return;
   1356     Py_DECREF(co);
   1357     Py_DECREF(m);
   1358     return 1;
   1359 err_return:
   1360     Py_DECREF(co);
   1361     return -1;
   1362 }
   1363 
   1364 int
   1365 PyImport_ImportFrozenModule(const char *name)
   1366 {
   1367     PyObject *nameobj;
   1368     int ret;
   1369     nameobj = PyUnicode_InternFromString(name);
   1370     if (nameobj == NULL)
   1371         return -1;
   1372     ret = PyImport_ImportFrozenModuleObject(nameobj);
   1373     Py_DECREF(nameobj);
   1374     return ret;
   1375 }
   1376 
   1377 
   1378 /* Import a module, either built-in, frozen, or external, and return
   1379    its module object WITH INCREMENTED REFERENCE COUNT */
   1380 
   1381 PyObject *
   1382 PyImport_ImportModule(const char *name)
   1383 {
   1384     PyObject *pname;
   1385     PyObject *result;
   1386 
   1387     pname = PyUnicode_FromString(name);
   1388     if (pname == NULL)
   1389         return NULL;
   1390     result = PyImport_Import(pname);
   1391     Py_DECREF(pname);
   1392     return result;
   1393 }
   1394 
   1395 /* Import a module without blocking
   1396  *
   1397  * At first it tries to fetch the module from sys.modules. If the module was
   1398  * never loaded before it loads it with PyImport_ImportModule() unless another
   1399  * thread holds the import lock. In the latter case the function raises an
   1400  * ImportError instead of blocking.
   1401  *
   1402  * Returns the module object with incremented ref count.
   1403  */
   1404 PyObject *
   1405 PyImport_ImportModuleNoBlock(const char *name)
   1406 {
   1407     return PyImport_ImportModule(name);
   1408 }
   1409 
   1410 
   1411 /* Remove importlib frames from the traceback,
   1412  * except in Verbose mode. */
   1413 static void
   1414 remove_importlib_frames(void)
   1415 {
   1416     const char *importlib_filename = "<frozen importlib._bootstrap>";
   1417     const char *external_filename = "<frozen importlib._bootstrap_external>";
   1418     const char *remove_frames = "_call_with_frames_removed";
   1419     int always_trim = 0;
   1420     int in_importlib = 0;
   1421     PyObject *exception, *value, *base_tb, *tb;
   1422     PyObject **prev_link, **outer_link = NULL;
   1423 
   1424     /* Synopsis: if it's an ImportError, we trim all importlib chunks
   1425        from the traceback. We always trim chunks
   1426        which end with a call to "_call_with_frames_removed". */
   1427 
   1428     PyErr_Fetch(&exception, &value, &base_tb);
   1429     if (!exception || Py_VerboseFlag)
   1430         goto done;
   1431     if (PyType_IsSubtype((PyTypeObject *) exception,
   1432                          (PyTypeObject *) PyExc_ImportError))
   1433         always_trim = 1;
   1434 
   1435     prev_link = &base_tb;
   1436     tb = base_tb;
   1437     while (tb != NULL) {
   1438         PyTracebackObject *traceback = (PyTracebackObject *)tb;
   1439         PyObject *next = (PyObject *) traceback->tb_next;
   1440         PyFrameObject *frame = traceback->tb_frame;
   1441         PyCodeObject *code = frame->f_code;
   1442         int now_in_importlib;
   1443 
   1444         assert(PyTraceBack_Check(tb));
   1445         now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) ||
   1446                            _PyUnicode_EqualToASCIIString(code->co_filename, external_filename);
   1447         if (now_in_importlib && !in_importlib) {
   1448             /* This is the link to this chunk of importlib tracebacks */
   1449             outer_link = prev_link;
   1450         }
   1451         in_importlib = now_in_importlib;
   1452 
   1453         if (in_importlib &&
   1454             (always_trim ||
   1455              _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) {
   1456             Py_XINCREF(next);
   1457             Py_XSETREF(*outer_link, next);
   1458             prev_link = outer_link;
   1459         }
   1460         else {
   1461             prev_link = (PyObject **) &traceback->tb_next;
   1462         }
   1463         tb = next;
   1464     }
   1465 done:
   1466     PyErr_Restore(exception, value, base_tb);
   1467 }
   1468 
   1469 
   1470 static PyObject *
   1471 resolve_name(PyObject *name, PyObject *globals, int level)
   1472 {
   1473     _Py_IDENTIFIER(__spec__);
   1474     _Py_IDENTIFIER(__package__);
   1475     _Py_IDENTIFIER(__path__);
   1476     _Py_IDENTIFIER(__name__);
   1477     _Py_IDENTIFIER(parent);
   1478     PyObject *abs_name;
   1479     PyObject *package = NULL;
   1480     PyObject *spec;
   1481     Py_ssize_t last_dot;
   1482     PyObject *base;
   1483     int level_up;
   1484 
   1485     if (globals == NULL) {
   1486         PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
   1487         goto error;
   1488     }
   1489     if (!PyDict_Check(globals)) {
   1490         PyErr_SetString(PyExc_TypeError, "globals must be a dict");
   1491         goto error;
   1492     }
   1493     package = _PyDict_GetItemId(globals, &PyId___package__);
   1494     if (package == Py_None) {
   1495         package = NULL;
   1496     }
   1497     spec = _PyDict_GetItemId(globals, &PyId___spec__);
   1498 
   1499     if (package != NULL) {
   1500         Py_INCREF(package);
   1501         if (!PyUnicode_Check(package)) {
   1502             PyErr_SetString(PyExc_TypeError, "package must be a string");
   1503             goto error;
   1504         }
   1505         else if (spec != NULL && spec != Py_None) {
   1506             int equal;
   1507             PyObject *parent = _PyObject_GetAttrId(spec, &PyId_parent);
   1508             if (parent == NULL) {
   1509                 goto error;
   1510             }
   1511 
   1512             equal = PyObject_RichCompareBool(package, parent, Py_EQ);
   1513             Py_DECREF(parent);
   1514             if (equal < 0) {
   1515                 goto error;
   1516             }
   1517             else if (equal == 0) {
   1518                 if (PyErr_WarnEx(PyExc_ImportWarning,
   1519                         "__package__ != __spec__.parent", 1) < 0) {
   1520                     goto error;
   1521                 }
   1522             }
   1523         }
   1524     }
   1525     else if (spec != NULL && spec != Py_None) {
   1526         package = _PyObject_GetAttrId(spec, &PyId_parent);
   1527         if (package == NULL) {
   1528             goto error;
   1529         }
   1530         else if (!PyUnicode_Check(package)) {
   1531             PyErr_SetString(PyExc_TypeError,
   1532                     "__spec__.parent must be a string");
   1533             goto error;
   1534         }
   1535     }
   1536     else {
   1537         if (PyErr_WarnEx(PyExc_ImportWarning,
   1538                     "can't resolve package from __spec__ or __package__, "
   1539                     "falling back on __name__ and __path__", 1) < 0) {
   1540             goto error;
   1541         }
   1542 
   1543         package = _PyDict_GetItemId(globals, &PyId___name__);
   1544         if (package == NULL) {
   1545             PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
   1546             goto error;
   1547         }
   1548 
   1549         Py_INCREF(package);
   1550         if (!PyUnicode_Check(package)) {
   1551             PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
   1552             goto error;
   1553         }
   1554 
   1555         if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
   1556             Py_ssize_t dot;
   1557 
   1558             if (PyUnicode_READY(package) < 0) {
   1559                 goto error;
   1560             }
   1561 
   1562             dot = PyUnicode_FindChar(package, '.',
   1563                                         0, PyUnicode_GET_LENGTH(package), -1);
   1564             if (dot == -2) {
   1565                 goto error;
   1566             }
   1567 
   1568             if (dot >= 0) {
   1569                 PyObject *substr = PyUnicode_Substring(package, 0, dot);
   1570                 if (substr == NULL) {
   1571                     goto error;
   1572                 }
   1573                 Py_SETREF(package, substr);
   1574             }
   1575         }
   1576     }
   1577 
   1578     last_dot = PyUnicode_GET_LENGTH(package);
   1579     if (last_dot == 0) {
   1580         PyErr_SetString(PyExc_ImportError,
   1581                 "attempted relative import with no known parent package");
   1582         goto error;
   1583     }
   1584 
   1585     for (level_up = 1; level_up < level; level_up += 1) {
   1586         last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
   1587         if (last_dot == -2) {
   1588             goto error;
   1589         }
   1590         else if (last_dot == -1) {
   1591             PyErr_SetString(PyExc_ValueError,
   1592                             "attempted relative import beyond top-level "
   1593                             "package");
   1594             goto error;
   1595         }
   1596     }
   1597 
   1598     base = PyUnicode_Substring(package, 0, last_dot);
   1599     Py_DECREF(package);
   1600     if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) {
   1601         return base;
   1602     }
   1603 
   1604     abs_name = PyUnicode_FromFormat("%U.%U", base, name);
   1605     Py_DECREF(base);
   1606     return abs_name;
   1607 
   1608   error:
   1609     Py_XDECREF(package);
   1610     return NULL;
   1611 }
   1612 
   1613 static PyObject *
   1614 import_find_and_load(PyObject *abs_name)
   1615 {
   1616     _Py_IDENTIFIER(_find_and_load);
   1617     PyObject *mod = NULL;
   1618     PyInterpreterState *interp = PyThreadState_GET()->interp;
   1619     int import_time = interp->core_config.import_time;
   1620     static int import_level;
   1621     static _PyTime_t accumulated;
   1622 
   1623     _PyTime_t t1 = 0, accumulated_copy = accumulated;
   1624 
   1625     /* XOptions is initialized after first some imports.
   1626      * So we can't have negative cache before completed initialization.
   1627      * Anyway, importlib._find_and_load is much slower than
   1628      * _PyDict_GetItemIdWithError().
   1629      */
   1630     if (import_time) {
   1631         static int header = 1;
   1632         if (header) {
   1633             fputs("import time: self [us] | cumulative | imported package\n",
   1634                   stderr);
   1635             header = 0;
   1636         }
   1637 
   1638         import_level++;
   1639         t1 = _PyTime_GetPerfCounter();
   1640         accumulated = 0;
   1641     }
   1642 
   1643     if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED())
   1644         PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name));
   1645 
   1646     mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
   1647                                         &PyId__find_and_load, abs_name,
   1648                                         interp->import_func, NULL);
   1649 
   1650     if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED())
   1651         PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
   1652                                        mod != NULL);
   1653 
   1654     if (import_time) {
   1655         _PyTime_t cum = _PyTime_GetPerfCounter() - t1;
   1656 
   1657         import_level--;
   1658         fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
   1659                 (long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING),
   1660                 (long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING),
   1661                 import_level*2, "", PyUnicode_AsUTF8(abs_name));
   1662 
   1663         accumulated = accumulated_copy + cum;
   1664     }
   1665 
   1666     return mod;
   1667 }
   1668 
   1669 PyObject *
   1670 PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
   1671                                  PyObject *locals, PyObject *fromlist,
   1672                                  int level)
   1673 {
   1674     _Py_IDENTIFIER(_handle_fromlist);
   1675     PyObject *abs_name = NULL;
   1676     PyObject *final_mod = NULL;
   1677     PyObject *mod = NULL;
   1678     PyObject *package = NULL;
   1679     PyInterpreterState *interp = PyThreadState_GET()->interp;
   1680     int has_from;
   1681 
   1682     if (name == NULL) {
   1683         PyErr_SetString(PyExc_ValueError, "Empty module name");
   1684         goto error;
   1685     }
   1686 
   1687     /* The below code is importlib.__import__() & _gcd_import(), ported to C
   1688        for added performance. */
   1689 
   1690     if (!PyUnicode_Check(name)) {
   1691         PyErr_SetString(PyExc_TypeError, "module name must be a string");
   1692         goto error;
   1693     }
   1694     if (PyUnicode_READY(name) < 0) {
   1695         goto error;
   1696     }
   1697     if (level < 0) {
   1698         PyErr_SetString(PyExc_ValueError, "level must be >= 0");
   1699         goto error;
   1700     }
   1701 
   1702     if (level > 0) {
   1703         abs_name = resolve_name(name, globals, level);
   1704         if (abs_name == NULL)
   1705             goto error;
   1706     }
   1707     else {  /* level == 0 */
   1708         if (PyUnicode_GET_LENGTH(name) == 0) {
   1709             PyErr_SetString(PyExc_ValueError, "Empty module name");
   1710             goto error;
   1711         }
   1712         abs_name = name;
   1713         Py_INCREF(abs_name);
   1714     }
   1715 
   1716     mod = PyImport_GetModule(abs_name);
   1717     if (mod != NULL && mod != Py_None) {
   1718         _Py_IDENTIFIER(__spec__);
   1719         _Py_IDENTIFIER(_initializing);
   1720         _Py_IDENTIFIER(_lock_unlock_module);
   1721         PyObject *value = NULL;
   1722         PyObject *spec;
   1723         int initializing = 0;
   1724 
   1725         /* Optimization: only call _bootstrap._lock_unlock_module() if
   1726            __spec__._initializing is true.
   1727            NOTE: because of this, initializing must be set *before*
   1728            stuffing the new module in sys.modules.
   1729          */
   1730         spec = _PyObject_GetAttrId(mod, &PyId___spec__);
   1731         if (spec != NULL) {
   1732             value = _PyObject_GetAttrId(spec, &PyId__initializing);
   1733             Py_DECREF(spec);
   1734         }
   1735         if (value == NULL)
   1736             PyErr_Clear();
   1737         else {
   1738             initializing = PyObject_IsTrue(value);
   1739             Py_DECREF(value);
   1740             if (initializing == -1)
   1741                 PyErr_Clear();
   1742             if (initializing > 0) {
   1743                 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
   1744                                                 &PyId__lock_unlock_module, abs_name,
   1745                                                 NULL);
   1746                 if (value == NULL)
   1747                     goto error;
   1748                 Py_DECREF(value);
   1749             }
   1750         }
   1751     }
   1752     else {
   1753         Py_XDECREF(mod);
   1754         mod = import_find_and_load(abs_name);
   1755         if (mod == NULL) {
   1756             goto error;
   1757         }
   1758     }
   1759 
   1760     has_from = 0;
   1761     if (fromlist != NULL && fromlist != Py_None) {
   1762         has_from = PyObject_IsTrue(fromlist);
   1763         if (has_from < 0)
   1764             goto error;
   1765     }
   1766     if (!has_from) {
   1767         Py_ssize_t len = PyUnicode_GET_LENGTH(name);
   1768         if (level == 0 || len > 0) {
   1769             Py_ssize_t dot;
   1770 
   1771             dot = PyUnicode_FindChar(name, '.', 0, len, 1);
   1772             if (dot == -2) {
   1773                 goto error;
   1774             }
   1775 
   1776             if (dot == -1) {
   1777                 /* No dot in module name, simple exit */
   1778                 final_mod = mod;
   1779                 Py_INCREF(mod);
   1780                 goto error;
   1781             }
   1782 
   1783             if (level == 0) {
   1784                 PyObject *front = PyUnicode_Substring(name, 0, dot);
   1785                 if (front == NULL) {
   1786                     goto error;
   1787                 }
   1788 
   1789                 final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0);
   1790                 Py_DECREF(front);
   1791             }
   1792             else {
   1793                 Py_ssize_t cut_off = len - dot;
   1794                 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
   1795                 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
   1796                                                         abs_name_len - cut_off);
   1797                 if (to_return == NULL) {
   1798                     goto error;
   1799                 }
   1800 
   1801                 final_mod = PyImport_GetModule(to_return);
   1802                 Py_DECREF(to_return);
   1803                 if (final_mod == NULL) {
   1804                     PyErr_Format(PyExc_KeyError,
   1805                                  "%R not in sys.modules as expected",
   1806                                  to_return);
   1807                     goto error;
   1808                 }
   1809             }
   1810         }
   1811         else {
   1812             final_mod = mod;
   1813             Py_INCREF(mod);
   1814         }
   1815     }
   1816     else {
   1817         final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
   1818                                                   &PyId__handle_fromlist, mod,
   1819                                                   fromlist, interp->import_func,
   1820                                                   NULL);
   1821     }
   1822 
   1823   error:
   1824     Py_XDECREF(abs_name);
   1825     Py_XDECREF(mod);
   1826     Py_XDECREF(package);
   1827     if (final_mod == NULL)
   1828         remove_importlib_frames();
   1829     return final_mod;
   1830 }
   1831 
   1832 PyObject *
   1833 PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
   1834                            PyObject *fromlist, int level)
   1835 {
   1836     PyObject *nameobj, *mod;
   1837     nameobj = PyUnicode_FromString(name);
   1838     if (nameobj == NULL)
   1839         return NULL;
   1840     mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
   1841                                            fromlist, level);
   1842     Py_DECREF(nameobj);
   1843     return mod;
   1844 }
   1845 
   1846 
   1847 /* Re-import a module of any kind and return its module object, WITH
   1848    INCREMENTED REFERENCE COUNT */
   1849 
   1850 PyObject *
   1851 PyImport_ReloadModule(PyObject *m)
   1852 {
   1853     _Py_IDENTIFIER(imp);
   1854     _Py_IDENTIFIER(reload);
   1855     PyObject *reloaded_module = NULL;
   1856     PyObject *imp = _PyImport_GetModuleId(&PyId_imp);
   1857     if (imp == NULL) {
   1858         imp = PyImport_ImportModule("imp");
   1859         if (imp == NULL) {
   1860             return NULL;
   1861         }
   1862     }
   1863 
   1864     reloaded_module = _PyObject_CallMethodIdObjArgs(imp, &PyId_reload, m, NULL);
   1865     Py_DECREF(imp);
   1866     return reloaded_module;
   1867 }
   1868 
   1869 
   1870 /* Higher-level import emulator which emulates the "import" statement
   1871    more accurately -- it invokes the __import__() function from the
   1872    builtins of the current globals.  This means that the import is
   1873    done using whatever import hooks are installed in the current
   1874    environment.
   1875    A dummy list ["__doc__"] is passed as the 4th argument so that
   1876    e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
   1877    will return <module "gencache"> instead of <module "win32com">. */
   1878 
   1879 PyObject *
   1880 PyImport_Import(PyObject *module_name)
   1881 {
   1882     static PyObject *silly_list = NULL;
   1883     static PyObject *builtins_str = NULL;
   1884     static PyObject *import_str = NULL;
   1885     PyObject *globals = NULL;
   1886     PyObject *import = NULL;
   1887     PyObject *builtins = NULL;
   1888     PyObject *r = NULL;
   1889 
   1890     /* Initialize constant string objects */
   1891     if (silly_list == NULL) {
   1892         import_str = PyUnicode_InternFromString("__import__");
   1893         if (import_str == NULL)
   1894             return NULL;
   1895         builtins_str = PyUnicode_InternFromString("__builtins__");
   1896         if (builtins_str == NULL)
   1897             return NULL;
   1898         silly_list = PyList_New(0);
   1899         if (silly_list == NULL)
   1900             return NULL;
   1901     }
   1902 
   1903     /* Get the builtins from current globals */
   1904     globals = PyEval_GetGlobals();
   1905     if (globals != NULL) {
   1906         Py_INCREF(globals);
   1907         builtins = PyObject_GetItem(globals, builtins_str);
   1908         if (builtins == NULL)
   1909             goto err;
   1910     }
   1911     else {
   1912         /* No globals -- use standard builtins, and fake globals */
   1913         builtins = PyImport_ImportModuleLevel("builtins",
   1914                                               NULL, NULL, NULL, 0);
   1915         if (builtins == NULL)
   1916             return NULL;
   1917         globals = Py_BuildValue("{OO}", builtins_str, builtins);
   1918         if (globals == NULL)
   1919             goto err;
   1920     }
   1921 
   1922     /* Get the __import__ function from the builtins */
   1923     if (PyDict_Check(builtins)) {
   1924         import = PyObject_GetItem(builtins, import_str);
   1925         if (import == NULL)
   1926             PyErr_SetObject(PyExc_KeyError, import_str);
   1927     }
   1928     else
   1929         import = PyObject_GetAttr(builtins, import_str);
   1930     if (import == NULL)
   1931         goto err;
   1932 
   1933     /* Call the __import__ function with the proper argument list
   1934        Always use absolute import here.
   1935        Calling for side-effect of import. */
   1936     r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
   1937                               globals, silly_list, 0, NULL);
   1938     if (r == NULL)
   1939         goto err;
   1940     Py_DECREF(r);
   1941 
   1942     r = PyImport_GetModule(module_name);
   1943     if (r == NULL && !PyErr_Occurred()) {
   1944         PyErr_SetObject(PyExc_KeyError, module_name);
   1945     }
   1946 
   1947   err:
   1948     Py_XDECREF(globals);
   1949     Py_XDECREF(builtins);
   1950     Py_XDECREF(import);
   1951 
   1952     return r;
   1953 }
   1954 
   1955 /*[clinic input]
   1956 _imp.extension_suffixes
   1957 
   1958 Returns the list of file suffixes used to identify extension modules.
   1959 [clinic start generated code]*/
   1960 
   1961 static PyObject *
   1962 _imp_extension_suffixes_impl(PyObject *module)
   1963 /*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/
   1964 {
   1965     PyObject *list;
   1966     const char *suffix;
   1967     unsigned int index = 0;
   1968 
   1969     list = PyList_New(0);
   1970     if (list == NULL)
   1971         return NULL;
   1972 #ifdef HAVE_DYNAMIC_LOADING
   1973     while ((suffix = _PyImport_DynLoadFiletab[index])) {
   1974         PyObject *item = PyUnicode_FromString(suffix);
   1975         if (item == NULL) {
   1976             Py_DECREF(list);
   1977             return NULL;
   1978         }
   1979         if (PyList_Append(list, item) < 0) {
   1980             Py_DECREF(list);
   1981             Py_DECREF(item);
   1982             return NULL;
   1983         }
   1984         Py_DECREF(item);
   1985         index += 1;
   1986     }
   1987 #endif
   1988     return list;
   1989 }
   1990 
   1991 /*[clinic input]
   1992 _imp.init_frozen
   1993 
   1994     name: unicode
   1995     /
   1996 
   1997 Initializes a frozen module.
   1998 [clinic start generated code]*/
   1999 
   2000 static PyObject *
   2001 _imp_init_frozen_impl(PyObject *module, PyObject *name)
   2002 /*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/
   2003 {
   2004     int ret;
   2005     PyObject *m;
   2006 
   2007     ret = PyImport_ImportFrozenModuleObject(name);
   2008     if (ret < 0)
   2009         return NULL;
   2010     if (ret == 0) {
   2011         Py_RETURN_NONE;
   2012     }
   2013     m = PyImport_AddModuleObject(name);
   2014     Py_XINCREF(m);
   2015     return m;
   2016 }
   2017 
   2018 /*[clinic input]
   2019 _imp.get_frozen_object
   2020 
   2021     name: unicode
   2022     /
   2023 
   2024 Create a code object for a frozen module.
   2025 [clinic start generated code]*/
   2026 
   2027 static PyObject *
   2028 _imp_get_frozen_object_impl(PyObject *module, PyObject *name)
   2029 /*[clinic end generated code: output=2568cc5b7aa0da63 input=ed689bc05358fdbd]*/
   2030 {
   2031     return get_frozen_object(name);
   2032 }
   2033 
   2034 /*[clinic input]
   2035 _imp.is_frozen_package
   2036 
   2037     name: unicode
   2038     /
   2039 
   2040 Returns True if the module name is of a frozen package.
   2041 [clinic start generated code]*/
   2042 
   2043 static PyObject *
   2044 _imp_is_frozen_package_impl(PyObject *module, PyObject *name)
   2045 /*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/
   2046 {
   2047     return is_frozen_package(name);
   2048 }
   2049 
   2050 /*[clinic input]
   2051 _imp.is_builtin
   2052 
   2053     name: unicode
   2054     /
   2055 
   2056 Returns True if the module name corresponds to a built-in module.
   2057 [clinic start generated code]*/
   2058 
   2059 static PyObject *
   2060 _imp_is_builtin_impl(PyObject *module, PyObject *name)
   2061 /*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/
   2062 {
   2063     return PyLong_FromLong(is_builtin(name));
   2064 }
   2065 
   2066 /*[clinic input]
   2067 _imp.is_frozen
   2068 
   2069     name: unicode
   2070     /
   2071 
   2072 Returns True if the module name corresponds to a frozen module.
   2073 [clinic start generated code]*/
   2074 
   2075 static PyObject *
   2076 _imp_is_frozen_impl(PyObject *module, PyObject *name)
   2077 /*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/
   2078 {
   2079     const struct _frozen *p;
   2080 
   2081     p = find_frozen(name);
   2082     return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
   2083 }
   2084 
   2085 /* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */
   2086 static int
   2087 exec_builtin_or_dynamic(PyObject *mod) {
   2088     PyModuleDef *def;
   2089     void *state;
   2090 
   2091     if (!PyModule_Check(mod)) {
   2092         return 0;
   2093     }
   2094 
   2095     def = PyModule_GetDef(mod);
   2096     if (def == NULL) {
   2097         return 0;
   2098     }
   2099 
   2100     state = PyModule_GetState(mod);
   2101     if (state) {
   2102         /* Already initialized; skip reload */
   2103         return 0;
   2104     }
   2105 
   2106     return PyModule_ExecDef(mod, def);
   2107 }
   2108 
   2109 #ifdef HAVE_DYNAMIC_LOADING
   2110 
   2111 /*[clinic input]
   2112 _imp.create_dynamic
   2113 
   2114     spec: object
   2115     file: object = NULL
   2116     /
   2117 
   2118 Create an extension module.
   2119 [clinic start generated code]*/
   2120 
   2121 static PyObject *
   2122 _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
   2123 /*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
   2124 {
   2125     PyObject *mod, *name, *path;
   2126     FILE *fp;
   2127 
   2128     name = PyObject_GetAttrString(spec, "name");
   2129     if (name == NULL) {
   2130         return NULL;
   2131     }
   2132 
   2133     path = PyObject_GetAttrString(spec, "origin");
   2134     if (path == NULL) {
   2135         Py_DECREF(name);
   2136         return NULL;
   2137     }
   2138 
   2139     mod = _PyImport_FindExtensionObject(name, path);
   2140     if (mod != NULL || PyErr_Occurred()) {
   2141         Py_DECREF(name);
   2142         Py_DECREF(path);
   2143         Py_XINCREF(mod);
   2144         return mod;
   2145     }
   2146 
   2147     if (file != NULL) {
   2148         fp = _Py_fopen_obj(path, "r");
   2149         if (fp == NULL) {
   2150             Py_DECREF(name);
   2151             Py_DECREF(path);
   2152             return NULL;
   2153         }
   2154     }
   2155     else
   2156         fp = NULL;
   2157 
   2158     mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp);
   2159 
   2160     Py_DECREF(name);
   2161     Py_DECREF(path);
   2162     if (fp)
   2163         fclose(fp);
   2164     return mod;
   2165 }
   2166 
   2167 /*[clinic input]
   2168 _imp.exec_dynamic -> int
   2169 
   2170     mod: object
   2171     /
   2172 
   2173 Initialize an extension module.
   2174 [clinic start generated code]*/
   2175 
   2176 static int
   2177 _imp_exec_dynamic_impl(PyObject *module, PyObject *mod)
   2178 /*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/
   2179 {
   2180     return exec_builtin_or_dynamic(mod);
   2181 }
   2182 
   2183 
   2184 #endif /* HAVE_DYNAMIC_LOADING */
   2185 
   2186 /*[clinic input]
   2187 _imp.exec_builtin -> int
   2188 
   2189     mod: object
   2190     /
   2191 
   2192 Initialize a built-in module.
   2193 [clinic start generated code]*/
   2194 
   2195 static int
   2196 _imp_exec_builtin_impl(PyObject *module, PyObject *mod)
   2197 /*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/
   2198 {
   2199     return exec_builtin_or_dynamic(mod);
   2200 }
   2201 
   2202 /*[clinic input]
   2203 _imp.source_hash
   2204 
   2205     key: long
   2206     source: Py_buffer
   2207 [clinic start generated code]*/
   2208 
   2209 static PyObject *
   2210 _imp_source_hash_impl(PyObject *module, long key, Py_buffer *source)
   2211 /*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/
   2212 {
   2213     union {
   2214         uint64_t x;
   2215         char data[sizeof(uint64_t)];
   2216     } hash;
   2217     hash.x = _Py_KeyedHash((uint64_t)key, source->buf, source->len);
   2218 #if !PY_LITTLE_ENDIAN
   2219     // Force to little-endian. There really ought to be a succinct standard way
   2220     // to do this.
   2221     for (size_t i = 0; i < sizeof(hash.data)/2; i++) {
   2222         char tmp = hash.data[i];
   2223         hash.data[i] = hash.data[sizeof(hash.data) - i - 1];
   2224         hash.data[sizeof(hash.data) - i - 1] = tmp;
   2225     }
   2226 #endif
   2227     return PyBytes_FromStringAndSize(hash.data, sizeof(hash.data));
   2228 }
   2229 
   2230 
   2231 PyDoc_STRVAR(doc_imp,
   2232 "(Extremely) low-level import machinery bits as used by importlib and imp.");
   2233 
   2234 static PyMethodDef imp_methods[] = {
   2235     _IMP_EXTENSION_SUFFIXES_METHODDEF
   2236     _IMP_LOCK_HELD_METHODDEF
   2237     _IMP_ACQUIRE_LOCK_METHODDEF
   2238     _IMP_RELEASE_LOCK_METHODDEF
   2239     _IMP_GET_FROZEN_OBJECT_METHODDEF
   2240     _IMP_IS_FROZEN_PACKAGE_METHODDEF
   2241     _IMP_CREATE_BUILTIN_METHODDEF
   2242     _IMP_INIT_FROZEN_METHODDEF
   2243     _IMP_IS_BUILTIN_METHODDEF
   2244     _IMP_IS_FROZEN_METHODDEF
   2245     _IMP_CREATE_DYNAMIC_METHODDEF
   2246     _IMP_EXEC_DYNAMIC_METHODDEF
   2247     _IMP_EXEC_BUILTIN_METHODDEF
   2248     _IMP__FIX_CO_FILENAME_METHODDEF
   2249     _IMP_SOURCE_HASH_METHODDEF
   2250     {NULL, NULL}  /* sentinel */
   2251 };
   2252 
   2253 
   2254 static struct PyModuleDef impmodule = {
   2255     PyModuleDef_HEAD_INIT,
   2256     "_imp",
   2257     doc_imp,
   2258     0,
   2259     imp_methods,
   2260     NULL,
   2261     NULL,
   2262     NULL,
   2263     NULL
   2264 };
   2265 
   2266 const char *_Py_CheckHashBasedPycsMode = "default";
   2267 
   2268 PyMODINIT_FUNC
   2269 PyInit__imp(void)
   2270 {
   2271     PyObject *m, *d;
   2272 
   2273     m = PyModule_Create(&impmodule);
   2274     if (m == NULL)
   2275         goto failure;
   2276     d = PyModule_GetDict(m);
   2277     if (d == NULL)
   2278         goto failure;
   2279     PyObject *pyc_mode = PyUnicode_FromString(_Py_CheckHashBasedPycsMode);
   2280     if (pyc_mode == NULL) {
   2281         goto failure;
   2282     }
   2283     if (PyDict_SetItemString(d, "check_hash_based_pycs", pyc_mode) < 0) {
   2284         Py_DECREF(pyc_mode);
   2285         goto failure;
   2286     }
   2287     Py_DECREF(pyc_mode);
   2288 
   2289     return m;
   2290   failure:
   2291     Py_XDECREF(m);
   2292     return NULL;
   2293 }
   2294 
   2295 
   2296 /* API for embedding applications that want to add their own entries
   2297    to the table of built-in modules.  This should normally be called
   2298    *before* Py_Initialize().  When the table resize fails, -1 is
   2299    returned and the existing table is unchanged.
   2300 
   2301    After a similar function by Just van Rossum. */
   2302 
   2303 int
   2304 PyImport_ExtendInittab(struct _inittab *newtab)
   2305 {
   2306     struct _inittab *p;
   2307     size_t i, n;
   2308     int res = 0;
   2309 
   2310     /* Count the number of entries in both tables */
   2311     for (n = 0; newtab[n].name != NULL; n++)
   2312         ;
   2313     if (n == 0)
   2314         return 0; /* Nothing to do */
   2315     for (i = 0; PyImport_Inittab[i].name != NULL; i++)
   2316         ;
   2317 
   2318     /* Force default raw memory allocator to get a known allocator to be able
   2319        to release the memory in _PyImport_Fini2() */
   2320     PyMemAllocatorEx old_alloc;
   2321     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
   2322 
   2323     /* Allocate new memory for the combined table */
   2324     p = NULL;
   2325     if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) {
   2326         size_t size = sizeof(struct _inittab) * (i + n + 1);
   2327         p = PyMem_RawRealloc(inittab_copy, size);
   2328     }
   2329     if (p == NULL) {
   2330         res = -1;
   2331         goto done;
   2332     }
   2333 
   2334     /* Copy the tables into the new memory at the first call
   2335        to PyImport_ExtendInittab(). */
   2336     if (inittab_copy != PyImport_Inittab) {
   2337         memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
   2338     }
   2339     memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab));
   2340     PyImport_Inittab = inittab_copy = p;
   2341 
   2342 done:
   2343     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
   2344     return res;
   2345 }
   2346 
   2347 /* Shorthand to add a single entry given a name and a function */
   2348 
   2349 int
   2350 PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
   2351 {
   2352     struct _inittab newtab[2];
   2353 
   2354     memset(newtab, '\0', sizeof newtab);
   2355 
   2356     newtab[0].name = name;
   2357     newtab[0].initfunc = initfunc;
   2358 
   2359     return PyImport_ExtendInittab(newtab);
   2360 }
   2361 
   2362 #ifdef __cplusplus
   2363 }
   2364 #endif
   2365