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