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