import.c revision fadcd317fa3df387a009afc4137e2b14ed8905e4
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 "osdefs.h"
12#include "importdl.h"
13
14#ifdef HAVE_FCNTL_H
15#include <fcntl.h>
16#endif
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#ifdef MS_WINDOWS
22/* for stat.st_mode */
23typedef unsigned short mode_t;
24/* for _mkdir */
25#include <direct.h>
26#endif
27
28
29/* Magic word to reject .pyc files generated by other Python versions.
30   It should change for each incompatible change to the bytecode.
31
32   The value of CR and LF is incorporated so if you ever read or write
33   a .pyc file in text mode the magic number will be wrong; also, the
34   Apple MPW compiler swaps their values, botching string constants.
35
36   The magic numbers must be spaced apart at least 2 values, as the
37   -U interpeter flag will cause MAGIC+1 being used. They have been
38   odd numbers for some time now.
39
40   There were a variety of old schemes for setting the magic number.
41   The current working scheme is to increment the previous value by
42   10.
43
44   Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic
45   number also includes a new "magic tag", i.e. a human readable string used
46   to represent the magic number in __pycache__ directories.  When you change
47   the magic number, you must also set a new unique magic tag.  Generally this
48   can be named after the Python major version of the magic number bump, but
49   it can really be anything, as long as it's different than anything else
50   that's come before.  The tags are included in the following table, starting
51   with Python 3.2a0.
52
53   Known values:
54       Python 1.5:   20121
55       Python 1.5.1: 20121
56       Python 1.5.2: 20121
57       Python 1.6:   50428
58       Python 2.0:   50823
59       Python 2.0.1: 50823
60       Python 2.1:   60202
61       Python 2.1.1: 60202
62       Python 2.1.2: 60202
63       Python 2.2:   60717
64       Python 2.3a0: 62011
65       Python 2.3a0: 62021
66       Python 2.3a0: 62011 (!)
67       Python 2.4a0: 62041
68       Python 2.4a3: 62051
69       Python 2.4b1: 62061
70       Python 2.5a0: 62071
71       Python 2.5a0: 62081 (ast-branch)
72       Python 2.5a0: 62091 (with)
73       Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
74       Python 2.5b3: 62101 (fix wrong code: for x, in ...)
75       Python 2.5b3: 62111 (fix wrong code: x += yield)
76       Python 2.5c1: 62121 (fix wrong lnotab with for loops and
77                            storing constants that should have been removed)
78       Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
79       Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
80       Python 2.6a1: 62161 (WITH_CLEANUP optimization)
81       Python 3000:   3000
82                      3010 (removed UNARY_CONVERT)
83                      3020 (added BUILD_SET)
84                      3030 (added keyword-only parameters)
85                      3040 (added signature annotations)
86                      3050 (print becomes a function)
87                      3060 (PEP 3115 metaclass syntax)
88                      3061 (string literals become unicode)
89                      3071 (PEP 3109 raise changes)
90                      3081 (PEP 3137 make __file__ and __name__ unicode)
91                      3091 (kill str8 interning)
92                      3101 (merge from 2.6a0, see 62151)
93                      3103 (__file__ points to source file)
94       Python 3.0a4: 3111 (WITH_CLEANUP optimization).
95       Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
96       Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
97               change LIST_APPEND and SET_ADD, add MAP_ADD)
98       Python 3.1a0: 3151 (optimize conditional branches:
99               introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
100       Python 3.2a0: 3160 (add SETUP_WITH)
101                     tag: cpython-32
102       Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR)
103                     tag: cpython-32
104       Python 3.2a2  3180 (add DELETE_DEREF)
105       Python 3.3a0  3190 __class__ super closure changed
106*/
107
108/* MAGIC must change whenever the bytecode emitted by the compiler may no
109   longer be understood by older implementations of the eval loop (usually
110   due to the addition of new opcodes)
111   TAG must change for each major Python release. The magic number will take
112   care of any bytecode changes that occur during development.
113*/
114#define QUOTE(arg) #arg
115#define STRIFY(name) QUOTE(name)
116#define MAJOR STRIFY(PY_MAJOR_VERSION)
117#define MINOR STRIFY(PY_MINOR_VERSION)
118#define MAGIC (3190 | ((long)'\r'<<16) | ((long)'\n'<<24))
119#define TAG "cpython-" MAJOR MINOR;
120#define CACHEDIR "__pycache__"
121/* Current magic word and string tag as globals. */
122static long pyc_magic = MAGIC;
123static const char *pyc_tag = TAG;
124#undef QUOTE
125#undef STRIFY
126#undef MAJOR
127#undef MINOR
128
129/* See _PyImport_FixupExtensionObject() below */
130static PyObject *extensions = NULL;
131
132/* Function from Parser/tokenizer.c */
133extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
134
135/* This table is defined in config.c: */
136extern struct _inittab _PyImport_Inittab[];
137
138struct _inittab *PyImport_Inittab = _PyImport_Inittab;
139
140/* these tables define the module suffixes that Python recognizes */
141struct filedescr * _PyImport_Filetab = NULL;
142
143static const struct filedescr _PyImport_StandardFiletab[] = {
144    {".py", "U", PY_SOURCE},
145#ifdef MS_WINDOWS
146    {".pyw", "U", PY_SOURCE},
147#endif
148    {".pyc", "rb", PY_COMPILED},
149    {0, 0}
150};
151
152static PyObject *initstr = NULL;
153_Py_IDENTIFIER(__path__);
154
155/* Initialize things */
156
157void
158_PyImport_Init(void)
159{
160    const struct filedescr *scan;
161    struct filedescr *filetab;
162    int countD = 0;
163    int countS = 0;
164
165    initstr = PyUnicode_InternFromString("__init__");
166    if (initstr == NULL)
167        Py_FatalError("Can't initialize import variables");
168
169    /* prepare _PyImport_Filetab: copy entries from
170       _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
171     */
172#ifdef HAVE_DYNAMIC_LOADING
173    for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
174        ++countD;
175#endif
176    for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
177        ++countS;
178    filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
179    if (filetab == NULL)
180        Py_FatalError("Can't initialize import file table.");
181#ifdef HAVE_DYNAMIC_LOADING
182    memcpy(filetab, _PyImport_DynLoadFiletab,
183           countD * sizeof(struct filedescr));
184#endif
185    memcpy(filetab + countD, _PyImport_StandardFiletab,
186           countS * sizeof(struct filedescr));
187    filetab[countD + countS].suffix = NULL;
188
189    _PyImport_Filetab = filetab;
190
191    if (Py_OptimizeFlag) {
192        /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
193        for (; filetab->suffix != NULL; filetab++) {
194            if (strcmp(filetab->suffix, ".pyc") == 0)
195                filetab->suffix = ".pyo";
196        }
197    }
198}
199
200void
201_PyImportHooks_Init(void)
202{
203    PyObject *v, *path_hooks = NULL, *zimpimport;
204    int err = 0;
205
206    /* adding sys.path_hooks and sys.path_importer_cache, setting up
207       zipimport */
208    if (PyType_Ready(&PyNullImporter_Type) < 0)
209        goto error;
210
211    if (Py_VerboseFlag)
212        PySys_WriteStderr("# installing zipimport hook\n");
213
214    v = PyList_New(0);
215    if (v == NULL)
216        goto error;
217    err = PySys_SetObject("meta_path", v);
218    Py_DECREF(v);
219    if (err)
220        goto error;
221    v = PyDict_New();
222    if (v == NULL)
223        goto error;
224    err = PySys_SetObject("path_importer_cache", v);
225    Py_DECREF(v);
226    if (err)
227        goto error;
228    path_hooks = PyList_New(0);
229    if (path_hooks == NULL)
230        goto error;
231    err = PySys_SetObject("path_hooks", path_hooks);
232    if (err) {
233  error:
234        PyErr_Print();
235        Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
236                      "path_importer_cache, or NullImporter failed"
237                      );
238    }
239
240    zimpimport = PyImport_ImportModule("zipimport");
241    if (zimpimport == NULL) {
242        PyErr_Clear(); /* No zip import module -- okay */
243        if (Py_VerboseFlag)
244            PySys_WriteStderr("# can't import zipimport\n");
245    }
246    else {
247        _Py_IDENTIFIER(zipimporter);
248        PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
249                                                    &PyId_zipimporter);
250        Py_DECREF(zimpimport);
251        if (zipimporter == NULL) {
252            PyErr_Clear(); /* No zipimporter object -- okay */
253            if (Py_VerboseFlag)
254                PySys_WriteStderr(
255                    "# can't import zipimport.zipimporter\n");
256        }
257        else {
258            /* sys.path_hooks.append(zipimporter) */
259            err = PyList_Append(path_hooks, zipimporter);
260            Py_DECREF(zipimporter);
261            if (err)
262                goto error;
263            if (Py_VerboseFlag)
264                PySys_WriteStderr(
265                    "# installed zipimport hook\n");
266        }
267    }
268    Py_DECREF(path_hooks);
269}
270
271void
272_PyImport_Fini(void)
273{
274    Py_XDECREF(extensions);
275    extensions = NULL;
276    PyMem_DEL(_PyImport_Filetab);
277    _PyImport_Filetab = NULL;
278}
279
280
281/* Locking primitives to prevent parallel imports of the same module
282   in different threads to return with a partially loaded module.
283   These calls are serialized by the global interpreter lock. */
284
285#ifdef WITH_THREAD
286
287#include "pythread.h"
288
289static PyThread_type_lock import_lock = 0;
290static long import_lock_thread = -1;
291static int import_lock_level = 0;
292
293void
294_PyImport_AcquireLock(void)
295{
296    long me = PyThread_get_thread_ident();
297    if (me == -1)
298        return; /* Too bad */
299    if (import_lock == NULL) {
300        import_lock = PyThread_allocate_lock();
301        if (import_lock == NULL)
302            return;  /* Nothing much we can do. */
303    }
304    if (import_lock_thread == me) {
305        import_lock_level++;
306        return;
307    }
308    if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
309    {
310        PyThreadState *tstate = PyEval_SaveThread();
311        PyThread_acquire_lock(import_lock, 1);
312        PyEval_RestoreThread(tstate);
313    }
314    import_lock_thread = me;
315    import_lock_level = 1;
316}
317
318int
319_PyImport_ReleaseLock(void)
320{
321    long me = PyThread_get_thread_ident();
322    if (me == -1 || import_lock == NULL)
323        return 0; /* Too bad */
324    if (import_lock_thread != me)
325        return -1;
326    import_lock_level--;
327    if (import_lock_level == 0) {
328        import_lock_thread = -1;
329        PyThread_release_lock(import_lock);
330    }
331    return 1;
332}
333
334/* This function is called from PyOS_AfterFork to ensure that newly
335   created child processes do not share locks with the parent.
336   We now acquire the import lock around fork() calls but on some platforms
337   (Solaris 9 and earlier? see isue7242) that still left us with problems. */
338
339void
340_PyImport_ReInitLock(void)
341{
342    if (import_lock != NULL)
343        import_lock = PyThread_allocate_lock();
344    if (import_lock_level > 1) {
345        /* Forked as a side effect of import */
346        long me = PyThread_get_thread_ident();
347        PyThread_acquire_lock(import_lock, 0);
348        /* XXX: can the previous line fail? */
349        import_lock_thread = me;
350        import_lock_level--;
351    } else {
352        import_lock_thread = -1;
353        import_lock_level = 0;
354    }
355}
356
357#endif
358
359static PyObject *
360imp_lock_held(PyObject *self, PyObject *noargs)
361{
362#ifdef WITH_THREAD
363    return PyBool_FromLong(import_lock_thread != -1);
364#else
365    return PyBool_FromLong(0);
366#endif
367}
368
369static PyObject *
370imp_acquire_lock(PyObject *self, PyObject *noargs)
371{
372#ifdef WITH_THREAD
373    _PyImport_AcquireLock();
374#endif
375    Py_INCREF(Py_None);
376    return Py_None;
377}
378
379static PyObject *
380imp_release_lock(PyObject *self, PyObject *noargs)
381{
382#ifdef WITH_THREAD
383    if (_PyImport_ReleaseLock() < 0) {
384        PyErr_SetString(PyExc_RuntimeError,
385                        "not holding the import lock");
386        return NULL;
387    }
388#endif
389    Py_INCREF(Py_None);
390    return Py_None;
391}
392
393static void
394imp_modules_reloading_clear(void)
395{
396    PyInterpreterState *interp = PyThreadState_Get()->interp;
397    if (interp->modules_reloading != NULL)
398        PyDict_Clear(interp->modules_reloading);
399}
400
401/* Helper for sys */
402
403PyObject *
404PyImport_GetModuleDict(void)
405{
406    PyInterpreterState *interp = PyThreadState_GET()->interp;
407    if (interp->modules == NULL)
408        Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
409    return interp->modules;
410}
411
412
413/* List of names to clear in sys */
414static char* sys_deletes[] = {
415    "path", "argv", "ps1", "ps2",
416    "last_type", "last_value", "last_traceback",
417    "path_hooks", "path_importer_cache", "meta_path",
418    /* misc stuff */
419    "flags", "float_info",
420    NULL
421};
422
423static char* sys_files[] = {
424    "stdin", "__stdin__",
425    "stdout", "__stdout__",
426    "stderr", "__stderr__",
427    NULL
428};
429
430
431/* Un-initialize things, as good as we can */
432
433void
434PyImport_Cleanup(void)
435{
436    Py_ssize_t pos, ndone;
437    PyObject *key, *value, *dict;
438    PyInterpreterState *interp = PyThreadState_GET()->interp;
439    PyObject *modules = interp->modules;
440
441    if (modules == NULL)
442        return; /* Already done */
443
444    /* Delete some special variables first.  These are common
445       places where user values hide and people complain when their
446       destructors fail.  Since the modules containing them are
447       deleted *last* of all, they would come too late in the normal
448       destruction order.  Sigh. */
449
450    value = PyDict_GetItemString(modules, "builtins");
451    if (value != NULL && PyModule_Check(value)) {
452        dict = PyModule_GetDict(value);
453        if (Py_VerboseFlag)
454            PySys_WriteStderr("# clear builtins._\n");
455        PyDict_SetItemString(dict, "_", Py_None);
456    }
457    value = PyDict_GetItemString(modules, "sys");
458    if (value != NULL && PyModule_Check(value)) {
459        char **p;
460        PyObject *v;
461        dict = PyModule_GetDict(value);
462        for (p = sys_deletes; *p != NULL; p++) {
463            if (Py_VerboseFlag)
464                PySys_WriteStderr("# clear sys.%s\n", *p);
465            PyDict_SetItemString(dict, *p, Py_None);
466        }
467        for (p = sys_files; *p != NULL; p+=2) {
468            if (Py_VerboseFlag)
469                PySys_WriteStderr("# restore sys.%s\n", *p);
470            v = PyDict_GetItemString(dict, *(p+1));
471            if (v == NULL)
472                v = Py_None;
473            PyDict_SetItemString(dict, *p, v);
474        }
475    }
476
477    /* First, delete __main__ */
478    value = PyDict_GetItemString(modules, "__main__");
479    if (value != NULL && PyModule_Check(value)) {
480        if (Py_VerboseFlag)
481            PySys_WriteStderr("# cleanup __main__\n");
482        _PyModule_Clear(value);
483        PyDict_SetItemString(modules, "__main__", Py_None);
484    }
485
486    /* The special treatment of "builtins" here is because even
487       when it's not referenced as a module, its dictionary is
488       referenced by almost every module's __builtins__.  Since
489       deleting a module clears its dictionary (even if there are
490       references left to it), we need to delete the "builtins"
491       module last.  Likewise, we don't delete sys until the very
492       end because it is implicitly referenced (e.g. by print).
493
494       Also note that we 'delete' modules by replacing their entry
495       in the modules dict with None, rather than really deleting
496       them; this avoids a rehash of the modules dictionary and
497       also marks them as "non existent" so they won't be
498       re-imported. */
499
500    /* Next, repeatedly delete modules with a reference count of
501       one (skipping builtins and sys) and delete them */
502    do {
503        ndone = 0;
504        pos = 0;
505        while (PyDict_Next(modules, &pos, &key, &value)) {
506            if (value->ob_refcnt != 1)
507                continue;
508            if (PyUnicode_Check(key) && PyModule_Check(value)) {
509                if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
510                    continue;
511                if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
512                    continue;
513                if (Py_VerboseFlag)
514                    PySys_FormatStderr(
515                        "# cleanup[1] %U\n", key);
516                _PyModule_Clear(value);
517                PyDict_SetItem(modules, key, Py_None);
518                ndone++;
519            }
520        }
521    } while (ndone > 0);
522
523    /* Next, delete all modules (still skipping builtins and sys) */
524    pos = 0;
525    while (PyDict_Next(modules, &pos, &key, &value)) {
526        if (PyUnicode_Check(key) && PyModule_Check(value)) {
527            if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
528                continue;
529            if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
530                continue;
531            if (Py_VerboseFlag)
532                PySys_FormatStderr("# cleanup[2] %U\n", key);
533            _PyModule_Clear(value);
534            PyDict_SetItem(modules, key, Py_None);
535        }
536    }
537
538    /* Next, delete sys and builtins (in that order) */
539    value = PyDict_GetItemString(modules, "sys");
540    if (value != NULL && PyModule_Check(value)) {
541        if (Py_VerboseFlag)
542            PySys_WriteStderr("# cleanup sys\n");
543        _PyModule_Clear(value);
544        PyDict_SetItemString(modules, "sys", Py_None);
545    }
546    value = PyDict_GetItemString(modules, "builtins");
547    if (value != NULL && PyModule_Check(value)) {
548        if (Py_VerboseFlag)
549            PySys_WriteStderr("# cleanup builtins\n");
550        _PyModule_Clear(value);
551        PyDict_SetItemString(modules, "builtins", Py_None);
552    }
553
554    /* Finally, clear and delete the modules directory */
555    PyDict_Clear(modules);
556    interp->modules = NULL;
557    Py_DECREF(modules);
558    Py_CLEAR(interp->modules_reloading);
559}
560
561
562/* Helper for pythonrun.c -- return magic number and tag. */
563
564long
565PyImport_GetMagicNumber(void)
566{
567    return pyc_magic;
568}
569
570
571const char *
572PyImport_GetMagicTag(void)
573{
574    return pyc_tag;
575}
576
577/* Magic for extension modules (built-in as well as dynamically
578   loaded).  To prevent initializing an extension module more than
579   once, we keep a static dictionary 'extensions' keyed by module name
580   (for built-in modules) or by filename (for dynamically loaded
581   modules), containing these modules.  A copy of the module's
582   dictionary is stored by calling _PyImport_FixupExtensionObject()
583   immediately after the module initialization function succeeds.  A
584   copy can be retrieved from there by calling
585   _PyImport_FindExtensionObject().
586
587   Modules which do support multiple initialization set their m_size
588   field to a non-negative number (indicating the size of the
589   module-specific state). They are still recorded in the extensions
590   dictionary, to avoid loading shared libraries twice.
591*/
592
593int
594_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
595                               PyObject *filename)
596{
597    PyObject *modules, *dict;
598    struct PyModuleDef *def;
599    if (extensions == NULL) {
600        extensions = PyDict_New();
601        if (extensions == NULL)
602            return -1;
603    }
604    if (mod == NULL || !PyModule_Check(mod)) {
605        PyErr_BadInternalCall();
606        return -1;
607    }
608    def = PyModule_GetDef(mod);
609    if (!def) {
610        PyErr_BadInternalCall();
611        return -1;
612    }
613    modules = PyImport_GetModuleDict();
614    if (PyDict_SetItem(modules, name, mod) < 0)
615        return -1;
616    if (_PyState_AddModule(mod, def) < 0) {
617        PyDict_DelItem(modules, name);
618        return -1;
619    }
620    if (def->m_size == -1) {
621        if (def->m_base.m_copy) {
622            /* Somebody already imported the module,
623               likely under a different name.
624               XXX this should really not happen. */
625            Py_DECREF(def->m_base.m_copy);
626            def->m_base.m_copy = NULL;
627        }
628        dict = PyModule_GetDict(mod);
629        if (dict == NULL)
630            return -1;
631        def->m_base.m_copy = PyDict_Copy(dict);
632        if (def->m_base.m_copy == NULL)
633            return -1;
634    }
635    PyDict_SetItem(extensions, filename, (PyObject*)def);
636    return 0;
637}
638
639int
640_PyImport_FixupBuiltin(PyObject *mod, char *name)
641{
642    int res;
643    PyObject *nameobj;
644    nameobj = PyUnicode_InternFromString(name);
645    if (nameobj == NULL)
646        return -1;
647    res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
648    Py_DECREF(nameobj);
649    return res;
650}
651
652PyObject *
653_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
654{
655    PyObject *mod, *mdict;
656    PyModuleDef* def;
657    if (extensions == NULL)
658        return NULL;
659    def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
660    if (def == NULL)
661        return NULL;
662    if (def->m_size == -1) {
663        /* Module does not support repeated initialization */
664        if (def->m_base.m_copy == NULL)
665            return NULL;
666        mod = PyImport_AddModuleObject(name);
667        if (mod == NULL)
668            return NULL;
669        mdict = PyModule_GetDict(mod);
670        if (mdict == NULL)
671            return NULL;
672        if (PyDict_Update(mdict, def->m_base.m_copy))
673            return NULL;
674    }
675    else {
676        if (def->m_base.m_init == NULL)
677            return NULL;
678        mod = def->m_base.m_init();
679        if (mod == NULL)
680            return NULL;
681        PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
682        Py_DECREF(mod);
683    }
684    if (_PyState_AddModule(mod, def) < 0) {
685        PyDict_DelItem(PyImport_GetModuleDict(), name);
686        Py_DECREF(mod);
687        return NULL;
688    }
689    if (Py_VerboseFlag)
690        PySys_FormatStderr("import %U # previously loaded (%R)\n",
691                          name, filename);
692    return mod;
693
694}
695
696PyObject *
697_PyImport_FindBuiltin(const char *name)
698{
699    PyObject *res, *nameobj;
700    nameobj = PyUnicode_InternFromString(name);
701    if (nameobj == NULL)
702        return NULL;
703    res = _PyImport_FindExtensionObject(nameobj, nameobj);
704    Py_DECREF(nameobj);
705    return res;
706}
707
708/* Get the module object corresponding to a module name.
709   First check the modules dictionary if there's one there,
710   if not, create a new one and insert it in the modules dictionary.
711   Because the former action is most common, THIS DOES NOT RETURN A
712   'NEW' REFERENCE! */
713
714PyObject *
715PyImport_AddModuleObject(PyObject *name)
716{
717    PyObject *modules = PyImport_GetModuleDict();
718    PyObject *m;
719
720    if ((m = PyDict_GetItem(modules, name)) != NULL &&
721        PyModule_Check(m))
722        return m;
723    m = PyModule_NewObject(name);
724    if (m == NULL)
725        return NULL;
726    if (PyDict_SetItem(modules, name, m) != 0) {
727        Py_DECREF(m);
728        return NULL;
729    }
730    Py_DECREF(m); /* Yes, it still exists, in modules! */
731
732    return m;
733}
734
735PyObject *
736PyImport_AddModule(const char *name)
737{
738    PyObject *nameobj, *module;
739    nameobj = PyUnicode_FromString(name);
740    if (nameobj == NULL)
741        return NULL;
742    module = PyImport_AddModuleObject(nameobj);
743    Py_DECREF(nameobj);
744    return module;
745}
746
747
748/* Remove name from sys.modules, if it's there. */
749static void
750remove_module(PyObject *name)
751{
752    PyObject *modules = PyImport_GetModuleDict();
753    if (PyDict_GetItem(modules, name) == NULL)
754        return;
755    if (PyDict_DelItem(modules, name) < 0)
756        Py_FatalError("import:  deleting existing key in"
757                      "sys.modules failed");
758}
759
760static PyObject * get_sourcefile(PyObject *filename);
761static PyObject *make_source_pathname(PyObject *pathname);
762static PyObject* make_compiled_pathname(PyObject *pathname, int debug);
763
764/* Execute a code object in a module and return the module object
765 * WITH INCREMENTED REFERENCE COUNT.  If an error occurs, name is
766 * removed from sys.modules, to avoid leaving damaged module objects
767 * in sys.modules.  The caller may wish to restore the original
768 * module object (if any) in this case; PyImport_ReloadModule is an
769 * example.
770 *
771 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
772 * interface.  The other two exist primarily for backward compatibility.
773 */
774PyObject *
775PyImport_ExecCodeModule(char *name, PyObject *co)
776{
777    return PyImport_ExecCodeModuleWithPathnames(
778        name, co, (char *)NULL, (char *)NULL);
779}
780
781PyObject *
782PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
783{
784    return PyImport_ExecCodeModuleWithPathnames(
785        name, co, pathname, (char *)NULL);
786}
787
788PyObject *
789PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
790                                     char *cpathname)
791{
792    PyObject *m = NULL;
793    PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
794
795    nameobj = PyUnicode_FromString(name);
796    if (nameobj == NULL)
797        return NULL;
798
799    if (pathname != NULL) {
800        pathobj = PyUnicode_DecodeFSDefault(pathname);
801        if (pathobj == NULL)
802            goto error;
803    } else
804        pathobj = NULL;
805    if (cpathname != NULL) {
806        cpathobj = PyUnicode_DecodeFSDefault(cpathname);
807        if (cpathobj == NULL)
808            goto error;
809    } else
810        cpathobj = NULL;
811    m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
812error:
813    Py_DECREF(nameobj);
814    Py_XDECREF(pathobj);
815    Py_XDECREF(cpathobj);
816    return m;
817}
818
819PyObject*
820PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
821                              PyObject *cpathname)
822{
823    PyObject *modules = PyImport_GetModuleDict();
824    PyObject *m, *d, *v;
825
826    m = PyImport_AddModuleObject(name);
827    if (m == NULL)
828        return NULL;
829    /* If the module is being reloaded, we get the old module back
830       and re-use its dict to exec the new code. */
831    d = PyModule_GetDict(m);
832    if (PyDict_GetItemString(d, "__builtins__") == NULL) {
833        if (PyDict_SetItemString(d, "__builtins__",
834                                 PyEval_GetBuiltins()) != 0)
835            goto error;
836    }
837    /* Remember the filename as the __file__ attribute */
838    if (pathname != NULL) {
839        v = get_sourcefile(pathname);
840        if (v == NULL)
841            PyErr_Clear();
842    }
843    else
844        v = NULL;
845    if (v == NULL) {
846        v = ((PyCodeObject *)co)->co_filename;
847        Py_INCREF(v);
848    }
849    if (PyDict_SetItemString(d, "__file__", v) != 0)
850        PyErr_Clear(); /* Not important enough to report */
851    Py_DECREF(v);
852
853    /* Remember the pyc path name as the __cached__ attribute. */
854    if (cpathname != NULL)
855        v = cpathname;
856    else
857        v = Py_None;
858    if (PyDict_SetItemString(d, "__cached__", v) != 0)
859        PyErr_Clear(); /* Not important enough to report */
860
861    v = PyEval_EvalCode(co, d, d);
862    if (v == NULL)
863        goto error;
864    Py_DECREF(v);
865
866    if ((m = PyDict_GetItem(modules, name)) == NULL) {
867        PyErr_Format(PyExc_ImportError,
868                     "Loaded module %R not found in sys.modules",
869                     name);
870        return NULL;
871    }
872
873    Py_INCREF(m);
874
875    return m;
876
877  error:
878    remove_module(name);
879    return NULL;
880}
881
882
883/* Like strrchr(string, '/') but searches for the rightmost of either SEP
884   or ALTSEP, if the latter is defined.
885*/
886static Py_UCS4*
887rightmost_sep(Py_UCS4 *s)
888{
889    Py_UCS4 *found, c;
890    for (found = NULL; (c = *s); s++) {
891        if (c == SEP
892#ifdef ALTSEP
893            || c == ALTSEP
894#endif
895            )
896        {
897            found = s;
898        }
899    }
900    return found;
901}
902
903/* Like rightmost_sep, but operate on unicode objects. */
904static Py_ssize_t
905rightmost_sep_obj(PyObject* o, Py_ssize_t start, Py_ssize_t end)
906{
907    Py_ssize_t found, i;
908    Py_UCS4 c;
909    for (found = -1, i = start; i < end; i++) {
910        c = PyUnicode_READ_CHAR(o, i);
911        if (c == SEP
912#ifdef ALTSEP
913            || c == ALTSEP
914#endif
915            )
916        {
917            found = i;
918        }
919    }
920    return found;
921}
922
923/* Given a pathname for a Python source file, fill a buffer with the
924   pathname for the corresponding compiled file.  Return the pathname
925   for the compiled file, or NULL if there's no space in the buffer.
926   Doesn't set an exception.
927
928   foo.py -> __pycache__/foo.<tag>.pyc
929
930   pathstr is assumed to be "ready".
931*/
932
933static PyObject*
934make_compiled_pathname(PyObject *pathstr, int debug)
935{
936    PyObject *result;
937    Py_ssize_t fname, ext, len, i, pos, taglen;
938    Py_ssize_t pycache_len = sizeof(CACHEDIR) - 1;
939    int kind;
940    void *data;
941
942    /* Compute the output string size. */
943    len = PyUnicode_GET_LENGTH(pathstr);
944    /* If there is no separator, this returns -1, so
945       lastsep will be 0. */
946    fname = rightmost_sep_obj(pathstr, 0, len) + 1;
947    ext = fname - 1;
948    for(i = fname; i < len; i++)
949        if (PyUnicode_READ_CHAR(pathstr, i) == '.')
950            ext = i + 1;
951    if (ext < fname)
952        /* No dot in filename; use entire filename */
953        ext = len;
954
955    /* result = pathstr[:fname] + "__pycache__" + SEP +
956                pathstr[fname:ext] + tag + ".py[co]" */
957    taglen = strlen(pyc_tag);
958    result = PyUnicode_New(ext + pycache_len + taglen + 4,
959                           PyUnicode_MAX_CHAR_VALUE(pathstr));
960    if (!result)
961        return NULL;
962    kind = PyUnicode_KIND(result);
963    data = PyUnicode_DATA(result);
964    PyUnicode_CopyCharacters(result, 0, pathstr, 0, fname);
965    pos = fname;
966    for (i = 0; i < pycache_len - 1; i++)
967        PyUnicode_WRITE(kind, data, pos++, CACHEDIR[i]);
968    PyUnicode_WRITE(kind, data, pos++, SEP);
969    PyUnicode_CopyCharacters(result, pos, pathstr,
970                             fname, ext - fname);
971    pos += ext - fname;
972    for (i = 0; pyc_tag[i]; i++)
973        PyUnicode_WRITE(kind, data, pos++, pyc_tag[i]);
974    PyUnicode_WRITE(kind, data, pos++, '.');
975    PyUnicode_WRITE(kind, data, pos++, 'p');
976    PyUnicode_WRITE(kind, data, pos++, 'y');
977    PyUnicode_WRITE(kind, data, pos++, debug ? 'c' : 'o');
978    return result;
979}
980
981
982/* Given a pathname to a Python byte compiled file, return the path to the
983   source file, if the path matches the PEP 3147 format.  This does not check
984   for any file existence, however, if the pyc file name does not match PEP
985   3147 style, NULL is returned.  buf must be at least as big as pathname;
986   the resulting path will always be shorter.
987
988   (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */
989
990static PyObject*
991make_source_pathname(PyObject *path)
992{
993    Py_ssize_t left, right, dot0, dot1, len;
994    Py_ssize_t i, j;
995    PyObject *result;
996    int kind;
997    void *data;
998
999    len = PyUnicode_GET_LENGTH(path);
1000    if (len > MAXPATHLEN)
1001        return NULL;
1002
1003    /* Look back two slashes from the end.  In between these two slashes
1004       must be the string __pycache__ or this is not a PEP 3147 style
1005       path.  It's possible for there to be only one slash.
1006    */
1007    right = rightmost_sep_obj(path, 0, len);
1008    if (right == -1)
1009        return NULL;
1010    left = rightmost_sep_obj(path, 0, right);
1011    if (left == -1)
1012        left = 0;
1013    else
1014        left++;
1015    if (right-left !=  sizeof(CACHEDIR)-1)
1016        return NULL;
1017    for (i = 0; i < sizeof(CACHEDIR)-1; i++)
1018        if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i])
1019            return NULL;
1020
1021    /* Now verify that the path component to the right of the last slash
1022       has two dots in it.
1023    */
1024    dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1);
1025    if (dot0 < 0)
1026        return NULL;
1027    dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1);
1028    if (dot1 < 0)
1029        return NULL;
1030    /* Too many dots? */
1031    if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1)
1032        return NULL;
1033
1034    /* This is a PEP 3147 path.  Start by copying everything from the
1035       start of pathname up to and including the leftmost slash.  Then
1036       copy the file's basename, removing the magic tag and adding a .py
1037       suffix.
1038    */
1039    result = PyUnicode_New(left + (dot0-right) + 2,
1040                           PyUnicode_MAX_CHAR_VALUE(path));
1041    if (!result)
1042        return NULL;
1043    kind = PyUnicode_KIND(result);
1044    data = PyUnicode_DATA(result);
1045    PyUnicode_CopyCharacters(result, 0, path, 0, (i = left));
1046    PyUnicode_CopyCharacters(result, left, path, right+1,
1047                             (j = dot0-right));
1048    PyUnicode_WRITE(kind, data, i+j,   'p');
1049    PyUnicode_WRITE(kind, data, i+j+1, 'y');
1050    return result;
1051}
1052
1053/* Given a pathname for a Python source file, its time of last
1054   modification, and a pathname for a compiled file, check whether the
1055   compiled file represents the same version of the source.  If so,
1056   return a FILE pointer for the compiled file, positioned just after
1057   the header; if not, return NULL.
1058   Doesn't set an exception. */
1059
1060static FILE *
1061check_compiled_module(PyObject *pathname, time_t mtime, PyObject *cpathname)
1062{
1063    FILE *fp;
1064    long magic;
1065    long pyc_mtime;
1066
1067    fp = _Py_fopen(cpathname, "rb");
1068    if (fp == NULL)
1069        return NULL;
1070    magic = PyMarshal_ReadLongFromFile(fp);
1071    if (magic != pyc_magic) {
1072        if (Py_VerboseFlag)
1073            PySys_FormatStderr("# %R has bad magic\n", cpathname);
1074        fclose(fp);
1075        return NULL;
1076    }
1077    pyc_mtime = PyMarshal_ReadLongFromFile(fp);
1078    if (pyc_mtime != mtime) {
1079        if (Py_VerboseFlag)
1080            PySys_FormatStderr("# %R has bad mtime\n", cpathname);
1081        fclose(fp);
1082        return NULL;
1083    }
1084    if (Py_VerboseFlag)
1085        PySys_FormatStderr("# %R matches %R\n", cpathname, pathname);
1086    return fp;
1087}
1088
1089
1090/* Read a code object from a file and check it for validity */
1091
1092static PyCodeObject *
1093read_compiled_module(PyObject *cpathname, FILE *fp)
1094{
1095    PyObject *co;
1096
1097    co = PyMarshal_ReadLastObjectFromFile(fp);
1098    if (co == NULL)
1099        return NULL;
1100    if (!PyCode_Check(co)) {
1101        PyErr_Format(PyExc_ImportError,
1102                     "Non-code object in %R", cpathname);
1103        Py_DECREF(co);
1104        return NULL;
1105    }
1106    return (PyCodeObject *)co;
1107}
1108
1109
1110/* Load a module from a compiled file, execute it, and return its
1111   module object WITH INCREMENTED REFERENCE COUNT */
1112
1113static PyObject *
1114load_compiled_module(PyObject *name, PyObject *cpathname, FILE *fp)
1115{
1116    long magic;
1117    PyCodeObject *co;
1118    PyObject *m;
1119
1120    magic = PyMarshal_ReadLongFromFile(fp);
1121    if (magic != pyc_magic) {
1122        PyErr_Format(PyExc_ImportError,
1123                     "Bad magic number in %R", cpathname);
1124        return NULL;
1125    }
1126    (void) PyMarshal_ReadLongFromFile(fp);
1127    co = read_compiled_module(cpathname, fp);
1128    if (co == NULL)
1129        return NULL;
1130    if (Py_VerboseFlag)
1131        PySys_FormatStderr("import %U # precompiled from %R\n",
1132                           name, cpathname);
1133    m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1134                                      cpathname, cpathname);
1135    Py_DECREF(co);
1136
1137    return m;
1138}
1139
1140/* Parse a source file and return the corresponding code object */
1141
1142static PyCodeObject *
1143parse_source_module(PyObject *pathname, FILE *fp)
1144{
1145    PyCodeObject *co;
1146    PyObject *pathbytes;
1147    mod_ty mod;
1148    PyCompilerFlags flags;
1149    PyArena *arena;
1150
1151    pathbytes = PyUnicode_EncodeFSDefault(pathname);
1152    if (pathbytes == NULL)
1153        return NULL;
1154
1155    arena = PyArena_New();
1156    if (arena == NULL) {
1157        Py_DECREF(pathbytes);
1158        return NULL;
1159    }
1160
1161    flags.cf_flags = 0;
1162    mod = PyParser_ASTFromFile(fp, PyBytes_AS_STRING(pathbytes), NULL,
1163                               Py_file_input, 0, 0, &flags,
1164                               NULL, arena);
1165    if (mod != NULL)
1166        co = PyAST_Compile(mod, PyBytes_AS_STRING(pathbytes), NULL, arena);
1167    else
1168        co = NULL;
1169    Py_DECREF(pathbytes);
1170    PyArena_Free(arena);
1171    return co;
1172}
1173
1174/* Helper to open a bytecode file for writing in exclusive mode */
1175
1176#ifndef MS_WINDOWS
1177static FILE *
1178open_exclusive(char *filename, mode_t mode)
1179{
1180#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
1181    /* Use O_EXCL to avoid a race condition when another process tries to
1182       write the same file.  When that happens, our open() call fails,
1183       which is just fine (since it's only a cache).
1184       XXX If the file exists and is writable but the directory is not
1185       writable, the file will never be written.  Oh well.
1186    */
1187    int fd;
1188    (void) unlink(filename);
1189    fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
1190#ifdef O_BINARY
1191                            |O_BINARY   /* necessary for Windows */
1192#endif
1193#ifdef __VMS
1194            , mode, "ctxt=bin", "shr=nil"
1195#else
1196            , mode
1197#endif
1198          );
1199    if (fd < 0)
1200        return NULL;
1201    return fdopen(fd, "wb");
1202#else
1203    /* Best we can do -- on Windows this can't happen anyway */
1204    return fopen(filename, "wb");
1205#endif
1206}
1207#endif
1208
1209
1210/* Write a compiled module to a file, placing the time of last
1211   modification of its source into the header.
1212   Errors are ignored, if a write error occurs an attempt is made to
1213   remove the file. */
1214
1215static void
1216write_compiled_module(PyCodeObject *co, PyObject *cpathname,
1217                      struct stat *srcstat)
1218{
1219    Py_UCS4 *cpathname_ucs4;
1220    FILE *fp;
1221    time_t mtime = srcstat->st_mtime;
1222#ifdef MS_WINDOWS   /* since Windows uses different permissions  */
1223    mode_t mode = srcstat->st_mode & ~S_IEXEC;
1224#else
1225    mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
1226    mode_t dirmode = (srcstat->st_mode |
1227                      S_IXUSR | S_IXGRP | S_IXOTH |
1228                      S_IWUSR | S_IWGRP | S_IWOTH);
1229    PyObject *dirbytes;
1230#endif
1231#ifdef MS_WINDOWS
1232    int fd;
1233#else
1234    PyObject *cpathbytes, *cpathbytes_tmp;
1235    Py_ssize_t cpathbytes_len;
1236#endif
1237    PyObject *dirname;
1238    Py_UCS4 *dirsep;
1239    int res, ok;
1240
1241    /* Ensure that the __pycache__ directory exists. */
1242    cpathname_ucs4 = PyUnicode_AsUCS4Copy(cpathname);
1243    if (!cpathname_ucs4)
1244        return;
1245    dirsep = rightmost_sep(cpathname_ucs4);
1246    if (dirsep == NULL) {
1247        if (Py_VerboseFlag)
1248            PySys_FormatStderr("# no %s path found %R\n", CACHEDIR, cpathname);
1249        return;
1250    }
1251    dirname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
1252                                        cpathname_ucs4,
1253                                        dirsep - cpathname_ucs4);
1254    PyMem_Free(cpathname_ucs4);
1255    if (dirname == NULL) {
1256        PyErr_Clear();
1257        return;
1258    }
1259
1260#ifdef MS_WINDOWS
1261    res = CreateDirectoryW(PyUnicode_AS_UNICODE(dirname), NULL);
1262    ok = (res != 0);
1263    if (!ok && GetLastError() == ERROR_ALREADY_EXISTS)
1264        ok = 1;
1265#else
1266    dirbytes = PyUnicode_EncodeFSDefault(dirname);
1267    if (dirbytes == NULL) {
1268        PyErr_Clear();
1269        return;
1270    }
1271    res = mkdir(PyBytes_AS_STRING(dirbytes), dirmode);
1272    Py_DECREF(dirbytes);
1273    if (0 <= res)
1274        ok = 1;
1275    else
1276        ok = (errno == EEXIST);
1277#endif
1278    if (!ok) {
1279        if (Py_VerboseFlag)
1280            PySys_FormatStderr("# cannot create cache dir %R\n", dirname);
1281        Py_DECREF(dirname);
1282        return;
1283    }
1284    Py_DECREF(dirname);
1285
1286#ifdef MS_WINDOWS
1287    (void)DeleteFileW(PyUnicode_AS_UNICODE(cpathname));
1288    fd = _wopen(PyUnicode_AS_UNICODE(cpathname),
1289                 O_EXCL | O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
1290                 mode);
1291    if (0 <= fd)
1292        fp = fdopen(fd, "wb");
1293    else
1294        fp = NULL;
1295#else
1296    /* Under POSIX, we first write to a tmp file and then take advantage
1297       of atomic renaming. */
1298    cpathbytes = PyUnicode_EncodeFSDefault(cpathname);
1299    if (cpathbytes == NULL) {
1300        PyErr_Clear();
1301        return;
1302    }
1303    cpathbytes_len = PyBytes_GET_SIZE(cpathbytes);
1304    cpathbytes_tmp = PyBytes_FromStringAndSize(NULL, cpathbytes_len + 4);
1305    if (cpathbytes_tmp == NULL) {
1306        Py_DECREF(cpathbytes);
1307        PyErr_Clear();
1308        return;
1309    }
1310    memcpy(PyBytes_AS_STRING(cpathbytes_tmp), PyBytes_AS_STRING(cpathbytes),
1311           cpathbytes_len);
1312    memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, ".tmp", 4);
1313
1314    fp = open_exclusive(PyBytes_AS_STRING(cpathbytes_tmp), mode);
1315#endif
1316    if (fp == NULL) {
1317        if (Py_VerboseFlag)
1318            PySys_FormatStderr(
1319                "# can't create %R\n", cpathname);
1320#ifndef MS_WINDOWS
1321        Py_DECREF(cpathbytes);
1322        Py_DECREF(cpathbytes_tmp);
1323#endif
1324        return;
1325    }
1326    PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
1327    /* First write a 0 for mtime */
1328    PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
1329    PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
1330    fflush(fp);
1331    /* Now write the true mtime */
1332    fseek(fp, 4L, 0);
1333    assert(mtime < LONG_MAX);
1334    PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
1335    if (fflush(fp) != 0 || ferror(fp)) {
1336        if (Py_VerboseFlag)
1337            PySys_FormatStderr("# can't write %R\n", cpathname);
1338        /* Don't keep partial file */
1339        fclose(fp);
1340#ifdef MS_WINDOWS
1341        (void)DeleteFileW(PyUnicode_AS_UNICODE(cpathname));
1342#else
1343        (void) unlink(PyBytes_AS_STRING(cpathbytes_tmp));
1344        Py_DECREF(cpathbytes);
1345        Py_DECREF(cpathbytes_tmp);
1346#endif
1347        return;
1348    }
1349    fclose(fp);
1350    /* Under POSIX, do an atomic rename */
1351#ifndef MS_WINDOWS
1352    if (rename(PyBytes_AS_STRING(cpathbytes_tmp),
1353               PyBytes_AS_STRING(cpathbytes))) {
1354        if (Py_VerboseFlag)
1355            PySys_FormatStderr("# can't write %R\n", cpathname);
1356        /* Don't keep tmp file */
1357        unlink(PyBytes_AS_STRING(cpathbytes_tmp));
1358        Py_DECREF(cpathbytes);
1359        Py_DECREF(cpathbytes_tmp);
1360        return;
1361    }
1362    Py_DECREF(cpathbytes);
1363    Py_DECREF(cpathbytes_tmp);
1364#endif
1365    if (Py_VerboseFlag)
1366        PySys_FormatStderr("# wrote %R\n", cpathname);
1367}
1368
1369static void
1370update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
1371{
1372    PyObject *constants, *tmp;
1373    Py_ssize_t i, n;
1374
1375    if (PyUnicode_Compare(co->co_filename, oldname))
1376        return;
1377
1378    tmp = co->co_filename;
1379    co->co_filename = newname;
1380    Py_INCREF(co->co_filename);
1381    Py_DECREF(tmp);
1382
1383    constants = co->co_consts;
1384    n = PyTuple_GET_SIZE(constants);
1385    for (i = 0; i < n; i++) {
1386        tmp = PyTuple_GET_ITEM(constants, i);
1387        if (PyCode_Check(tmp))
1388            update_code_filenames((PyCodeObject *)tmp,
1389                                  oldname, newname);
1390    }
1391}
1392
1393static void
1394update_compiled_module(PyCodeObject *co, PyObject *newname)
1395{
1396    PyObject *oldname;
1397
1398    if (PyUnicode_Compare(co->co_filename, newname) == 0)
1399        return;
1400
1401    oldname = co->co_filename;
1402    Py_INCREF(oldname);
1403    update_code_filenames(co, oldname, newname);
1404    Py_DECREF(oldname);
1405}
1406
1407static PyObject *
1408imp_fix_co_filename(PyObject *self, PyObject *args)
1409{
1410    PyObject *co;
1411    PyObject *file_path;
1412
1413    if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
1414        return NULL;
1415
1416    if (!PyCode_Check(co)) {
1417        PyErr_SetString(PyExc_TypeError,
1418                        "first argument must be a code object");
1419        return NULL;
1420    }
1421
1422    if (!PyUnicode_Check(file_path)) {
1423        PyErr_SetString(PyExc_TypeError,
1424                        "second argument must be a string");
1425        return NULL;
1426    }
1427
1428    update_compiled_module((PyCodeObject*)co, file_path);
1429
1430    Py_RETURN_NONE;
1431}
1432
1433/* Load a source module from a given file and return its module
1434   object WITH INCREMENTED REFERENCE COUNT.  If there's a matching
1435   byte-compiled file, use that instead. */
1436
1437static PyObject *
1438load_source_module(PyObject *name, PyObject *pathname, FILE *fp)
1439{
1440    struct stat st;
1441    FILE *fpc;
1442    PyObject *cpathname = NULL, *cpathbytes = NULL;
1443    PyCodeObject *co;
1444    PyObject *m = NULL;
1445
1446    if (fstat(fileno(fp), &st) != 0) {
1447        PyErr_Format(PyExc_RuntimeError,
1448                     "unable to get file status from %R",
1449                     pathname);
1450        goto error;
1451    }
1452#if SIZEOF_TIME_T > 4
1453    /* Python's .pyc timestamp handling presumes that the timestamp fits
1454       in 4 bytes. This will be fine until sometime in the year 2038,
1455       when a 4-byte signed time_t will overflow.
1456     */
1457    if (st.st_mtime >> 32) {
1458        PyErr_SetString(PyExc_OverflowError,
1459            "modification time overflows a 4 byte field");
1460        goto error;
1461    }
1462#endif
1463    if (PyUnicode_READY(pathname) < 0)
1464        return NULL;
1465    cpathname = make_compiled_pathname(pathname, !Py_OptimizeFlag);
1466
1467    if (cpathname != NULL)
1468        fpc = check_compiled_module(pathname, st.st_mtime, cpathname);
1469    else
1470        fpc = NULL;
1471
1472    if (fpc) {
1473        co = read_compiled_module(cpathname, fpc);
1474        fclose(fpc);
1475        if (co == NULL)
1476            goto error;
1477        update_compiled_module(co, pathname);
1478        if (Py_VerboseFlag)
1479            PySys_FormatStderr("import %U # precompiled from %R\n",
1480                               name, cpathname);
1481        m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1482                                          cpathname, cpathname);
1483    }
1484    else {
1485        co = parse_source_module(pathname, fp);
1486        if (co == NULL)
1487            goto error;
1488        if (Py_VerboseFlag)
1489            PySys_FormatStderr("import %U # from %R\n",
1490                               name, pathname);
1491        if (cpathname != NULL) {
1492            PyObject *ro = PySys_GetObject("dont_write_bytecode");
1493            if (ro == NULL || !PyObject_IsTrue(ro))
1494                write_compiled_module(co, cpathname, &st);
1495        }
1496        m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1497                                          pathname, cpathname);
1498    }
1499    Py_DECREF(co);
1500
1501error:
1502    Py_XDECREF(cpathbytes);
1503    Py_XDECREF(cpathname);
1504    return m;
1505}
1506
1507/* Get source file -> unicode or None
1508 * Returns the path to the py file if available, else the given path
1509 */
1510static PyObject *
1511get_sourcefile(PyObject *filename)
1512{
1513    Py_ssize_t len;
1514    Py_UCS4 *fileuni;
1515    PyObject *py;
1516    struct stat statbuf;
1517
1518    len = PyUnicode_GET_LENGTH(filename);
1519    if (len == 0)
1520        Py_RETURN_NONE;
1521
1522    /* don't match *.pyc or *.pyo? */
1523    fileuni = PyUnicode_AsUCS4Copy(filename);
1524    if (!fileuni)
1525        return NULL;
1526    if (len < 5
1527        || fileuni[len-4] != '.'
1528        || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P')
1529        || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y'))
1530        goto unchanged;
1531
1532    /* Start by trying to turn PEP 3147 path into source path.  If that
1533     * fails, just chop off the trailing character, i.e. legacy pyc path
1534     * to py.
1535     */
1536    py = make_source_pathname(filename);
1537    if (py == NULL) {
1538        PyErr_Clear();
1539        py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1);
1540    }
1541    if (py == NULL)
1542        goto error;
1543
1544    if (_Py_stat(py, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
1545        PyMem_Free(fileuni);
1546        return py;
1547    }
1548    Py_DECREF(py);
1549    goto unchanged;
1550
1551error:
1552    PyErr_Clear();
1553unchanged:
1554    PyMem_Free(fileuni);
1555    Py_INCREF(filename);
1556    return filename;
1557}
1558
1559/* Forward */
1560static PyObject *load_module(PyObject *, FILE *, PyObject *, int, PyObject *);
1561static struct filedescr *find_module(PyObject *, PyObject *, PyObject *,
1562                                     PyObject **, FILE **, PyObject **);
1563static struct _frozen * find_frozen(PyObject *);
1564
1565/* Load a package and return its module object WITH INCREMENTED
1566   REFERENCE COUNT */
1567
1568static PyObject *
1569load_package(PyObject *name, PyObject *pathname)
1570{
1571    PyObject *m, *d, *bufobj;
1572    PyObject *file = NULL, *path_list = NULL;
1573    int err;
1574    FILE *fp = NULL;
1575    struct filedescr *fdp;
1576
1577    m = PyImport_AddModuleObject(name);
1578    if (m == NULL)
1579        return NULL;
1580    if (Py_VerboseFlag)
1581        PySys_FormatStderr("import %U # directory %R\n",
1582                           name, pathname);
1583    file = get_sourcefile(pathname);
1584    if (file == NULL)
1585        return NULL;
1586    path_list = Py_BuildValue("[O]", file);
1587    if (path_list == NULL) {
1588        Py_DECREF(file);
1589        return NULL;
1590    }
1591    d = PyModule_GetDict(m);
1592    err = PyDict_SetItemString(d, "__file__", file);
1593    Py_DECREF(file);
1594    if (err == 0)
1595        err = PyDict_SetItemString(d, "__path__", path_list);
1596    if (err != 0) {
1597        Py_DECREF(path_list);
1598        return NULL;
1599    }
1600    fdp = find_module(name, initstr, path_list,
1601                      &bufobj, &fp, NULL);
1602    Py_DECREF(path_list);
1603    if (fdp == NULL) {
1604        if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1605            PyErr_Clear();
1606            Py_INCREF(m);
1607            return m;
1608        }
1609        else
1610            return NULL;
1611    }
1612    m = load_module(name, fp, bufobj, fdp->type, NULL);
1613    Py_XDECREF(bufobj);
1614    if (fp != NULL)
1615        fclose(fp);
1616    return m;
1617}
1618
1619
1620/* Helper to test for built-in module */
1621
1622static int
1623is_builtin(PyObject *name)
1624{
1625    int i, cmp;
1626    for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1627        cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
1628        if (cmp == 0) {
1629            if (PyImport_Inittab[i].initfunc == NULL)
1630                return -1;
1631            else
1632                return 1;
1633        }
1634    }
1635    return 0;
1636}
1637
1638
1639/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1640   possibly by fetching it from the path_importer_cache dict. If it
1641   wasn't yet cached, traverse path_hooks until a hook is found
1642   that can handle the path item. Return None if no hook could;
1643   this tells our caller it should fall back to the builtin
1644   import mechanism. Cache the result in path_importer_cache.
1645   Returns a borrowed reference. */
1646
1647static PyObject *
1648get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1649                  PyObject *p)
1650{
1651    PyObject *importer;
1652    Py_ssize_t j, nhooks;
1653
1654    /* These conditions are the caller's responsibility: */
1655    assert(PyList_Check(path_hooks));
1656    assert(PyDict_Check(path_importer_cache));
1657
1658    nhooks = PyList_Size(path_hooks);
1659    if (nhooks < 0)
1660        return NULL; /* Shouldn't happen */
1661
1662    importer = PyDict_GetItem(path_importer_cache, p);
1663    if (importer != NULL)
1664        return importer;
1665
1666    /* set path_importer_cache[p] to None to avoid recursion */
1667    if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1668        return NULL;
1669
1670    for (j = 0; j < nhooks; j++) {
1671        PyObject *hook = PyList_GetItem(path_hooks, j);
1672        if (hook == NULL)
1673            return NULL;
1674        importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1675        if (importer != NULL)
1676            break;
1677
1678        if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1679            return NULL;
1680        }
1681        PyErr_Clear();
1682    }
1683    if (importer == NULL) {
1684        importer = PyObject_CallFunctionObjArgs(
1685            (PyObject *)&PyNullImporter_Type, p, NULL
1686        );
1687        if (importer == NULL) {
1688            if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1689                PyErr_Clear();
1690                return Py_None;
1691            }
1692        }
1693    }
1694    if (importer != NULL) {
1695        int err = PyDict_SetItem(path_importer_cache, p, importer);
1696        Py_DECREF(importer);
1697        if (err != 0)
1698            return NULL;
1699    }
1700    return importer;
1701}
1702
1703PyAPI_FUNC(PyObject *)
1704PyImport_GetImporter(PyObject *path) {
1705    PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1706
1707    if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1708        if ((path_hooks = PySys_GetObject("path_hooks"))) {
1709            importer = get_path_importer(path_importer_cache,
1710                                         path_hooks, path);
1711        }
1712    }
1713    Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1714    return importer;
1715}
1716
1717/* Search the path (default sys.path) for a module.  Return the
1718   corresponding filedescr struct, and (via return arguments) the
1719   pathname and an open file.  Return NULL if the module is not found. */
1720
1721#ifdef MS_COREDLL
1722extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
1723                                         PyObject **p_path);
1724#endif
1725
1726/* Forward */
1727static int case_ok(PyObject *, Py_ssize_t, PyObject *);
1728static int find_init_module(PyObject *);
1729static struct filedescr importhookdescr = {"", "", IMP_HOOK};
1730
1731/* Get the path of a module: get its importer and call importer.find_module()
1732   hook, or check if the module if a package (if path/__init__.py exists).
1733
1734    -1: error: a Python error occurred
1735     0: ignore: an error occurred because of invalid data, but the error is not
1736        important enough to be reported.
1737     1: get path: module not found, but *buf contains its path
1738     2: found: *p_fd is the file descriptor (IMP_HOOK or PKG_DIRECTORY)
1739        and *buf is the path */
1740
1741static int
1742find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
1743                 PyObject *path_hooks, PyObject *path_importer_cache,
1744                 PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd)
1745{
1746    Py_UCS4 buf[MAXPATHLEN+1];
1747    PyObject *path_unicode, *filename;
1748    Py_ssize_t len;
1749    struct stat statbuf;
1750    static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1751
1752    if (PyUnicode_Check(path)) {
1753        Py_INCREF(path);
1754        path_unicode = path;
1755    }
1756    else if (PyBytes_Check(path)) {
1757        path_unicode = PyUnicode_DecodeFSDefaultAndSize(
1758            PyBytes_AS_STRING(path), PyBytes_GET_SIZE(path));
1759        if (path_unicode == NULL)
1760            return -1;
1761    }
1762    else
1763        return 0;
1764
1765    if (PyUnicode_READY(path_unicode))
1766        return -1;
1767
1768    len = PyUnicode_GET_LENGTH(path_unicode);
1769    if (!PyUnicode_AsUCS4(path_unicode, buf, Py_ARRAY_LENGTH(buf), 1)) {
1770        Py_DECREF(path_unicode);
1771        PyErr_Clear();
1772        return 0;
1773    }
1774    Py_DECREF(path_unicode);
1775
1776    if (Py_UCS4_strlen(buf) != len)
1777        return 0; /* path contains '\0' */
1778
1779    /* sys.path_hooks import hook */
1780    if (p_loader != NULL) {
1781        _Py_IDENTIFIER(find_module);
1782        PyObject *importer;
1783
1784        importer = get_path_importer(path_importer_cache,
1785                                     path_hooks, path);
1786        if (importer == NULL) {
1787            return -1;
1788        }
1789        /* Note: importer is a borrowed reference */
1790        if (importer != Py_None) {
1791            PyObject *loader;
1792            loader = _PyObject_CallMethodId(importer,
1793                                            &PyId_find_module, "O", fullname);
1794            if (loader == NULL)
1795                return -1;  /* error */
1796            if (loader != Py_None) {
1797                /* a loader was found */
1798                *p_loader = loader;
1799                *p_fd = &importhookdescr;
1800                return 2;
1801            }
1802            Py_DECREF(loader);
1803            return 0;
1804        }
1805    }
1806    /* no hook was found, use builtin import */
1807
1808    if (len > 0 && buf[len-1] != SEP
1809#ifdef ALTSEP
1810        && buf[len-1] != ALTSEP
1811#endif
1812        )
1813        buf[len++] = SEP;
1814    if (!PyUnicode_AsUCS4(name, buf+len, Py_ARRAY_LENGTH(buf)-len, 1)) {
1815        PyErr_Clear();
1816        return 0;
1817    }
1818    len += PyUnicode_GET_LENGTH(name);
1819
1820    filename = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
1821                                         buf, len);
1822    if (filename == NULL)
1823        return -1;
1824
1825    /* Check for package import (buf holds a directory name,
1826       and there's an __init__ module in that directory */
1827#ifdef HAVE_STAT
1828    if (_Py_stat(filename, &statbuf) == 0 &&         /* it exists */
1829        S_ISDIR(statbuf.st_mode))           /* it's a directory */
1830    {
1831        int match;
1832
1833        match = case_ok(filename, 0, name);
1834        if (match < 0) {
1835            Py_DECREF(filename);
1836            return -1;
1837        }
1838        if (match) { /* case matches */
1839            if (find_init_module(filename)) { /* and has __init__.py */
1840                *p_path = filename;
1841                *p_fd = &fd_package;
1842                return 2;
1843            }
1844            else {
1845                int err;
1846                err = PyErr_WarnFormat(PyExc_ImportWarning, 1,
1847                    "Not importing directory %R: missing __init__.py",
1848                    filename);
1849                if (err) {
1850                    Py_DECREF(filename);
1851                    return -1;
1852                }
1853            }
1854        }
1855    }
1856#endif
1857    *p_path = filename;
1858    return 1;
1859}
1860
1861/* Find a module in search_path_list. For each path, try
1862   find_module_path() or try each _PyImport_Filetab suffix.
1863
1864   If the module is found, return a file descriptor, write the path in
1865   *p_filename, write the pointer to the file object into *p_fp, and (if
1866   p_loader is not NULL) the loader into *p_loader.
1867
1868   Otherwise, raise an exception and return NULL. */
1869
1870static struct filedescr*
1871find_module_path_list(PyObject *fullname, PyObject *name,
1872                      PyObject *search_path_list, PyObject *path_hooks,
1873                      PyObject *path_importer_cache,
1874                      PyObject **p_path, FILE **p_fp, PyObject **p_loader)
1875{
1876    Py_ssize_t i, npath;
1877    struct filedescr *fdp = NULL;
1878    char *filemode;
1879    FILE *fp = NULL;
1880    PyObject *prefix, *filename;
1881    int match;
1882
1883    npath = PyList_Size(search_path_list);
1884    for (i = 0; i < npath; i++) {
1885        PyObject *path;
1886        int ok;
1887
1888        path = PyList_GetItem(search_path_list, i);
1889        if (path == NULL)
1890            return NULL;
1891
1892        prefix = NULL;
1893        ok = find_module_path(fullname, name, path,
1894                              path_hooks, path_importer_cache,
1895                              &prefix, p_loader, &fdp);
1896        if (ok < 0)
1897            return NULL;
1898        if (ok == 0)
1899            continue;
1900        if (ok == 2) {
1901            *p_path = prefix;
1902            return fdp;
1903        }
1904
1905        for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1906            struct stat statbuf;
1907
1908            filemode = fdp->mode;
1909            if (filemode[0] == 'U')
1910                filemode = "r" PY_STDIOTEXTMODE;
1911
1912            filename = PyUnicode_FromFormat("%U%s", prefix, fdp->suffix);
1913            if (filename == NULL) {
1914                Py_DECREF(prefix);
1915                return NULL;
1916            }
1917
1918            if (Py_VerboseFlag > 1)
1919                PySys_FormatStderr("# trying %R\n", filename);
1920
1921            if (_Py_stat(filename, &statbuf) == 0 &&         /* it exists */
1922                S_ISDIR(statbuf.st_mode))           /* it's a directory */
1923            {
1924                Py_DECREF(filename);
1925                continue;
1926            }
1927
1928            fp = _Py_fopen(filename, filemode);
1929            if (fp == NULL) {
1930                Py_DECREF(filename);
1931                if (PyErr_Occurred()) {
1932                    Py_DECREF(prefix);
1933                    return NULL;
1934                }
1935                continue;
1936            }
1937            match = case_ok(filename, -(Py_ssize_t)strlen(fdp->suffix), name);
1938            if (match < 0) {
1939                Py_DECREF(prefix);
1940                Py_DECREF(filename);
1941                return NULL;
1942            }
1943            if (match) {
1944                Py_DECREF(prefix);
1945                *p_path = filename;
1946                *p_fp = fp;
1947                return fdp;
1948            }
1949            Py_DECREF(filename);
1950
1951            fclose(fp);
1952            fp = NULL;
1953        }
1954        Py_DECREF(prefix);
1955    }
1956    PyErr_Format(PyExc_ImportError,
1957                 "No module named %R", name);
1958    return NULL;
1959}
1960
1961/* Find a module:
1962
1963   - try find_module() of each sys.meta_path hook
1964   - try find_frozen()
1965   - try is_builtin()
1966   - try _PyWin_FindRegisteredModule() (Windows only)
1967   - otherwise, call find_module_path_list() with search_path_list (if not
1968     NULL) or sys.path
1969
1970   fullname can be NULL, but only if p_loader is NULL.
1971
1972   Return:
1973
1974   - &fd_builtin (C_BUILTIN) if it is a builtin
1975   - &fd_frozen (PY_FROZEN) if it is frozen
1976   - &fd_package (PKG_DIRECTORY) and write the filename into *p_path
1977     if it is a package
1978   - &importhookdescr (IMP_HOOK) and write the loader into *p_loader if a
1979     importer loader was found
1980   - a file descriptor (PY_SOURCE, PY_COMPILED, C_EXTENSION, PY_RESOURCE or
1981     PY_CODERESOURCE: see _PyImport_Filetab), write the filename into
1982     *p_path and the pointer to the open file into *p_fp
1983   - NULL on error
1984
1985   By default, *p_path, *p_fp and *p_loader (if set) are set to NULL.
1986   Eg. *p_path is set to NULL for a builtin package.
1987*/
1988
1989static struct filedescr *
1990find_module(PyObject *fullname, PyObject *name, PyObject *search_path_list,
1991            PyObject **p_path, FILE **p_fp, PyObject **p_loader)
1992{
1993    Py_ssize_t i, npath;
1994    static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1995    static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1996    PyObject *path_hooks, *path_importer_cache;
1997
1998    *p_path = NULL;
1999    *p_fp = NULL;
2000    if (p_loader != NULL)
2001        *p_loader = NULL;
2002
2003    if (PyUnicode_GET_LENGTH(name) > MAXPATHLEN) {
2004        PyErr_SetString(PyExc_OverflowError,
2005                        "module name is too long");
2006        return NULL;
2007    }
2008
2009    /* sys.meta_path import hook */
2010    if (p_loader != NULL) {
2011        _Py_IDENTIFIER(find_module);
2012        PyObject *meta_path;
2013
2014        meta_path = PySys_GetObject("meta_path");
2015        if (meta_path == NULL || !PyList_Check(meta_path)) {
2016            PyErr_SetString(PyExc_RuntimeError,
2017                            "sys.meta_path must be a list of "
2018                            "import hooks");
2019            return NULL;
2020        }
2021        Py_INCREF(meta_path);  /* zap guard */
2022        npath = PyList_Size(meta_path);
2023        for (i = 0; i < npath; i++) {
2024            PyObject *loader;
2025            PyObject *hook = PyList_GetItem(meta_path, i);
2026            loader = _PyObject_CallMethodId(hook, &PyId_find_module,
2027                                         "OO", fullname,
2028                                         search_path_list != NULL ?
2029                                         search_path_list : Py_None);
2030            if (loader == NULL) {
2031                Py_DECREF(meta_path);
2032                return NULL;  /* true error */
2033            }
2034            if (loader != Py_None) {
2035                /* a loader was found */
2036                *p_loader = loader;
2037                Py_DECREF(meta_path);
2038                return &importhookdescr;
2039            }
2040            Py_DECREF(loader);
2041        }
2042        Py_DECREF(meta_path);
2043    }
2044
2045    if (find_frozen(fullname) != NULL)
2046        return &fd_frozen;
2047
2048    if (search_path_list == NULL) {
2049#ifdef MS_COREDLL
2050        FILE *fp;
2051        struct filedescr *fdp;
2052#endif
2053        if (is_builtin(name))
2054            return &fd_builtin;
2055#ifdef MS_COREDLL
2056        fp = _PyWin_FindRegisteredModule(name, &fdp, p_path);
2057        if (fp != NULL) {
2058            *p_fp = fp;
2059            return fdp;
2060        }
2061        else if (PyErr_Occurred())
2062            return NULL;
2063#endif
2064        search_path_list = PySys_GetObject("path");
2065    }
2066
2067    if (search_path_list == NULL || !PyList_Check(search_path_list)) {
2068        PyErr_SetString(PyExc_RuntimeError,
2069                        "sys.path must be a list of directory names");
2070        return NULL;
2071    }
2072
2073    path_hooks = PySys_GetObject("path_hooks");
2074    if (path_hooks == NULL || !PyList_Check(path_hooks)) {
2075        PyErr_SetString(PyExc_RuntimeError,
2076                        "sys.path_hooks must be a list of "
2077                        "import hooks");
2078        return NULL;
2079    }
2080    path_importer_cache = PySys_GetObject("path_importer_cache");
2081    if (path_importer_cache == NULL ||
2082        !PyDict_Check(path_importer_cache)) {
2083        PyErr_SetString(PyExc_RuntimeError,
2084                        "sys.path_importer_cache must be a dict");
2085        return NULL;
2086    }
2087
2088    return find_module_path_list(fullname, name,
2089                                 search_path_list, path_hooks,
2090                                 path_importer_cache,
2091                                 p_path, p_fp, p_loader);
2092}
2093
2094/* case_bytes(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
2095 * The arguments here are tricky, best shown by example:
2096 *    /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
2097 *    ^                      ^                   ^    ^
2098 *    |--------------------- buf ---------------------|
2099 *    |------------------- len ------------------|
2100 *                           |------ name -------|
2101 *                           |----- namelen -----|
2102 * buf is the full path, but len only counts up to (& exclusive of) the
2103 * extension.  name is the module name, also exclusive of extension.
2104 *
2105 * We've already done a successful stat() or fopen() on buf, so know that
2106 * there's some match, possibly case-insensitive.
2107 *
2108 * case_bytes() is to return 1 if there's a case-sensitive match for
2109 * name, else 0.  case_bytes() is also to return 1 if envar PYTHONCASEOK
2110 * exists.
2111 *
2112 * case_bytes() is used to implement case-sensitive import semantics even
2113 * on platforms with case-insensitive filesystems.  It's trivial to implement
2114 * for case-sensitive filesystems.  It's pretty much a cross-platform
2115 * nightmare for systems with case-insensitive filesystems.
2116 */
2117
2118/* First we may need a pile of platform-specific header files; the sequence
2119 * of #if's here should match the sequence in the body of case_bytes().
2120 */
2121#if defined(MS_WINDOWS)
2122#include <windows.h>
2123
2124#elif defined(DJGPP)
2125#include <dir.h>
2126
2127#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
2128#include <sys/types.h>
2129#include <dirent.h>
2130
2131#elif defined(PYOS_OS2)
2132#define INCL_DOS
2133#define INCL_DOSERRORS
2134#define INCL_NOPMAPI
2135#include <os2.h>
2136#endif
2137
2138#if defined(DJGPP) \
2139    || ((defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) \
2140        && defined(HAVE_DIRENT_H)) \
2141    || defined(PYOS_OS2)
2142#  define USE_CASE_OK_BYTES
2143#endif
2144
2145
2146#ifdef USE_CASE_OK_BYTES
2147static int
2148case_bytes(char *buf, Py_ssize_t len, Py_ssize_t namelen, const char *name)
2149{
2150/* Pick a platform-specific implementation; the sequence of #if's here should
2151 * match the sequence just above.
2152 */
2153
2154/* DJGPP */
2155#if defined(DJGPP)
2156    struct ffblk ffblk;
2157    int done;
2158
2159    if (Py_GETENV("PYTHONCASEOK") != NULL)
2160        return 1;
2161
2162    done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
2163    if (done) {
2164        PyErr_Format(PyExc_NameError,
2165          "Can't find file for module %.100s\n(filename %.300s)",
2166          name, buf);
2167        return -1;
2168    }
2169    return strncmp(ffblk.ff_name, name, namelen) == 0;
2170
2171/* new-fangled macintosh (macosx) or Cygwin */
2172#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
2173    DIR *dirp;
2174    struct dirent *dp;
2175    char dirname[MAXPATHLEN + 1];
2176    const int dirlen = len - namelen - 1; /* don't want trailing SEP */
2177
2178    if (Py_GETENV("PYTHONCASEOK") != NULL)
2179        return 1;
2180
2181    /* Copy the dir component into dirname; substitute "." if empty */
2182    if (dirlen <= 0) {
2183        dirname[0] = '.';
2184        dirname[1] = '\0';
2185    }
2186    else {
2187        assert(dirlen <= MAXPATHLEN);
2188        memcpy(dirname, buf, dirlen);
2189        dirname[dirlen] = '\0';
2190    }
2191    /* Open the directory and search the entries for an exact match. */
2192    dirp = opendir(dirname);
2193    if (dirp) {
2194        char *nameWithExt = buf + len - namelen;
2195        while ((dp = readdir(dirp)) != NULL) {
2196            const int thislen =
2197#ifdef _DIRENT_HAVE_D_NAMELEN
2198                                    dp->d_namlen;
2199#else
2200                                    strlen(dp->d_name);
2201#endif
2202            if (thislen >= namelen &&
2203                strcmp(dp->d_name, nameWithExt) == 0) {
2204                (void)closedir(dirp);
2205                return 1; /* Found */
2206            }
2207        }
2208        (void)closedir(dirp);
2209    }
2210    return 0 ; /* Not found */
2211
2212/* OS/2 */
2213#elif defined(PYOS_OS2)
2214    HDIR hdir = 1;
2215    ULONG srchcnt = 1;
2216    FILEFINDBUF3 ffbuf;
2217    APIRET rc;
2218
2219    if (Py_GETENV("PYTHONCASEOK") != NULL)
2220        return 1;
2221
2222    rc = DosFindFirst(buf,
2223                      &hdir,
2224                      FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2225                      &ffbuf, sizeof(ffbuf),
2226                      &srchcnt,
2227                      FIL_STANDARD);
2228    if (rc != NO_ERROR)
2229        return 0;
2230    return strncmp(ffbuf.achName, name, namelen) == 0;
2231
2232/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
2233#else
2234#   error "USE_CASE_OK_BYTES is not correctly defined"
2235#endif
2236}
2237#endif
2238
2239/*
2240 * Check if a filename case matchs the name case. We've already done a
2241 * successful stat() or fopen() on buf, so know that there's some match,
2242 * possibly case-insensitive.
2243 *
2244 * case_ok() is to return 1 if there's a case-sensitive match for name, 0 if it
2245 * the filename doesn't match, or -1 on error.  case_ok() is also to return 1
2246 * if envar PYTHONCASEOK exists.
2247 *
2248 * case_ok() is used to implement case-sensitive import semantics even
2249 * on platforms with case-insensitive filesystems.  It's trivial to implement
2250 * for case-sensitive filesystems.  It's pretty much a cross-platform
2251 * nightmare for systems with case-insensitive filesystems.
2252 */
2253
2254static int
2255case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
2256{
2257#ifdef MS_WINDOWS
2258    WIN32_FIND_DATAW data;
2259    HANDLE h;
2260    int cmp;
2261    wchar_t *wname;
2262    Py_ssize_t wname_len;
2263
2264    if (Py_GETENV("PYTHONCASEOK") != NULL)
2265        return 1;
2266
2267    h = FindFirstFileW(PyUnicode_AS_UNICODE(filename), &data);
2268    if (h == INVALID_HANDLE_VALUE) {
2269        PyErr_Format(PyExc_NameError,
2270          "Can't find file for module %R\n(filename %R)",
2271          name, filename);
2272        return 0;
2273    }
2274    FindClose(h);
2275
2276    wname = PyUnicode_AsUnicodeAndSize(name, &wname_len);
2277    if (wname == NULL)
2278        return -1;
2279
2280    cmp = wcsncmp(data.cFileName, wname, wname_len);
2281    return cmp == 0;
2282#elif defined(USE_CASE_OK_BYTES)
2283    int match;
2284    PyObject *filebytes, *namebytes;
2285    filebytes = PyUnicode_EncodeFSDefault(filename);
2286    if (filebytes == NULL)
2287        return -1;
2288    namebytes = PyUnicode_EncodeFSDefault(name);
2289    if (namebytes == NULL) {
2290        Py_DECREF(filebytes);
2291        return -1;
2292    }
2293    match = case_bytes(
2294        PyBytes_AS_STRING(filebytes),
2295        PyBytes_GET_SIZE(filebytes) + prefix_delta,
2296        PyBytes_GET_SIZE(namebytes),
2297        PyBytes_AS_STRING(namebytes));
2298    Py_DECREF(filebytes);
2299    Py_DECREF(namebytes);
2300    return match;
2301#else
2302    /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
2303    return 1;
2304
2305#endif
2306}
2307
2308#ifdef HAVE_STAT
2309
2310/* Helper to look for __init__.py or __init__.py[co] in potential package.
2311   Return 1 if __init__ was found, 0 if not, or -1 on error. */
2312static int
2313find_init_module(PyObject *directory)
2314{
2315    struct stat statbuf;
2316    PyObject *filename;
2317    int match;
2318
2319    filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
2320    if (filename == NULL)
2321        return -1;
2322    if (_Py_stat(filename, &statbuf) == 0) {
2323        /* 3=len(".py") */
2324        match = case_ok(filename, -3, initstr);
2325        if (match < 0) {
2326            Py_DECREF(filename);
2327            return -1;
2328        }
2329        if (match) {
2330            Py_DECREF(filename);
2331            return 1;
2332        }
2333    }
2334    Py_DECREF(filename);
2335
2336    filename = PyUnicode_FromFormat("%U%c__init__.py%c",
2337        directory, SEP, Py_OptimizeFlag ? 'o' : 'c');
2338    if (filename == NULL)
2339        return -1;
2340    if (_Py_stat(filename, &statbuf) == 0) {
2341        /* 4=len(".pyc") */
2342        match = case_ok(filename, -4, initstr);
2343        if (match < 0) {
2344            Py_DECREF(filename);
2345            return -1;
2346        }
2347        if (match) {
2348            Py_DECREF(filename);
2349            return 1;
2350        }
2351    }
2352    Py_DECREF(filename);
2353    return 0;
2354}
2355
2356#endif /* HAVE_STAT */
2357
2358
2359static int init_builtin(PyObject *); /* Forward */
2360
2361static PyObject*
2362load_builtin(PyObject *name, int type)
2363{
2364    PyObject *m, *modules;
2365    int err;
2366
2367    if (type == C_BUILTIN)
2368        err = init_builtin(name);
2369    else
2370        err = PyImport_ImportFrozenModuleObject(name);
2371    if (err < 0)
2372        return NULL;
2373    if (err == 0) {
2374        PyErr_Format(PyExc_ImportError,
2375                "Purported %s module %R not found",
2376                type == C_BUILTIN ? "builtin" : "frozen",
2377                name);
2378        return NULL;
2379    }
2380
2381    modules = PyImport_GetModuleDict();
2382    m = PyDict_GetItem(modules, name);
2383    if (m == NULL) {
2384        PyErr_Format(
2385                PyExc_ImportError,
2386                "%s module %R not properly initialized",
2387                type == C_BUILTIN ? "builtin" : "frozen",
2388                name);
2389        return NULL;
2390    }
2391    Py_INCREF(m);
2392    return m;
2393}
2394
2395/* Load an external module using the default search path and return
2396   its module object WITH INCREMENTED REFERENCE COUNT */
2397
2398static PyObject *
2399load_module(PyObject *name, FILE *fp, PyObject *pathname, int type, PyObject *loader)
2400{
2401    PyObject *m;
2402
2403    /* First check that there's an open file (if we need one)  */
2404    switch (type) {
2405    case PY_SOURCE:
2406    case PY_COMPILED:
2407        if (fp == NULL) {
2408            PyErr_Format(PyExc_ValueError,
2409                         "file object required for import (type code %d)",
2410                         type);
2411            return NULL;
2412        }
2413    }
2414
2415    switch (type) {
2416
2417    case PY_SOURCE:
2418        m = load_source_module(name, pathname, fp);
2419        break;
2420
2421    case PY_COMPILED:
2422        m = load_compiled_module(name, pathname, fp);
2423        break;
2424
2425#ifdef HAVE_DYNAMIC_LOADING
2426    case C_EXTENSION:
2427        m = _PyImport_LoadDynamicModule(name, pathname, fp);
2428        break;
2429#endif
2430
2431    case PKG_DIRECTORY:
2432        m = load_package(name, pathname);
2433        break;
2434
2435    case C_BUILTIN:
2436    case PY_FROZEN:
2437        m = load_builtin(name, type);
2438        break;
2439
2440    case IMP_HOOK: {
2441        _Py_IDENTIFIER(load_module);
2442        if (loader == NULL) {
2443            PyErr_SetString(PyExc_ImportError,
2444                            "import hook without loader");
2445            return NULL;
2446        }
2447        m = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
2448        break;
2449    }
2450
2451    default:
2452        PyErr_Format(PyExc_ImportError,
2453                     "Don't know how to import %R (type code %d)",
2454                      name, type);
2455        m = NULL;
2456
2457    }
2458
2459    return m;
2460}
2461
2462
2463/* Initialize a built-in module.
2464   Return 1 for success, 0 if the module is not found, and -1 with
2465   an exception set if the initialization failed. */
2466
2467static int
2468init_builtin(PyObject *name)
2469{
2470    struct _inittab *p;
2471
2472    if (_PyImport_FindExtensionObject(name, name) != NULL)
2473        return 1;
2474
2475    for (p = PyImport_Inittab; p->name != NULL; p++) {
2476        PyObject *mod;
2477        if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
2478            if (p->initfunc == NULL) {
2479                PyErr_Format(PyExc_ImportError,
2480                    "Cannot re-init internal module %R",
2481                    name);
2482                return -1;
2483            }
2484            if (Py_VerboseFlag)
2485                PySys_FormatStderr("import %U # builtin\n", name);
2486            mod = (*p->initfunc)();
2487            if (mod == 0)
2488                return -1;
2489            if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
2490                return -1;
2491            /* FixupExtension has put the module into sys.modules,
2492               so we can release our own reference. */
2493            Py_DECREF(mod);
2494            return 1;
2495        }
2496    }
2497    return 0;
2498}
2499
2500
2501/* Frozen modules */
2502
2503static struct _frozen *
2504find_frozen(PyObject *name)
2505{
2506    struct _frozen *p;
2507
2508    if (name == NULL)
2509        return NULL;
2510
2511    for (p = PyImport_FrozenModules; ; p++) {
2512        if (p->name == NULL)
2513            return NULL;
2514        if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
2515            break;
2516    }
2517    return p;
2518}
2519
2520static PyObject *
2521get_frozen_object(PyObject *name)
2522{
2523    struct _frozen *p = find_frozen(name);
2524    int size;
2525
2526    if (p == NULL) {
2527        PyErr_Format(PyExc_ImportError,
2528                     "No such frozen object named %R",
2529                     name);
2530        return NULL;
2531    }
2532    if (p->code == NULL) {
2533        PyErr_Format(PyExc_ImportError,
2534                     "Excluded frozen object named %R",
2535                     name);
2536        return NULL;
2537    }
2538    size = p->size;
2539    if (size < 0)
2540        size = -size;
2541    return PyMarshal_ReadObjectFromString((char *)p->code, size);
2542}
2543
2544static PyObject *
2545is_frozen_package(PyObject *name)
2546{
2547    struct _frozen *p = find_frozen(name);
2548    int size;
2549
2550    if (p == NULL) {
2551        PyErr_Format(PyExc_ImportError,
2552                     "No such frozen object named %R",
2553                     name);
2554        return NULL;
2555    }
2556
2557    size = p->size;
2558
2559    if (size < 0)
2560        Py_RETURN_TRUE;
2561    else
2562        Py_RETURN_FALSE;
2563}
2564
2565
2566/* Initialize a frozen module.
2567   Return 1 for success, 0 if the module is not found, and -1 with
2568   an exception set if the initialization failed.
2569   This function is also used from frozenmain.c */
2570
2571int
2572PyImport_ImportFrozenModuleObject(PyObject *name)
2573{
2574    struct _frozen *p;
2575    PyObject *co, *m, *path;
2576    int ispackage;
2577    int size;
2578
2579    p = find_frozen(name);
2580
2581    if (p == NULL)
2582        return 0;
2583    if (p->code == NULL) {
2584        PyErr_Format(PyExc_ImportError,
2585                     "Excluded frozen object named %R",
2586                     name);
2587        return -1;
2588    }
2589    size = p->size;
2590    ispackage = (size < 0);
2591    if (ispackage)
2592        size = -size;
2593    if (Py_VerboseFlag)
2594        PySys_FormatStderr("import %U # frozen%s\n",
2595            name, ispackage ? " package" : "");
2596    co = PyMarshal_ReadObjectFromString((char *)p->code, size);
2597    if (co == NULL)
2598        return -1;
2599    if (!PyCode_Check(co)) {
2600        PyErr_Format(PyExc_TypeError,
2601                     "frozen object %R is not a code object",
2602                     name);
2603        goto err_return;
2604    }
2605    if (ispackage) {
2606        /* Set __path__ to the package name */
2607        PyObject *d, *l;
2608        int err;
2609        m = PyImport_AddModuleObject(name);
2610        if (m == NULL)
2611            goto err_return;
2612        d = PyModule_GetDict(m);
2613        l = PyList_New(1);
2614        if (l == NULL) {
2615            goto err_return;
2616        }
2617        Py_INCREF(name);
2618        PyList_SET_ITEM(l, 0, name);
2619        err = PyDict_SetItemString(d, "__path__", l);
2620        Py_DECREF(l);
2621        if (err != 0)
2622            goto err_return;
2623    }
2624    path = PyUnicode_FromString("<frozen>");
2625    if (path == NULL)
2626        goto err_return;
2627    m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
2628    Py_DECREF(path);
2629    if (m == NULL)
2630        goto err_return;
2631    Py_DECREF(co);
2632    Py_DECREF(m);
2633    return 1;
2634err_return:
2635    Py_DECREF(co);
2636    return -1;
2637}
2638
2639int
2640PyImport_ImportFrozenModule(char *name)
2641{
2642    PyObject *nameobj;
2643    int ret;
2644    nameobj = PyUnicode_InternFromString(name);
2645    if (nameobj == NULL)
2646        return -1;
2647    ret = PyImport_ImportFrozenModuleObject(nameobj);
2648    Py_DECREF(nameobj);
2649    return ret;
2650}
2651
2652
2653/* Import a module, either built-in, frozen, or external, and return
2654   its module object WITH INCREMENTED REFERENCE COUNT */
2655
2656PyObject *
2657PyImport_ImportModule(const char *name)
2658{
2659    PyObject *pname;
2660    PyObject *result;
2661
2662    pname = PyUnicode_FromString(name);
2663    if (pname == NULL)
2664        return NULL;
2665    result = PyImport_Import(pname);
2666    Py_DECREF(pname);
2667    return result;
2668}
2669
2670/* Import a module without blocking
2671 *
2672 * At first it tries to fetch the module from sys.modules. If the module was
2673 * never loaded before it loads it with PyImport_ImportModule() unless another
2674 * thread holds the import lock. In the latter case the function raises an
2675 * ImportError instead of blocking.
2676 *
2677 * Returns the module object with incremented ref count.
2678 */
2679PyObject *
2680PyImport_ImportModuleNoBlock(const char *name)
2681{
2682    PyObject *nameobj, *modules, *result;
2683#ifdef WITH_THREAD
2684    long me;
2685#endif
2686
2687    /* Try to get the module from sys.modules[name] */
2688    modules = PyImport_GetModuleDict();
2689    if (modules == NULL)
2690        return NULL;
2691
2692    nameobj = PyUnicode_FromString(name);
2693    if (nameobj == NULL)
2694        return NULL;
2695    result = PyDict_GetItem(modules, nameobj);
2696    if (result != NULL) {
2697        Py_DECREF(nameobj);
2698        Py_INCREF(result);
2699        return result;
2700    }
2701    PyErr_Clear();
2702#ifdef WITH_THREAD
2703    /* check the import lock
2704     * me might be -1 but I ignore the error here, the lock function
2705     * takes care of the problem */
2706    me = PyThread_get_thread_ident();
2707    if (import_lock_thread == -1 || import_lock_thread == me) {
2708        /* no thread or me is holding the lock */
2709        result = PyImport_Import(nameobj);
2710    }
2711    else {
2712        PyErr_Format(PyExc_ImportError,
2713                     "Failed to import %R because the import lock"
2714                     "is held by another thread.",
2715                     nameobj);
2716        result = NULL;
2717    }
2718#else
2719    result = PyImport_Import(nameobj);
2720#endif
2721    Py_DECREF(nameobj);
2722    return result;
2723}
2724
2725/* Forward declarations for helper routines */
2726static PyObject *get_parent(PyObject *globals,
2727                            PyObject **p_name,
2728                            int level);
2729static PyObject *load_next(PyObject *mod, PyObject *altmod,
2730                           PyObject *inputname, PyObject **p_outputname,
2731                           Py_UCS4 *buf, Py_ssize_t *p_buflen,
2732                           Py_ssize_t bufsize);
2733static int mark_miss(PyObject *name);
2734static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
2735                           PyObject *buf, int recursive);
2736static PyObject * import_submodule(PyObject *mod, PyObject *name,
2737                                   PyObject *fullname);
2738
2739/* The Magnum Opus of dotted-name import :-) */
2740
2741static PyObject *
2742import_module_level(PyObject *name, PyObject *globals, PyObject *locals,
2743                    PyObject *fromlist, int level)
2744{
2745    Py_UCS4 buf[MAXPATHLEN+1];
2746    Py_ssize_t buflen;
2747    Py_ssize_t bufsize = MAXPATHLEN+1;
2748    PyObject *parent, *head, *next, *tail, *inputname, *outputname;
2749    PyObject *parent_name, *ensure_name;
2750    Py_ssize_t sep, altsep;
2751
2752    if (PyUnicode_READY(name))
2753        return NULL;
2754
2755    sep = PyUnicode_FindChar(name, SEP, 0, PyUnicode_GET_LENGTH(name), 1);
2756    if (sep == -2)
2757        return NULL;
2758#ifdef ALTSEP
2759    altsep = PyUnicode_FindChar(name, ALTSEP, 0, PyUnicode_GET_LENGTH(name), 1);
2760    if (altsep == -2)
2761        return NULL;
2762#else
2763    altsep = -1;
2764#endif
2765    if (sep != -1 || altsep != -1)
2766    {
2767        PyErr_SetString(PyExc_ImportError,
2768                        "Import by filename is not supported.");
2769        return NULL;
2770    }
2771
2772    parent = get_parent(globals, &parent_name, level);
2773    if (parent == NULL) {
2774        return NULL;
2775    }
2776
2777    if (PyUnicode_READY(parent_name))
2778        return NULL;
2779    buflen = PyUnicode_GET_LENGTH(parent_name);
2780    if (!PyUnicode_AsUCS4(parent_name, buf, Py_ARRAY_LENGTH(buf), 1)) {
2781        Py_DECREF(parent_name);
2782        PyErr_SetString(PyExc_ValueError,
2783                        "Module name too long");
2784        return NULL;
2785    }
2786    Py_DECREF(parent_name);
2787
2788    head = load_next(parent, level < 0 ? Py_None : parent, name, &outputname,
2789                     buf, &buflen, bufsize);
2790    if (head == NULL)
2791        return NULL;
2792
2793    tail = head;
2794    Py_INCREF(tail);
2795
2796    if (outputname != NULL) {
2797        while (1) {
2798            inputname = outputname;
2799            next = load_next(tail, tail, inputname, &outputname,
2800                             buf, &buflen, bufsize);
2801            Py_DECREF(tail);
2802            Py_DECREF(inputname);
2803            if (next == NULL) {
2804                Py_DECREF(head);
2805                return NULL;
2806            }
2807            tail = next;
2808
2809            if (outputname == NULL) {
2810                break;
2811            }
2812        }
2813    }
2814    if (tail == Py_None) {
2815        /* If tail is Py_None, both get_parent and load_next found
2816           an empty module name: someone called __import__("") or
2817           doctored faulty bytecode */
2818        Py_DECREF(tail);
2819        Py_DECREF(head);
2820        PyErr_SetString(PyExc_ValueError, "Empty module name");
2821        return NULL;
2822    }
2823
2824    if (fromlist != NULL) {
2825        if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2826            fromlist = NULL;
2827    }
2828
2829    if (fromlist == NULL) {
2830        Py_DECREF(tail);
2831        return head;
2832    }
2833
2834    Py_DECREF(head);
2835
2836    ensure_name = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
2837                                            buf, Py_UCS4_strlen(buf));
2838    if (ensure_name == NULL) {
2839        Py_DECREF(tail);
2840        return NULL;
2841    }
2842    if (!ensure_fromlist(tail, fromlist, ensure_name, 0)) {
2843        Py_DECREF(tail);
2844        Py_DECREF(ensure_name);
2845        return NULL;
2846    }
2847    Py_DECREF(ensure_name);
2848
2849    return tail;
2850}
2851
2852PyObject *
2853PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
2854                                 PyObject *locals, PyObject *fromlist,
2855                                 int level)
2856{
2857    PyObject *mod;
2858    _PyImport_AcquireLock();
2859    mod = import_module_level(name, globals, locals, fromlist, level);
2860    if (_PyImport_ReleaseLock() < 0) {
2861        Py_XDECREF(mod);
2862        PyErr_SetString(PyExc_RuntimeError,
2863                        "not holding the import lock");
2864        return NULL;
2865    }
2866    return mod;
2867}
2868
2869PyObject *
2870PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
2871                           PyObject *fromlist, int level)
2872{
2873    PyObject *nameobj, *mod;
2874    nameobj = PyUnicode_FromString(name);
2875    if (nameobj == NULL)
2876        return NULL;
2877    mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
2878                                           fromlist, level);
2879    Py_DECREF(nameobj);
2880    return mod;
2881}
2882
2883
2884/* Return the package that an import is being performed in.  If globals comes
2885   from the module foo.bar.bat (not itself a package), this returns the
2886   sys.modules entry for foo.bar.  If globals is from a package's __init__.py,
2887   the package's entry in sys.modules is returned, as a borrowed reference.
2888
2889   The name of the returned package is returned in *p_name.
2890
2891   If globals doesn't come from a package or a module in a package, or a
2892   corresponding entry is not found in sys.modules, Py_None is returned.
2893*/
2894static PyObject *
2895get_parent(PyObject *globals, PyObject **p_name, int level)
2896{
2897    PyObject *nameobj;
2898
2899    static PyObject *namestr = NULL;
2900    static PyObject *pathstr = NULL;
2901    static PyObject *pkgstr = NULL;
2902    PyObject *pkgname, *modname, *modpath, *modules, *parent;
2903    int orig_level = level;
2904
2905    if (globals == NULL || !PyDict_Check(globals) || !level)
2906        goto return_none;
2907
2908    if (namestr == NULL) {
2909        namestr = PyUnicode_InternFromString("__name__");
2910        if (namestr == NULL)
2911            return NULL;
2912    }
2913    if (pathstr == NULL) {
2914        pathstr = PyUnicode_InternFromString("__path__");
2915        if (pathstr == NULL)
2916            return NULL;
2917    }
2918    if (pkgstr == NULL) {
2919        pkgstr = PyUnicode_InternFromString("__package__");
2920        if (pkgstr == NULL)
2921            return NULL;
2922    }
2923
2924    pkgname = PyDict_GetItem(globals, pkgstr);
2925
2926    if ((pkgname != NULL) && (pkgname != Py_None)) {
2927        /* __package__ is set, so use it */
2928        if (!PyUnicode_Check(pkgname)) {
2929            PyErr_SetString(PyExc_ValueError,
2930                            "__package__ set to non-string");
2931            return NULL;
2932        }
2933        if (PyUnicode_GET_LENGTH(pkgname) == 0) {
2934            if (level > 0) {
2935                PyErr_SetString(PyExc_ValueError,
2936                    "Attempted relative import in non-package");
2937                return NULL;
2938            }
2939            goto return_none;
2940        }
2941        Py_INCREF(pkgname);
2942        nameobj = pkgname;
2943    } else {
2944        /* __package__ not set, so figure it out and set it */
2945        modname = PyDict_GetItem(globals, namestr);
2946        if (modname == NULL || !PyUnicode_Check(modname))
2947            goto return_none;
2948
2949        modpath = PyDict_GetItem(globals, pathstr);
2950        if (modpath != NULL) {
2951            /* __path__ is set, so modname is already the package name */
2952            int error;
2953
2954            error = PyDict_SetItem(globals, pkgstr, modname);
2955            if (error) {
2956                PyErr_SetString(PyExc_ValueError,
2957                                "Could not set __package__");
2958                return NULL;
2959            }
2960            Py_INCREF(modname);
2961            nameobj = modname;
2962        } else {
2963            /* Normal module, so work out the package name if any */
2964            Py_ssize_t len;
2965            len = PyUnicode_FindChar(modname, '.',
2966                                     0, PyUnicode_GET_LENGTH(modname), -1);
2967            if (len == -2)
2968                return NULL;
2969            if (len < 0) {
2970                if (level > 0) {
2971                    PyErr_SetString(PyExc_ValueError,
2972                        "Attempted relative import in non-package");
2973                    return NULL;
2974                }
2975                if (PyDict_SetItem(globals, pkgstr, Py_None)) {
2976                    PyErr_SetString(PyExc_ValueError,
2977                        "Could not set __package__");
2978                    return NULL;
2979                }
2980                goto return_none;
2981            }
2982            pkgname = PyUnicode_Substring(modname, 0, len);
2983            if (pkgname == NULL)
2984                return NULL;
2985            if (PyDict_SetItem(globals, pkgstr, pkgname)) {
2986                Py_DECREF(pkgname);
2987                PyErr_SetString(PyExc_ValueError,
2988                                "Could not set __package__");
2989                return NULL;
2990            }
2991            nameobj = pkgname;
2992        }
2993    }
2994    if (level > 1) {
2995        Py_ssize_t dot, end = PyUnicode_GET_LENGTH(nameobj);
2996        PyObject *newname;
2997        while (--level > 0) {
2998            dot = PyUnicode_FindChar(nameobj, '.', 0, end, -1);
2999            if (dot == -2) {
3000                Py_DECREF(nameobj);
3001                return NULL;
3002            }
3003            if (dot < 0) {
3004                Py_DECREF(nameobj);
3005                PyErr_SetString(PyExc_ValueError,
3006                    "Attempted relative import beyond "
3007                    "toplevel package");
3008                return NULL;
3009            }
3010            end = dot;
3011        }
3012        newname = PyUnicode_Substring(nameobj, 0, end);
3013        Py_DECREF(nameobj);
3014        if (newname == NULL)
3015            return NULL;
3016        nameobj = newname;
3017    }
3018
3019    modules = PyImport_GetModuleDict();
3020    parent = PyDict_GetItem(modules, nameobj);
3021    if (parent == NULL) {
3022        int err;
3023
3024        if (orig_level >= 1) {
3025            PyErr_Format(PyExc_SystemError,
3026                "Parent module %R not loaded, "
3027                "cannot perform relative import", nameobj);
3028            Py_DECREF(nameobj);
3029            return NULL;
3030        }
3031
3032        err = PyErr_WarnFormat(
3033            PyExc_RuntimeWarning, 1,
3034            "Parent module %R not found while handling absolute import",
3035            nameobj);
3036        Py_DECREF(nameobj);
3037        if (err)
3038            return NULL;
3039
3040        goto return_none;
3041    }
3042    *p_name = nameobj;
3043    return parent;
3044    /* We expect, but can't guarantee, if parent != None, that:
3045       - parent.__name__ == name
3046       - parent.__dict__ is globals
3047       If this is violated...  Who cares? */
3048
3049return_none:
3050    nameobj = PyUnicode_New(0, 0);
3051    if (nameobj == NULL)
3052        return NULL;
3053    *p_name = nameobj;
3054    return Py_None;
3055}
3056
3057/* altmod is either None or same as mod */
3058static PyObject *
3059load_next(PyObject *mod, PyObject *altmod,
3060          PyObject *inputname, PyObject **p_outputname,
3061          Py_UCS4 *buf, Py_ssize_t *p_buflen, Py_ssize_t bufsize)
3062{
3063    Py_UCS4 *dot;
3064    Py_ssize_t len;
3065    Py_UCS4 *p;
3066    PyObject *fullname, *name, *result, *mark_name;
3067    Py_UCS4 *nameuni;
3068
3069    *p_outputname = NULL;
3070
3071    if (PyUnicode_GET_LENGTH(inputname) == 0) {
3072        /* completely empty module name should only happen in
3073           'from . import' (or '__import__("")')*/
3074        Py_INCREF(mod);
3075        return mod;
3076    }
3077
3078    nameuni = PyUnicode_AsUCS4Copy(inputname);
3079    if (nameuni == NULL)
3080        return NULL;
3081
3082    dot = Py_UCS4_strchr(nameuni, '.');
3083    if (dot != NULL) {
3084        len = dot - nameuni;
3085        if (len == 0) {
3086            PyErr_SetString(PyExc_ValueError,
3087                            "Empty module name");
3088            goto error;
3089        }
3090    }
3091    else
3092        len = PyUnicode_GET_LENGTH(inputname);
3093
3094    if (*p_buflen+len+1 >= bufsize) {
3095        PyErr_SetString(PyExc_ValueError,
3096                        "Module name too long");
3097        goto error;
3098    }
3099
3100    p = buf + *p_buflen;
3101    if (p != buf) {
3102        *p++ = '.';
3103        *p_buflen += 1;
3104    }
3105    Py_UCS4_strncpy(p, nameuni, len);
3106    p[len] = '\0';
3107    *p_buflen += len;
3108
3109    fullname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
3110                                         buf, *p_buflen);
3111    if (fullname == NULL)
3112        goto error;
3113    name = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
3114                                     p, len);
3115    if (name == NULL) {
3116        Py_DECREF(fullname);
3117        goto error;
3118    }
3119    result = import_submodule(mod, name, fullname);
3120    Py_DECREF(fullname);
3121    if (result == Py_None && altmod != mod) {
3122        Py_DECREF(result);
3123        /* Here, altmod must be None and mod must not be None */
3124        result = import_submodule(altmod, name, name);
3125        Py_DECREF(name);
3126        if (result != NULL && result != Py_None) {
3127            mark_name = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
3128                                                  buf, *p_buflen);
3129            if (mark_name == NULL) {
3130                Py_DECREF(result);
3131                goto error;
3132            }
3133            if (mark_miss(mark_name) != 0) {
3134                Py_DECREF(result);
3135                Py_DECREF(mark_name);
3136                goto error;
3137            }
3138            Py_DECREF(mark_name);
3139            Py_UCS4_strncpy(buf, nameuni, len);
3140            buf[len] = '\0';
3141            *p_buflen = len;
3142        }
3143    }
3144    else
3145        Py_DECREF(name);
3146    if (result == NULL)
3147        goto error;
3148
3149    if (result == Py_None) {
3150        Py_DECREF(result);
3151        PyErr_Format(PyExc_ImportError,
3152                     "No module named %R", inputname);
3153        goto error;
3154    }
3155
3156    if (dot != NULL) {
3157        *p_outputname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
3158                                                  dot+1, Py_UCS4_strlen(dot+1));
3159        if (*p_outputname == NULL) {
3160            Py_DECREF(result);
3161            goto error;
3162        }
3163    }
3164
3165    PyMem_Free(nameuni);
3166    return result;
3167
3168error:
3169    PyMem_Free(nameuni);
3170    return NULL;
3171}
3172
3173static int
3174mark_miss(PyObject *name)
3175{
3176    PyObject *modules = PyImport_GetModuleDict();
3177    return PyDict_SetItem(modules, name, Py_None);
3178}
3179
3180static int
3181ensure_fromlist(PyObject *mod, PyObject *fromlist, PyObject *name,
3182                int recursive)
3183{
3184    int i;
3185    PyObject *fullname;
3186    Py_ssize_t fromlist_len;
3187
3188    if (!_PyObject_HasAttrId(mod, &PyId___path__))
3189        return 1;
3190
3191    fromlist_len = PySequence_Size(fromlist);
3192
3193    for (i = 0; i < fromlist_len; i++) {
3194        PyObject *item = PySequence_GetItem(fromlist, i);
3195        int hasit;
3196        if (item == NULL)
3197            return 0;
3198        if (!PyUnicode_Check(item)) {
3199            PyErr_SetString(PyExc_TypeError,
3200                            "Item in ``from list'' not a string");
3201            Py_DECREF(item);
3202            return 0;
3203        }
3204        if (PyUnicode_READ_CHAR(item, 0) == '*') {
3205            PyObject *all;
3206            _Py_IDENTIFIER(__all__);
3207            Py_DECREF(item);
3208            /* See if the package defines __all__ */
3209            if (recursive)
3210                continue; /* Avoid endless recursion */
3211            all = _PyObject_GetAttrId(mod, &PyId___all__);
3212            if (all == NULL)
3213                PyErr_Clear();
3214            else {
3215                int ret = ensure_fromlist(mod, all, name, 1);
3216                Py_DECREF(all);
3217                if (!ret)
3218                    return 0;
3219            }
3220            continue;
3221        }
3222        hasit = PyObject_HasAttr(mod, item);
3223        if (!hasit) {
3224            PyObject *submod;
3225            fullname = PyUnicode_FromFormat("%U.%U", name, item);
3226            if (fullname != NULL) {
3227                submod = import_submodule(mod, item, fullname);
3228                Py_DECREF(fullname);
3229            }
3230            else
3231                submod = NULL;
3232            Py_XDECREF(submod);
3233            if (submod == NULL) {
3234                Py_DECREF(item);
3235                return 0;
3236            }
3237        }
3238        Py_DECREF(item);
3239    }
3240
3241    return 1;
3242}
3243
3244static int
3245add_submodule(PyObject *mod, PyObject *submod, PyObject *fullname,
3246              PyObject *subname, PyObject *modules)
3247{
3248    if (mod == Py_None)
3249        return 1;
3250    /* Irrespective of the success of this load, make a
3251       reference to it in the parent package module.  A copy gets
3252       saved in the modules dictionary under the full name, so get a
3253       reference from there, if need be.  (The exception is when the
3254       load failed with a SyntaxError -- then there's no trace in
3255       sys.modules.  In that case, of course, do nothing extra.) */
3256    if (submod == NULL) {
3257        submod = PyDict_GetItem(modules, fullname);
3258        if (submod == NULL)
3259            return 1;
3260    }
3261    if (PyModule_Check(mod)) {
3262        /* We can't use setattr here since it can give a
3263         * spurious warning if the submodule name shadows a
3264         * builtin name */
3265        PyObject *dict = PyModule_GetDict(mod);
3266        if (!dict)
3267            return 0;
3268        if (PyDict_SetItem(dict, subname, submod) < 0)
3269            return 0;
3270    }
3271    else {
3272        if (PyObject_SetAttr(mod, subname, submod) < 0)
3273            return 0;
3274    }
3275    return 1;
3276}
3277
3278static PyObject *
3279import_submodule(PyObject *mod, PyObject *subname, PyObject *fullname)
3280{
3281    PyObject *modules = PyImport_GetModuleDict();
3282    PyObject *m = NULL, *bufobj, *path_list, *loader;
3283    struct filedescr *fdp;
3284    FILE *fp;
3285
3286    /* Require:
3287       if mod == None: subname == fullname
3288       else: mod.__name__ + "." + subname == fullname
3289    */
3290
3291    if ((m = PyDict_GetItem(modules, fullname)) != NULL) {
3292        Py_INCREF(m);
3293        return m;
3294    }
3295
3296    if (mod == Py_None)
3297        path_list = NULL;
3298    else {
3299        path_list = _PyObject_GetAttrId(mod, &PyId___path__);
3300        if (path_list == NULL) {
3301            PyErr_Clear();
3302            Py_INCREF(Py_None);
3303            return Py_None;
3304        }
3305    }
3306
3307    fdp = find_module(fullname, subname, path_list,
3308                      &bufobj, &fp, &loader);
3309    Py_XDECREF(path_list);
3310    if (fdp == NULL) {
3311        if (!PyErr_ExceptionMatches(PyExc_ImportError))
3312            return NULL;
3313        PyErr_Clear();
3314        Py_INCREF(Py_None);
3315        return Py_None;
3316    }
3317    m = load_module(fullname, fp, bufobj, fdp->type, loader);
3318    Py_XDECREF(bufobj);
3319    Py_XDECREF(loader);
3320    if (fp)
3321        fclose(fp);
3322    if (m == NULL)
3323        return NULL;
3324    if (!add_submodule(mod, m, fullname, subname, modules)) {
3325        Py_XDECREF(m);
3326        return NULL;
3327    }
3328    return m;
3329}
3330
3331
3332/* Re-import a module of any kind and return its module object, WITH
3333   INCREMENTED REFERENCE COUNT */
3334
3335PyObject *
3336PyImport_ReloadModule(PyObject *m)
3337{
3338    PyInterpreterState *interp = PyThreadState_Get()->interp;
3339    PyObject *modules_reloading = interp->modules_reloading;
3340    PyObject *modules = PyImport_GetModuleDict();
3341    PyObject *path_list = NULL, *loader = NULL, *existing_m = NULL;
3342    PyObject *nameobj, *bufobj, *subnameobj;
3343    Py_UCS4 *name = NULL, *subname;
3344    struct filedescr *fdp;
3345    FILE *fp = NULL;
3346    PyObject *newm = NULL;
3347
3348    if (modules_reloading == NULL) {
3349        Py_FatalError("PyImport_ReloadModule: "
3350                      "no modules_reloading dictionary!");
3351        return NULL;
3352    }
3353
3354    if (m == NULL || !PyModule_Check(m)) {
3355        PyErr_SetString(PyExc_TypeError,
3356                        "reload() argument must be module");
3357        return NULL;
3358    }
3359    nameobj = PyModule_GetNameObject(m);
3360    if (nameobj == NULL || PyUnicode_READY(nameobj) == -1)
3361        return NULL;
3362    if (m != PyDict_GetItem(modules, nameobj)) {
3363        PyErr_Format(PyExc_ImportError,
3364                     "reload(): module %R not in sys.modules",
3365                     nameobj);
3366        Py_DECREF(nameobj);
3367        return NULL;
3368    }
3369    existing_m = PyDict_GetItem(modules_reloading, nameobj);
3370    if (existing_m != NULL) {
3371        /* Due to a recursive reload, this module is already
3372           being reloaded. */
3373        Py_DECREF(nameobj);
3374        Py_INCREF(existing_m);
3375        return existing_m;
3376    }
3377    if (PyDict_SetItem(modules_reloading, nameobj, m) < 0) {
3378        Py_DECREF(nameobj);
3379        return NULL;
3380    }
3381
3382    name = PyUnicode_AsUCS4Copy(nameobj);
3383    if (!name) {
3384        Py_DECREF(nameobj);
3385        return NULL;
3386    }
3387    subname = Py_UCS4_strrchr(name, '.');
3388    if (subname == NULL) {
3389        Py_INCREF(nameobj);
3390        subnameobj = nameobj;
3391    }
3392    else {
3393        PyObject *parentname, *parent;
3394        Py_ssize_t len;
3395        len = subname - name;
3396        parentname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
3397                                               name, len);
3398        if (parentname == NULL) {
3399            goto error;
3400        }
3401        parent = PyDict_GetItem(modules, parentname);
3402        if (parent == NULL) {
3403            PyErr_Format(PyExc_ImportError,
3404                "reload(): parent %R not in sys.modules",
3405                 parentname);
3406            Py_DECREF(parentname);
3407            goto error;
3408        }
3409        Py_DECREF(parentname);
3410        path_list = _PyObject_GetAttrId(parent, &PyId___path__);
3411        if (path_list == NULL)
3412            PyErr_Clear();
3413        subname++;
3414        len = PyUnicode_GET_LENGTH(nameobj) - (len + 1);
3415        subnameobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
3416                                               subname, len);
3417    }
3418    if (subnameobj == NULL)
3419        goto error;
3420    fdp = find_module(nameobj, subnameobj, path_list,
3421                      &bufobj, &fp, &loader);
3422    Py_DECREF(subnameobj);
3423    Py_XDECREF(path_list);
3424
3425    if (fdp == NULL) {
3426        Py_XDECREF(loader);
3427        goto error;
3428    }
3429
3430    newm = load_module(nameobj, fp, bufobj, fdp->type, loader);
3431    Py_XDECREF(bufobj);
3432    Py_XDECREF(loader);
3433
3434    if (fp)
3435        fclose(fp);
3436    if (newm == NULL) {
3437        /* load_module probably removed name from modules because of
3438         * the error.  Put back the original module object.  We're
3439         * going to return NULL in this case regardless of whether
3440         * replacing name succeeds, so the return value is ignored.
3441         */
3442        PyDict_SetItem(modules, nameobj, m);
3443    }
3444
3445error:
3446    imp_modules_reloading_clear();
3447    Py_DECREF(nameobj);
3448    PyMem_Free(name);
3449    return newm;
3450}
3451
3452
3453/* Higher-level import emulator which emulates the "import" statement
3454   more accurately -- it invokes the __import__() function from the
3455   builtins of the current globals.  This means that the import is
3456   done using whatever import hooks are installed in the current
3457   environment.
3458   A dummy list ["__doc__"] is passed as the 4th argument so that
3459   e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
3460   will return <module "gencache"> instead of <module "win32com">. */
3461
3462PyObject *
3463PyImport_Import(PyObject *module_name)
3464{
3465    static PyObject *silly_list = NULL;
3466    static PyObject *builtins_str = NULL;
3467    static PyObject *import_str = NULL;
3468    PyObject *globals = NULL;
3469    PyObject *import = NULL;
3470    PyObject *builtins = NULL;
3471    PyObject *modules = NULL;
3472    PyObject *r = NULL;
3473
3474    /* Initialize constant string objects */
3475    if (silly_list == NULL) {
3476        import_str = PyUnicode_InternFromString("__import__");
3477        if (import_str == NULL)
3478            return NULL;
3479        builtins_str = PyUnicode_InternFromString("__builtins__");
3480        if (builtins_str == NULL)
3481            return NULL;
3482        silly_list = PyList_New(0);
3483        if (silly_list == NULL)
3484            return NULL;
3485    }
3486
3487    /* Get the builtins from current globals */
3488    globals = PyEval_GetGlobals();
3489    if (globals != NULL) {
3490        Py_INCREF(globals);
3491        builtins = PyObject_GetItem(globals, builtins_str);
3492        if (builtins == NULL)
3493            goto err;
3494    }
3495    else {
3496        /* No globals -- use standard builtins, and fake globals */
3497        builtins = PyImport_ImportModuleLevel("builtins",
3498                                              NULL, NULL, NULL, 0);
3499        if (builtins == NULL)
3500            return NULL;
3501        globals = Py_BuildValue("{OO}", builtins_str, builtins);
3502        if (globals == NULL)
3503            goto err;
3504    }
3505
3506    /* Get the __import__ function from the builtins */
3507    if (PyDict_Check(builtins)) {
3508        import = PyObject_GetItem(builtins, import_str);
3509        if (import == NULL)
3510            PyErr_SetObject(PyExc_KeyError, import_str);
3511    }
3512    else
3513        import = PyObject_GetAttr(builtins, import_str);
3514    if (import == NULL)
3515        goto err;
3516
3517    /* Call the __import__ function with the proper argument list
3518       Always use absolute import here.
3519       Calling for side-effect of import. */
3520    r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
3521                              globals, silly_list, 0, NULL);
3522    if (r == NULL)
3523        goto err;
3524    Py_DECREF(r);
3525
3526    modules = PyImport_GetModuleDict();
3527    r = PyDict_GetItem(modules, module_name);
3528    if (r != NULL)
3529        Py_INCREF(r);
3530
3531  err:
3532    Py_XDECREF(globals);
3533    Py_XDECREF(builtins);
3534    Py_XDECREF(import);
3535
3536    return r;
3537}
3538
3539
3540/* Module 'imp' provides Python access to the primitives used for
3541   importing modules.
3542*/
3543
3544static PyObject *
3545imp_make_magic(long magic)
3546{
3547    char buf[4];
3548
3549    buf[0] = (char) ((magic >>  0) & 0xff);
3550    buf[1] = (char) ((magic >>  8) & 0xff);
3551    buf[2] = (char) ((magic >> 16) & 0xff);
3552    buf[3] = (char) ((magic >> 24) & 0xff);
3553
3554    return PyBytes_FromStringAndSize(buf, 4);
3555}
3556
3557static PyObject *
3558imp_get_magic(PyObject *self, PyObject *noargs)
3559{
3560    return imp_make_magic(pyc_magic);
3561}
3562
3563static PyObject *
3564imp_get_tag(PyObject *self, PyObject *noargs)
3565{
3566    return PyUnicode_FromString(pyc_tag);
3567}
3568
3569static PyObject *
3570imp_get_suffixes(PyObject *self, PyObject *noargs)
3571{
3572    PyObject *list;
3573    struct filedescr *fdp;
3574
3575    list = PyList_New(0);
3576    if (list == NULL)
3577        return NULL;
3578    for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
3579        PyObject *item = Py_BuildValue("ssi",
3580                               fdp->suffix, fdp->mode, fdp->type);
3581        if (item == NULL) {
3582            Py_DECREF(list);
3583            return NULL;
3584        }
3585        if (PyList_Append(list, item) < 0) {
3586            Py_DECREF(list);
3587            Py_DECREF(item);
3588            return NULL;
3589        }
3590        Py_DECREF(item);
3591    }
3592    return list;
3593}
3594
3595static PyObject *
3596call_find_module(PyObject *name, PyObject *path_list)
3597{
3598    extern int fclose(FILE *);
3599    PyObject *fob, *ret;
3600    PyObject *pathobj;
3601    struct filedescr *fdp;
3602    FILE *fp;
3603    int fd = -1;
3604    char *found_encoding = NULL;
3605    char *encoding = NULL;
3606
3607    if (path_list == Py_None)
3608        path_list = NULL;
3609    fdp = find_module(NULL, name, path_list,
3610                      &pathobj, &fp, NULL);
3611    if (fdp == NULL)
3612        return NULL;
3613    if (fp != NULL) {
3614        fd = fileno(fp);
3615        if (fd != -1)
3616            fd = dup(fd);
3617        fclose(fp);
3618        if (fd == -1) {
3619            PyErr_SetFromErrno(PyExc_OSError);
3620            return NULL;
3621        }
3622        fp = NULL;
3623    }
3624    if (fd != -1) {
3625        if (strchr(fdp->mode, 'b') == NULL) {
3626            /* PyTokenizer_FindEncodingFilename() returns PyMem_MALLOC'ed
3627               memory. */
3628            found_encoding = PyTokenizer_FindEncodingFilename(fd, pathobj);
3629            lseek(fd, 0, 0); /* Reset position */
3630            if (found_encoding == NULL && PyErr_Occurred()) {
3631                Py_XDECREF(pathobj);
3632                return NULL;
3633            }
3634            encoding = (found_encoding != NULL) ? found_encoding :
3635                   (char*)PyUnicode_GetDefaultEncoding();
3636        }
3637        fob = PyFile_FromFd(fd, NULL, fdp->mode, -1,
3638                            (char*)encoding, NULL, NULL, 1);
3639        if (fob == NULL) {
3640            Py_XDECREF(pathobj);
3641            close(fd);
3642            PyMem_FREE(found_encoding);
3643            return NULL;
3644        }
3645    }
3646    else {
3647        fob = Py_None;
3648        Py_INCREF(fob);
3649    }
3650    if (pathobj == NULL) {
3651        Py_INCREF(Py_None);
3652        pathobj = Py_None;
3653    }
3654    ret = Py_BuildValue("NN(ssi)",
3655                  fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
3656    PyMem_FREE(found_encoding);
3657
3658    return ret;
3659}
3660
3661static PyObject *
3662imp_find_module(PyObject *self, PyObject *args)
3663{
3664    PyObject *name, *path_list = NULL;
3665    if (!PyArg_ParseTuple(args, "U|O:find_module",
3666                          &name, &path_list))
3667        return NULL;
3668    return call_find_module(name, path_list);
3669}
3670
3671static PyObject *
3672imp_init_builtin(PyObject *self, PyObject *args)
3673{
3674    PyObject *name;
3675    int ret;
3676    PyObject *m;
3677    if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
3678        return NULL;
3679    ret = init_builtin(name);
3680    if (ret < 0)
3681        return NULL;
3682    if (ret == 0) {
3683        Py_INCREF(Py_None);
3684        return Py_None;
3685    }
3686    m = PyImport_AddModuleObject(name);
3687    Py_XINCREF(m);
3688    return m;
3689}
3690
3691static PyObject *
3692imp_init_frozen(PyObject *self, PyObject *args)
3693{
3694    PyObject *name;
3695    int ret;
3696    PyObject *m;
3697    if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
3698        return NULL;
3699    ret = PyImport_ImportFrozenModuleObject(name);
3700    if (ret < 0)
3701        return NULL;
3702    if (ret == 0) {
3703        Py_INCREF(Py_None);
3704        return Py_None;
3705    }
3706    m = PyImport_AddModuleObject(name);
3707    Py_XINCREF(m);
3708    return m;
3709}
3710
3711static PyObject *
3712imp_get_frozen_object(PyObject *self, PyObject *args)
3713{
3714    PyObject *name;
3715
3716    if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
3717        return NULL;
3718    return get_frozen_object(name);
3719}
3720
3721static PyObject *
3722imp_is_frozen_package(PyObject *self, PyObject *args)
3723{
3724    PyObject *name;
3725
3726    if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
3727        return NULL;
3728    return is_frozen_package(name);
3729}
3730
3731static PyObject *
3732imp_is_builtin(PyObject *self, PyObject *args)
3733{
3734    PyObject *name;
3735    if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
3736        return NULL;
3737    return PyLong_FromLong(is_builtin(name));
3738}
3739
3740static PyObject *
3741imp_is_frozen(PyObject *self, PyObject *args)
3742{
3743    PyObject *name;
3744    struct _frozen *p;
3745    if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
3746        return NULL;
3747    p = find_frozen(name);
3748    return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
3749}
3750
3751static FILE *
3752get_file(PyObject *pathname, PyObject *fob, char *mode)
3753{
3754    FILE *fp;
3755    if (mode[0] == 'U')
3756        mode = "r" PY_STDIOTEXTMODE;
3757    if (fob == NULL) {
3758        fp = _Py_fopen(pathname, mode);
3759    }
3760    else {
3761        int fd = PyObject_AsFileDescriptor(fob);
3762        if (fd == -1)
3763            return NULL;
3764        if (!_PyVerify_fd(fd))
3765            goto error;
3766        /* the FILE struct gets a new fd, so that it can be closed
3767         * independently of the file descriptor given
3768         */
3769        fd = dup(fd);
3770        if (fd == -1)
3771            goto error;
3772        fp = fdopen(fd, mode);
3773    }
3774    if (fp)
3775        return fp;
3776error:
3777    PyErr_SetFromErrno(PyExc_IOError);
3778    return NULL;
3779}
3780
3781static PyObject *
3782imp_load_compiled(PyObject *self, PyObject *args)
3783{
3784    PyObject *name, *pathname;
3785    PyObject *fob = NULL;
3786    PyObject *m;
3787    FILE *fp;
3788    if (!PyArg_ParseTuple(args, "UO&|O:load_compiled",
3789                          &name,
3790                          PyUnicode_FSDecoder, &pathname,
3791                          &fob))
3792        return NULL;
3793    fp = get_file(pathname, fob, "rb");
3794    if (fp == NULL) {
3795        Py_DECREF(pathname);
3796        return NULL;
3797    }
3798    m = load_compiled_module(name, pathname, fp);
3799    fclose(fp);
3800    Py_DECREF(pathname);
3801    return m;
3802}
3803
3804#ifdef HAVE_DYNAMIC_LOADING
3805
3806static PyObject *
3807imp_load_dynamic(PyObject *self, PyObject *args)
3808{
3809    PyObject *name, *pathname, *fob = NULL, *mod;
3810    FILE *fp;
3811
3812    if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
3813                          &name, PyUnicode_FSDecoder, &pathname, &fob))
3814        return NULL;
3815    if (fob != NULL) {
3816        fp = get_file(NULL, fob, "r");
3817        if (fp == NULL) {
3818            Py_DECREF(pathname);
3819            return NULL;
3820        }
3821    }
3822    else
3823        fp = NULL;
3824    mod = _PyImport_LoadDynamicModule(name, pathname, fp);
3825    Py_DECREF(pathname);
3826    if (fp)
3827        fclose(fp);
3828    return mod;
3829}
3830
3831#endif /* HAVE_DYNAMIC_LOADING */
3832
3833static PyObject *
3834imp_load_source(PyObject *self, PyObject *args)
3835{
3836    PyObject *name, *pathname;
3837    PyObject *fob = NULL;
3838    PyObject *m;
3839    FILE *fp;
3840    if (!PyArg_ParseTuple(args, "UO&|O:load_source",
3841                          &name,
3842                          PyUnicode_FSDecoder, &pathname,
3843                          &fob))
3844        return NULL;
3845    fp = get_file(pathname, fob, "r");
3846    if (fp == NULL) {
3847        Py_DECREF(pathname);
3848        return NULL;
3849    }
3850    m = load_source_module(name, pathname, fp);
3851    Py_DECREF(pathname);
3852    fclose(fp);
3853    return m;
3854}
3855
3856static PyObject *
3857imp_load_module(PyObject *self, PyObject *args)
3858{
3859    PyObject *name, *fob, *pathname, *pathname_obj, *ret;
3860    char *suffix; /* Unused */
3861    char *mode;
3862    int type;
3863    FILE *fp;
3864
3865    if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
3866                          &name, &fob, &pathname_obj, &suffix, &mode, &type))
3867        return NULL;
3868    if (pathname_obj != Py_None) {
3869        if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
3870            return NULL;
3871    }
3872    else
3873        pathname = NULL;
3874
3875    if (*mode) {
3876        /* Mode must start with 'r' or 'U' and must not contain '+'.
3877           Implicit in this test is the assumption that the mode
3878           may contain other modifiers like 'b' or 't'. */
3879
3880        if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3881            PyErr_Format(PyExc_ValueError,
3882                         "invalid file open mode %.200s", mode);
3883            Py_XDECREF(pathname);
3884            return NULL;
3885        }
3886    }
3887    if (fob == Py_None)
3888        fp = NULL;
3889    else {
3890        fp = get_file(NULL, fob, mode);
3891        if (fp == NULL) {
3892            Py_XDECREF(pathname);
3893            return NULL;
3894        }
3895    }
3896    ret = load_module(name, fp, pathname, type, NULL);
3897    Py_XDECREF(pathname);
3898    if (fp)
3899        fclose(fp);
3900    return ret;
3901}
3902
3903static PyObject *
3904imp_load_package(PyObject *self, PyObject *args)
3905{
3906    PyObject *name, *pathname;
3907    PyObject * ret;
3908    if (!PyArg_ParseTuple(args, "UO&:load_package",
3909                          &name, PyUnicode_FSDecoder, &pathname))
3910        return NULL;
3911    ret = load_package(name, pathname);
3912    Py_DECREF(pathname);
3913    return ret;
3914}
3915
3916static PyObject *
3917imp_new_module(PyObject *self, PyObject *args)
3918{
3919    PyObject *name;
3920    if (!PyArg_ParseTuple(args, "U:new_module", &name))
3921        return NULL;
3922    return PyModule_NewObject(name);
3923}
3924
3925static PyObject *
3926imp_reload(PyObject *self, PyObject *v)
3927{
3928    return PyImport_ReloadModule(v);
3929}
3930
3931PyDoc_STRVAR(doc_reload,
3932"reload(module) -> module\n\
3933\n\
3934Reload the module.  The module must have been successfully imported before.");
3935
3936static PyObject *
3937imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws)
3938{
3939    static char *kwlist[] = {"path", "debug_override", NULL};
3940
3941    PyObject *pathname, *cpathname;
3942    PyObject *debug_override = NULL;
3943    int debug = !Py_OptimizeFlag;
3944
3945    if (!PyArg_ParseTupleAndKeywords(
3946                args, kws, "O&|O", kwlist,
3947                PyUnicode_FSDecoder, &pathname, &debug_override))
3948        return NULL;
3949
3950    if (debug_override != NULL &&
3951        (debug = PyObject_IsTrue(debug_override)) < 0) {
3952        Py_DECREF(pathname);
3953        return NULL;
3954    }
3955
3956    if (PyUnicode_READY(pathname) < 0)
3957        return NULL;
3958
3959    cpathname = make_compiled_pathname(pathname, debug);
3960    Py_DECREF(pathname);
3961
3962    if (cpathname == NULL) {
3963        PyErr_Format(PyExc_SystemError, "path buffer too short");
3964        return NULL;
3965    }
3966    return cpathname;
3967}
3968
3969PyDoc_STRVAR(doc_cache_from_source,
3970"Given the path to a .py file, return the path to its .pyc/.pyo file.\n\
3971\n\
3972The .py file does not need to exist; this simply returns the path to the\n\
3973.pyc/.pyo file calculated as if the .py file were imported.  The extension\n\
3974will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\
3975\n\
3976If debug_override is not None, then it must be a boolean and is taken as\n\
3977the value of __debug__ instead.");
3978
3979static PyObject *
3980imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)
3981{
3982    static char *kwlist[] = {"path", NULL};
3983    PyObject *pathname, *source;
3984
3985    if (!PyArg_ParseTupleAndKeywords(
3986                args, kws, "O&", kwlist,
3987                PyUnicode_FSDecoder, &pathname))
3988        return NULL;
3989
3990    source = make_source_pathname(pathname);
3991    if (source == NULL) {
3992        PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R",
3993                     pathname);
3994        Py_DECREF(pathname);
3995        return NULL;
3996    }
3997    Py_DECREF(pathname);
3998    return source;
3999}
4000
4001PyDoc_STRVAR(doc_source_from_cache,
4002"Given the path to a .pyc./.pyo file, return the path to its .py file.\n\
4003\n\
4004The .pyc/.pyo file does not need to exist; this simply returns the path to\n\
4005the .py file calculated to correspond to the .pyc/.pyo file.  If path\n\
4006does not conform to PEP 3147 format, ValueError will be raised.");
4007
4008/* Doc strings */
4009
4010PyDoc_STRVAR(doc_imp,
4011"This module provides the components needed to build your own\n\
4012__import__ function.  Undocumented functions are obsolete.");
4013
4014PyDoc_STRVAR(doc_find_module,
4015"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
4016Search for a module.  If path is omitted or None, search for a\n\
4017built-in, frozen or special module and continue search in sys.path.\n\
4018The module name cannot contain '.'; to search for a submodule of a\n\
4019package, pass the submodule name and the package's __path__.");
4020
4021PyDoc_STRVAR(doc_load_module,
4022"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
4023Load a module, given information returned by find_module().\n\
4024The module name must include the full package name, if any.");
4025
4026PyDoc_STRVAR(doc_get_magic,
4027"get_magic() -> string\n\
4028Return the magic number for .pyc or .pyo files.");
4029
4030PyDoc_STRVAR(doc_get_tag,
4031"get_tag() -> string\n\
4032Return the magic tag for .pyc or .pyo files.");
4033
4034PyDoc_STRVAR(doc_get_suffixes,
4035"get_suffixes() -> [(suffix, mode, type), ...]\n\
4036Return a list of (suffix, mode, type) tuples describing the files\n\
4037that find_module() looks for.");
4038
4039PyDoc_STRVAR(doc_new_module,
4040"new_module(name) -> module\n\
4041Create a new module.  Do not enter it in sys.modules.\n\
4042The module name must include the full package name, if any.");
4043
4044PyDoc_STRVAR(doc_lock_held,
4045"lock_held() -> boolean\n\
4046Return True if the import lock is currently held, else False.\n\
4047On platforms without threads, return False.");
4048
4049PyDoc_STRVAR(doc_acquire_lock,
4050"acquire_lock() -> None\n\
4051Acquires the interpreter's import lock for the current thread.\n\
4052This lock should be used by import hooks to ensure thread-safety\n\
4053when importing modules.\n\
4054On platforms without threads, this function does nothing.");
4055
4056PyDoc_STRVAR(doc_release_lock,
4057"release_lock() -> None\n\
4058Release the interpreter's import lock.\n\
4059On platforms without threads, this function does nothing.");
4060
4061static PyMethodDef imp_methods[] = {
4062    {"find_module",      imp_find_module,  METH_VARARGS, doc_find_module},
4063    {"get_magic",        imp_get_magic,    METH_NOARGS,  doc_get_magic},
4064    {"get_tag",          imp_get_tag,      METH_NOARGS,  doc_get_tag},
4065    {"get_suffixes", imp_get_suffixes, METH_NOARGS,  doc_get_suffixes},
4066    {"load_module",      imp_load_module,  METH_VARARGS, doc_load_module},
4067    {"new_module",       imp_new_module,   METH_VARARGS, doc_new_module},
4068    {"lock_held",        imp_lock_held,    METH_NOARGS,  doc_lock_held},
4069    {"acquire_lock", imp_acquire_lock, METH_NOARGS,  doc_acquire_lock},
4070    {"release_lock", imp_release_lock, METH_NOARGS,  doc_release_lock},
4071    {"reload",       imp_reload,       METH_O,       doc_reload},
4072    {"cache_from_source", (PyCFunction)imp_cache_from_source,
4073     METH_VARARGS | METH_KEYWORDS, doc_cache_from_source},
4074    {"source_from_cache", (PyCFunction)imp_source_from_cache,
4075     METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},
4076    /* The rest are obsolete */
4077    {"get_frozen_object",       imp_get_frozen_object,  METH_VARARGS},
4078    {"is_frozen_package",   imp_is_frozen_package,  METH_VARARGS},
4079    {"init_builtin",            imp_init_builtin,       METH_VARARGS},
4080    {"init_frozen",             imp_init_frozen,        METH_VARARGS},
4081    {"is_builtin",              imp_is_builtin,         METH_VARARGS},
4082    {"is_frozen",               imp_is_frozen,          METH_VARARGS},
4083    {"load_compiled",           imp_load_compiled,      METH_VARARGS},
4084#ifdef HAVE_DYNAMIC_LOADING
4085    {"load_dynamic",            imp_load_dynamic,       METH_VARARGS},
4086#endif
4087    {"load_package",            imp_load_package,       METH_VARARGS},
4088    {"load_source",             imp_load_source,        METH_VARARGS},
4089    {"_fix_co_filename",        imp_fix_co_filename,    METH_VARARGS},
4090    {NULL,                      NULL}           /* sentinel */
4091};
4092
4093static int
4094setint(PyObject *d, char *name, int value)
4095{
4096    PyObject *v;
4097    int err;
4098
4099    v = PyLong_FromLong((long)value);
4100    err = PyDict_SetItemString(d, name, v);
4101    Py_XDECREF(v);
4102    return err;
4103}
4104
4105typedef struct {
4106    PyObject_HEAD
4107} NullImporter;
4108
4109static int
4110NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
4111{
4112#ifndef MS_WINDOWS
4113    PyObject *path;
4114    struct stat statbuf;
4115    int rv;
4116
4117    if (!_PyArg_NoKeywords("NullImporter()", kwds))
4118        return -1;
4119
4120    if (!PyArg_ParseTuple(args, "O&:NullImporter",
4121                          PyUnicode_FSConverter, &path))
4122        return -1;
4123
4124    if (PyBytes_GET_SIZE(path) == 0) {
4125        Py_DECREF(path);
4126        PyErr_SetString(PyExc_ImportError, "empty pathname");
4127        return -1;
4128    }
4129
4130    rv = stat(PyBytes_AS_STRING(path), &statbuf);
4131    Py_DECREF(path);
4132    if (rv == 0) {
4133        /* it exists */
4134        if (S_ISDIR(statbuf.st_mode)) {
4135            /* it's a directory */
4136            PyErr_SetString(PyExc_ImportError, "existing directory");
4137            return -1;
4138        }
4139    }
4140#else /* MS_WINDOWS */
4141    PyObject *pathobj;
4142    DWORD rv;
4143    wchar_t *path;
4144
4145    if (!_PyArg_NoKeywords("NullImporter()", kwds))
4146        return -1;
4147
4148    if (!PyArg_ParseTuple(args, "U:NullImporter",
4149                          &pathobj))
4150        return -1;
4151
4152    if (PyUnicode_GET_LENGTH(pathobj) == 0) {
4153        PyErr_SetString(PyExc_ImportError, "empty pathname");
4154        return -1;
4155    }
4156
4157    path = PyUnicode_AsWideCharString(pathobj, NULL);
4158    if (path == NULL)
4159        return -1;
4160    /* see issue1293 and issue3677:
4161     * stat() on Windows doesn't recognise paths like
4162     * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
4163     */
4164    rv = GetFileAttributesW(path);
4165    PyMem_Free(path);
4166    if (rv != INVALID_FILE_ATTRIBUTES) {
4167        /* it exists */
4168        if (rv & FILE_ATTRIBUTE_DIRECTORY) {
4169            /* it's a directory */
4170            PyErr_SetString(PyExc_ImportError, "existing directory");
4171            return -1;
4172        }
4173    }
4174#endif
4175    return 0;
4176}
4177
4178static PyObject *
4179NullImporter_find_module(NullImporter *self, PyObject *args)
4180{
4181    Py_RETURN_NONE;
4182}
4183
4184static PyMethodDef NullImporter_methods[] = {
4185    {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
4186     "Always return None"
4187    },
4188    {NULL}  /* Sentinel */
4189};
4190
4191
4192PyTypeObject PyNullImporter_Type = {
4193    PyVarObject_HEAD_INIT(NULL, 0)
4194    "imp.NullImporter",        /*tp_name*/
4195    sizeof(NullImporter),      /*tp_basicsize*/
4196    0,                         /*tp_itemsize*/
4197    0,                         /*tp_dealloc*/
4198    0,                         /*tp_print*/
4199    0,                         /*tp_getattr*/
4200    0,                         /*tp_setattr*/
4201    0,                         /*tp_reserved*/
4202    0,                         /*tp_repr*/
4203    0,                         /*tp_as_number*/
4204    0,                         /*tp_as_sequence*/
4205    0,                         /*tp_as_mapping*/
4206    0,                         /*tp_hash */
4207    0,                         /*tp_call*/
4208    0,                         /*tp_str*/
4209    0,                         /*tp_getattro*/
4210    0,                         /*tp_setattro*/
4211    0,                         /*tp_as_buffer*/
4212    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
4213    "Null importer object",    /* tp_doc */
4214    0,                             /* tp_traverse */
4215    0,                             /* tp_clear */
4216    0,                             /* tp_richcompare */
4217    0,                             /* tp_weaklistoffset */
4218    0,                             /* tp_iter */
4219    0,                             /* tp_iternext */
4220    NullImporter_methods,      /* tp_methods */
4221    0,                         /* tp_members */
4222    0,                         /* tp_getset */
4223    0,                         /* tp_base */
4224    0,                         /* tp_dict */
4225    0,                         /* tp_descr_get */
4226    0,                         /* tp_descr_set */
4227    0,                         /* tp_dictoffset */
4228    (initproc)NullImporter_init,      /* tp_init */
4229    0,                         /* tp_alloc */
4230    PyType_GenericNew          /* tp_new */
4231};
4232
4233static struct PyModuleDef impmodule = {
4234    PyModuleDef_HEAD_INIT,
4235    "imp",
4236    doc_imp,
4237    0,
4238    imp_methods,
4239    NULL,
4240    NULL,
4241    NULL,
4242    NULL
4243};
4244
4245PyMODINIT_FUNC
4246PyInit_imp(void)
4247{
4248    PyObject *m, *d;
4249
4250    if (PyType_Ready(&PyNullImporter_Type) < 0)
4251        return NULL;
4252
4253    m = PyModule_Create(&impmodule);
4254    if (m == NULL)
4255        goto failure;
4256    d = PyModule_GetDict(m);
4257    if (d == NULL)
4258        goto failure;
4259
4260    if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
4261    if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
4262    if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
4263    if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
4264    if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
4265    if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
4266    if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
4267    if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
4268    if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
4269    if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
4270
4271    Py_INCREF(&PyNullImporter_Type);
4272    PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
4273    return m;
4274  failure:
4275    Py_XDECREF(m);
4276    return NULL;
4277}
4278
4279
4280/* API for embedding applications that want to add their own entries
4281   to the table of built-in modules.  This should normally be called
4282   *before* Py_Initialize().  When the table resize fails, -1 is
4283   returned and the existing table is unchanged.
4284
4285   After a similar function by Just van Rossum. */
4286
4287int
4288PyImport_ExtendInittab(struct _inittab *newtab)
4289{
4290    static struct _inittab *our_copy = NULL;
4291    struct _inittab *p;
4292    int i, n;
4293
4294    /* Count the number of entries in both tables */
4295    for (n = 0; newtab[n].name != NULL; n++)
4296        ;
4297    if (n == 0)
4298        return 0; /* Nothing to do */
4299    for (i = 0; PyImport_Inittab[i].name != NULL; i++)
4300        ;
4301
4302    /* Allocate new memory for the combined table */
4303    p = our_copy;
4304    PyMem_RESIZE(p, struct _inittab, i+n+1);
4305    if (p == NULL)
4306        return -1;
4307
4308    /* Copy the tables into the new memory */
4309    if (our_copy != PyImport_Inittab)
4310        memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
4311    PyImport_Inittab = our_copy = p;
4312    memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
4313
4314    return 0;
4315}
4316
4317/* Shorthand to add a single entry given a name and a function */
4318
4319int
4320PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
4321{
4322    struct _inittab newtab[2];
4323
4324    memset(newtab, '\0', sizeof newtab);
4325
4326    newtab[0].name = (char *)name;
4327    newtab[0].initfunc = initfunc;
4328
4329    return PyImport_ExtendInittab(newtab);
4330}
4331
4332#ifdef __cplusplus
4333}
4334#endif
4335