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