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