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