import.c revision 1e53bbacedaed883104454693c29d1ad31f5029b
1
2/* Module definition and import implementation */
3
4#include "Python.h"
5
6#include "Python-ast.h"
7#undef Yield /* undefine macro conflicting with winbase.h */
8#include "errcode.h"
9#include "marshal.h"
10#include "code.h"
11#include "frameobject.h"
12#include "osdefs.h"
13#include "importdl.h"
14
15#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#define CACHEDIR "__pycache__"
23
24/* See _PyImport_FixupExtensionObject() below */
25static PyObject *extensions = NULL;
26
27/* Function from Parser/tokenizer.c */
28extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
29
30/* This table is defined in config.c: */
31extern struct _inittab _PyImport_Inittab[];
32
33struct _inittab *PyImport_Inittab = _PyImport_Inittab;
34
35static PyObject *initstr = NULL;
36
37/* Initialize things */
38
39void
40_PyImport_Init(void)
41{
42    initstr = PyUnicode_InternFromString("__init__");
43    if (initstr == NULL)
44        Py_FatalError("Can't initialize import variables");
45}
46
47void
48_PyImportHooks_Init(void)
49{
50    PyObject *v, *path_hooks = NULL;
51    int err = 0;
52
53    /* adding sys.path_hooks and sys.path_importer_cache */
54    v = PyList_New(0);
55    if (v == NULL)
56        goto error;
57    err = PySys_SetObject("meta_path", v);
58    Py_DECREF(v);
59    if (err)
60        goto error;
61    v = PyDict_New();
62    if (v == NULL)
63        goto error;
64    err = PySys_SetObject("path_importer_cache", v);
65    Py_DECREF(v);
66    if (err)
67        goto error;
68    path_hooks = PyList_New(0);
69    if (path_hooks == NULL)
70        goto error;
71    err = PySys_SetObject("path_hooks", path_hooks);
72    if (err) {
73  error:
74    PyErr_Print();
75    Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
76                  "or path_importer_cache failed");
77    }
78    Py_DECREF(path_hooks);
79}
80
81void
82_PyImportZip_Init(void)
83{
84    PyObject *path_hooks, *zimpimport;
85    int err = 0;
86
87    path_hooks = PySys_GetObject("path_hooks");
88    if (path_hooks == NULL) {
89        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
90        goto error;
91    }
92
93    if (Py_VerboseFlag)
94        PySys_WriteStderr("# installing zipimport hook\n");
95
96    zimpimport = PyImport_ImportModule("zipimport");
97    if (zimpimport == NULL) {
98        PyErr_Clear(); /* No zip import module -- okay */
99        if (Py_VerboseFlag)
100            PySys_WriteStderr("# can't import zipimport\n");
101    }
102    else {
103        _Py_IDENTIFIER(zipimporter);
104        PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
105                                                    &PyId_zipimporter);
106        Py_DECREF(zimpimport);
107        if (zipimporter == NULL) {
108            PyErr_Clear(); /* No zipimporter object -- okay */
109            if (Py_VerboseFlag)
110                PySys_WriteStderr(
111                    "# can't import zipimport.zipimporter\n");
112        }
113        else {
114            /* sys.path_hooks.insert(0, zipimporter) */
115            err = PyList_Insert(path_hooks, 0, zipimporter);
116            Py_DECREF(zipimporter);
117            if (err < 0) {
118                goto error;
119            }
120            if (Py_VerboseFlag)
121                PySys_WriteStderr(
122                    "# installed zipimport hook\n");
123        }
124    }
125
126    return;
127
128  error:
129    PyErr_Print();
130    Py_FatalError("initializing zipimport failed");
131}
132
133/* Locking primitives to prevent parallel imports of the same module
134   in different threads to return with a partially loaded module.
135   These calls are serialized by the global interpreter lock. */
136
137#ifdef WITH_THREAD
138
139#include "pythread.h"
140
141static PyThread_type_lock import_lock = 0;
142static long import_lock_thread = -1;
143static int import_lock_level = 0;
144
145void
146_PyImport_AcquireLock(void)
147{
148    long me = PyThread_get_thread_ident();
149    if (me == -1)
150        return; /* Too bad */
151    if (import_lock == NULL) {
152        import_lock = PyThread_allocate_lock();
153        if (import_lock == NULL)
154            return;  /* Nothing much we can do. */
155    }
156    if (import_lock_thread == me) {
157        import_lock_level++;
158        return;
159    }
160    if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
161    {
162        PyThreadState *tstate = PyEval_SaveThread();
163        PyThread_acquire_lock(import_lock, 1);
164        PyEval_RestoreThread(tstate);
165    }
166    assert(import_lock_level == 0);
167    import_lock_thread = me;
168    import_lock_level = 1;
169}
170
171int
172_PyImport_ReleaseLock(void)
173{
174    long me = PyThread_get_thread_ident();
175    if (me == -1 || import_lock == NULL)
176        return 0; /* Too bad */
177    if (import_lock_thread != me)
178        return -1;
179    import_lock_level--;
180    assert(import_lock_level >= 0);
181    if (import_lock_level == 0) {
182        import_lock_thread = -1;
183        PyThread_release_lock(import_lock);
184    }
185    return 1;
186}
187
188/* This function is called from PyOS_AfterFork to ensure that newly
189   created child processes do not share locks with the parent.
190   We now acquire the import lock around fork() calls but on some platforms
191   (Solaris 9 and earlier? see isue7242) that still left us with problems. */
192
193void
194_PyImport_ReInitLock(void)
195{
196    if (import_lock != NULL)
197        import_lock = PyThread_allocate_lock();
198    if (import_lock_level > 1) {
199        /* Forked as a side effect of import */
200        long me = PyThread_get_thread_ident();
201        /* The following could fail if the lock is already held, but forking as
202           a side-effect of an import is a) rare, b) nuts, and c) difficult to
203           do thanks to the lock only being held when doing individual module
204           locks per import. */
205        PyThread_acquire_lock(import_lock, NOWAIT_LOCK);
206        import_lock_thread = me;
207        import_lock_level--;
208    } else {
209        import_lock_thread = -1;
210        import_lock_level = 0;
211    }
212}
213
214#endif
215
216static PyObject *
217imp_lock_held(PyObject *self, PyObject *noargs)
218{
219#ifdef WITH_THREAD
220    return PyBool_FromLong(import_lock_thread != -1);
221#else
222    return PyBool_FromLong(0);
223#endif
224}
225
226static PyObject *
227imp_acquire_lock(PyObject *self, PyObject *noargs)
228{
229#ifdef WITH_THREAD
230    _PyImport_AcquireLock();
231#endif
232    Py_INCREF(Py_None);
233    return Py_None;
234}
235
236static PyObject *
237imp_release_lock(PyObject *self, PyObject *noargs)
238{
239#ifdef WITH_THREAD
240    if (_PyImport_ReleaseLock() < 0) {
241        PyErr_SetString(PyExc_RuntimeError,
242                        "not holding the import lock");
243        return NULL;
244    }
245#endif
246    Py_INCREF(Py_None);
247    return Py_None;
248}
249
250void
251_PyImport_Fini(void)
252{
253    Py_XDECREF(extensions);
254    extensions = NULL;
255#ifdef WITH_THREAD
256    if (import_lock != NULL) {
257        PyThread_free_lock(import_lock);
258        import_lock = NULL;
259    }
260#endif
261}
262
263/* Helper for sys */
264
265PyObject *
266PyImport_GetModuleDict(void)
267{
268    PyInterpreterState *interp = PyThreadState_GET()->interp;
269    if (interp->modules == NULL)
270        Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
271    return interp->modules;
272}
273
274
275/* List of names to clear in sys */
276static char* sys_deletes[] = {
277    "path", "argv", "ps1", "ps2",
278    "last_type", "last_value", "last_traceback",
279    "path_hooks", "path_importer_cache", "meta_path",
280    /* misc stuff */
281    "flags", "float_info",
282    NULL
283};
284
285static char* sys_files[] = {
286    "stdin", "__stdin__",
287    "stdout", "__stdout__",
288    "stderr", "__stderr__",
289    NULL
290};
291
292static int
293is_essential_module(PyObject *name)
294{
295    Py_ssize_t name_len;
296    char *name_str = PyUnicode_AsUTF8AndSize(name, &name_len);
297
298    if (name_str == NULL) {
299        PyErr_Clear();
300        return 0;
301    }
302    if (strcmp(name_str, "builtins") == 0)
303        return 1;
304    if (strcmp(name_str, "sys") == 0)
305        return 1;
306    /* These are all needed for stderr to still function */
307    if (strcmp(name_str, "codecs") == 0)
308        return 1;
309    if (strcmp(name_str, "_codecs") == 0)
310        return 1;
311    if (strncmp(name_str, "encodings.", 10) == 0)
312        return 1;
313    return 0;
314}
315
316
317/* Un-initialize things, as good as we can */
318
319void
320PyImport_Cleanup(void)
321{
322    Py_ssize_t pos, ndone;
323    PyObject *key, *value, *dict;
324    PyInterpreterState *interp = PyThreadState_GET()->interp;
325    PyObject *modules = interp->modules;
326
327    if (modules == NULL)
328        return; /* Already done */
329
330    /* Delete some special variables first.  These are common
331       places where user values hide and people complain when their
332       destructors fail.  Since the modules containing them are
333       deleted *last* of all, they would come too late in the normal
334       destruction order.  Sigh. */
335
336    value = PyDict_GetItemString(modules, "builtins");
337    if (value != NULL && PyModule_Check(value)) {
338        dict = PyModule_GetDict(value);
339        if (Py_VerboseFlag)
340            PySys_WriteStderr("# clear builtins._\n");
341        PyDict_SetItemString(dict, "_", Py_None);
342    }
343    value = PyDict_GetItemString(modules, "sys");
344    if (value != NULL && PyModule_Check(value)) {
345        char **p;
346        PyObject *v;
347        dict = PyModule_GetDict(value);
348        for (p = sys_deletes; *p != NULL; p++) {
349            if (Py_VerboseFlag)
350                PySys_WriteStderr("# clear sys.%s\n", *p);
351            PyDict_SetItemString(dict, *p, Py_None);
352        }
353        for (p = sys_files; *p != NULL; p+=2) {
354            if (Py_VerboseFlag)
355                PySys_WriteStderr("# restore sys.%s\n", *p);
356            v = PyDict_GetItemString(dict, *(p+1));
357            if (v == NULL)
358                v = Py_None;
359            PyDict_SetItemString(dict, *p, v);
360        }
361    }
362
363    /* First, delete __main__ */
364    value = PyDict_GetItemString(modules, "__main__");
365    if (value != NULL && PyModule_Check(value)) {
366        if (Py_VerboseFlag)
367            PySys_WriteStderr("# cleanup __main__\n");
368        _PyModule_Clear(value);
369        PyDict_SetItemString(modules, "__main__", Py_None);
370    }
371
372    /* The special treatment of "builtins" here is because even
373       when it's not referenced as a module, its dictionary is
374       referenced by almost every module's __builtins__.  Since
375       deleting a module clears its dictionary (even if there are
376       references left to it), we need to delete the "builtins"
377       module last.  Likewise, we don't delete sys until the very
378       end because it is implicitly referenced (e.g. by print).
379
380       Also note that we 'delete' modules by replacing their entry
381       in the modules dict with None, rather than really deleting
382       them; this avoids a rehash of the modules dictionary and
383       also marks them as "non existent" so they won't be
384       re-imported. */
385
386    /* Next, repeatedly delete modules with a reference count of
387       one (skipping builtins and sys) and delete them */
388    do {
389        ndone = 0;
390        pos = 0;
391        while (PyDict_Next(modules, &pos, &key, &value)) {
392            if (value->ob_refcnt != 1)
393                continue;
394            if (PyUnicode_Check(key) && PyModule_Check(value)) {
395                if (is_essential_module(key))
396                    continue;
397                if (Py_VerboseFlag)
398                    PySys_FormatStderr(
399                        "# cleanup[1] %U\n", key);
400                _PyModule_Clear(value);
401                PyDict_SetItem(modules, key, Py_None);
402                ndone++;
403            }
404        }
405    } while (ndone > 0);
406
407    /* Next, delete all modules (still skipping builtins and sys) */
408    pos = 0;
409    while (PyDict_Next(modules, &pos, &key, &value)) {
410        if (PyUnicode_Check(key) && PyModule_Check(value)) {
411            if (is_essential_module(key))
412                continue;
413            if (Py_VerboseFlag)
414                PySys_FormatStderr("# cleanup[2] %U\n", key);
415            _PyModule_Clear(value);
416            PyDict_SetItem(modules, key, Py_None);
417        }
418    }
419
420    /* Collect garbage remaining after deleting the modules. Mostly
421       reference cycles created by classes. */
422    PyGC_Collect();
423
424    /* Dump GC stats before it's too late, since it uses the warnings
425       machinery. */
426    _PyGC_DumpShutdownStats();
427
428    /* Next, delete all remaining modules */
429    pos = 0;
430    while (PyDict_Next(modules, &pos, &key, &value)) {
431        if (PyUnicode_Check(key) && PyModule_Check(value)) {
432            if (Py_VerboseFlag)
433                PySys_FormatStderr("# cleanup[3] %U\n", key);
434            _PyModule_Clear(value);
435            PyDict_SetItem(modules, key, Py_None);
436        }
437    }
438
439    /* Finally, clear and delete the modules directory */
440    PyDict_Clear(modules);
441    _PyGC_CollectNoFail();
442    interp->modules = NULL;
443    Py_DECREF(modules);
444}
445
446
447/* Helper for pythonrun.c -- return magic number and tag. */
448
449long
450PyImport_GetMagicNumber(void)
451{
452    long res;
453    PyInterpreterState *interp = PyThreadState_Get()->interp;
454    PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
455                                                 "_RAW_MAGIC_NUMBER");
456    if (pyc_magic == NULL)
457        return -1;
458    res = PyLong_AsLong(pyc_magic);
459    Py_DECREF(pyc_magic);
460    return res;
461}
462
463
464extern const char * _PySys_ImplCacheTag;
465
466const char *
467PyImport_GetMagicTag(void)
468{
469    return _PySys_ImplCacheTag;
470}
471
472
473/* Magic for extension modules (built-in as well as dynamically
474   loaded).  To prevent initializing an extension module more than
475   once, we keep a static dictionary 'extensions' keyed by the tuple
476   (module name, module name)  (for built-in modules) or by
477   (filename, module name) (for dynamically loaded modules), containing these
478   modules.  A copy of the module's dictionary is stored by calling
479   _PyImport_FixupExtensionObject() immediately after the module initialization
480   function succeeds.  A copy can be retrieved from there by calling
481   _PyImport_FindExtensionObject().
482
483   Modules which do support multiple initialization set their m_size
484   field to a non-negative number (indicating the size of the
485   module-specific state). They are still recorded in the extensions
486   dictionary, to avoid loading shared libraries twice.
487*/
488
489int
490_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
491                               PyObject *filename)
492{
493    PyObject *modules, *dict, *key;
494    struct PyModuleDef *def;
495    int res;
496    if (extensions == NULL) {
497        extensions = PyDict_New();
498        if (extensions == NULL)
499            return -1;
500    }
501    if (mod == NULL || !PyModule_Check(mod)) {
502        PyErr_BadInternalCall();
503        return -1;
504    }
505    def = PyModule_GetDef(mod);
506    if (!def) {
507        PyErr_BadInternalCall();
508        return -1;
509    }
510    modules = PyImport_GetModuleDict();
511    if (PyDict_SetItem(modules, name, mod) < 0)
512        return -1;
513    if (_PyState_AddModule(mod, def) < 0) {
514        PyDict_DelItem(modules, name);
515        return -1;
516    }
517    if (def->m_size == -1) {
518        if (def->m_base.m_copy) {
519            /* Somebody already imported the module,
520               likely under a different name.
521               XXX this should really not happen. */
522            Py_DECREF(def->m_base.m_copy);
523            def->m_base.m_copy = NULL;
524        }
525        dict = PyModule_GetDict(mod);
526        if (dict == NULL)
527            return -1;
528        def->m_base.m_copy = PyDict_Copy(dict);
529        if (def->m_base.m_copy == NULL)
530            return -1;
531    }
532    key = PyTuple_Pack(2, filename, name);
533    if (key == NULL)
534        return -1;
535    res = PyDict_SetItem(extensions, key, (PyObject *)def);
536    Py_DECREF(key);
537    if (res < 0)
538        return -1;
539    return 0;
540}
541
542int
543_PyImport_FixupBuiltin(PyObject *mod, char *name)
544{
545    int res;
546    PyObject *nameobj;
547    nameobj = PyUnicode_InternFromString(name);
548    if (nameobj == NULL)
549        return -1;
550    res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
551    Py_DECREF(nameobj);
552    return res;
553}
554
555PyObject *
556_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
557{
558    PyObject *mod, *mdict, *key;
559    PyModuleDef* def;
560    if (extensions == NULL)
561        return NULL;
562    key = PyTuple_Pack(2, filename, name);
563    if (key == NULL)
564        return NULL;
565    def = (PyModuleDef *)PyDict_GetItem(extensions, key);
566    Py_DECREF(key);
567    if (def == NULL)
568        return NULL;
569    if (def->m_size == -1) {
570        /* Module does not support repeated initialization */
571        if (def->m_base.m_copy == NULL)
572            return NULL;
573        mod = PyImport_AddModuleObject(name);
574        if (mod == NULL)
575            return NULL;
576        mdict = PyModule_GetDict(mod);
577        if (mdict == NULL)
578            return NULL;
579        if (PyDict_Update(mdict, def->m_base.m_copy))
580            return NULL;
581    }
582    else {
583        if (def->m_base.m_init == NULL)
584            return NULL;
585        mod = def->m_base.m_init();
586        if (mod == NULL)
587            return NULL;
588        PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
589        Py_DECREF(mod);
590    }
591    if (_PyState_AddModule(mod, def) < 0) {
592        PyDict_DelItem(PyImport_GetModuleDict(), name);
593        Py_DECREF(mod);
594        return NULL;
595    }
596    if (Py_VerboseFlag)
597        PySys_FormatStderr("import %U # previously loaded (%R)\n",
598                          name, filename);
599    return mod;
600
601}
602
603PyObject *
604_PyImport_FindBuiltin(const char *name)
605{
606    PyObject *res, *nameobj;
607    nameobj = PyUnicode_InternFromString(name);
608    if (nameobj == NULL)
609        return NULL;
610    res = _PyImport_FindExtensionObject(nameobj, nameobj);
611    Py_DECREF(nameobj);
612    return res;
613}
614
615/* Get the module object corresponding to a module name.
616   First check the modules dictionary if there's one there,
617   if not, create a new one and insert it in the modules dictionary.
618   Because the former action is most common, THIS DOES NOT RETURN A
619   'NEW' REFERENCE! */
620
621PyObject *
622PyImport_AddModuleObject(PyObject *name)
623{
624    PyObject *modules = PyImport_GetModuleDict();
625    PyObject *m;
626
627    if ((m = PyDict_GetItem(modules, name)) != NULL &&
628        PyModule_Check(m))
629        return m;
630    m = PyModule_NewObject(name);
631    if (m == NULL)
632        return NULL;
633    if (PyDict_SetItem(modules, name, m) != 0) {
634        Py_DECREF(m);
635        return NULL;
636    }
637    Py_DECREF(m); /* Yes, it still exists, in modules! */
638
639    return m;
640}
641
642PyObject *
643PyImport_AddModule(const char *name)
644{
645    PyObject *nameobj, *module;
646    nameobj = PyUnicode_FromString(name);
647    if (nameobj == NULL)
648        return NULL;
649    module = PyImport_AddModuleObject(nameobj);
650    Py_DECREF(nameobj);
651    return module;
652}
653
654
655/* Remove name from sys.modules, if it's there. */
656static void
657remove_module(PyObject *name)
658{
659    PyObject *modules = PyImport_GetModuleDict();
660    if (PyDict_GetItem(modules, name) == NULL)
661        return;
662    if (PyDict_DelItem(modules, name) < 0)
663        Py_FatalError("import:  deleting existing key in"
664                      "sys.modules failed");
665}
666
667
668/* Execute a code object in a module and return the module object
669 * WITH INCREMENTED REFERENCE COUNT.  If an error occurs, name is
670 * removed from sys.modules, to avoid leaving damaged module objects
671 * in sys.modules.  The caller may wish to restore the original
672 * module object (if any) in this case; PyImport_ReloadModule is an
673 * example.
674 *
675 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
676 * interface.  The other two exist primarily for backward compatibility.
677 */
678PyObject *
679PyImport_ExecCodeModule(char *name, PyObject *co)
680{
681    return PyImport_ExecCodeModuleWithPathnames(
682        name, co, (char *)NULL, (char *)NULL);
683}
684
685PyObject *
686PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
687{
688    return PyImport_ExecCodeModuleWithPathnames(
689        name, co, pathname, (char *)NULL);
690}
691
692PyObject *
693PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
694                                     char *cpathname)
695{
696    PyObject *m = NULL;
697    PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
698
699    nameobj = PyUnicode_FromString(name);
700    if (nameobj == NULL)
701        return NULL;
702
703    if (cpathname != NULL) {
704        cpathobj = PyUnicode_DecodeFSDefault(cpathname);
705        if (cpathobj == NULL)
706            goto error;
707    }
708    else
709        cpathobj = NULL;
710
711    if (pathname != NULL) {
712        pathobj = PyUnicode_DecodeFSDefault(pathname);
713        if (pathobj == NULL)
714            goto error;
715    }
716    else if (cpathobj != NULL) {
717        PyInterpreterState *interp = PyThreadState_GET()->interp;
718        _Py_IDENTIFIER(_get_sourcefile);
719
720        if (interp == NULL) {
721            Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
722                          "no interpreter!");
723        }
724
725        pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
726                                                &PyId__get_sourcefile, cpathobj,
727                                                NULL);
728        if (pathobj == NULL)
729            PyErr_Clear();
730    }
731    else
732        pathobj = NULL;
733
734    m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
735error:
736    Py_DECREF(nameobj);
737    Py_XDECREF(pathobj);
738    Py_XDECREF(cpathobj);
739    return m;
740}
741
742PyObject*
743PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
744                              PyObject *cpathname)
745{
746    PyObject *modules = PyImport_GetModuleDict();
747    PyObject *m, *d, *v;
748
749    m = PyImport_AddModuleObject(name);
750    if (m == NULL)
751        return NULL;
752    /* If the module is being reloaded, we get the old module back
753       and re-use its dict to exec the new code. */
754    d = PyModule_GetDict(m);
755    if (PyDict_GetItemString(d, "__builtins__") == NULL) {
756        if (PyDict_SetItemString(d, "__builtins__",
757                                 PyEval_GetBuiltins()) != 0)
758            goto error;
759    }
760    if (pathname != NULL) {
761        v = pathname;
762    }
763    else {
764        v = ((PyCodeObject *)co)->co_filename;
765    }
766    Py_INCREF(v);
767    if (PyDict_SetItemString(d, "__file__", v) != 0)
768        PyErr_Clear(); /* Not important enough to report */
769    Py_DECREF(v);
770
771    /* Remember the pyc path name as the __cached__ attribute. */
772    if (cpathname != NULL)
773        v = cpathname;
774    else
775        v = Py_None;
776    if (PyDict_SetItemString(d, "__cached__", v) != 0)
777        PyErr_Clear(); /* Not important enough to report */
778
779    v = PyEval_EvalCode(co, d, d);
780    if (v == NULL)
781        goto error;
782    Py_DECREF(v);
783
784    if ((m = PyDict_GetItem(modules, name)) == NULL) {
785        PyErr_Format(PyExc_ImportError,
786                     "Loaded module %R not found in sys.modules",
787                     name);
788        return NULL;
789    }
790
791    Py_INCREF(m);
792
793    return m;
794
795  error:
796    remove_module(name);
797    return NULL;
798}
799
800
801static void
802update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
803{
804    PyObject *constants, *tmp;
805    Py_ssize_t i, n;
806
807    if (PyUnicode_Compare(co->co_filename, oldname))
808        return;
809
810    tmp = co->co_filename;
811    co->co_filename = newname;
812    Py_INCREF(co->co_filename);
813    Py_DECREF(tmp);
814
815    constants = co->co_consts;
816    n = PyTuple_GET_SIZE(constants);
817    for (i = 0; i < n; i++) {
818        tmp = PyTuple_GET_ITEM(constants, i);
819        if (PyCode_Check(tmp))
820            update_code_filenames((PyCodeObject *)tmp,
821                                  oldname, newname);
822    }
823}
824
825static void
826update_compiled_module(PyCodeObject *co, PyObject *newname)
827{
828    PyObject *oldname;
829
830    if (PyUnicode_Compare(co->co_filename, newname) == 0)
831        return;
832
833    oldname = co->co_filename;
834    Py_INCREF(oldname);
835    update_code_filenames(co, oldname, newname);
836    Py_DECREF(oldname);
837}
838
839static PyObject *
840imp_fix_co_filename(PyObject *self, PyObject *args)
841{
842    PyObject *co;
843    PyObject *file_path;
844
845    if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
846        return NULL;
847
848    if (!PyCode_Check(co)) {
849        PyErr_SetString(PyExc_TypeError,
850                        "first argument must be a code object");
851        return NULL;
852    }
853
854    if (!PyUnicode_Check(file_path)) {
855        PyErr_SetString(PyExc_TypeError,
856                        "second argument must be a string");
857        return NULL;
858    }
859
860    update_compiled_module((PyCodeObject*)co, file_path);
861
862    Py_RETURN_NONE;
863}
864
865
866/* Forward */
867static const struct _frozen * find_frozen(PyObject *);
868
869
870/* Helper to test for built-in module */
871
872static int
873is_builtin(PyObject *name)
874{
875    int i, cmp;
876    for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
877        cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
878        if (cmp == 0) {
879            if (PyImport_Inittab[i].initfunc == NULL)
880                return -1;
881            else
882                return 1;
883        }
884    }
885    return 0;
886}
887
888
889/* Return an importer object for a sys.path/pkg.__path__ item 'p',
890   possibly by fetching it from the path_importer_cache dict. If it
891   wasn't yet cached, traverse path_hooks until a hook is found
892   that can handle the path item. Return None if no hook could;
893   this tells our caller it should fall back to the builtin
894   import mechanism. Cache the result in path_importer_cache.
895   Returns a borrowed reference. */
896
897static PyObject *
898get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
899                  PyObject *p)
900{
901    PyObject *importer;
902    Py_ssize_t j, nhooks;
903
904    /* These conditions are the caller's responsibility: */
905    assert(PyList_Check(path_hooks));
906    assert(PyDict_Check(path_importer_cache));
907
908    nhooks = PyList_Size(path_hooks);
909    if (nhooks < 0)
910        return NULL; /* Shouldn't happen */
911
912    importer = PyDict_GetItem(path_importer_cache, p);
913    if (importer != NULL)
914        return importer;
915
916    /* set path_importer_cache[p] to None to avoid recursion */
917    if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
918        return NULL;
919
920    for (j = 0; j < nhooks; j++) {
921        PyObject *hook = PyList_GetItem(path_hooks, j);
922        if (hook == NULL)
923            return NULL;
924        importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
925        if (importer != NULL)
926            break;
927
928        if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
929            return NULL;
930        }
931        PyErr_Clear();
932    }
933    if (importer == NULL) {
934        return Py_None;
935    }
936    if (importer != NULL) {
937        int err = PyDict_SetItem(path_importer_cache, p, importer);
938        Py_DECREF(importer);
939        if (err != 0)
940            return NULL;
941    }
942    return importer;
943}
944
945PyAPI_FUNC(PyObject *)
946PyImport_GetImporter(PyObject *path) {
947    PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
948
949    path_importer_cache = PySys_GetObject("path_importer_cache");
950    path_hooks = PySys_GetObject("path_hooks");
951    if (path_importer_cache != NULL && path_hooks != NULL) {
952        importer = get_path_importer(path_importer_cache,
953                                     path_hooks, path);
954    }
955    Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
956    return importer;
957}
958
959
960static int init_builtin(PyObject *); /* Forward */
961
962/* Initialize a built-in module.
963   Return 1 for success, 0 if the module is not found, and -1 with
964   an exception set if the initialization failed. */
965
966static int
967init_builtin(PyObject *name)
968{
969    struct _inittab *p;
970
971    if (_PyImport_FindExtensionObject(name, name) != NULL)
972        return 1;
973
974    for (p = PyImport_Inittab; p->name != NULL; p++) {
975        PyObject *mod;
976        PyModuleDef *def;
977        if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
978            if (p->initfunc == NULL) {
979                PyErr_Format(PyExc_ImportError,
980                    "Cannot re-init internal module %R",
981                    name);
982                return -1;
983            }
984            mod = (*p->initfunc)();
985            if (mod == 0)
986                return -1;
987            /* Remember pointer to module init function. */
988            def = PyModule_GetDef(mod);
989            def->m_base.m_init = p->initfunc;
990            if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
991                return -1;
992            /* FixupExtension has put the module into sys.modules,
993               so we can release our own reference. */
994            Py_DECREF(mod);
995            return 1;
996        }
997    }
998    return 0;
999}
1000
1001
1002/* Frozen modules */
1003
1004static const struct _frozen *
1005find_frozen(PyObject *name)
1006{
1007    const struct _frozen *p;
1008
1009    if (name == NULL)
1010        return NULL;
1011
1012    for (p = PyImport_FrozenModules; ; p++) {
1013        if (p->name == NULL)
1014            return NULL;
1015        if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
1016            break;
1017    }
1018    return p;
1019}
1020
1021static PyObject *
1022get_frozen_object(PyObject *name)
1023{
1024    const struct _frozen *p = find_frozen(name);
1025    int size;
1026
1027    if (p == NULL) {
1028        PyErr_Format(PyExc_ImportError,
1029                     "No such frozen object named %R",
1030                     name);
1031        return NULL;
1032    }
1033    if (p->code == NULL) {
1034        PyErr_Format(PyExc_ImportError,
1035                     "Excluded frozen object named %R",
1036                     name);
1037        return NULL;
1038    }
1039    size = p->size;
1040    if (size < 0)
1041        size = -size;
1042    return PyMarshal_ReadObjectFromString((char *)p->code, size);
1043}
1044
1045static PyObject *
1046is_frozen_package(PyObject *name)
1047{
1048    const struct _frozen *p = find_frozen(name);
1049    int size;
1050
1051    if (p == NULL) {
1052        PyErr_Format(PyExc_ImportError,
1053                     "No such frozen object named %R",
1054                     name);
1055        return NULL;
1056    }
1057
1058    size = p->size;
1059
1060    if (size < 0)
1061        Py_RETURN_TRUE;
1062    else
1063        Py_RETURN_FALSE;
1064}
1065
1066
1067/* Initialize a frozen module.
1068   Return 1 for success, 0 if the module is not found, and -1 with
1069   an exception set if the initialization failed.
1070   This function is also used from frozenmain.c */
1071
1072int
1073PyImport_ImportFrozenModuleObject(PyObject *name)
1074{
1075    const struct _frozen *p;
1076    PyObject *co, *m, *path;
1077    int ispackage;
1078    int size;
1079
1080    p = find_frozen(name);
1081
1082    if (p == NULL)
1083        return 0;
1084    if (p->code == NULL) {
1085        PyErr_Format(PyExc_ImportError,
1086                     "Excluded frozen object named %R",
1087                     name);
1088        return -1;
1089    }
1090    size = p->size;
1091    ispackage = (size < 0);
1092    if (ispackage)
1093        size = -size;
1094    co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1095    if (co == NULL)
1096        return -1;
1097    if (!PyCode_Check(co)) {
1098        PyErr_Format(PyExc_TypeError,
1099                     "frozen object %R is not a code object",
1100                     name);
1101        goto err_return;
1102    }
1103    if (ispackage) {
1104        /* Set __path__ to the empty list */
1105        PyObject *d, *l;
1106        int err;
1107        m = PyImport_AddModuleObject(name);
1108        if (m == NULL)
1109            goto err_return;
1110        d = PyModule_GetDict(m);
1111        l = PyList_New(0);
1112        if (l == NULL) {
1113            goto err_return;
1114        }
1115        err = PyDict_SetItemString(d, "__path__", l);
1116        Py_DECREF(l);
1117        if (err != 0)
1118            goto err_return;
1119    }
1120    path = PyUnicode_FromString("<frozen>");
1121    if (path == NULL)
1122        goto err_return;
1123    m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1124    Py_DECREF(path);
1125    if (m == NULL)
1126        goto err_return;
1127    Py_DECREF(co);
1128    Py_DECREF(m);
1129    return 1;
1130err_return:
1131    Py_DECREF(co);
1132    return -1;
1133}
1134
1135int
1136PyImport_ImportFrozenModule(char *name)
1137{
1138    PyObject *nameobj;
1139    int ret;
1140    nameobj = PyUnicode_InternFromString(name);
1141    if (nameobj == NULL)
1142        return -1;
1143    ret = PyImport_ImportFrozenModuleObject(nameobj);
1144    Py_DECREF(nameobj);
1145    return ret;
1146}
1147
1148
1149/* Import a module, either built-in, frozen, or external, and return
1150   its module object WITH INCREMENTED REFERENCE COUNT */
1151
1152PyObject *
1153PyImport_ImportModule(const char *name)
1154{
1155    PyObject *pname;
1156    PyObject *result;
1157
1158    pname = PyUnicode_FromString(name);
1159    if (pname == NULL)
1160        return NULL;
1161    result = PyImport_Import(pname);
1162    Py_DECREF(pname);
1163    return result;
1164}
1165
1166/* Import a module without blocking
1167 *
1168 * At first it tries to fetch the module from sys.modules. If the module was
1169 * never loaded before it loads it with PyImport_ImportModule() unless another
1170 * thread holds the import lock. In the latter case the function raises an
1171 * ImportError instead of blocking.
1172 *
1173 * Returns the module object with incremented ref count.
1174 */
1175PyObject *
1176PyImport_ImportModuleNoBlock(const char *name)
1177{
1178    return PyImport_ImportModule(name);
1179}
1180
1181
1182/* Remove importlib frames from the traceback,
1183 * except in Verbose mode. */
1184static void
1185remove_importlib_frames(void)
1186{
1187    const char *importlib_filename = "<frozen importlib._bootstrap>";
1188    const char *remove_frames = "_call_with_frames_removed";
1189    int always_trim = 0;
1190    int in_importlib = 0;
1191    PyObject *exception, *value, *base_tb, *tb;
1192    PyObject **prev_link, **outer_link = NULL;
1193
1194    /* Synopsis: if it's an ImportError, we trim all importlib chunks
1195       from the traceback. We always trim chunks
1196       which end with a call to "_call_with_frames_removed". */
1197
1198    PyErr_Fetch(&exception, &value, &base_tb);
1199    if (!exception || Py_VerboseFlag)
1200        goto done;
1201    if (PyType_IsSubtype((PyTypeObject *) exception,
1202                         (PyTypeObject *) PyExc_ImportError))
1203        always_trim = 1;
1204
1205    prev_link = &base_tb;
1206    tb = base_tb;
1207    while (tb != NULL) {
1208        PyTracebackObject *traceback = (PyTracebackObject *)tb;
1209        PyObject *next = (PyObject *) traceback->tb_next;
1210        PyFrameObject *frame = traceback->tb_frame;
1211        PyCodeObject *code = frame->f_code;
1212        int now_in_importlib;
1213
1214        assert(PyTraceBack_Check(tb));
1215        now_in_importlib = (PyUnicode_CompareWithASCIIString(
1216                                code->co_filename,
1217                                importlib_filename) == 0);
1218        if (now_in_importlib && !in_importlib) {
1219            /* This is the link to this chunk of importlib tracebacks */
1220            outer_link = prev_link;
1221        }
1222        in_importlib = now_in_importlib;
1223
1224        if (in_importlib &&
1225            (always_trim ||
1226             PyUnicode_CompareWithASCIIString(code->co_name,
1227                                              remove_frames) == 0)) {
1228            PyObject *tmp = *outer_link;
1229            *outer_link = next;
1230            Py_XINCREF(next);
1231            Py_DECREF(tmp);
1232            prev_link = outer_link;
1233        }
1234        else {
1235            prev_link = (PyObject **) &traceback->tb_next;
1236        }
1237        tb = next;
1238    }
1239done:
1240    PyErr_Restore(exception, value, base_tb);
1241}
1242
1243
1244PyObject *
1245PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1246                                 PyObject *locals, PyObject *given_fromlist,
1247                                 int level)
1248{
1249    _Py_IDENTIFIER(__import__);
1250    _Py_IDENTIFIER(__initializing__);
1251    _Py_IDENTIFIER(__package__);
1252    _Py_IDENTIFIER(__path__);
1253    _Py_IDENTIFIER(__name__);
1254    _Py_IDENTIFIER(_find_and_load);
1255    _Py_IDENTIFIER(_handle_fromlist);
1256    _Py_IDENTIFIER(_lock_unlock_module);
1257    _Py_static_string(single_dot, ".");
1258    PyObject *abs_name = NULL;
1259    PyObject *builtins_import = NULL;
1260    PyObject *final_mod = NULL;
1261    PyObject *mod = NULL;
1262    PyObject *package = NULL;
1263    PyObject *globals = NULL;
1264    PyObject *fromlist = NULL;
1265    PyInterpreterState *interp = PyThreadState_GET()->interp;
1266
1267    /* Make sure to use default values so as to not have
1268       PyObject_CallMethodObjArgs() truncate the parameter list because of a
1269       NULL argument. */
1270    if (given_globals == NULL) {
1271        globals = PyDict_New();
1272        if (globals == NULL) {
1273            goto error;
1274        }
1275    }
1276    else {
1277        /* Only have to care what given_globals is if it will be used
1278           for something. */
1279        if (level > 0 && !PyDict_Check(given_globals)) {
1280            PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1281            goto error;
1282        }
1283        globals = given_globals;
1284        Py_INCREF(globals);
1285    }
1286
1287    if (given_fromlist == NULL) {
1288        fromlist = PyList_New(0);
1289        if (fromlist == NULL) {
1290            goto error;
1291        }
1292    }
1293    else {
1294        fromlist = given_fromlist;
1295        Py_INCREF(fromlist);
1296    }
1297    if (name == NULL) {
1298        PyErr_SetString(PyExc_ValueError, "Empty module name");
1299        goto error;
1300    }
1301
1302    /* The below code is importlib.__import__() & _gcd_import(), ported to C
1303       for added performance. */
1304
1305    if (!PyUnicode_Check(name)) {
1306        PyErr_SetString(PyExc_TypeError, "module name must be a string");
1307        goto error;
1308    }
1309    else if (PyUnicode_READY(name) < 0) {
1310        goto error;
1311    }
1312    if (level < 0) {
1313        PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1314        goto error;
1315    }
1316    else if (level > 0) {
1317        package = _PyDict_GetItemId(globals, &PyId___package__);
1318        if (package != NULL && package != Py_None) {
1319            Py_INCREF(package);
1320            if (!PyUnicode_Check(package)) {
1321                PyErr_SetString(PyExc_TypeError, "package must be a string");
1322                goto error;
1323            }
1324        }
1325        else {
1326            package = _PyDict_GetItemId(globals, &PyId___name__);
1327            if (package == NULL) {
1328                PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
1329                goto error;
1330            }
1331            else if (!PyUnicode_Check(package)) {
1332                PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1333            }
1334            Py_INCREF(package);
1335
1336            if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
1337                PyObject *partition = NULL;
1338                PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1339                if (borrowed_dot == NULL) {
1340                    goto error;
1341                }
1342                partition = PyUnicode_RPartition(package, borrowed_dot);
1343                Py_DECREF(package);
1344                if (partition == NULL) {
1345                    goto error;
1346                }
1347                package = PyTuple_GET_ITEM(partition, 0);
1348                Py_INCREF(package);
1349                Py_DECREF(partition);
1350            }
1351        }
1352
1353        if (PyDict_GetItem(interp->modules, package) == NULL) {
1354            PyErr_Format(PyExc_SystemError,
1355                    "Parent module %R not loaded, cannot perform relative "
1356                    "import", package);
1357            goto error;
1358        }
1359    }
1360    else {  /* level == 0 */
1361        if (PyUnicode_GET_LENGTH(name) == 0) {
1362            PyErr_SetString(PyExc_ValueError, "Empty module name");
1363            goto error;
1364        }
1365        package = Py_None;
1366        Py_INCREF(package);
1367    }
1368
1369    if (level > 0) {
1370        Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1371        PyObject *base = NULL;
1372        int level_up = 1;
1373
1374        for (level_up = 1; level_up < level; level_up += 1) {
1375            last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1376            if (last_dot == -2) {
1377                goto error;
1378            }
1379            else if (last_dot == -1) {
1380                PyErr_SetString(PyExc_ValueError,
1381                                "attempted relative import beyond top-level "
1382                                "package");
1383                goto error;
1384            }
1385        }
1386        base = PyUnicode_Substring(package, 0, last_dot);
1387        if (PyUnicode_GET_LENGTH(name) > 0) {
1388            PyObject *borrowed_dot, *seq = NULL;
1389
1390            borrowed_dot = _PyUnicode_FromId(&single_dot);
1391            seq = PyTuple_Pack(2, base, name);
1392            Py_DECREF(base);
1393            if (borrowed_dot == NULL || seq == NULL) {
1394                goto error;
1395            }
1396
1397            abs_name = PyUnicode_Join(borrowed_dot, seq);
1398            Py_DECREF(seq);
1399            if (abs_name == NULL) {
1400                goto error;
1401            }
1402        }
1403        else {
1404            abs_name = base;
1405        }
1406    }
1407    else {
1408        abs_name = name;
1409        Py_INCREF(abs_name);
1410    }
1411
1412#ifdef WITH_THREAD
1413    _PyImport_AcquireLock();
1414#endif
1415   /* From this point forward, goto error_with_unlock! */
1416    if (PyDict_Check(globals)) {
1417        builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1418    }
1419    if (builtins_import == NULL) {
1420        builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1421        if (builtins_import == NULL) {
1422            PyErr_SetString(PyExc_ImportError, "__import__ not found");
1423            goto error_with_unlock;
1424        }
1425    }
1426    Py_INCREF(builtins_import);
1427
1428    mod = PyDict_GetItem(interp->modules, abs_name);
1429    if (mod == Py_None) {
1430        PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1431                                             "None in sys.modules", abs_name);
1432        if (msg != NULL) {
1433            PyErr_SetImportError(msg, abs_name, NULL);
1434            Py_DECREF(msg);
1435        }
1436        mod = NULL;
1437        goto error_with_unlock;
1438    }
1439    else if (mod != NULL) {
1440        PyObject *value;
1441        int initializing = 0;
1442
1443        Py_INCREF(mod);
1444        /* Optimization: only call _bootstrap._lock_unlock_module() if
1445           __initializing__ is true.
1446           NOTE: because of this, __initializing__ must be set *before*
1447           stuffing the new module in sys.modules.
1448         */
1449        value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1450        if (value == NULL)
1451            PyErr_Clear();
1452        else {
1453            initializing = PyObject_IsTrue(value);
1454            Py_DECREF(value);
1455            if (initializing == -1)
1456                PyErr_Clear();
1457        }
1458        if (initializing > 0) {
1459            /* _bootstrap._lock_unlock_module() releases the import lock */
1460            value = _PyObject_CallMethodIdObjArgs(interp->importlib,
1461                                            &PyId__lock_unlock_module, abs_name,
1462                                            NULL);
1463            if (value == NULL)
1464                goto error;
1465            Py_DECREF(value);
1466        }
1467        else {
1468#ifdef WITH_THREAD
1469            if (_PyImport_ReleaseLock() < 0) {
1470                PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1471                goto error;
1472            }
1473#endif
1474        }
1475    }
1476    else {
1477        /* _bootstrap._find_and_load() releases the import lock */
1478        mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
1479                                            &PyId__find_and_load, abs_name,
1480                                            builtins_import, NULL);
1481        if (mod == NULL) {
1482            goto error;
1483        }
1484    }
1485    /* From now on we don't hold the import lock anymore. */
1486
1487    if (PyObject_Not(fromlist)) {
1488        if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1489            PyObject *front = NULL;
1490            PyObject *partition = NULL;
1491            PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1492
1493            if (borrowed_dot == NULL) {
1494                goto error;
1495            }
1496
1497            partition = PyUnicode_Partition(name, borrowed_dot);
1498            if (partition == NULL) {
1499                goto error;
1500            }
1501
1502            if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1503                /* No dot in module name, simple exit */
1504                Py_DECREF(partition);
1505                final_mod = mod;
1506                Py_INCREF(mod);
1507                goto error;
1508            }
1509
1510            front = PyTuple_GET_ITEM(partition, 0);
1511            Py_INCREF(front);
1512            Py_DECREF(partition);
1513
1514            if (level == 0) {
1515                final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
1516                Py_DECREF(front);
1517            }
1518            else {
1519                Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1520                                        PyUnicode_GET_LENGTH(front);
1521                Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
1522                PyObject *to_return = PyUnicode_Substring(abs_name, 0,
1523                                                        abs_name_len - cut_off);
1524                Py_DECREF(front);
1525                if (to_return == NULL) {
1526                    goto error;
1527                }
1528
1529                final_mod = PyDict_GetItem(interp->modules, to_return);
1530                if (final_mod == NULL) {
1531                    PyErr_Format(PyExc_KeyError,
1532                                 "%R not in sys.modules as expected",
1533                                 to_return);
1534                }
1535                else {
1536                    Py_INCREF(final_mod);
1537                }
1538                Py_DECREF(to_return);
1539            }
1540        }
1541        else {
1542            final_mod = mod;
1543            Py_INCREF(mod);
1544        }
1545    }
1546    else {
1547        final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
1548                                                  &PyId__handle_fromlist, mod,
1549                                                  fromlist, builtins_import,
1550                                                  NULL);
1551    }
1552    goto error;
1553
1554  error_with_unlock:
1555#ifdef WITH_THREAD
1556    if (_PyImport_ReleaseLock() < 0) {
1557        PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1558    }
1559#endif
1560  error:
1561    Py_XDECREF(abs_name);
1562    Py_XDECREF(builtins_import);
1563    Py_XDECREF(mod);
1564    Py_XDECREF(package);
1565    Py_XDECREF(globals);
1566    Py_XDECREF(fromlist);
1567    if (final_mod == NULL)
1568        remove_importlib_frames();
1569    return final_mod;
1570}
1571
1572PyObject *
1573PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
1574                           PyObject *fromlist, int level)
1575{
1576    PyObject *nameobj, *mod;
1577    nameobj = PyUnicode_FromString(name);
1578    if (nameobj == NULL)
1579        return NULL;
1580    mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1581                                           fromlist, level);
1582    Py_DECREF(nameobj);
1583    return mod;
1584}
1585
1586
1587/* Re-import a module of any kind and return its module object, WITH
1588   INCREMENTED REFERENCE COUNT */
1589
1590PyObject *
1591PyImport_ReloadModule(PyObject *m)
1592{
1593    _Py_IDENTIFIER(reload);
1594    PyObject *reloaded_module = NULL;
1595    PyObject *modules = PyImport_GetModuleDict();
1596    PyObject *imp = PyDict_GetItemString(modules, "imp");
1597    if (imp == NULL) {
1598        imp = PyImport_ImportModule("imp");
1599        if (imp == NULL) {
1600            return NULL;
1601        }
1602    }
1603    else {
1604        Py_INCREF(imp);
1605    }
1606
1607    reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1608    Py_DECREF(imp);
1609    return reloaded_module;
1610}
1611
1612
1613/* Higher-level import emulator which emulates the "import" statement
1614   more accurately -- it invokes the __import__() function from the
1615   builtins of the current globals.  This means that the import is
1616   done using whatever import hooks are installed in the current
1617   environment.
1618   A dummy list ["__doc__"] is passed as the 4th argument so that
1619   e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
1620   will return <module "gencache"> instead of <module "win32com">. */
1621
1622PyObject *
1623PyImport_Import(PyObject *module_name)
1624{
1625    static PyObject *silly_list = NULL;
1626    static PyObject *builtins_str = NULL;
1627    static PyObject *import_str = NULL;
1628    PyObject *globals = NULL;
1629    PyObject *import = NULL;
1630    PyObject *builtins = NULL;
1631    PyObject *modules = NULL;
1632    PyObject *r = NULL;
1633
1634    /* Initialize constant string objects */
1635    if (silly_list == NULL) {
1636        import_str = PyUnicode_InternFromString("__import__");
1637        if (import_str == NULL)
1638            return NULL;
1639        builtins_str = PyUnicode_InternFromString("__builtins__");
1640        if (builtins_str == NULL)
1641            return NULL;
1642        silly_list = PyList_New(0);
1643        if (silly_list == NULL)
1644            return NULL;
1645    }
1646
1647    /* Get the builtins from current globals */
1648    globals = PyEval_GetGlobals();
1649    if (globals != NULL) {
1650        Py_INCREF(globals);
1651        builtins = PyObject_GetItem(globals, builtins_str);
1652        if (builtins == NULL)
1653            goto err;
1654    }
1655    else {
1656        /* No globals -- use standard builtins, and fake globals */
1657        builtins = PyImport_ImportModuleLevel("builtins",
1658                                              NULL, NULL, NULL, 0);
1659        if (builtins == NULL)
1660            return NULL;
1661        globals = Py_BuildValue("{OO}", builtins_str, builtins);
1662        if (globals == NULL)
1663            goto err;
1664    }
1665
1666    /* Get the __import__ function from the builtins */
1667    if (PyDict_Check(builtins)) {
1668        import = PyObject_GetItem(builtins, import_str);
1669        if (import == NULL)
1670            PyErr_SetObject(PyExc_KeyError, import_str);
1671    }
1672    else
1673        import = PyObject_GetAttr(builtins, import_str);
1674    if (import == NULL)
1675        goto err;
1676
1677    /* Call the __import__ function with the proper argument list
1678       Always use absolute import here.
1679       Calling for side-effect of import. */
1680    r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1681                              globals, silly_list, 0, NULL);
1682    if (r == NULL)
1683        goto err;
1684    Py_DECREF(r);
1685
1686    modules = PyImport_GetModuleDict();
1687    r = PyDict_GetItem(modules, module_name);
1688    if (r != NULL)
1689        Py_INCREF(r);
1690
1691  err:
1692    Py_XDECREF(globals);
1693    Py_XDECREF(builtins);
1694    Py_XDECREF(import);
1695
1696    return r;
1697}
1698
1699static PyObject *
1700imp_extension_suffixes(PyObject *self, PyObject *noargs)
1701{
1702    PyObject *list;
1703    const char *suffix;
1704    unsigned int index = 0;
1705
1706    list = PyList_New(0);
1707    if (list == NULL)
1708        return NULL;
1709#ifdef HAVE_DYNAMIC_LOADING
1710    while ((suffix = _PyImport_DynLoadFiletab[index])) {
1711        PyObject *item = PyUnicode_FromString(suffix);
1712        if (item == NULL) {
1713            Py_DECREF(list);
1714            return NULL;
1715        }
1716        if (PyList_Append(list, item) < 0) {
1717            Py_DECREF(list);
1718            Py_DECREF(item);
1719            return NULL;
1720        }
1721        Py_DECREF(item);
1722        index += 1;
1723    }
1724#endif
1725    return list;
1726}
1727
1728static PyObject *
1729imp_init_builtin(PyObject *self, PyObject *args)
1730{
1731    PyObject *name;
1732    int ret;
1733    PyObject *m;
1734    if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
1735        return NULL;
1736    ret = init_builtin(name);
1737    if (ret < 0)
1738        return NULL;
1739    if (ret == 0) {
1740        Py_INCREF(Py_None);
1741        return Py_None;
1742    }
1743    m = PyImport_AddModuleObject(name);
1744    Py_XINCREF(m);
1745    return m;
1746}
1747
1748static PyObject *
1749imp_init_frozen(PyObject *self, PyObject *args)
1750{
1751    PyObject *name;
1752    int ret;
1753    PyObject *m;
1754    if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
1755        return NULL;
1756    ret = PyImport_ImportFrozenModuleObject(name);
1757    if (ret < 0)
1758        return NULL;
1759    if (ret == 0) {
1760        Py_INCREF(Py_None);
1761        return Py_None;
1762    }
1763    m = PyImport_AddModuleObject(name);
1764    Py_XINCREF(m);
1765    return m;
1766}
1767
1768static PyObject *
1769imp_get_frozen_object(PyObject *self, PyObject *args)
1770{
1771    PyObject *name;
1772
1773    if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
1774        return NULL;
1775    return get_frozen_object(name);
1776}
1777
1778static PyObject *
1779imp_is_frozen_package(PyObject *self, PyObject *args)
1780{
1781    PyObject *name;
1782
1783    if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
1784        return NULL;
1785    return is_frozen_package(name);
1786}
1787
1788static PyObject *
1789imp_is_builtin(PyObject *self, PyObject *args)
1790{
1791    PyObject *name;
1792    if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
1793        return NULL;
1794    return PyLong_FromLong(is_builtin(name));
1795}
1796
1797static PyObject *
1798imp_is_frozen(PyObject *self, PyObject *args)
1799{
1800    PyObject *name;
1801    const struct _frozen *p;
1802    if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
1803        return NULL;
1804    p = find_frozen(name);
1805    return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
1806}
1807
1808#ifdef HAVE_DYNAMIC_LOADING
1809
1810static PyObject *
1811imp_load_dynamic(PyObject *self, PyObject *args)
1812{
1813    PyObject *name, *pathname, *fob = NULL, *mod;
1814    FILE *fp;
1815
1816    if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1817                          &name, PyUnicode_FSDecoder, &pathname, &fob))
1818        return NULL;
1819    if (fob != NULL) {
1820        fp = _Py_fopen(pathname, "r");
1821        if (fp == NULL) {
1822            Py_DECREF(pathname);
1823            if (!PyErr_Occurred())
1824                PyErr_SetFromErrno(PyExc_IOError);
1825            return NULL;
1826        }
1827    }
1828    else
1829        fp = NULL;
1830    mod = _PyImport_LoadDynamicModule(name, pathname, fp);
1831    Py_DECREF(pathname);
1832    if (fp)
1833        fclose(fp);
1834    return mod;
1835}
1836
1837#endif /* HAVE_DYNAMIC_LOADING */
1838
1839
1840/* Doc strings */
1841
1842PyDoc_STRVAR(doc_imp,
1843"(Extremely) low-level import machinery bits as used by importlib and imp.");
1844
1845PyDoc_STRVAR(doc_extension_suffixes,
1846"extension_suffixes() -> list of strings\n\
1847Returns the list of file suffixes used to identify extension modules.");
1848
1849PyDoc_STRVAR(doc_lock_held,
1850"lock_held() -> boolean\n\
1851Return True if the import lock is currently held, else False.\n\
1852On platforms without threads, return False.");
1853
1854PyDoc_STRVAR(doc_acquire_lock,
1855"acquire_lock() -> None\n\
1856Acquires the interpreter's import lock for the current thread.\n\
1857This lock should be used by import hooks to ensure thread-safety\n\
1858when importing modules.\n\
1859On platforms without threads, this function does nothing.");
1860
1861PyDoc_STRVAR(doc_release_lock,
1862"release_lock() -> None\n\
1863Release the interpreter's import lock.\n\
1864On platforms without threads, this function does nothing.");
1865
1866static PyMethodDef imp_methods[] = {
1867    {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1868        doc_extension_suffixes},
1869    {"lock_held",        imp_lock_held,    METH_NOARGS,  doc_lock_held},
1870    {"acquire_lock", imp_acquire_lock, METH_NOARGS,  doc_acquire_lock},
1871    {"release_lock", imp_release_lock, METH_NOARGS,  doc_release_lock},
1872    {"get_frozen_object",       imp_get_frozen_object,  METH_VARARGS},
1873    {"is_frozen_package",   imp_is_frozen_package,  METH_VARARGS},
1874    {"init_builtin",            imp_init_builtin,       METH_VARARGS},
1875    {"init_frozen",             imp_init_frozen,        METH_VARARGS},
1876    {"is_builtin",              imp_is_builtin,         METH_VARARGS},
1877    {"is_frozen",               imp_is_frozen,          METH_VARARGS},
1878#ifdef HAVE_DYNAMIC_LOADING
1879    {"load_dynamic",            imp_load_dynamic,       METH_VARARGS},
1880#endif
1881    {"_fix_co_filename",        imp_fix_co_filename,    METH_VARARGS},
1882    {NULL,                      NULL}           /* sentinel */
1883};
1884
1885
1886static struct PyModuleDef impmodule = {
1887    PyModuleDef_HEAD_INIT,
1888    "_imp",
1889    doc_imp,
1890    0,
1891    imp_methods,
1892    NULL,
1893    NULL,
1894    NULL,
1895    NULL
1896};
1897
1898PyMODINIT_FUNC
1899PyInit_imp(void)
1900{
1901    PyObject *m, *d;
1902
1903    m = PyModule_Create(&impmodule);
1904    if (m == NULL)
1905        goto failure;
1906    d = PyModule_GetDict(m);
1907    if (d == NULL)
1908        goto failure;
1909
1910    return m;
1911  failure:
1912    Py_XDECREF(m);
1913    return NULL;
1914}
1915
1916
1917/* API for embedding applications that want to add their own entries
1918   to the table of built-in modules.  This should normally be called
1919   *before* Py_Initialize().  When the table resize fails, -1 is
1920   returned and the existing table is unchanged.
1921
1922   After a similar function by Just van Rossum. */
1923
1924int
1925PyImport_ExtendInittab(struct _inittab *newtab)
1926{
1927    static struct _inittab *our_copy = NULL;
1928    struct _inittab *p;
1929    int i, n;
1930
1931    /* Count the number of entries in both tables */
1932    for (n = 0; newtab[n].name != NULL; n++)
1933        ;
1934    if (n == 0)
1935        return 0; /* Nothing to do */
1936    for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1937        ;
1938
1939    /* Allocate new memory for the combined table */
1940    p = our_copy;
1941    PyMem_RESIZE(p, struct _inittab, i+n+1);
1942    if (p == NULL)
1943        return -1;
1944
1945    /* Copy the tables into the new memory */
1946    if (our_copy != PyImport_Inittab)
1947        memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1948    PyImport_Inittab = our_copy = p;
1949    memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
1950
1951    return 0;
1952}
1953
1954/* Shorthand to add a single entry given a name and a function */
1955
1956int
1957PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
1958{
1959    struct _inittab newtab[2];
1960
1961    memset(newtab, '\0', sizeof newtab);
1962
1963    newtab[0].name = (char *)name;
1964    newtab[0].initfunc = initfunc;
1965
1966    return PyImport_ExtendInittab(newtab);
1967}
1968
1969#ifdef __cplusplus
1970}
1971#endif
1972