• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Module definition and import implementation */
2 
3 #include "Python.h"
4 #include "pycore_ceval.h"
5 #include "pycore_hashtable.h"     // _Py_hashtable_new_full()
6 #include "pycore_import.h"        // _PyImport_BootstrapImp()
7 #include "pycore_initconfig.h"    // _PyStatus_OK()
8 #include "pycore_interp.h"        // struct _import_runtime_state
9 #include "pycore_namespace.h"     // _PyNamespace_Type
10 #include "pycore_object.h"        // _Py_SetImmortal()
11 #include "pycore_pyerrors.h"      // _PyErr_SetString()
12 #include "pycore_pyhash.h"        // _Py_KeyedHash()
13 #include "pycore_pylifecycle.h"
14 #include "pycore_pymem.h"         // _PyMem_SetDefaultAllocator()
15 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
16 #include "pycore_sysmodule.h"     // _PySys_Audit()
17 #include "pycore_time.h"          // _PyTime_AsMicroseconds()
18 #include "pycore_weakref.h"       // _PyWeakref_GET_REF()
19 
20 #include "marshal.h"              // PyMarshal_ReadObjectFromString()
21 #include "pycore_importdl.h"      // _PyImport_DynLoadFiletab
22 #include "pydtrace.h"             // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()
23 #include <stdbool.h>              // bool
24 
25 #ifdef HAVE_FCNTL_H
26 #include <fcntl.h>
27 #endif
28 
29 
30 /*[clinic input]
31 module _imp
32 [clinic start generated code]*/
33 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/
34 
35 #include "clinic/import.c.h"
36 
37 
38 #ifndef NDEBUG
39 static bool
is_interpreter_isolated(PyInterpreterState * interp)40 is_interpreter_isolated(PyInterpreterState *interp)
41 {
42     return !_Py_IsMainInterpreter(interp)
43         && !(interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC)
44         && interp->ceval.own_gil;
45 }
46 #endif
47 
48 
49 /*******************************/
50 /* process-global import state */
51 /*******************************/
52 
53 /* This table is defined in config.c: */
54 extern struct _inittab _PyImport_Inittab[];
55 
56 // This is not used after Py_Initialize() is called.
57 // (See _PyRuntimeState.imports.inittab.)
58 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
59 // When we dynamically allocate a larger table for PyImport_ExtendInittab(),
60 // we track the pointer here so we can deallocate it during finalization.
61 static struct _inittab *inittab_copy = NULL;
62 
63 
64 /*******************************/
65 /* runtime-global import state */
66 /*******************************/
67 
68 #define INITTAB _PyRuntime.imports.inittab
69 #define LAST_MODULE_INDEX _PyRuntime.imports.last_module_index
70 #define EXTENSIONS _PyRuntime.imports.extensions
71 
72 #define PKGCONTEXT (_PyRuntime.imports.pkgcontext)
73 
74 
75 /*******************************/
76 /* interpreter import state */
77 /*******************************/
78 
79 #define MODULES(interp) \
80     (interp)->imports.modules
81 #define MODULES_BY_INDEX(interp) \
82     (interp)->imports.modules_by_index
83 #define IMPORTLIB(interp) \
84     (interp)->imports.importlib
85 #define OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp) \
86     (interp)->imports.override_multi_interp_extensions_check
87 #define OVERRIDE_FROZEN_MODULES(interp) \
88     (interp)->imports.override_frozen_modules
89 #ifdef HAVE_DLOPEN
90 #  define DLOPENFLAGS(interp) \
91         (interp)->imports.dlopenflags
92 #endif
93 #define IMPORT_FUNC(interp) \
94     (interp)->imports.import_func
95 
96 #define IMPORT_LOCK(interp) \
97     (interp)->imports.lock
98 
99 #define FIND_AND_LOAD(interp) \
100     (interp)->imports.find_and_load
101 
102 
103 /*******************/
104 /* the import lock */
105 /*******************/
106 
107 /* Locking primitives to prevent parallel imports of the same module
108    in different threads to return with a partially loaded module.
109    These calls are serialized by the global interpreter lock. */
110 
111 void
_PyImport_AcquireLock(PyInterpreterState * interp)112 _PyImport_AcquireLock(PyInterpreterState *interp)
113 {
114     _PyRecursiveMutex_Lock(&IMPORT_LOCK(interp));
115 }
116 
117 void
_PyImport_ReleaseLock(PyInterpreterState * interp)118 _PyImport_ReleaseLock(PyInterpreterState *interp)
119 {
120     _PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp));
121 }
122 
123 void
_PyImport_ReInitLock(PyInterpreterState * interp)124 _PyImport_ReInitLock(PyInterpreterState *interp)
125 {
126     // gh-126688: Thread id may change after fork() on some operating systems.
127     IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex();
128 }
129 
130 
131 /***************/
132 /* sys.modules */
133 /***************/
134 
135 PyObject *
_PyImport_InitModules(PyInterpreterState * interp)136 _PyImport_InitModules(PyInterpreterState *interp)
137 {
138     assert(MODULES(interp) == NULL);
139     MODULES(interp) = PyDict_New();
140     if (MODULES(interp) == NULL) {
141         return NULL;
142     }
143     return MODULES(interp);
144 }
145 
146 PyObject *
_PyImport_GetModules(PyInterpreterState * interp)147 _PyImport_GetModules(PyInterpreterState *interp)
148 {
149     return MODULES(interp);
150 }
151 
152 void
_PyImport_ClearModules(PyInterpreterState * interp)153 _PyImport_ClearModules(PyInterpreterState *interp)
154 {
155     Py_SETREF(MODULES(interp), NULL);
156 }
157 
158 static inline PyObject *
get_modules_dict(PyThreadState * tstate,bool fatal)159 get_modules_dict(PyThreadState *tstate, bool fatal)
160 {
161     /* Technically, it would make sense to incref the dict,
162      * since sys.modules could be swapped out and decref'ed to 0
163      * before the caller is done using it.  However, that is highly
164      * unlikely, especially since we can rely on a global lock
165      * (i.e. the GIL) for thread-safety. */
166     PyObject *modules = MODULES(tstate->interp);
167     if (modules == NULL) {
168         if (fatal) {
169             Py_FatalError("interpreter has no modules dictionary");
170         }
171         _PyErr_SetString(tstate, PyExc_RuntimeError,
172                          "unable to get sys.modules");
173         return NULL;
174     }
175     return modules;
176 }
177 
178 PyObject *
PyImport_GetModuleDict(void)179 PyImport_GetModuleDict(void)
180 {
181     PyThreadState *tstate = _PyThreadState_GET();
182     return get_modules_dict(tstate, true);
183 }
184 
185 int
_PyImport_SetModule(PyObject * name,PyObject * m)186 _PyImport_SetModule(PyObject *name, PyObject *m)
187 {
188     PyThreadState *tstate = _PyThreadState_GET();
189     PyObject *modules = get_modules_dict(tstate, true);
190     return PyObject_SetItem(modules, name, m);
191 }
192 
193 int
_PyImport_SetModuleString(const char * name,PyObject * m)194 _PyImport_SetModuleString(const char *name, PyObject *m)
195 {
196     PyThreadState *tstate = _PyThreadState_GET();
197     PyObject *modules = get_modules_dict(tstate, true);
198     return PyMapping_SetItemString(modules, name, m);
199 }
200 
201 static PyObject *
import_get_module(PyThreadState * tstate,PyObject * name)202 import_get_module(PyThreadState *tstate, PyObject *name)
203 {
204     PyObject *modules = get_modules_dict(tstate, false);
205     if (modules == NULL) {
206         return NULL;
207     }
208 
209     PyObject *m;
210     Py_INCREF(modules);
211     (void)PyMapping_GetOptionalItem(modules, name, &m);
212     Py_DECREF(modules);
213     return m;
214 }
215 
216 static int
import_ensure_initialized(PyInterpreterState * interp,PyObject * mod,PyObject * name)217 import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name)
218 {
219     PyObject *spec;
220 
221     /* Optimization: only call _bootstrap._lock_unlock_module() if
222        __spec__._initializing is true.
223        NOTE: because of this, initializing must be set *before*
224        stuffing the new module in sys.modules.
225     */
226     int rc = PyObject_GetOptionalAttr(mod, &_Py_ID(__spec__), &spec);
227     if (rc > 0) {
228         rc = _PyModuleSpec_IsInitializing(spec);
229         Py_DECREF(spec);
230     }
231     if (rc <= 0) {
232         return rc;
233     }
234     /* Wait until module is done importing. */
235     PyObject *value = PyObject_CallMethodOneArg(
236         IMPORTLIB(interp), &_Py_ID(_lock_unlock_module), name);
237     if (value == NULL) {
238         return -1;
239     }
240     Py_DECREF(value);
241     return 0;
242 }
243 
244 static void remove_importlib_frames(PyThreadState *tstate);
245 
246 PyObject *
PyImport_GetModule(PyObject * name)247 PyImport_GetModule(PyObject *name)
248 {
249     PyThreadState *tstate = _PyThreadState_GET();
250     PyObject *mod;
251 
252     mod = import_get_module(tstate, name);
253     if (mod != NULL && mod != Py_None) {
254         if (import_ensure_initialized(tstate->interp, mod, name) < 0) {
255             Py_DECREF(mod);
256             remove_importlib_frames(tstate);
257             return NULL;
258         }
259     }
260     return mod;
261 }
262 
263 /* Get the module object corresponding to a module name.
264    First check the modules dictionary if there's one there,
265    if not, create a new one and insert it in the modules dictionary. */
266 
267 static PyObject *
import_add_module(PyThreadState * tstate,PyObject * name)268 import_add_module(PyThreadState *tstate, PyObject *name)
269 {
270     PyObject *modules = get_modules_dict(tstate, false);
271     if (modules == NULL) {
272         return NULL;
273     }
274 
275     PyObject *m;
276     if (PyMapping_GetOptionalItem(modules, name, &m) < 0) {
277         return NULL;
278     }
279     if (m != NULL && PyModule_Check(m)) {
280         return m;
281     }
282     Py_XDECREF(m);
283     m = PyModule_NewObject(name);
284     if (m == NULL)
285         return NULL;
286     if (PyObject_SetItem(modules, name, m) != 0) {
287         Py_DECREF(m);
288         return NULL;
289     }
290 
291     return m;
292 }
293 
294 PyObject *
PyImport_AddModuleRef(const char * name)295 PyImport_AddModuleRef(const char *name)
296 {
297     PyObject *name_obj = PyUnicode_FromString(name);
298     if (name_obj == NULL) {
299         return NULL;
300     }
301     PyThreadState *tstate = _PyThreadState_GET();
302     PyObject *module = import_add_module(tstate, name_obj);
303     Py_DECREF(name_obj);
304     return module;
305 }
306 
307 
308 PyObject *
PyImport_AddModuleObject(PyObject * name)309 PyImport_AddModuleObject(PyObject *name)
310 {
311     PyThreadState *tstate = _PyThreadState_GET();
312     PyObject *mod = import_add_module(tstate, name);
313     if (!mod) {
314         return NULL;
315     }
316 
317     // gh-86160: PyImport_AddModuleObject() returns a borrowed reference.
318     // Create a weak reference to produce a borrowed reference, since it can
319     // become NULL. sys.modules type can be different than dict and it is not
320     // guaranteed that it keeps a strong reference to the module. It can be a
321     // custom mapping with __getitem__() which returns a new object or removes
322     // returned object, or __setitem__ which does nothing. There is so much
323     // unknown.  With weakref we can be sure that we get either a reference to
324     // live object or NULL.
325     //
326     // Use PyImport_AddModuleRef() to avoid these issues.
327     PyObject *ref = PyWeakref_NewRef(mod, NULL);
328     Py_DECREF(mod);
329     if (ref == NULL) {
330         return NULL;
331     }
332     mod = _PyWeakref_GET_REF(ref);
333     Py_DECREF(ref);
334     Py_XDECREF(mod);
335 
336     if (mod == NULL && !PyErr_Occurred()) {
337         PyErr_SetString(PyExc_RuntimeError,
338                         "sys.modules does not hold a strong reference "
339                         "to the module");
340     }
341     return mod; /* borrowed reference */
342 }
343 
344 
345 PyObject *
PyImport_AddModule(const char * name)346 PyImport_AddModule(const char *name)
347 {
348     PyObject *nameobj = PyUnicode_FromString(name);
349     if (nameobj == NULL) {
350         return NULL;
351     }
352     PyObject *module = PyImport_AddModuleObject(nameobj);
353     Py_DECREF(nameobj);
354     return module;
355 }
356 
357 
358 /* Remove name from sys.modules, if it's there.
359  * Can be called with an exception raised.
360  * If fail to remove name a new exception will be chained with the old
361  * exception, otherwise the old exception is preserved.
362  */
363 static void
remove_module(PyThreadState * tstate,PyObject * name)364 remove_module(PyThreadState *tstate, PyObject *name)
365 {
366     PyObject *exc = _PyErr_GetRaisedException(tstate);
367 
368     PyObject *modules = get_modules_dict(tstate, true);
369     if (PyDict_CheckExact(modules)) {
370         // Error is reported to the caller
371         (void)PyDict_Pop(modules, name, NULL);
372     }
373     else if (PyMapping_DelItem(modules, name) < 0) {
374         if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
375             _PyErr_Clear(tstate);
376         }
377     }
378 
379     _PyErr_ChainExceptions1(exc);
380 }
381 
382 
383 /************************************/
384 /* per-interpreter modules-by-index */
385 /************************************/
386 
387 Py_ssize_t
_PyImport_GetNextModuleIndex(void)388 _PyImport_GetNextModuleIndex(void)
389 {
390     return _Py_atomic_add_ssize(&LAST_MODULE_INDEX, 1) + 1;
391 }
392 
393 #ifndef NDEBUG
394 struct extensions_cache_value;
395 static struct extensions_cache_value * _find_cached_def(PyModuleDef *);
396 static Py_ssize_t _get_cached_module_index(struct extensions_cache_value *);
397 #endif
398 
399 static Py_ssize_t
_get_module_index_from_def(PyModuleDef * def)400 _get_module_index_from_def(PyModuleDef *def)
401 {
402     Py_ssize_t index = def->m_base.m_index;
403 #ifndef NDEBUG
404     struct extensions_cache_value *cached = _find_cached_def(def);
405     assert(cached == NULL || index == _get_cached_module_index(cached));
406 #endif
407     return index;
408 }
409 
410 static void
_set_module_index(PyModuleDef * def,Py_ssize_t index)411 _set_module_index(PyModuleDef *def, Py_ssize_t index)
412 {
413     assert(index > 0);
414     if (index == def->m_base.m_index) {
415         /* There's nothing to do. */
416     }
417     else if (def->m_base.m_index == 0) {
418         /* It should have been initialized by PyModuleDef_Init().
419          * We assert here to catch this in dev, but keep going otherwise. */
420         assert(def->m_base.m_index != 0);
421         def->m_base.m_index = index;
422     }
423     else {
424         /* It was already set for a different module.
425          * We replace the old value. */
426         assert(def->m_base.m_index > 0);
427         def->m_base.m_index = index;
428     }
429 }
430 
431 static const char *
_modules_by_index_check(PyInterpreterState * interp,Py_ssize_t index)432 _modules_by_index_check(PyInterpreterState *interp, Py_ssize_t index)
433 {
434     if (index <= 0) {
435         return "invalid module index";
436     }
437     if (MODULES_BY_INDEX(interp) == NULL) {
438         return "Interpreters module-list not accessible.";
439     }
440     if (index >= PyList_GET_SIZE(MODULES_BY_INDEX(interp))) {
441         return "Module index out of bounds.";
442     }
443     return NULL;
444 }
445 
446 static PyObject *
_modules_by_index_get(PyInterpreterState * interp,Py_ssize_t index)447 _modules_by_index_get(PyInterpreterState *interp, Py_ssize_t index)
448 {
449     if (_modules_by_index_check(interp, index) != NULL) {
450         return NULL;
451     }
452     PyObject *res = PyList_GET_ITEM(MODULES_BY_INDEX(interp), index);
453     return res==Py_None ? NULL : res;
454 }
455 
456 static int
_modules_by_index_set(PyInterpreterState * interp,Py_ssize_t index,PyObject * module)457 _modules_by_index_set(PyInterpreterState *interp,
458                       Py_ssize_t index, PyObject *module)
459 {
460     assert(index > 0);
461 
462     if (MODULES_BY_INDEX(interp) == NULL) {
463         MODULES_BY_INDEX(interp) = PyList_New(0);
464         if (MODULES_BY_INDEX(interp) == NULL) {
465             return -1;
466         }
467     }
468 
469     while (PyList_GET_SIZE(MODULES_BY_INDEX(interp)) <= index) {
470         if (PyList_Append(MODULES_BY_INDEX(interp), Py_None) < 0) {
471             return -1;
472         }
473     }
474 
475     return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(module));
476 }
477 
478 static int
_modules_by_index_clear_one(PyInterpreterState * interp,Py_ssize_t index)479 _modules_by_index_clear_one(PyInterpreterState *interp, Py_ssize_t index)
480 {
481     const char *err = _modules_by_index_check(interp, index);
482     if (err != NULL) {
483         Py_FatalError(err);
484         return -1;
485     }
486     return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(Py_None));
487 }
488 
489 
490 PyObject*
PyState_FindModule(PyModuleDef * module)491 PyState_FindModule(PyModuleDef* module)
492 {
493     PyInterpreterState *interp = _PyInterpreterState_GET();
494     if (module->m_slots) {
495         return NULL;
496     }
497     Py_ssize_t index = _get_module_index_from_def(module);
498     return _modules_by_index_get(interp, index);
499 }
500 
501 /* _PyState_AddModule() has been completely removed from the C-API
502    (and was removed from the limited API in 3.6).  However, we're
503    playing it safe and keeping it around for any stable ABI extensions
504    built against 3.2-3.5. */
505 int
_PyState_AddModule(PyThreadState * tstate,PyObject * module,PyModuleDef * def)506 _PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def)
507 {
508     if (!def) {
509         assert(_PyErr_Occurred(tstate));
510         return -1;
511     }
512     if (def->m_slots) {
513         _PyErr_SetString(tstate,
514                          PyExc_SystemError,
515                          "PyState_AddModule called on module with slots");
516         return -1;
517     }
518     assert(def->m_slots == NULL);
519     Py_ssize_t index = _get_module_index_from_def(def);
520     return _modules_by_index_set(tstate->interp, index, module);
521 }
522 
523 int
PyState_AddModule(PyObject * module,PyModuleDef * def)524 PyState_AddModule(PyObject* module, PyModuleDef* def)
525 {
526     if (!def) {
527         Py_FatalError("module definition is NULL");
528         return -1;
529     }
530 
531     PyThreadState *tstate = _PyThreadState_GET();
532     if (def->m_slots) {
533         _PyErr_SetString(tstate,
534                          PyExc_SystemError,
535                          "PyState_AddModule called on module with slots");
536         return -1;
537     }
538 
539     PyInterpreterState *interp = tstate->interp;
540     Py_ssize_t index = _get_module_index_from_def(def);
541     if (MODULES_BY_INDEX(interp) &&
542         index < PyList_GET_SIZE(MODULES_BY_INDEX(interp)) &&
543         module == PyList_GET_ITEM(MODULES_BY_INDEX(interp), index))
544     {
545         _Py_FatalErrorFormat(__func__, "module %p already added", module);
546         return -1;
547     }
548 
549     assert(def->m_slots == NULL);
550     return _modules_by_index_set(interp, index, module);
551 }
552 
553 int
PyState_RemoveModule(PyModuleDef * def)554 PyState_RemoveModule(PyModuleDef* def)
555 {
556     PyThreadState *tstate = _PyThreadState_GET();
557     if (def->m_slots) {
558         _PyErr_SetString(tstate,
559                          PyExc_SystemError,
560                          "PyState_RemoveModule called on module with slots");
561         return -1;
562     }
563     Py_ssize_t index = _get_module_index_from_def(def);
564     return _modules_by_index_clear_one(tstate->interp, index);
565 }
566 
567 
568 // Used by finalize_modules()
569 void
_PyImport_ClearModulesByIndex(PyInterpreterState * interp)570 _PyImport_ClearModulesByIndex(PyInterpreterState *interp)
571 {
572     if (!MODULES_BY_INDEX(interp)) {
573         return;
574     }
575 
576     Py_ssize_t i;
577     for (i = 0; i < PyList_GET_SIZE(MODULES_BY_INDEX(interp)); i++) {
578         PyObject *m = PyList_GET_ITEM(MODULES_BY_INDEX(interp), i);
579         if (PyModule_Check(m)) {
580             /* cleanup the saved copy of module dicts */
581             PyModuleDef *md = PyModule_GetDef(m);
582             if (md) {
583                 // XXX Do this more carefully.  The dict might be owned
584                 // by another interpreter.
585                 Py_CLEAR(md->m_base.m_copy);
586             }
587         }
588     }
589 
590     /* Setting modules_by_index to NULL could be dangerous, so we
591        clear the list instead. */
592     if (PyList_SetSlice(MODULES_BY_INDEX(interp),
593                         0, PyList_GET_SIZE(MODULES_BY_INDEX(interp)),
594                         NULL)) {
595         PyErr_FormatUnraisable("Exception ignored on clearing interpreters module list");
596     }
597 }
598 
599 
600 /*********************/
601 /* extension modules */
602 /*********************/
603 
604 /*
605     It may help to have a big picture view of what happens
606     when an extension is loaded.  This includes when it is imported
607     for the first time.
608 
609     Here's a summary, using importlib._bootstrap._load() as a starting point.
610 
611     1.  importlib._bootstrap._load()
612     2.    _load():  acquire import lock
613     3.    _load() -> importlib._bootstrap._load_unlocked()
614     4.      _load_unlocked() -> importlib._bootstrap.module_from_spec()
615     5.        module_from_spec() -> ExtensionFileLoader.create_module()
616     6.          create_module() -> _imp.create_dynamic()
617                     (see below)
618     7.        module_from_spec() -> importlib._bootstrap._init_module_attrs()
619     8.      _load_unlocked():  sys.modules[name] = module
620     9.      _load_unlocked() -> ExtensionFileLoader.exec_module()
621     10.       exec_module() -> _imp.exec_dynamic()
622                   (see below)
623     11.   _load():  release import lock
624 
625 
626     ...for single-phase init modules, where m_size == -1:
627 
628     (6). first time  (not found in _PyRuntime.imports.extensions):
629        A. _imp_create_dynamic_impl() -> import_find_extension()
630        B. _imp_create_dynamic_impl() -> _PyImport_GetModInitFunc()
631        C.   _PyImport_GetModInitFunc():  load <module init func>
632        D. _imp_create_dynamic_impl() -> import_run_extension()
633        E.   import_run_extension() -> _PyImport_RunModInitFunc()
634        F.     _PyImport_RunModInitFunc():  call <module init func>
635        G.       <module init func> -> PyModule_Create() -> PyModule_Create2()
636                                           -> PyModule_CreateInitialized()
637        H.         PyModule_CreateInitialized() -> PyModule_New()
638        I.         PyModule_CreateInitialized():  allocate mod->md_state
639        J.         PyModule_CreateInitialized() -> PyModule_AddFunctions()
640        K.         PyModule_CreateInitialized() -> PyModule_SetDocString()
641        L.       PyModule_CreateInitialized():  set mod->md_def
642        M.       <module init func>:  initialize the module, etc.
643        N.   import_run_extension()
644                 -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed()
645        O.   import_run_extension():  set __file__
646        P.   import_run_extension() -> update_global_state_for_extension()
647        Q.     update_global_state_for_extension():
648                       copy __dict__ into def->m_base.m_copy
649        R.     update_global_state_for_extension():
650                       add it to _PyRuntime.imports.extensions
651        S.   import_run_extension() -> finish_singlephase_extension()
652        T.     finish_singlephase_extension():
653                       add it to interp->imports.modules_by_index
654        U.     finish_singlephase_extension():  add it to sys.modules
655 
656        Step (Q) is skipped for core modules (sys/builtins).
657 
658     (6). subsequent times  (found in _PyRuntime.imports.extensions):
659        A. _imp_create_dynamic_impl() -> import_find_extension()
660        B.   import_find_extension() -> reload_singlephase_extension()
661        C.     reload_singlephase_extension()
662                   -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed()
663        D.     reload_singlephase_extension() -> import_add_module()
664        E.       if name in sys.modules:  use that module
665        F.       else:
666                   1. import_add_module() -> PyModule_NewObject()
667                   2. import_add_module():  set it on sys.modules
668        G.     reload_singlephase_extension():  copy the "m_copy" dict into __dict__
669        H.     reload_singlephase_extension():  add to modules_by_index
670 
671     (10). (every time):
672        A. noop
673 
674 
675     ...for single-phase init modules, where m_size >= 0:
676 
677     (6). not main interpreter and never loaded there - every time  (not found in _PyRuntime.imports.extensions):
678        A-P. (same as for m_size == -1)
679        Q.     _PyImport_RunModInitFunc():  set def->m_base.m_init
680        R. (skipped)
681        S-U. (same as for m_size == -1)
682 
683     (6). main interpreter - first time  (not found in _PyRuntime.imports.extensions):
684        A-P. (same as for m_size == -1)
685        Q.     _PyImport_RunModInitFunc():  set def->m_base.m_init
686        R-U. (same as for m_size == -1)
687 
688     (6). subsequent times  (found in _PyRuntime.imports.extensions):
689        A. _imp_create_dynamic_impl() -> import_find_extension()
690        B.   import_find_extension() -> reload_singlephase_extension()
691        C.     reload_singlephase_extension()
692                   -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed()
693        D.     reload_singlephase_extension():  call def->m_base.m_init  (see above)
694        E.     reload_singlephase_extension():  add the module to sys.modules
695        F.     reload_singlephase_extension():  add to modules_by_index
696 
697     (10). every time:
698        A. noop
699 
700 
701     ...for multi-phase init modules:
702 
703     (6). every time:
704        A. _imp_create_dynamic_impl() -> import_find_extension()  (not found)
705        B. _imp_create_dynamic_impl() -> _PyImport_GetModInitFunc()
706        C.   _PyImport_GetModInitFunc():  load <module init func>
707        D. _imp_create_dynamic_impl() -> import_run_extension()
708        E.   import_run_extension() -> _PyImport_RunModInitFunc()
709        F.     _PyImport_RunModInitFunc():  call <module init func>
710        G.   import_run_extension() -> PyModule_FromDefAndSpec()
711        H.      PyModule_FromDefAndSpec(): gather/check moduledef slots
712        I.      if there's a Py_mod_create slot:
713                  1. PyModule_FromDefAndSpec():  call its function
714        J.      else:
715                  1. PyModule_FromDefAndSpec() -> PyModule_NewObject()
716        K:      PyModule_FromDefAndSpec():  set mod->md_def
717        L.      PyModule_FromDefAndSpec() -> _add_methods_to_object()
718        M.      PyModule_FromDefAndSpec() -> PyModule_SetDocString()
719 
720     (10). every time:
721        A. _imp_exec_dynamic_impl() -> exec_builtin_or_dynamic()
722        B.   if mod->md_state == NULL (including if m_size == 0):
723             1. exec_builtin_or_dynamic() -> PyModule_ExecDef()
724             2.   PyModule_ExecDef():  allocate mod->md_state
725             3.   if there's a Py_mod_exec slot:
726                  1. PyModule_ExecDef():  call its function
727  */
728 
729 
730 /* Make sure name is fully qualified.
731 
732    This is a bit of a hack: when the shared library is loaded,
733    the module name is "package.module", but the module calls
734    PyModule_Create*() with just "module" for the name.  The shared
735    library loader squirrels away the true name of the module in
736    _PyRuntime.imports.pkgcontext, and PyModule_Create*() will
737    substitute this (if the name actually matches).
738 */
739 
740 #ifdef HAVE_THREAD_LOCAL
741 _Py_thread_local const char *pkgcontext = NULL;
742 # undef PKGCONTEXT
743 # define PKGCONTEXT pkgcontext
744 #endif
745 
746 const char *
_PyImport_ResolveNameWithPackageContext(const char * name)747 _PyImport_ResolveNameWithPackageContext(const char *name)
748 {
749 #ifndef HAVE_THREAD_LOCAL
750     PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
751 #endif
752     if (PKGCONTEXT != NULL) {
753         const char *p = strrchr(PKGCONTEXT, '.');
754         if (p != NULL && strcmp(name, p+1) == 0) {
755             name = PKGCONTEXT;
756             PKGCONTEXT = NULL;
757         }
758     }
759 #ifndef HAVE_THREAD_LOCAL
760     PyThread_release_lock(EXTENSIONS.mutex);
761 #endif
762     return name;
763 }
764 
765 const char *
_PyImport_SwapPackageContext(const char * newcontext)766 _PyImport_SwapPackageContext(const char *newcontext)
767 {
768 #ifndef HAVE_THREAD_LOCAL
769     PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
770 #endif
771     const char *oldcontext = PKGCONTEXT;
772     PKGCONTEXT = newcontext;
773 #ifndef HAVE_THREAD_LOCAL
774     PyThread_release_lock(EXTENSIONS.mutex);
775 #endif
776     return oldcontext;
777 }
778 
779 #ifdef HAVE_DLOPEN
780 int
_PyImport_GetDLOpenFlags(PyInterpreterState * interp)781 _PyImport_GetDLOpenFlags(PyInterpreterState *interp)
782 {
783     return DLOPENFLAGS(interp);
784 }
785 
786 void
_PyImport_SetDLOpenFlags(PyInterpreterState * interp,int new_val)787 _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val)
788 {
789     DLOPENFLAGS(interp) = new_val;
790 }
791 #endif  // HAVE_DLOPEN
792 
793 
794 /* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */
795 static int
exec_builtin_or_dynamic(PyObject * mod)796 exec_builtin_or_dynamic(PyObject *mod) {
797     PyModuleDef *def;
798     void *state;
799 
800     if (!PyModule_Check(mod)) {
801         return 0;
802     }
803 
804     def = PyModule_GetDef(mod);
805     if (def == NULL) {
806         return 0;
807     }
808 
809     state = PyModule_GetState(mod);
810     if (state) {
811         /* Already initialized; skip reload */
812         return 0;
813     }
814 
815     return PyModule_ExecDef(mod, def);
816 }
817 
818 
819 static int clear_singlephase_extension(PyInterpreterState *interp,
820                                        PyObject *name, PyObject *filename);
821 
822 // Currently, this is only used for testing.
823 // (See _testinternalcapi.clear_extension().)
824 // If adding another use, be careful about modules that import themselves
825 // recursively (see gh-123880).
826 int
_PyImport_ClearExtension(PyObject * name,PyObject * filename)827 _PyImport_ClearExtension(PyObject *name, PyObject *filename)
828 {
829     PyInterpreterState *interp = _PyInterpreterState_GET();
830 
831     /* Clearing a module's C globals is up to the module. */
832     if (clear_singlephase_extension(interp, name, filename) < 0) {
833         return -1;
834     }
835 
836     // In the future we'll probably also make sure the extension's
837     // file handle (and DL handle) is closed (requires saving it).
838 
839     return 0;
840 }
841 
842 
843 /*****************************/
844 /* single-phase init modules */
845 /*****************************/
846 
847 /*
848 We support a number of kinds of single-phase init builtin/extension modules:
849 
850 * "basic"
851     * no module state (PyModuleDef.m_size == -1)
852     * does not support repeated init (we use PyModuleDef.m_base.m_copy)
853     * may have process-global state
854     * the module's def is cached in _PyRuntime.imports.extensions,
855       by (name, filename)
856 * "reinit"
857     * no module state (PyModuleDef.m_size == 0)
858     * supports repeated init (m_copy is never used)
859     * should not have any process-global state
860     * its def is never cached in _PyRuntime.imports.extensions
861       (except, currently, under the main interpreter, for some reason)
862 * "with state"  (almost the same as reinit)
863     * has module state (PyModuleDef.m_size > 0)
864     * supports repeated init (m_copy is never used)
865     * should not have any process-global state
866     * its def is never cached in _PyRuntime.imports.extensions
867       (except, currently, under the main interpreter, for some reason)
868 
869 There are also variants within those classes:
870 
871 * two or more modules share a PyModuleDef
872     * a module's init func uses another module's PyModuleDef
873     * a module's init func calls another's module's init func
874     * a module's init "func" is actually a variable statically initialized
875       to another module's init func
876 * two or modules share "methods"
877     * a module's init func copies another module's PyModuleDef
878       (with a different name)
879 * (basic-only) two or modules share process-global state
880 
881 In the first case, where modules share a PyModuleDef, the following
882 notable weirdness happens:
883 
884 * the module's __name__ matches the def, not the requested name
885 * the last module (with the same def) to be imported for the first time wins
886     * returned by PyState_Find_Module() (via interp->modules_by_index)
887     * (non-basic-only) its init func is used when re-loading any of them
888       (via the def's m_init)
889     * (basic-only) the copy of its __dict__ is used when re-loading any of them
890       (via the def's m_copy)
891 
892 However, the following happens as expected:
893 
894 * a new module object (with its own __dict__) is created for each request
895 * the module's __spec__ has the requested name
896 * the loaded module is cached in sys.modules under the requested name
897 * the m_index field of the shared def is not changed,
898   so at least PyState_FindModule() will always look in the same place
899 
900 For "basic" modules there are other quirks:
901 
902 * (whether sharing a def or not) when loaded the first time,
903   m_copy is set before _init_module_attrs() is called
904   in importlib._bootstrap.module_from_spec(),
905   so when the module is re-loaded, the previous value
906   for __wpec__ (and others) is reset, possibly unexpectedly.
907 
908 Generally, when multiple interpreters are involved, some of the above
909 gets even messier.
910 */
911 
912 static inline void
extensions_lock_acquire(void)913 extensions_lock_acquire(void)
914 {
915     PyMutex_Lock(&_PyRuntime.imports.extensions.mutex);
916 }
917 
918 static inline void
extensions_lock_release(void)919 extensions_lock_release(void)
920 {
921     PyMutex_Unlock(&_PyRuntime.imports.extensions.mutex);
922 }
923 
924 
925 /* Magic for extension modules (built-in as well as dynamically
926    loaded).  To prevent initializing an extension module more than
927    once, we keep a static dictionary 'extensions' keyed by the tuple
928    (module name, module name)  (for built-in modules) or by
929    (filename, module name) (for dynamically loaded modules), containing these
930    modules.  A copy of the module's dictionary is stored by calling
931    fix_up_extension() immediately after the module initialization
932    function succeeds.  A copy can be retrieved from there by calling
933    import_find_extension().
934 
935    Modules which do support multiple initialization set their m_size
936    field to a non-negative number (indicating the size of the
937    module-specific state). They are still recorded in the extensions
938    dictionary, to avoid loading shared libraries twice.
939 */
940 
941 typedef struct cached_m_dict {
942     /* A shallow copy of the original module's __dict__. */
943     PyObject *copied;
944     /* The interpreter that owns the copy. */
945     int64_t interpid;
946 } *cached_m_dict_t;
947 
948 struct extensions_cache_value {
949     PyModuleDef *def;
950 
951     /* The function used to re-initialize the module.
952        This is only set for legacy (single-phase init) extension modules
953        and only used for those that support multiple initializations
954        (m_size >= 0).
955        It is set by update_global_state_for_extension(). */
956     PyModInitFunction m_init;
957 
958     /* The module's index into its interpreter's modules_by_index cache.
959        This is set for all extension modules but only used for legacy ones.
960        (See PyInterpreterState.modules_by_index for more info.) */
961     Py_ssize_t m_index;
962 
963     /* A copy of the module's __dict__ after the first time it was loaded.
964        This is only set/used for legacy modules that do not support
965        multiple initializations.
966        It is set exclusively by fixup_cached_def(). */
967     cached_m_dict_t m_dict;
968     struct cached_m_dict _m_dict;
969 
970     _Py_ext_module_origin origin;
971 
972 #ifdef Py_GIL_DISABLED
973     /* The module's md_gil slot, for legacy modules that are reinitialized from
974        m_dict rather than calling their initialization function again. */
975     void *md_gil;
976 #endif
977 };
978 
979 static struct extensions_cache_value *
alloc_extensions_cache_value(void)980 alloc_extensions_cache_value(void)
981 {
982     struct extensions_cache_value *value
983             = PyMem_RawMalloc(sizeof(struct extensions_cache_value));
984     if (value == NULL) {
985         PyErr_NoMemory();
986         return NULL;
987     }
988     *value = (struct extensions_cache_value){0};
989     return value;
990 }
991 
992 static void
free_extensions_cache_value(struct extensions_cache_value * value)993 free_extensions_cache_value(struct extensions_cache_value *value)
994 {
995     PyMem_RawFree(value);
996 }
997 
998 static Py_ssize_t
_get_cached_module_index(struct extensions_cache_value * cached)999 _get_cached_module_index(struct extensions_cache_value *cached)
1000 {
1001     assert(cached->m_index > 0);
1002     return cached->m_index;
1003 }
1004 
1005 static void
fixup_cached_def(struct extensions_cache_value * value)1006 fixup_cached_def(struct extensions_cache_value *value)
1007 {
1008     /* For the moment, the values in the def's m_base may belong
1009      * to another module, and we're replacing them here.  This can
1010      * cause problems later if the old module is reloaded.
1011      *
1012      * Also, we don't decref any old cached values first when we
1013      * replace them here, in case we need to restore them in the
1014      * near future.  Instead, the caller is responsible for wrapping
1015      * this up by calling cleanup_old_cached_def() or
1016      * restore_old_cached_def() if there was an error. */
1017     PyModuleDef *def = value->def;
1018     assert(def != NULL);
1019 
1020     /* We assume that all module defs are statically allocated
1021        and will never be freed.  Otherwise, we would incref here. */
1022     _Py_SetImmortalUntracked((PyObject *)def);
1023 
1024     def->m_base.m_init = value->m_init;
1025 
1026     assert(value->m_index > 0);
1027     _set_module_index(def, value->m_index);
1028 
1029     /* Different modules can share the same def, so we can't just
1030      * expect m_copy to be NULL. */
1031     assert(def->m_base.m_copy == NULL
1032            || def->m_base.m_init == NULL
1033            || value->m_dict != NULL);
1034     if (value->m_dict != NULL) {
1035         assert(value->m_dict->copied != NULL);
1036         /* As noted above, we don't first decref the old value, if any. */
1037         def->m_base.m_copy = Py_NewRef(value->m_dict->copied);
1038     }
1039 }
1040 
1041 static void
restore_old_cached_def(PyModuleDef * def,PyModuleDef_Base * oldbase)1042 restore_old_cached_def(PyModuleDef *def, PyModuleDef_Base *oldbase)
1043 {
1044     def->m_base = *oldbase;
1045 }
1046 
1047 static void
cleanup_old_cached_def(PyModuleDef_Base * oldbase)1048 cleanup_old_cached_def(PyModuleDef_Base *oldbase)
1049 {
1050     Py_XDECREF(oldbase->m_copy);
1051 }
1052 
1053 static void
del_cached_def(struct extensions_cache_value * value)1054 del_cached_def(struct extensions_cache_value *value)
1055 {
1056     /* If we hadn't made the stored defs immortal, we would decref here.
1057        However, this decref would be problematic if the module def were
1058        dynamically allocated, it were the last ref, and this function
1059        were called with an interpreter other than the def's owner. */
1060     assert(value->def == NULL || _Py_IsImmortalLoose(value->def));
1061 
1062     Py_XDECREF(value->def->m_base.m_copy);
1063     value->def->m_base.m_copy = NULL;
1064 }
1065 
1066 static int
init_cached_m_dict(struct extensions_cache_value * value,PyObject * m_dict)1067 init_cached_m_dict(struct extensions_cache_value *value, PyObject *m_dict)
1068 {
1069     assert(value != NULL);
1070     /* This should only have been called without an m_dict already set. */
1071     assert(value->m_dict == NULL);
1072     if (m_dict == NULL) {
1073         return 0;
1074     }
1075     assert(PyDict_Check(m_dict));
1076     assert(value->origin != _Py_ext_module_origin_CORE);
1077 
1078     PyInterpreterState *interp = _PyInterpreterState_GET();
1079     assert(!is_interpreter_isolated(interp));
1080 
1081     /* XXX gh-88216: The copied dict is owned by the current
1082      * interpreter.  That's a problem if the interpreter has
1083      * its own obmalloc state or if the module is successfully
1084      * imported into such an interpreter.  If the interpreter
1085      * has its own GIL then there may be data races and
1086      * PyImport_ClearModulesByIndex() can crash.  Normally,
1087      * a single-phase init module cannot be imported in an
1088      * isolated interpreter, but there are ways around that.
1089      * Hence, heere be dragons!  Ideally we would instead do
1090      * something like make a read-only, immortal copy of the
1091      * dict using PyMem_RawMalloc() and store *that* in m_copy.
1092      * Then we'd need to make sure to clear that when the
1093      * runtime is finalized, rather than in
1094      * PyImport_ClearModulesByIndex(). */
1095     PyObject *copied = PyDict_Copy(m_dict);
1096     if (copied == NULL) {
1097         /* We expect this can only be "out of memory". */
1098         return -1;
1099     }
1100     // XXX We may want to make the copy immortal.
1101 
1102     value->_m_dict = (struct cached_m_dict){
1103         .copied=copied,
1104         .interpid=PyInterpreterState_GetID(interp),
1105     };
1106 
1107     value->m_dict = &value->_m_dict;
1108     return 0;
1109 }
1110 
1111 static void
del_cached_m_dict(struct extensions_cache_value * value)1112 del_cached_m_dict(struct extensions_cache_value *value)
1113 {
1114     if (value->m_dict != NULL) {
1115         assert(value->m_dict == &value->_m_dict);
1116         assert(value->m_dict->copied != NULL);
1117         /* In the future we can take advantage of m_dict->interpid
1118          * to decref the dict using the owning interpreter. */
1119         Py_XDECREF(value->m_dict->copied);
1120         value->m_dict = NULL;
1121     }
1122 }
1123 
1124 static PyObject * get_core_module_dict(
1125         PyInterpreterState *interp, PyObject *name, PyObject *path);
1126 
1127 static PyObject *
get_cached_m_dict(struct extensions_cache_value * value,PyObject * name,PyObject * path)1128 get_cached_m_dict(struct extensions_cache_value *value,
1129                   PyObject *name, PyObject *path)
1130 {
1131     assert(value != NULL);
1132     PyInterpreterState *interp = _PyInterpreterState_GET();
1133     /* It might be a core module (e.g. sys & builtins),
1134        for which we don't cache m_dict. */
1135     if (value->origin == _Py_ext_module_origin_CORE) {
1136         return get_core_module_dict(interp, name, path);
1137     }
1138     assert(value->def != NULL);
1139     // XXX Switch to value->m_dict.
1140     PyObject *m_dict = value->def->m_base.m_copy;
1141     Py_XINCREF(m_dict);
1142     return m_dict;
1143 }
1144 
1145 static void
del_extensions_cache_value(struct extensions_cache_value * value)1146 del_extensions_cache_value(struct extensions_cache_value *value)
1147 {
1148     if (value != NULL) {
1149         del_cached_m_dict(value);
1150         del_cached_def(value);
1151         free_extensions_cache_value(value);
1152     }
1153 }
1154 
1155 static void *
hashtable_key_from_2_strings(PyObject * str1,PyObject * str2,const char sep)1156 hashtable_key_from_2_strings(PyObject *str1, PyObject *str2, const char sep)
1157 {
1158     const char *str1_data = _PyUnicode_AsUTF8NoNUL(str1);
1159     const char *str2_data = _PyUnicode_AsUTF8NoNUL(str2);
1160     if (str1_data == NULL || str2_data == NULL) {
1161         return NULL;
1162     }
1163     Py_ssize_t str1_len = strlen(str1_data);
1164     Py_ssize_t str2_len = strlen(str2_data);
1165 
1166     /* Make sure sep and the NULL byte won't cause an overflow. */
1167     assert(SIZE_MAX - str1_len - str2_len > 2);
1168     size_t size = str1_len + 1 + str2_len + 1;
1169 
1170     // XXX Use a buffer if it's a temp value (every case but "set").
1171     char *key = PyMem_RawMalloc(size);
1172     if (key == NULL) {
1173         PyErr_NoMemory();
1174         return NULL;
1175     }
1176 
1177     strncpy(key, str1_data, str1_len);
1178     key[str1_len] = sep;
1179     strncpy(key + str1_len + 1, str2_data, str2_len + 1);
1180     assert(strlen(key) == size - 1);
1181     return key;
1182 }
1183 
1184 static Py_uhash_t
hashtable_hash_str(const void * key)1185 hashtable_hash_str(const void *key)
1186 {
1187     return _Py_HashBytes(key, strlen((const char *)key));
1188 }
1189 
1190 static int
hashtable_compare_str(const void * key1,const void * key2)1191 hashtable_compare_str(const void *key1, const void *key2)
1192 {
1193     return strcmp((const char *)key1, (const char *)key2) == 0;
1194 }
1195 
1196 static void
hashtable_destroy_str(void * ptr)1197 hashtable_destroy_str(void *ptr)
1198 {
1199     PyMem_RawFree(ptr);
1200 }
1201 
1202 #ifndef NDEBUG
1203 struct hashtable_next_match_def_data {
1204     PyModuleDef *def;
1205     struct extensions_cache_value *matched;
1206 };
1207 
1208 static int
hashtable_next_match_def(_Py_hashtable_t * ht,const void * key,const void * value,void * user_data)1209 hashtable_next_match_def(_Py_hashtable_t *ht,
1210                          const void *key, const void *value, void *user_data)
1211 {
1212     if (value == NULL) {
1213         /* It was previously deleted. */
1214         return 0;
1215     }
1216     struct hashtable_next_match_def_data *data
1217             = (struct hashtable_next_match_def_data *)user_data;
1218     struct extensions_cache_value *cur
1219             = (struct extensions_cache_value *)value;
1220     if (cur->def == data->def) {
1221         data->matched = cur;
1222         return 1;
1223     }
1224     return 0;
1225 }
1226 
1227 static struct extensions_cache_value *
_find_cached_def(PyModuleDef * def)1228 _find_cached_def(PyModuleDef *def)
1229 {
1230     struct hashtable_next_match_def_data data = {0};
1231     (void)_Py_hashtable_foreach(
1232             EXTENSIONS.hashtable, hashtable_next_match_def, &data);
1233     return data.matched;
1234 }
1235 #endif
1236 
1237 #define HTSEP ':'
1238 
1239 static int
_extensions_cache_init(void)1240 _extensions_cache_init(void)
1241 {
1242     _Py_hashtable_allocator_t alloc = {PyMem_RawMalloc, PyMem_RawFree};
1243     EXTENSIONS.hashtable = _Py_hashtable_new_full(
1244         hashtable_hash_str,
1245         hashtable_compare_str,
1246         hashtable_destroy_str,  // key
1247         (_Py_hashtable_destroy_func)del_extensions_cache_value,  // value
1248         &alloc
1249     );
1250     if (EXTENSIONS.hashtable == NULL) {
1251         PyErr_NoMemory();
1252         return -1;
1253     }
1254     return 0;
1255 }
1256 
1257 static _Py_hashtable_entry_t *
_extensions_cache_find_unlocked(PyObject * path,PyObject * name,void ** p_key)1258 _extensions_cache_find_unlocked(PyObject *path, PyObject *name,
1259                                 void **p_key)
1260 {
1261     if (EXTENSIONS.hashtable == NULL) {
1262         return NULL;
1263     }
1264     void *key = hashtable_key_from_2_strings(path, name, HTSEP);
1265     if (key == NULL) {
1266         return NULL;
1267     }
1268     _Py_hashtable_entry_t *entry =
1269             _Py_hashtable_get_entry(EXTENSIONS.hashtable, key);
1270     if (p_key != NULL) {
1271         *p_key = key;
1272     }
1273     else {
1274         hashtable_destroy_str(key);
1275     }
1276     return entry;
1277 }
1278 
1279 /* This can only fail with "out of memory". */
1280 static struct extensions_cache_value *
_extensions_cache_get(PyObject * path,PyObject * name)1281 _extensions_cache_get(PyObject *path, PyObject *name)
1282 {
1283     struct extensions_cache_value *value = NULL;
1284     extensions_lock_acquire();
1285 
1286     _Py_hashtable_entry_t *entry =
1287             _extensions_cache_find_unlocked(path, name, NULL);
1288     if (entry == NULL) {
1289         /* It was never added. */
1290         goto finally;
1291     }
1292     value = (struct extensions_cache_value *)entry->value;
1293 
1294 finally:
1295     extensions_lock_release();
1296     return value;
1297 }
1298 
1299 /* This can only fail with "out of memory". */
1300 static struct extensions_cache_value *
_extensions_cache_set(PyObject * path,PyObject * name,PyModuleDef * def,PyModInitFunction m_init,Py_ssize_t m_index,PyObject * m_dict,_Py_ext_module_origin origin,void * md_gil)1301 _extensions_cache_set(PyObject *path, PyObject *name,
1302                       PyModuleDef *def, PyModInitFunction m_init,
1303                       Py_ssize_t m_index, PyObject *m_dict,
1304                       _Py_ext_module_origin origin, void *md_gil)
1305 {
1306     struct extensions_cache_value *value = NULL;
1307     void *key = NULL;
1308     struct extensions_cache_value *newvalue = NULL;
1309     PyModuleDef_Base olddefbase = def->m_base;
1310 
1311     assert(def != NULL);
1312     assert(m_init == NULL || m_dict == NULL);
1313     /* We expect the same symbol to be used and the shared object file
1314      * to have remained loaded, so it must be the same pointer. */
1315     assert(def->m_base.m_init == NULL || def->m_base.m_init == m_init);
1316     /* For now we don't worry about comparing value->m_copy. */
1317     assert(def->m_base.m_copy == NULL || m_dict != NULL);
1318     assert((origin == _Py_ext_module_origin_DYNAMIC) == (name != path));
1319     assert(origin != _Py_ext_module_origin_CORE || m_dict == NULL);
1320 
1321     extensions_lock_acquire();
1322 
1323     if (EXTENSIONS.hashtable == NULL) {
1324         if (_extensions_cache_init() < 0) {
1325             goto finally;
1326         }
1327     }
1328 
1329     /* Create a cached value to populate for the module. */
1330     _Py_hashtable_entry_t *entry =
1331             _extensions_cache_find_unlocked(path, name, &key);
1332     value = entry == NULL
1333         ? NULL
1334         : (struct extensions_cache_value *)entry->value;
1335     if (value != NULL) {
1336         /* gh-123880: If there's an existing cache value, it means a module is
1337          * being imported recursively from its PyInit_* or Py_mod_* function.
1338          * (That function presumably handles returning a partially
1339          *  constructed module in such a case.)
1340          * We can reuse the existing cache value; it is owned by the cache.
1341          * (Entries get removed from it in exceptional circumstances,
1342          *  after interpreter shutdown, and in runtime shutdown.)
1343          */
1344         goto finally_oldvalue;
1345     }
1346     newvalue = alloc_extensions_cache_value();
1347     if (newvalue == NULL) {
1348         goto finally;
1349     }
1350 
1351     /* Populate the new cache value data. */
1352     *newvalue = (struct extensions_cache_value){
1353         .def=def,
1354         .m_init=m_init,
1355         .m_index=m_index,
1356         /* m_dict is set by set_cached_m_dict(). */
1357         .origin=origin,
1358 #ifdef Py_GIL_DISABLED
1359         .md_gil=md_gil,
1360 #endif
1361     };
1362 #ifndef Py_GIL_DISABLED
1363     (void)md_gil;
1364 #endif
1365     if (init_cached_m_dict(newvalue, m_dict) < 0) {
1366         goto finally;
1367     }
1368     fixup_cached_def(newvalue);
1369 
1370     if (entry == NULL) {
1371         /* It was never added. */
1372         if (_Py_hashtable_set(EXTENSIONS.hashtable, key, newvalue) < 0) {
1373             PyErr_NoMemory();
1374             goto finally;
1375         }
1376         /* The hashtable owns the key now. */
1377         key = NULL;
1378     }
1379     else if (value == NULL) {
1380         /* It was previously deleted. */
1381         entry->value = newvalue;
1382     }
1383     else {
1384         /* We are updating the entry for an existing module. */
1385         /* We expect def to be static, so it must be the same pointer. */
1386         assert(value->def == def);
1387         /* We expect the same symbol to be used and the shared object file
1388          * to have remained loaded, so it must be the same pointer. */
1389         assert(value->m_init == m_init);
1390         /* The same module can't switch between caching __dict__ and not. */
1391         assert((value->m_dict == NULL) == (m_dict == NULL));
1392         /* This shouldn't ever happen. */
1393         Py_UNREACHABLE();
1394     }
1395 
1396     value = newvalue;
1397 
1398 finally:
1399     if (value == NULL) {
1400         restore_old_cached_def(def, &olddefbase);
1401         if (newvalue != NULL) {
1402             del_extensions_cache_value(newvalue);
1403         }
1404     }
1405     else {
1406         cleanup_old_cached_def(&olddefbase);
1407     }
1408 
1409 finally_oldvalue:
1410     extensions_lock_release();
1411     if (key != NULL) {
1412         hashtable_destroy_str(key);
1413     }
1414 
1415     return value;
1416 }
1417 
1418 static void
_extensions_cache_delete(PyObject * path,PyObject * name)1419 _extensions_cache_delete(PyObject *path, PyObject *name)
1420 {
1421     extensions_lock_acquire();
1422 
1423     if (EXTENSIONS.hashtable == NULL) {
1424         /* It was never added. */
1425         goto finally;
1426     }
1427 
1428     _Py_hashtable_entry_t *entry =
1429             _extensions_cache_find_unlocked(path, name, NULL);
1430     if (entry == NULL) {
1431         /* It was never added. */
1432         goto finally;
1433     }
1434     if (entry->value == NULL) {
1435         /* It was already removed. */
1436         goto finally;
1437     }
1438     struct extensions_cache_value *value = entry->value;
1439     entry->value = NULL;
1440 
1441     del_extensions_cache_value(value);
1442 
1443 finally:
1444     extensions_lock_release();
1445 }
1446 
1447 static void
_extensions_cache_clear_all(void)1448 _extensions_cache_clear_all(void)
1449 {
1450     /* The runtime (i.e. main interpreter) must be finalizing,
1451        so we don't need to worry about the lock. */
1452     _Py_hashtable_destroy(EXTENSIONS.hashtable);
1453     EXTENSIONS.hashtable = NULL;
1454 }
1455 
1456 #undef HTSEP
1457 
1458 
1459 static bool
check_multi_interp_extensions(PyInterpreterState * interp)1460 check_multi_interp_extensions(PyInterpreterState *interp)
1461 {
1462     int override = OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp);
1463     if (override < 0) {
1464         return false;
1465     }
1466     else if (override > 0) {
1467         return true;
1468     }
1469     else if (_PyInterpreterState_HasFeature(
1470                 interp, Py_RTFLAGS_MULTI_INTERP_EXTENSIONS)) {
1471         return true;
1472     }
1473     return false;
1474 }
1475 
1476 int
_PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char * name)1477 _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
1478 {
1479     PyInterpreterState *interp = _PyInterpreterState_GET();
1480     if (check_multi_interp_extensions(interp)) {
1481         assert(!_Py_IsMainInterpreter(interp));
1482         PyErr_Format(PyExc_ImportError,
1483                      "module %s does not support loading in subinterpreters",
1484                      name);
1485         return -1;
1486     }
1487     return 0;
1488 }
1489 
1490 #ifdef Py_GIL_DISABLED
1491 int
_PyImport_CheckGILForModule(PyObject * module,PyObject * module_name)1492 _PyImport_CheckGILForModule(PyObject* module, PyObject *module_name)
1493 {
1494     PyThreadState *tstate = _PyThreadState_GET();
1495     if (module == NULL) {
1496         _PyEval_DisableGIL(tstate);
1497         return 0;
1498     }
1499 
1500     if (!PyModule_Check(module) ||
1501         ((PyModuleObject *)module)->md_gil == Py_MOD_GIL_USED) {
1502         if (_PyEval_EnableGILPermanent(tstate)) {
1503             int warn_result = PyErr_WarnFormat(
1504                 PyExc_RuntimeWarning,
1505                 1,
1506                 "The global interpreter lock (GIL) has been enabled to load "
1507                 "module '%U', which has not declared that it can run safely "
1508                 "without the GIL. To override this behavior and keep the GIL "
1509                 "disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.",
1510                 module_name
1511             );
1512             if (warn_result < 0) {
1513                 return warn_result;
1514             }
1515         }
1516 
1517         const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
1518         if (config->enable_gil == _PyConfig_GIL_DEFAULT && config->verbose) {
1519             PySys_FormatStderr("# loading module '%U', which requires the GIL\n",
1520                                module_name);
1521         }
1522     }
1523     else {
1524         _PyEval_DisableGIL(tstate);
1525     }
1526 
1527     return 0;
1528 }
1529 #endif
1530 
1531 static PyThreadState *
switch_to_main_interpreter(PyThreadState * tstate)1532 switch_to_main_interpreter(PyThreadState *tstate)
1533 {
1534     if (_Py_IsMainInterpreter(tstate->interp)) {
1535         return tstate;
1536     }
1537     PyThreadState *main_tstate = _PyThreadState_NewBound(
1538             _PyInterpreterState_Main(), _PyThreadState_WHENCE_EXEC);
1539     if (main_tstate == NULL) {
1540         return NULL;
1541     }
1542 #ifndef NDEBUG
1543     PyThreadState *old_tstate = PyThreadState_Swap(main_tstate);
1544     assert(old_tstate == tstate);
1545 #else
1546     (void)PyThreadState_Swap(main_tstate);
1547 #endif
1548     return main_tstate;
1549 }
1550 
1551 static void
switch_back_from_main_interpreter(PyThreadState * tstate,PyThreadState * main_tstate,PyObject * tempobj)1552 switch_back_from_main_interpreter(PyThreadState *tstate,
1553                                   PyThreadState *main_tstate,
1554                                   PyObject *tempobj)
1555 {
1556     assert(main_tstate == PyThreadState_GET());
1557     assert(_Py_IsMainInterpreter(main_tstate->interp));
1558     assert(tstate->interp != main_tstate->interp);
1559 
1560     /* Handle any exceptions, which we cannot propagate directly
1561      * to the subinterpreter. */
1562     if (PyErr_Occurred()) {
1563         if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
1564             /* We trust it will be caught again soon. */
1565             PyErr_Clear();
1566         }
1567         else {
1568             /* Printing the exception should be sufficient. */
1569             PyErr_PrintEx(0);
1570         }
1571     }
1572 
1573     Py_XDECREF(tempobj);
1574 
1575     PyThreadState_Clear(main_tstate);
1576     (void)PyThreadState_Swap(tstate);
1577     PyThreadState_Delete(main_tstate);
1578 }
1579 
1580 static PyObject *
get_core_module_dict(PyInterpreterState * interp,PyObject * name,PyObject * path)1581 get_core_module_dict(PyInterpreterState *interp,
1582                      PyObject *name, PyObject *path)
1583 {
1584     /* Only builtin modules are core. */
1585     if (path == name) {
1586         assert(!PyErr_Occurred());
1587         if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
1588             return Py_NewRef(interp->sysdict_copy);
1589         }
1590         assert(!PyErr_Occurred());
1591         if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
1592             return Py_NewRef(interp->builtins_copy);
1593         }
1594         assert(!PyErr_Occurred());
1595     }
1596     return NULL;
1597 }
1598 
1599 #ifndef NDEBUG
1600 static inline int
is_core_module(PyInterpreterState * interp,PyObject * name,PyObject * path)1601 is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *path)
1602 {
1603     /* This might be called before the core dict copies are in place,
1604        so we can't rely on get_core_module_dict() here. */
1605     if (path == name) {
1606         if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
1607             return 1;
1608         }
1609         if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
1610             return 1;
1611         }
1612     }
1613     return 0;
1614 }
1615 
1616 
1617 static _Py_ext_module_kind
_get_extension_kind(PyModuleDef * def,bool check_size)1618 _get_extension_kind(PyModuleDef *def, bool check_size)
1619 {
1620     _Py_ext_module_kind kind;
1621     if (def == NULL) {
1622         /* It must be a module created by reload_singlephase_extension()
1623          * from m_copy.  Ideally we'd do away with this case. */
1624         kind = _Py_ext_module_kind_SINGLEPHASE;
1625     }
1626     else if (def->m_slots != NULL) {
1627         kind = _Py_ext_module_kind_MULTIPHASE;
1628     }
1629     else if (check_size && def->m_size == -1) {
1630         kind = _Py_ext_module_kind_SINGLEPHASE;
1631     }
1632     else if (def->m_base.m_init != NULL) {
1633         kind = _Py_ext_module_kind_SINGLEPHASE;
1634     }
1635     else {
1636         // This is probably single-phase init, but a multi-phase
1637         // module *can* have NULL m_slots.
1638         kind = _Py_ext_module_kind_UNKNOWN;
1639     }
1640     return kind;
1641 }
1642 
1643 /* The module might not be fully initialized yet
1644  * and PyModule_FromDefAndSpec() checks m_size
1645  * so we skip m_size. */
1646 #define assert_multiphase_def(def)                                  \
1647     do {                                                            \
1648         _Py_ext_module_kind kind = _get_extension_kind(def, false); \
1649         assert(kind == _Py_ext_module_kind_MULTIPHASE               \
1650                 /* m_slots can be NULL. */                          \
1651                 || kind == _Py_ext_module_kind_UNKNOWN);            \
1652     } while (0)
1653 
1654 #define assert_singlephase_def(def)                                 \
1655     do {                                                            \
1656         _Py_ext_module_kind kind = _get_extension_kind(def, true);  \
1657         assert(kind == _Py_ext_module_kind_SINGLEPHASE              \
1658                 || kind == _Py_ext_module_kind_UNKNOWN);            \
1659     } while (0)
1660 
1661 #define assert_singlephase(cached)                                          \
1662     do {                                                                    \
1663         _Py_ext_module_kind kind = _get_extension_kind(cached->def, true);  \
1664         assert(kind == _Py_ext_module_kind_SINGLEPHASE);                    \
1665     } while (0)
1666 
1667 #else  /* defined(NDEBUG) */
1668 #define assert_multiphase_def(def)
1669 #define assert_singlephase_def(def)
1670 #define assert_singlephase(cached)
1671 #endif
1672 
1673 
1674 struct singlephase_global_update {
1675     PyModInitFunction m_init;
1676     Py_ssize_t m_index;
1677     PyObject *m_dict;
1678     _Py_ext_module_origin origin;
1679     void *md_gil;
1680 };
1681 
1682 static struct extensions_cache_value *
update_global_state_for_extension(PyThreadState * tstate,PyObject * path,PyObject * name,PyModuleDef * def,struct singlephase_global_update * singlephase)1683 update_global_state_for_extension(PyThreadState *tstate,
1684                                   PyObject *path, PyObject *name,
1685                                   PyModuleDef *def,
1686                                   struct singlephase_global_update *singlephase)
1687 {
1688     struct extensions_cache_value *cached = NULL;
1689     PyModInitFunction m_init = NULL;
1690     PyObject *m_dict = NULL;
1691 
1692     /* Set up for _extensions_cache_set(). */
1693     if (singlephase == NULL) {
1694         assert(def->m_base.m_init == NULL);
1695         assert(def->m_base.m_copy == NULL);
1696     }
1697     else {
1698         if (singlephase->m_init != NULL) {
1699             assert(singlephase->m_dict == NULL);
1700             assert(def->m_base.m_copy == NULL);
1701             assert(def->m_size >= 0);
1702             /* Remember pointer to module init function. */
1703             // XXX If two modules share a def then def->m_base will
1704             // reflect the last one added (here) to the global cache.
1705             // We should prevent this somehow.  The simplest solution
1706             // is probably to store m_copy/m_init in the cache along
1707             // with the def, rather than within the def.
1708             m_init = singlephase->m_init;
1709         }
1710         else if (singlephase->m_dict == NULL) {
1711             /* It must be a core builtin module. */
1712             assert(is_core_module(tstate->interp, name, path));
1713             assert(def->m_size == -1);
1714             assert(def->m_base.m_copy == NULL);
1715             assert(def->m_base.m_init == NULL);
1716         }
1717         else {
1718             assert(PyDict_Check(singlephase->m_dict));
1719             // gh-88216: Extensions and def->m_base.m_copy can be updated
1720             // when the extension module doesn't support sub-interpreters.
1721             assert(def->m_size == -1);
1722             assert(!is_core_module(tstate->interp, name, path));
1723             assert(PyUnicode_CompareWithASCIIString(name, "sys") != 0);
1724             assert(PyUnicode_CompareWithASCIIString(name, "builtins") != 0);
1725             m_dict = singlephase->m_dict;
1726         }
1727     }
1728 
1729     /* Add the module's def to the global cache. */
1730     // XXX Why special-case the main interpreter?
1731     if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
1732 #ifndef NDEBUG
1733         cached = _extensions_cache_get(path, name);
1734         assert(cached == NULL || cached->def == def);
1735 #endif
1736         cached = _extensions_cache_set(
1737                 path, name, def, m_init, singlephase->m_index, m_dict,
1738                 singlephase->origin, singlephase->md_gil);
1739         if (cached == NULL) {
1740             // XXX Ignore this error?  Doing so would effectively
1741             // mark the module as not loadable.
1742             return NULL;
1743         }
1744     }
1745 
1746     return cached;
1747 }
1748 
1749 /* For multi-phase init modules, the module is finished
1750  * by PyModule_FromDefAndSpec(). */
1751 static int
finish_singlephase_extension(PyThreadState * tstate,PyObject * mod,struct extensions_cache_value * cached,PyObject * name,PyObject * modules)1752 finish_singlephase_extension(PyThreadState *tstate, PyObject *mod,
1753                              struct extensions_cache_value *cached,
1754                              PyObject *name, PyObject *modules)
1755 {
1756     assert(mod != NULL && PyModule_Check(mod));
1757     assert(cached->def == _PyModule_GetDef(mod));
1758 
1759     Py_ssize_t index = _get_cached_module_index(cached);
1760     if (_modules_by_index_set(tstate->interp, index, mod) < 0) {
1761         return -1;
1762     }
1763 
1764     if (modules != NULL) {
1765         if (PyObject_SetItem(modules, name, mod) < 0) {
1766             return -1;
1767         }
1768     }
1769 
1770     return 0;
1771 }
1772 
1773 
1774 static PyObject *
reload_singlephase_extension(PyThreadState * tstate,struct extensions_cache_value * cached,struct _Py_ext_module_loader_info * info)1775 reload_singlephase_extension(PyThreadState *tstate,
1776                              struct extensions_cache_value *cached,
1777                              struct _Py_ext_module_loader_info *info)
1778 {
1779     PyModuleDef *def = cached->def;
1780     assert(def != NULL);
1781     assert_singlephase(cached);
1782     PyObject *mod = NULL;
1783 
1784     /* It may have been successfully imported previously
1785        in an interpreter that allows legacy modules
1786        but is not allowed in the current interpreter. */
1787     const char *name_buf = PyUnicode_AsUTF8(info->name);
1788     assert(name_buf != NULL);
1789     if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
1790         return NULL;
1791     }
1792 
1793     PyObject *modules = get_modules_dict(tstate, true);
1794     if (def->m_size == -1) {
1795         /* Module does not support repeated initialization */
1796         assert(cached->m_init == NULL);
1797         assert(def->m_base.m_init == NULL);
1798         // XXX Copying the cached dict may break interpreter isolation.
1799         // We could solve this by temporarily acquiring the original
1800         // interpreter's GIL.
1801         PyObject *m_copy = get_cached_m_dict(cached, info->name, info->path);
1802         if (m_copy == NULL) {
1803             assert(!PyErr_Occurred());
1804             return NULL;
1805         }
1806         mod = import_add_module(tstate, info->name);
1807         if (mod == NULL) {
1808             Py_DECREF(m_copy);
1809             return NULL;
1810         }
1811         PyObject *mdict = PyModule_GetDict(mod);
1812         if (mdict == NULL) {
1813             Py_DECREF(m_copy);
1814             Py_DECREF(mod);
1815             return NULL;
1816         }
1817         int rc = PyDict_Update(mdict, m_copy);
1818         Py_DECREF(m_copy);
1819         if (rc < 0) {
1820             Py_DECREF(mod);
1821             return NULL;
1822         }
1823 #ifdef Py_GIL_DISABLED
1824         if (def->m_base.m_copy != NULL) {
1825             // For non-core modules, fetch the GIL slot that was stored by
1826             // import_run_extension().
1827             ((PyModuleObject *)mod)->md_gil = cached->md_gil;
1828         }
1829 #endif
1830         /* We can't set mod->md_def if it's missing,
1831          * because _PyImport_ClearModulesByIndex() might break
1832          * due to violating interpreter isolation.
1833          * See the note in set_cached_m_dict().
1834          * Until that is solved, we leave md_def set to NULL. */
1835         assert(_PyModule_GetDef(mod) == NULL
1836                || _PyModule_GetDef(mod) == def);
1837     }
1838     else {
1839         assert(cached->m_dict == NULL);
1840         assert(def->m_base.m_copy == NULL);
1841         // XXX Use cached->m_init.
1842         PyModInitFunction p0 = def->m_base.m_init;
1843         if (p0 == NULL) {
1844             assert(!PyErr_Occurred());
1845             return NULL;
1846         }
1847         struct _Py_ext_module_loader_result res;
1848         if (_PyImport_RunModInitFunc(p0, info, &res) < 0) {
1849             _Py_ext_module_loader_result_apply_error(&res, name_buf);
1850             return NULL;
1851         }
1852         assert(!PyErr_Occurred());
1853         assert(res.err == NULL);
1854         assert(res.kind == _Py_ext_module_kind_SINGLEPHASE);
1855         mod = res.module;
1856         /* Tchnically, the init function could return a different module def.
1857          * Then we would probably need to update the global cache.
1858          * However, we don't expect anyone to change the def. */
1859         assert(res.def == def);
1860         _Py_ext_module_loader_result_clear(&res);
1861 
1862         /* Remember the filename as the __file__ attribute */
1863         if (info->filename != NULL) {
1864             if (PyModule_AddObjectRef(mod, "__file__", info->filename) < 0) {
1865                 PyErr_Clear(); /* Not important enough to report */
1866             }
1867         }
1868 
1869         if (PyObject_SetItem(modules, info->name, mod) == -1) {
1870             Py_DECREF(mod);
1871             return NULL;
1872         }
1873     }
1874 
1875     Py_ssize_t index = _get_cached_module_index(cached);
1876     if (_modules_by_index_set(tstate->interp, index, mod) < 0) {
1877         PyMapping_DelItem(modules, info->name);
1878         Py_DECREF(mod);
1879         return NULL;
1880     }
1881 
1882     return mod;
1883 }
1884 
1885 static PyObject *
import_find_extension(PyThreadState * tstate,struct _Py_ext_module_loader_info * info,struct extensions_cache_value ** p_cached)1886 import_find_extension(PyThreadState *tstate,
1887                       struct _Py_ext_module_loader_info *info,
1888                       struct extensions_cache_value **p_cached)
1889 {
1890     /* Only single-phase init modules will be in the cache. */
1891     struct extensions_cache_value *cached
1892             = _extensions_cache_get(info->path, info->name);
1893     if (cached == NULL) {
1894         return NULL;
1895     }
1896     assert(cached->def != NULL);
1897     assert_singlephase(cached);
1898     *p_cached = cached;
1899 
1900     /* It may have been successfully imported previously
1901        in an interpreter that allows legacy modules
1902        but is not allowed in the current interpreter. */
1903     const char *name_buf = PyUnicode_AsUTF8(info->name);
1904     assert(name_buf != NULL);
1905     if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
1906         return NULL;
1907     }
1908 
1909     PyObject *mod = reload_singlephase_extension(tstate, cached, info);
1910     if (mod == NULL) {
1911         return NULL;
1912     }
1913 
1914     int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
1915     if (verbose) {
1916         PySys_FormatStderr("import %U # previously loaded (%R)\n",
1917                            info->name, info->path);
1918     }
1919 
1920     return mod;
1921 }
1922 
1923 static PyObject *
import_run_extension(PyThreadState * tstate,PyModInitFunction p0,struct _Py_ext_module_loader_info * info,PyObject * spec,PyObject * modules)1924 import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1925                      struct _Py_ext_module_loader_info *info,
1926                      PyObject *spec, PyObject *modules)
1927 {
1928     /* Core modules go through _PyImport_FixupBuiltin(). */
1929     assert(!is_core_module(tstate->interp, info->name, info->path));
1930 
1931     PyObject *mod = NULL;
1932     PyModuleDef *def = NULL;
1933     struct extensions_cache_value *cached = NULL;
1934     const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
1935 
1936     /* We cannot know if the module is single-phase init or
1937      * multi-phase init until after we call its init function. Even
1938      * in isolated interpreters (that do not support single-phase init),
1939      * the init function will run without restriction.  For multi-phase
1940      * init modules that isn't a problem because the init function only
1941      * runs PyModuleDef_Init() on the module's def and then returns it.
1942      *
1943      * However, for single-phase init the module's init function will
1944      * create the module, create other objects (and allocate other
1945      * memory), populate it and its module state, and initialze static
1946      * types.  Some modules store other objects and data in global C
1947      * variables and register callbacks with the runtime/stdlib or
1948      * even external libraries (which is part of why we can't just
1949      * dlclose() the module in the error case).  That's a problem
1950      * for isolated interpreters since all of the above happens
1951      * and only then * will the import fail.  Memory will leak,
1952      * callbacks will still get used, and sometimes there
1953      * will be crashes (memory access violations
1954      * and use-after-free).
1955      *
1956      * To put it another way, if the module is single-phase init
1957      * then the import will probably break interpreter isolation
1958      * and should fail ASAP.  However, the module's init function
1959      * will still get run.  That means it may still store state
1960      * in the shared-object/DLL address space (which never gets
1961      * closed/cleared), including objects (e.g. static types).
1962      * This is a problem for isolated subinterpreters since each
1963      * has its own object allocator.  If the loaded shared-object
1964      * still holds a reference to an object after the corresponding
1965      * interpreter has finalized then either we must let it leak
1966      * or else any later use of that object by another interpreter
1967      * (or across multiple init-fini cycles) will crash the process.
1968      *
1969      * To avoid all of that, we make sure the module's init function
1970      * is always run first with the main interpreter active.  If it was
1971      * already the main interpreter then we can continue loading the
1972      * module like normal.  Otherwise, right after the init function,
1973      * we take care of some import state bookkeeping, switch back
1974      * to the subinterpreter, check for single-phase init,
1975      * and then continue loading like normal. */
1976 
1977     bool switched = false;
1978     /* We *could* leave in place a legacy interpreter here
1979      * (one that shares obmalloc/GIL with main interp),
1980      * but there isn't a big advantage, we anticipate
1981      * such interpreters will be increasingly uncommon,
1982      * and the code is a bit simpler if we always switch
1983      * to the main interpreter. */
1984     PyThreadState *main_tstate = switch_to_main_interpreter(tstate);
1985     if (main_tstate == NULL) {
1986         return NULL;
1987     }
1988     else if (main_tstate != tstate) {
1989         switched = true;
1990         /* In the switched case, we could play it safe
1991          * by getting the main interpreter's import lock here.
1992          * It's unlikely to matter though. */
1993     }
1994 
1995     struct _Py_ext_module_loader_result res;
1996     int rc = _PyImport_RunModInitFunc(p0, info, &res);
1997     if (rc < 0) {
1998         /* We discard res.def. */
1999         assert(res.module == NULL);
2000     }
2001     else {
2002         assert(!PyErr_Occurred());
2003         assert(res.err == NULL);
2004 
2005         mod = res.module;
2006         res.module = NULL;
2007         def = res.def;
2008         assert(def != NULL);
2009 
2010         /* Do anything else that should be done
2011          * while still using the main interpreter. */
2012         if (res.kind == _Py_ext_module_kind_SINGLEPHASE) {
2013             /* Remember the filename as the __file__ attribute */
2014             if (info->filename != NULL) {
2015                 PyObject *filename = NULL;
2016                 if (switched) {
2017                     // The original filename may be allocated by subinterpreter's
2018                     // obmalloc, so we create a copy here.
2019                     filename = _PyUnicode_Copy(info->filename);
2020                     if (filename == NULL) {
2021                         return NULL;
2022                     }
2023                 } else {
2024                     filename = Py_NewRef(info->filename);
2025                 }
2026                 // XXX There's a refleak somewhere with the filename.
2027                 // Until we can track it down, we immortalize it.
2028                 PyInterpreterState *interp = _PyInterpreterState_GET();
2029                 _PyUnicode_InternImmortal(interp, &filename);
2030 
2031                 if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) {
2032                     PyErr_Clear(); /* Not important enough to report */
2033                 }
2034             }
2035 
2036             /* Update global import state. */
2037             assert(def->m_base.m_index != 0);
2038             struct singlephase_global_update singlephase = {
2039                 // XXX Modules that share a def should each get their own index,
2040                 // whereas currently they share (which means the per-interpreter
2041                 // cache is less reliable than it should be).
2042                 .m_index=def->m_base.m_index,
2043                 .origin=info->origin,
2044 #ifdef Py_GIL_DISABLED
2045                 .md_gil=((PyModuleObject *)mod)->md_gil,
2046 #endif
2047             };
2048             // gh-88216: Extensions and def->m_base.m_copy can be updated
2049             // when the extension module doesn't support sub-interpreters.
2050             if (def->m_size == -1) {
2051                 /* We will reload from m_copy. */
2052                 assert(def->m_base.m_init == NULL);
2053                 singlephase.m_dict = PyModule_GetDict(mod);
2054                 assert(singlephase.m_dict != NULL);
2055             }
2056             else {
2057                 /* We will reload via the init function. */
2058                 assert(def->m_size >= 0);
2059                 assert(def->m_base.m_copy == NULL);
2060                 singlephase.m_init = p0;
2061             }
2062             cached = update_global_state_for_extension(
2063                     main_tstate, info->path, info->name, def, &singlephase);
2064             if (cached == NULL) {
2065                 assert(PyErr_Occurred());
2066                 goto main_finally;
2067             }
2068         }
2069     }
2070 
2071 main_finally:
2072     /* Switch back to the subinterpreter. */
2073     if (switched) {
2074         assert(main_tstate != tstate);
2075         switch_back_from_main_interpreter(tstate, main_tstate, mod);
2076         /* Any module we got from the init function will have to be
2077          * reloaded in the subinterpreter. */
2078         mod = NULL;
2079     }
2080 
2081     /*****************************************************************/
2082     /* At this point we are back to the interpreter we started with. */
2083     /*****************************************************************/
2084 
2085     /* Finally we handle the error return from _PyImport_RunModInitFunc(). */
2086     if (rc < 0) {
2087         _Py_ext_module_loader_result_apply_error(&res, name_buf);
2088         goto error;
2089     }
2090 
2091     if (res.kind == _Py_ext_module_kind_MULTIPHASE) {
2092         assert_multiphase_def(def);
2093         assert(mod == NULL);
2094         /* Note that we cheat a little by not repeating the calls
2095          * to _PyImport_GetModInitFunc() and _PyImport_RunModInitFunc(). */
2096         mod = PyModule_FromDefAndSpec(def, spec);
2097         if (mod == NULL) {
2098             goto error;
2099         }
2100     }
2101     else {
2102         assert(res.kind == _Py_ext_module_kind_SINGLEPHASE);
2103         assert_singlephase_def(def);
2104 
2105         if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
2106             goto error;
2107         }
2108         assert(!PyErr_Occurred());
2109 
2110         if (switched) {
2111             /* We switched to the main interpreter to run the init
2112              * function, so now we will "reload" the module from the
2113              * cached data using the original subinterpreter. */
2114             assert(mod == NULL);
2115             mod = reload_singlephase_extension(tstate, cached, info);
2116             if (mod == NULL) {
2117                 goto error;
2118             }
2119             assert(!PyErr_Occurred());
2120             assert(PyModule_Check(mod));
2121         }
2122         else {
2123             assert(mod != NULL);
2124             assert(PyModule_Check(mod));
2125 
2126             /* Update per-interpreter import state. */
2127             PyObject *modules = get_modules_dict(tstate, true);
2128             if (finish_singlephase_extension(
2129                     tstate, mod, cached, info->name, modules) < 0)
2130             {
2131                 goto error;
2132             }
2133         }
2134     }
2135 
2136     _Py_ext_module_loader_result_clear(&res);
2137     return mod;
2138 
2139 error:
2140     Py_XDECREF(mod);
2141     _Py_ext_module_loader_result_clear(&res);
2142     return NULL;
2143 }
2144 
2145 
2146 // Used in _PyImport_ClearExtension; see notes there.
2147 static int
clear_singlephase_extension(PyInterpreterState * interp,PyObject * name,PyObject * path)2148 clear_singlephase_extension(PyInterpreterState *interp,
2149                             PyObject *name, PyObject *path)
2150 {
2151     struct extensions_cache_value *cached = _extensions_cache_get(path, name);
2152     if (cached == NULL) {
2153         if (PyErr_Occurred()) {
2154             return -1;
2155         }
2156         return 0;
2157     }
2158     PyModuleDef *def = cached->def;
2159 
2160     /* Clear data set when the module was initially loaded. */
2161     def->m_base.m_init = NULL;
2162     Py_CLEAR(def->m_base.m_copy);
2163     def->m_base.m_index = 0;
2164 
2165     /* Clear the PyState_*Module() cache entry. */
2166     Py_ssize_t index = _get_cached_module_index(cached);
2167     if (_modules_by_index_check(interp, index) == NULL) {
2168         if (_modules_by_index_clear_one(interp, index) < 0) {
2169             return -1;
2170         }
2171     }
2172 
2173     /* We must use the main interpreter to clean up the cache.
2174      * See the note in import_run_extension(). */
2175     PyThreadState *tstate = PyThreadState_GET();
2176     PyThreadState *main_tstate = switch_to_main_interpreter(tstate);
2177     if (main_tstate == NULL) {
2178         return -1;
2179     }
2180 
2181     /* Clear the cached module def. */
2182     _extensions_cache_delete(path, name);
2183 
2184     if (main_tstate != tstate) {
2185         switch_back_from_main_interpreter(tstate, main_tstate, NULL);
2186     }
2187 
2188     return 0;
2189 }
2190 
2191 
2192 /*******************/
2193 /* builtin modules */
2194 /*******************/
2195 
2196 int
_PyImport_FixupBuiltin(PyThreadState * tstate,PyObject * mod,const char * name,PyObject * modules)2197 _PyImport_FixupBuiltin(PyThreadState *tstate, PyObject *mod, const char *name,
2198                        PyObject *modules)
2199 {
2200     int res = -1;
2201     assert(mod != NULL && PyModule_Check(mod));
2202 
2203     PyObject *nameobj;
2204     nameobj = PyUnicode_InternFromString(name);
2205     if (nameobj == NULL) {
2206         return -1;
2207     }
2208 
2209     PyModuleDef *def = PyModule_GetDef(mod);
2210     if (def == NULL) {
2211         PyErr_BadInternalCall();
2212         goto finally;
2213     }
2214 
2215     /* We only use _PyImport_FixupBuiltin() for the core builtin modules
2216      * (sys and builtins).  These modules are single-phase init with no
2217      * module state, but we also don't populate def->m_base.m_copy
2218      * for them. */
2219     assert(is_core_module(tstate->interp, nameobj, nameobj));
2220     assert_singlephase_def(def);
2221     assert(def->m_size == -1);
2222     assert(def->m_base.m_copy == NULL);
2223     assert(def->m_base.m_index >= 0);
2224 
2225     /* We aren't using import_find_extension() for core modules,
2226      * so we have to do the extra check to make sure the module
2227      * isn't already in the global cache before calling
2228      * update_global_state_for_extension(). */
2229     struct extensions_cache_value *cached
2230             = _extensions_cache_get(nameobj, nameobj);
2231     if (cached == NULL) {
2232         struct singlephase_global_update singlephase = {
2233             .m_index=def->m_base.m_index,
2234             /* We don't want def->m_base.m_copy populated. */
2235             .m_dict=NULL,
2236             .origin=_Py_ext_module_origin_CORE,
2237 #ifdef Py_GIL_DISABLED
2238             /* Unused when m_dict == NULL. */
2239             .md_gil=NULL,
2240 #endif
2241         };
2242         cached = update_global_state_for_extension(
2243                 tstate, nameobj, nameobj, def, &singlephase);
2244         if (cached == NULL) {
2245             goto finally;
2246         }
2247     }
2248 
2249     if (finish_singlephase_extension(tstate, mod, cached, nameobj, modules) < 0) {
2250         goto finally;
2251     }
2252 
2253     res = 0;
2254 
2255 finally:
2256     Py_DECREF(nameobj);
2257     return res;
2258 }
2259 
2260 /* Helper to test for built-in module */
2261 
2262 static int
is_builtin(PyObject * name)2263 is_builtin(PyObject *name)
2264 {
2265     int i;
2266     struct _inittab *inittab = INITTAB;
2267     for (i = 0; inittab[i].name != NULL; i++) {
2268         if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) {
2269             if (inittab[i].initfunc == NULL)
2270                 return -1;
2271             else
2272                 return 1;
2273         }
2274     }
2275     return 0;
2276 }
2277 
2278 static PyObject*
create_builtin(PyThreadState * tstate,PyObject * name,PyObject * spec)2279 create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
2280 {
2281     struct _Py_ext_module_loader_info info;
2282     if (_Py_ext_module_loader_info_init_for_builtin(&info, name) < 0) {
2283         return NULL;
2284     }
2285 
2286     struct extensions_cache_value *cached = NULL;
2287     PyObject *mod = import_find_extension(tstate, &info, &cached);
2288     if (mod != NULL) {
2289         assert(!_PyErr_Occurred(tstate));
2290         assert(cached != NULL);
2291         /* The module might not have md_def set in certain reload cases. */
2292         assert(_PyModule_GetDef(mod) == NULL
2293                 || cached->def == _PyModule_GetDef(mod));
2294         assert_singlephase(cached);
2295         goto finally;
2296     }
2297     else if (_PyErr_Occurred(tstate)) {
2298         goto finally;
2299     }
2300 
2301     /* If the module was added to the global cache
2302      * but def->m_base.m_copy was cleared (e.g. subinterp fini)
2303      * then we have to do a little dance here. */
2304     if (cached != NULL) {
2305         assert(cached->def->m_base.m_copy == NULL);
2306         /* For now we clear the cache and move on. */
2307         _extensions_cache_delete(info.path, info.name);
2308     }
2309 
2310     struct _inittab *found = NULL;
2311     for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
2312         if (_PyUnicode_EqualToASCIIString(info.name, p->name)) {
2313             found = p;
2314         }
2315     }
2316     if (found == NULL) {
2317         // not found
2318         mod = Py_NewRef(Py_None);
2319         goto finally;
2320     }
2321 
2322     PyModInitFunction p0 = (PyModInitFunction)found->initfunc;
2323     if (p0 == NULL) {
2324         /* Cannot re-init internal module ("sys" or "builtins") */
2325         assert(is_core_module(tstate->interp, info.name, info.path));
2326         mod = import_add_module(tstate, info.name);
2327         goto finally;
2328     }
2329 
2330 #ifdef Py_GIL_DISABLED
2331     // This call (and the corresponding call to _PyImport_CheckGILForModule())
2332     // would ideally be inside import_run_extension(). They are kept in the
2333     // callers for now because that would complicate the control flow inside
2334     // import_run_extension(). It should be possible to restructure
2335     // import_run_extension() to address this.
2336     _PyEval_EnableGILTransient(tstate);
2337 #endif
2338     /* Now load it. */
2339     mod = import_run_extension(
2340                     tstate, p0, &info, spec, get_modules_dict(tstate, true));
2341 #ifdef Py_GIL_DISABLED
2342     if (_PyImport_CheckGILForModule(mod, info.name) < 0) {
2343         Py_CLEAR(mod);
2344         goto finally;
2345     }
2346 #endif
2347 
2348 finally:
2349     _Py_ext_module_loader_info_clear(&info);
2350     return mod;
2351 }
2352 
2353 
2354 /*****************************/
2355 /* the builtin modules table */
2356 /*****************************/
2357 
2358 /* API for embedding applications that want to add their own entries
2359    to the table of built-in modules.  This should normally be called
2360    *before* Py_Initialize().  When the table resize fails, -1 is
2361    returned and the existing table is unchanged.
2362 
2363    After a similar function by Just van Rossum. */
2364 
2365 int
PyImport_ExtendInittab(struct _inittab * newtab)2366 PyImport_ExtendInittab(struct _inittab *newtab)
2367 {
2368     struct _inittab *p;
2369     size_t i, n;
2370     int res = 0;
2371 
2372     if (INITTAB != NULL) {
2373         Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()");
2374     }
2375 
2376     /* Count the number of entries in both tables */
2377     for (n = 0; newtab[n].name != NULL; n++)
2378         ;
2379     if (n == 0)
2380         return 0; /* Nothing to do */
2381     for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2382         ;
2383 
2384     /* Force default raw memory allocator to get a known allocator to be able
2385        to release the memory in _PyImport_Fini2() */
2386     PyMemAllocatorEx old_alloc;
2387     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2388 
2389     /* Allocate new memory for the combined table */
2390     p = NULL;
2391     if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) {
2392         size_t size = sizeof(struct _inittab) * (i + n + 1);
2393         p = PyMem_RawRealloc(inittab_copy, size);
2394     }
2395     if (p == NULL) {
2396         res = -1;
2397         goto done;
2398     }
2399 
2400     /* Copy the tables into the new memory at the first call
2401        to PyImport_ExtendInittab(). */
2402     if (inittab_copy != PyImport_Inittab) {
2403         memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2404     }
2405     memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab));
2406     PyImport_Inittab = inittab_copy = p;
2407 
2408 done:
2409     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2410     return res;
2411 }
2412 
2413 /* Shorthand to add a single entry given a name and a function */
2414 
2415 int
PyImport_AppendInittab(const char * name,PyObject * (* initfunc)(void))2416 PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
2417 {
2418     struct _inittab newtab[2];
2419 
2420     if (INITTAB != NULL) {
2421         Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()");
2422     }
2423 
2424     memset(newtab, '\0', sizeof newtab);
2425 
2426     newtab[0].name = name;
2427     newtab[0].initfunc = initfunc;
2428 
2429     return PyImport_ExtendInittab(newtab);
2430 }
2431 
2432 
2433 /* the internal table */
2434 
2435 static int
init_builtin_modules_table(void)2436 init_builtin_modules_table(void)
2437 {
2438     size_t size;
2439     for (size = 0; PyImport_Inittab[size].name != NULL; size++)
2440         ;
2441     size++;
2442 
2443     /* Make the copy. */
2444     struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab));
2445     if (copied == NULL) {
2446         return -1;
2447     }
2448     memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab));
2449     INITTAB = copied;
2450     return 0;
2451 }
2452 
2453 static void
fini_builtin_modules_table(void)2454 fini_builtin_modules_table(void)
2455 {
2456     struct _inittab *inittab = INITTAB;
2457     INITTAB = NULL;
2458     PyMem_RawFree(inittab);
2459 }
2460 
2461 PyObject *
_PyImport_GetBuiltinModuleNames(void)2462 _PyImport_GetBuiltinModuleNames(void)
2463 {
2464     PyObject *list = PyList_New(0);
2465     if (list == NULL) {
2466         return NULL;
2467     }
2468     struct _inittab *inittab = INITTAB;
2469     for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) {
2470         PyObject *name = PyUnicode_FromString(inittab[i].name);
2471         if (name == NULL) {
2472             Py_DECREF(list);
2473             return NULL;
2474         }
2475         if (PyList_Append(list, name) < 0) {
2476             Py_DECREF(name);
2477             Py_DECREF(list);
2478             return NULL;
2479         }
2480         Py_DECREF(name);
2481     }
2482     return list;
2483 }
2484 
2485 
2486 /********************/
2487 /* the magic number */
2488 /********************/
2489 
2490 /* Helper for pythonrun.c -- return magic number and tag. */
2491 
2492 long
PyImport_GetMagicNumber(void)2493 PyImport_GetMagicNumber(void)
2494 {
2495     long res;
2496     PyInterpreterState *interp = _PyInterpreterState_GET();
2497     PyObject *external, *pyc_magic;
2498 
2499     external = PyObject_GetAttrString(IMPORTLIB(interp), "_bootstrap_external");
2500     if (external == NULL)
2501         return -1;
2502     pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER");
2503     Py_DECREF(external);
2504     if (pyc_magic == NULL)
2505         return -1;
2506     res = PyLong_AsLong(pyc_magic);
2507     Py_DECREF(pyc_magic);
2508     return res;
2509 }
2510 
2511 
2512 extern const char * _PySys_ImplCacheTag;
2513 
2514 const char *
PyImport_GetMagicTag(void)2515 PyImport_GetMagicTag(void)
2516 {
2517     return _PySys_ImplCacheTag;
2518 }
2519 
2520 
2521 /*********************************/
2522 /* a Python module's code object */
2523 /*********************************/
2524 
2525 /* Execute a code object in a module and return the module object
2526  * WITH INCREMENTED REFERENCE COUNT.  If an error occurs, name is
2527  * removed from sys.modules, to avoid leaving damaged module objects
2528  * in sys.modules.  The caller may wish to restore the original
2529  * module object (if any) in this case; PyImport_ReloadModule is an
2530  * example.
2531  *
2532  * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
2533  * interface.  The other two exist primarily for backward compatibility.
2534  */
2535 PyObject *
PyImport_ExecCodeModule(const char * name,PyObject * co)2536 PyImport_ExecCodeModule(const char *name, PyObject *co)
2537 {
2538     return PyImport_ExecCodeModuleWithPathnames(
2539         name, co, (char *)NULL, (char *)NULL);
2540 }
2541 
2542 PyObject *
PyImport_ExecCodeModuleEx(const char * name,PyObject * co,const char * pathname)2543 PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
2544 {
2545     return PyImport_ExecCodeModuleWithPathnames(
2546         name, co, pathname, (char *)NULL);
2547 }
2548 
2549 PyObject *
PyImport_ExecCodeModuleWithPathnames(const char * name,PyObject * co,const char * pathname,const char * cpathname)2550 PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
2551                                      const char *pathname,
2552                                      const char *cpathname)
2553 {
2554     PyObject *m = NULL;
2555     PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL;
2556 
2557     nameobj = PyUnicode_FromString(name);
2558     if (nameobj == NULL)
2559         return NULL;
2560 
2561     if (cpathname != NULL) {
2562         cpathobj = PyUnicode_DecodeFSDefault(cpathname);
2563         if (cpathobj == NULL)
2564             goto error;
2565     }
2566     else
2567         cpathobj = NULL;
2568 
2569     if (pathname != NULL) {
2570         pathobj = PyUnicode_DecodeFSDefault(pathname);
2571         if (pathobj == NULL)
2572             goto error;
2573     }
2574     else if (cpathobj != NULL) {
2575         PyInterpreterState *interp = _PyInterpreterState_GET();
2576 
2577         if (interp == NULL) {
2578             Py_FatalError("no current interpreter");
2579         }
2580 
2581         external= PyObject_GetAttrString(IMPORTLIB(interp),
2582                                          "_bootstrap_external");
2583         if (external != NULL) {
2584             pathobj = PyObject_CallMethodOneArg(
2585                 external, &_Py_ID(_get_sourcefile), cpathobj);
2586             Py_DECREF(external);
2587         }
2588         if (pathobj == NULL)
2589             PyErr_Clear();
2590     }
2591     else
2592         pathobj = NULL;
2593 
2594     m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
2595 error:
2596     Py_DECREF(nameobj);
2597     Py_XDECREF(pathobj);
2598     Py_XDECREF(cpathobj);
2599     return m;
2600 }
2601 
2602 static PyObject *
module_dict_for_exec(PyThreadState * tstate,PyObject * name)2603 module_dict_for_exec(PyThreadState *tstate, PyObject *name)
2604 {
2605     PyObject *m, *d;
2606 
2607     m = import_add_module(tstate, name);
2608     if (m == NULL)
2609         return NULL;
2610     /* If the module is being reloaded, we get the old module back
2611        and re-use its dict to exec the new code. */
2612     d = PyModule_GetDict(m);
2613     int r = PyDict_Contains(d, &_Py_ID(__builtins__));
2614     if (r == 0) {
2615         r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins());
2616     }
2617     if (r < 0) {
2618         remove_module(tstate, name);
2619         Py_DECREF(m);
2620         return NULL;
2621     }
2622 
2623     Py_INCREF(d);
2624     Py_DECREF(m);
2625     return d;
2626 }
2627 
2628 static PyObject *
exec_code_in_module(PyThreadState * tstate,PyObject * name,PyObject * module_dict,PyObject * code_object)2629 exec_code_in_module(PyThreadState *tstate, PyObject *name,
2630                     PyObject *module_dict, PyObject *code_object)
2631 {
2632     PyObject *v, *m;
2633 
2634     v = PyEval_EvalCode(code_object, module_dict, module_dict);
2635     if (v == NULL) {
2636         remove_module(tstate, name);
2637         return NULL;
2638     }
2639     Py_DECREF(v);
2640 
2641     m = import_get_module(tstate, name);
2642     if (m == NULL && !_PyErr_Occurred(tstate)) {
2643         _PyErr_Format(tstate, PyExc_ImportError,
2644                       "Loaded module %R not found in sys.modules",
2645                       name);
2646     }
2647 
2648     return m;
2649 }
2650 
2651 PyObject*
PyImport_ExecCodeModuleObject(PyObject * name,PyObject * co,PyObject * pathname,PyObject * cpathname)2652 PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
2653                               PyObject *cpathname)
2654 {
2655     PyThreadState *tstate = _PyThreadState_GET();
2656     PyObject *d, *external, *res;
2657 
2658     d = module_dict_for_exec(tstate, name);
2659     if (d == NULL) {
2660         return NULL;
2661     }
2662 
2663     if (pathname == NULL) {
2664         pathname = ((PyCodeObject *)co)->co_filename;
2665     }
2666     external = PyObject_GetAttrString(IMPORTLIB(tstate->interp),
2667                                       "_bootstrap_external");
2668     if (external == NULL) {
2669         Py_DECREF(d);
2670         return NULL;
2671     }
2672     res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module),
2673                                      d, name, pathname, cpathname, NULL);
2674     Py_DECREF(external);
2675     if (res != NULL) {
2676         Py_DECREF(res);
2677         res = exec_code_in_module(tstate, name, d, co);
2678     }
2679     Py_DECREF(d);
2680     return res;
2681 }
2682 
2683 
2684 static void
update_code_filenames(PyCodeObject * co,PyObject * oldname,PyObject * newname)2685 update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
2686 {
2687     PyObject *constants, *tmp;
2688     Py_ssize_t i, n;
2689 
2690     if (PyUnicode_Compare(co->co_filename, oldname))
2691         return;
2692 
2693     Py_XSETREF(co->co_filename, Py_NewRef(newname));
2694 
2695     constants = co->co_consts;
2696     n = PyTuple_GET_SIZE(constants);
2697     for (i = 0; i < n; i++) {
2698         tmp = PyTuple_GET_ITEM(constants, i);
2699         if (PyCode_Check(tmp))
2700             update_code_filenames((PyCodeObject *)tmp,
2701                                   oldname, newname);
2702     }
2703 }
2704 
2705 static void
update_compiled_module(PyCodeObject * co,PyObject * newname)2706 update_compiled_module(PyCodeObject *co, PyObject *newname)
2707 {
2708     PyObject *oldname;
2709 
2710     if (PyUnicode_Compare(co->co_filename, newname) == 0)
2711         return;
2712 
2713     oldname = co->co_filename;
2714     Py_INCREF(oldname);
2715     update_code_filenames(co, oldname, newname);
2716     Py_DECREF(oldname);
2717 }
2718 
2719 
2720 /******************/
2721 /* frozen modules */
2722 /******************/
2723 
2724 /* Return true if the name is an alias.  In that case, "alias" is set
2725    to the original module name.  If it is an alias but the original
2726    module isn't known then "alias" is set to NULL while true is returned. */
2727 static bool
resolve_module_alias(const char * name,const struct _module_alias * aliases,const char ** alias)2728 resolve_module_alias(const char *name, const struct _module_alias *aliases,
2729                      const char **alias)
2730 {
2731     const struct _module_alias *entry;
2732     for (entry = aliases; ; entry++) {
2733         if (entry->name == NULL) {
2734             /* It isn't an alias. */
2735             return false;
2736         }
2737         if (strcmp(name, entry->name) == 0) {
2738             if (alias != NULL) {
2739                 *alias = entry->orig;
2740             }
2741             return true;
2742         }
2743     }
2744 }
2745 
2746 static bool
use_frozen(void)2747 use_frozen(void)
2748 {
2749     PyInterpreterState *interp = _PyInterpreterState_GET();
2750     int override = OVERRIDE_FROZEN_MODULES(interp);
2751     if (override > 0) {
2752         return true;
2753     }
2754     else if (override < 0) {
2755         return false;
2756     }
2757     else {
2758         return interp->config.use_frozen_modules;
2759     }
2760 }
2761 
2762 static PyObject *
list_frozen_module_names(void)2763 list_frozen_module_names(void)
2764 {
2765     PyObject *names = PyList_New(0);
2766     if (names == NULL) {
2767         return NULL;
2768     }
2769     bool enabled = use_frozen();
2770     const struct _frozen *p;
2771 #define ADD_MODULE(name) \
2772     do { \
2773         PyObject *nameobj = PyUnicode_FromString(name); \
2774         if (nameobj == NULL) { \
2775             goto error; \
2776         } \
2777         int res = PyList_Append(names, nameobj); \
2778         Py_DECREF(nameobj); \
2779         if (res != 0) { \
2780             goto error; \
2781         } \
2782     } while(0)
2783     // We always use the bootstrap modules.
2784     for (p = _PyImport_FrozenBootstrap; ; p++) {
2785         if (p->name == NULL) {
2786             break;
2787         }
2788         ADD_MODULE(p->name);
2789     }
2790     // Frozen stdlib modules may be disabled.
2791     for (p = _PyImport_FrozenStdlib; ; p++) {
2792         if (p->name == NULL) {
2793             break;
2794         }
2795         if (enabled) {
2796             ADD_MODULE(p->name);
2797         }
2798     }
2799     for (p = _PyImport_FrozenTest; ; p++) {
2800         if (p->name == NULL) {
2801             break;
2802         }
2803         if (enabled) {
2804             ADD_MODULE(p->name);
2805         }
2806     }
2807 #undef ADD_MODULE
2808     // Add any custom modules.
2809     if (PyImport_FrozenModules != NULL) {
2810         for (p = PyImport_FrozenModules; ; p++) {
2811             if (p->name == NULL) {
2812                 break;
2813             }
2814             PyObject *nameobj = PyUnicode_FromString(p->name);
2815             if (nameobj == NULL) {
2816                 goto error;
2817             }
2818             int found = PySequence_Contains(names, nameobj);
2819             if (found < 0) {
2820                 Py_DECREF(nameobj);
2821                 goto error;
2822             }
2823             else if (found) {
2824                 Py_DECREF(nameobj);
2825             }
2826             else {
2827                 int res = PyList_Append(names, nameobj);
2828                 Py_DECREF(nameobj);
2829                 if (res != 0) {
2830                     goto error;
2831                 }
2832             }
2833         }
2834     }
2835     return names;
2836 
2837 error:
2838     Py_DECREF(names);
2839     return NULL;
2840 }
2841 
2842 typedef enum {
2843     FROZEN_OKAY,
2844     FROZEN_BAD_NAME,    // The given module name wasn't valid.
2845     FROZEN_NOT_FOUND,   // It wasn't in PyImport_FrozenModules.
2846     FROZEN_DISABLED,    // -X frozen_modules=off (and not essential)
2847     FROZEN_EXCLUDED,    /* The PyImport_FrozenModules entry has NULL "code"
2848                            (module is present but marked as unimportable, stops search). */
2849     FROZEN_INVALID,     /* The PyImport_FrozenModules entry is bogus
2850                            (eg. does not contain executable code). */
2851 } frozen_status;
2852 
2853 static inline void
set_frozen_error(frozen_status status,PyObject * modname)2854 set_frozen_error(frozen_status status, PyObject *modname)
2855 {
2856     const char *err = NULL;
2857     switch (status) {
2858         case FROZEN_BAD_NAME:
2859         case FROZEN_NOT_FOUND:
2860             err = "No such frozen object named %R";
2861             break;
2862         case FROZEN_DISABLED:
2863             err = "Frozen modules are disabled and the frozen object named %R is not essential";
2864             break;
2865         case FROZEN_EXCLUDED:
2866             err = "Excluded frozen object named %R";
2867             break;
2868         case FROZEN_INVALID:
2869             err = "Frozen object named %R is invalid";
2870             break;
2871         case FROZEN_OKAY:
2872             // There was no error.
2873             break;
2874         default:
2875             Py_UNREACHABLE();
2876     }
2877     if (err != NULL) {
2878         PyObject *msg = PyUnicode_FromFormat(err, modname);
2879         if (msg == NULL) {
2880             PyErr_Clear();
2881         }
2882         PyErr_SetImportError(msg, modname, NULL);
2883         Py_XDECREF(msg);
2884     }
2885 }
2886 
2887 static const struct _frozen *
look_up_frozen(const char * name)2888 look_up_frozen(const char *name)
2889 {
2890     const struct _frozen *p;
2891     // We always use the bootstrap modules.
2892     for (p = _PyImport_FrozenBootstrap; ; p++) {
2893         if (p->name == NULL) {
2894             // We hit the end-of-list sentinel value.
2895             break;
2896         }
2897         if (strcmp(name, p->name) == 0) {
2898             return p;
2899         }
2900     }
2901     // Prefer custom modules, if any.  Frozen stdlib modules can be
2902     // disabled here by setting "code" to NULL in the array entry.
2903     if (PyImport_FrozenModules != NULL) {
2904         for (p = PyImport_FrozenModules; ; p++) {
2905             if (p->name == NULL) {
2906                 break;
2907             }
2908             if (strcmp(name, p->name) == 0) {
2909                 return p;
2910             }
2911         }
2912     }
2913     // Frozen stdlib modules may be disabled.
2914     if (use_frozen()) {
2915         for (p = _PyImport_FrozenStdlib; ; p++) {
2916             if (p->name == NULL) {
2917                 break;
2918             }
2919             if (strcmp(name, p->name) == 0) {
2920                 return p;
2921             }
2922         }
2923         for (p = _PyImport_FrozenTest; ; p++) {
2924             if (p->name == NULL) {
2925                 break;
2926             }
2927             if (strcmp(name, p->name) == 0) {
2928                 return p;
2929             }
2930         }
2931     }
2932     return NULL;
2933 }
2934 
2935 struct frozen_info {
2936     PyObject *nameobj;
2937     const char *data;
2938     Py_ssize_t size;
2939     bool is_package;
2940     bool is_alias;
2941     const char *origname;
2942 };
2943 
2944 static frozen_status
find_frozen(PyObject * nameobj,struct frozen_info * info)2945 find_frozen(PyObject *nameobj, struct frozen_info *info)
2946 {
2947     if (info != NULL) {
2948         memset(info, 0, sizeof(*info));
2949     }
2950 
2951     if (nameobj == NULL || nameobj == Py_None) {
2952         return FROZEN_BAD_NAME;
2953     }
2954     const char *name = PyUnicode_AsUTF8(nameobj);
2955     if (name == NULL) {
2956         // Note that this function previously used
2957         // _PyUnicode_EqualToASCIIString().  We clear the error here
2958         // (instead of propagating it) to match the earlier behavior
2959         // more closely.
2960         PyErr_Clear();
2961         return FROZEN_BAD_NAME;
2962     }
2963 
2964     const struct _frozen *p = look_up_frozen(name);
2965     if (p == NULL) {
2966         return FROZEN_NOT_FOUND;
2967     }
2968     if (info != NULL) {
2969         info->nameobj = nameobj;  // borrowed
2970         info->data = (const char *)p->code;
2971         info->size = p->size;
2972         info->is_package = p->is_package;
2973         if (p->size < 0) {
2974             // backward compatibility with negative size values
2975             info->size = -(p->size);
2976             info->is_package = true;
2977         }
2978         info->origname = name;
2979         info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases,
2980                                               &info->origname);
2981     }
2982     if (p->code == NULL) {
2983         /* It is frozen but marked as un-importable. */
2984         return FROZEN_EXCLUDED;
2985     }
2986     if (p->code[0] == '\0' || p->size == 0) {
2987         /* Does not contain executable code. */
2988         return FROZEN_INVALID;
2989     }
2990     return FROZEN_OKAY;
2991 }
2992 
2993 static PyObject *
unmarshal_frozen_code(PyInterpreterState * interp,struct frozen_info * info)2994 unmarshal_frozen_code(PyInterpreterState *interp, struct frozen_info *info)
2995 {
2996     PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size);
2997     if (co == NULL) {
2998         /* Does not contain executable code. */
2999         PyErr_Clear();
3000         set_frozen_error(FROZEN_INVALID, info->nameobj);
3001         return NULL;
3002     }
3003     if (!PyCode_Check(co)) {
3004         // We stick with TypeError for backward compatibility.
3005         PyErr_Format(PyExc_TypeError,
3006                      "frozen object %R is not a code object",
3007                      info->nameobj);
3008         Py_DECREF(co);
3009         return NULL;
3010     }
3011     return co;
3012 }
3013 
3014 
3015 /* Initialize a frozen module.
3016    Return 1 for success, 0 if the module is not found, and -1 with
3017    an exception set if the initialization failed.
3018    This function is also used from frozenmain.c */
3019 
3020 int
PyImport_ImportFrozenModuleObject(PyObject * name)3021 PyImport_ImportFrozenModuleObject(PyObject *name)
3022 {
3023     PyThreadState *tstate = _PyThreadState_GET();
3024     PyObject *co, *m, *d = NULL;
3025     int err;
3026 
3027     struct frozen_info info;
3028     frozen_status status = find_frozen(name, &info);
3029     if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) {
3030         return 0;
3031     }
3032     else if (status == FROZEN_BAD_NAME) {
3033         return 0;
3034     }
3035     else if (status != FROZEN_OKAY) {
3036         set_frozen_error(status, name);
3037         return -1;
3038     }
3039     co = unmarshal_frozen_code(tstate->interp, &info);
3040     if (co == NULL) {
3041         return -1;
3042     }
3043     if (info.is_package) {
3044         /* Set __path__ to the empty list */
3045         PyObject *l;
3046         m = import_add_module(tstate, name);
3047         if (m == NULL)
3048             goto err_return;
3049         d = PyModule_GetDict(m);
3050         l = PyList_New(0);
3051         if (l == NULL) {
3052             Py_DECREF(m);
3053             goto err_return;
3054         }
3055         err = PyDict_SetItemString(d, "__path__", l);
3056         Py_DECREF(l);
3057         Py_DECREF(m);
3058         if (err != 0)
3059             goto err_return;
3060     }
3061     d = module_dict_for_exec(tstate, name);
3062     if (d == NULL) {
3063         goto err_return;
3064     }
3065     m = exec_code_in_module(tstate, name, d, co);
3066     if (m == NULL) {
3067         goto err_return;
3068     }
3069     Py_DECREF(m);
3070     /* Set __origname__ (consumed in FrozenImporter._setup_module()). */
3071     PyObject *origname;
3072     if (info.origname) {
3073         origname = PyUnicode_FromString(info.origname);
3074         if (origname == NULL) {
3075             goto err_return;
3076         }
3077     }
3078     else {
3079         origname = Py_NewRef(Py_None);
3080     }
3081     err = PyDict_SetItemString(d, "__origname__", origname);
3082     Py_DECREF(origname);
3083     if (err != 0) {
3084         goto err_return;
3085     }
3086     Py_DECREF(d);
3087     Py_DECREF(co);
3088     return 1;
3089 
3090 err_return:
3091     Py_XDECREF(d);
3092     Py_DECREF(co);
3093     return -1;
3094 }
3095 
3096 int
PyImport_ImportFrozenModule(const char * name)3097 PyImport_ImportFrozenModule(const char *name)
3098 {
3099     PyObject *nameobj;
3100     int ret;
3101     nameobj = PyUnicode_InternFromString(name);
3102     if (nameobj == NULL)
3103         return -1;
3104     ret = PyImport_ImportFrozenModuleObject(nameobj);
3105     Py_DECREF(nameobj);
3106     return ret;
3107 }
3108 
3109 
3110 /*************/
3111 /* importlib */
3112 /*************/
3113 
3114 /* Import the _imp extension by calling manually _imp.create_builtin() and
3115    _imp.exec_builtin() since importlib is not initialized yet. Initializing
3116    importlib requires the _imp module: this function fix the bootstrap issue.
3117  */
3118 static PyObject*
bootstrap_imp(PyThreadState * tstate)3119 bootstrap_imp(PyThreadState *tstate)
3120 {
3121     PyObject *name = PyUnicode_FromString("_imp");
3122     if (name == NULL) {
3123         return NULL;
3124     }
3125 
3126     // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec():
3127     // an object with just a name attribute.
3128     //
3129     // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway.
3130     PyObject *attrs = Py_BuildValue("{sO}", "name", name);
3131     if (attrs == NULL) {
3132         goto error;
3133     }
3134     PyObject *spec = _PyNamespace_New(attrs);
3135     Py_DECREF(attrs);
3136     if (spec == NULL) {
3137         goto error;
3138     }
3139 
3140     // Create the _imp module from its definition.
3141     PyObject *mod = create_builtin(tstate, name, spec);
3142     Py_CLEAR(name);
3143     Py_DECREF(spec);
3144     if (mod == NULL) {
3145         goto error;
3146     }
3147     assert(mod != Py_None);  // not found
3148 
3149     // Execute the _imp module: call imp_module_exec().
3150     if (exec_builtin_or_dynamic(mod) < 0) {
3151         Py_DECREF(mod);
3152         goto error;
3153     }
3154     return mod;
3155 
3156 error:
3157     Py_XDECREF(name);
3158     return NULL;
3159 }
3160 
3161 /* Global initializations.  Can be undone by Py_FinalizeEx().  Don't
3162    call this twice without an intervening Py_FinalizeEx() call.  When
3163    initializations fail, a fatal error is issued and the function does
3164    not return.  On return, the first thread and interpreter state have
3165    been created.
3166 
3167    Locking: you must hold the interpreter lock while calling this.
3168    (If the lock has not yet been initialized, that's equivalent to
3169    having the lock, but you cannot use multiple threads.)
3170 
3171 */
3172 static int
init_importlib(PyThreadState * tstate,PyObject * sysmod)3173 init_importlib(PyThreadState *tstate, PyObject *sysmod)
3174 {
3175     assert(!_PyErr_Occurred(tstate));
3176 
3177     PyInterpreterState *interp = tstate->interp;
3178     int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
3179 
3180     // Import _importlib through its frozen version, _frozen_importlib.
3181     if (verbose) {
3182         PySys_FormatStderr("import _frozen_importlib # frozen\n");
3183     }
3184     if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
3185         return -1;
3186     }
3187 
3188     PyObject *importlib = PyImport_AddModuleRef("_frozen_importlib");
3189     if (importlib == NULL) {
3190         return -1;
3191     }
3192     IMPORTLIB(interp) = importlib;
3193 
3194     // Import the _imp module
3195     if (verbose) {
3196         PySys_FormatStderr("import _imp # builtin\n");
3197     }
3198     PyObject *imp_mod = bootstrap_imp(tstate);
3199     if (imp_mod == NULL) {
3200         return -1;
3201     }
3202     if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
3203         Py_DECREF(imp_mod);
3204         return -1;
3205     }
3206 
3207     // Install importlib as the implementation of import
3208     PyObject *value = PyObject_CallMethod(importlib, "_install",
3209                                           "OO", sysmod, imp_mod);
3210     Py_DECREF(imp_mod);
3211     if (value == NULL) {
3212         return -1;
3213     }
3214     Py_DECREF(value);
3215 
3216     assert(!_PyErr_Occurred(tstate));
3217     return 0;
3218 }
3219 
3220 
3221 static int
init_importlib_external(PyInterpreterState * interp)3222 init_importlib_external(PyInterpreterState *interp)
3223 {
3224     PyObject *value;
3225     value = PyObject_CallMethod(IMPORTLIB(interp),
3226                                 "_install_external_importers", "");
3227     if (value == NULL) {
3228         return -1;
3229     }
3230     Py_DECREF(value);
3231     return 0;
3232 }
3233 
3234 PyObject *
_PyImport_GetImportlibLoader(PyInterpreterState * interp,const char * loader_name)3235 _PyImport_GetImportlibLoader(PyInterpreterState *interp,
3236                              const char *loader_name)
3237 {
3238     return PyObject_GetAttrString(IMPORTLIB(interp), loader_name);
3239 }
3240 
3241 PyObject *
_PyImport_GetImportlibExternalLoader(PyInterpreterState * interp,const char * loader_name)3242 _PyImport_GetImportlibExternalLoader(PyInterpreterState *interp,
3243                                      const char *loader_name)
3244 {
3245     PyObject *bootstrap = PyObject_GetAttrString(IMPORTLIB(interp),
3246                                                  "_bootstrap_external");
3247     if (bootstrap == NULL) {
3248         return NULL;
3249     }
3250 
3251     PyObject *loader_type = PyObject_GetAttrString(bootstrap, loader_name);
3252     Py_DECREF(bootstrap);
3253     return loader_type;
3254 }
3255 
3256 PyObject *
_PyImport_BlessMyLoader(PyInterpreterState * interp,PyObject * module_globals)3257 _PyImport_BlessMyLoader(PyInterpreterState *interp, PyObject *module_globals)
3258 {
3259     PyObject *external = PyObject_GetAttrString(IMPORTLIB(interp),
3260                                                 "_bootstrap_external");
3261     if (external == NULL) {
3262         return NULL;
3263     }
3264 
3265     PyObject *loader = PyObject_CallMethod(external, "_bless_my_loader",
3266                                            "O", module_globals, NULL);
3267     Py_DECREF(external);
3268     return loader;
3269 }
3270 
3271 PyObject *
_PyImport_ImportlibModuleRepr(PyInterpreterState * interp,PyObject * m)3272 _PyImport_ImportlibModuleRepr(PyInterpreterState *interp, PyObject *m)
3273 {
3274     return PyObject_CallMethod(IMPORTLIB(interp), "_module_repr", "O", m);
3275 }
3276 
3277 
3278 /*******************/
3279 
3280 /* Return a finder object for a sys.path/pkg.__path__ item 'p',
3281    possibly by fetching it from the path_importer_cache dict. If it
3282    wasn't yet cached, traverse path_hooks until a hook is found
3283    that can handle the path item. Return None if no hook could;
3284    this tells our caller that the path based finder could not find
3285    a finder for this path item. Cache the result in
3286    path_importer_cache. */
3287 
3288 static PyObject *
get_path_importer(PyThreadState * tstate,PyObject * path_importer_cache,PyObject * path_hooks,PyObject * p)3289 get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
3290                   PyObject *path_hooks, PyObject *p)
3291 {
3292     PyObject *importer;
3293     Py_ssize_t j, nhooks;
3294 
3295     if (!PyList_Check(path_hooks)) {
3296         PyErr_SetString(PyExc_RuntimeError, "sys.path_hooks is not a list");
3297         return NULL;
3298     }
3299     if (!PyDict_Check(path_importer_cache)) {
3300         PyErr_SetString(PyExc_RuntimeError, "sys.path_importer_cache is not a dict");
3301         return NULL;
3302     }
3303 
3304     nhooks = PyList_Size(path_hooks);
3305     if (nhooks < 0)
3306         return NULL; /* Shouldn't happen */
3307 
3308     if (PyDict_GetItemRef(path_importer_cache, p, &importer) != 0) {
3309         // found or error
3310         return importer;
3311     }
3312     // not found
3313     /* set path_importer_cache[p] to None to avoid recursion */
3314     if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
3315         return NULL;
3316 
3317     for (j = 0; j < nhooks; j++) {
3318         PyObject *hook = PyList_GetItem(path_hooks, j);
3319         if (hook == NULL)
3320             return NULL;
3321         importer = PyObject_CallOneArg(hook, p);
3322         if (importer != NULL)
3323             break;
3324 
3325         if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) {
3326             return NULL;
3327         }
3328         _PyErr_Clear(tstate);
3329     }
3330     if (importer == NULL) {
3331         Py_RETURN_NONE;
3332     }
3333     if (PyDict_SetItem(path_importer_cache, p, importer) < 0) {
3334         Py_DECREF(importer);
3335         return NULL;
3336     }
3337     return importer;
3338 }
3339 
3340 PyObject *
PyImport_GetImporter(PyObject * path)3341 PyImport_GetImporter(PyObject *path)
3342 {
3343     PyThreadState *tstate = _PyThreadState_GET();
3344     PyObject *path_importer_cache = PySys_GetObject("path_importer_cache");
3345     if (path_importer_cache == NULL) {
3346         PyErr_SetString(PyExc_RuntimeError, "lost sys.path_importer_cache");
3347         return NULL;
3348     }
3349     Py_INCREF(path_importer_cache);
3350     PyObject *path_hooks = PySys_GetObject("path_hooks");
3351     if (path_hooks == NULL) {
3352         PyErr_SetString(PyExc_RuntimeError, "lost sys.path_hooks");
3353         Py_DECREF(path_importer_cache);
3354         return NULL;
3355     }
3356     Py_INCREF(path_hooks);
3357     PyObject *importer = get_path_importer(tstate, path_importer_cache, path_hooks, path);
3358     Py_DECREF(path_hooks);
3359     Py_DECREF(path_importer_cache);
3360     return importer;
3361 }
3362 
3363 
3364 /*********************/
3365 /* importing modules */
3366 /*********************/
3367 
3368 int
_PyImport_InitDefaultImportFunc(PyInterpreterState * interp)3369 _PyImport_InitDefaultImportFunc(PyInterpreterState *interp)
3370 {
3371     // Get the __import__ function
3372     PyObject *import_func;
3373     if (PyDict_GetItemStringRef(interp->builtins, "__import__", &import_func) <= 0) {
3374         return -1;
3375     }
3376     IMPORT_FUNC(interp) = import_func;
3377     return 0;
3378 }
3379 
3380 int
_PyImport_IsDefaultImportFunc(PyInterpreterState * interp,PyObject * func)3381 _PyImport_IsDefaultImportFunc(PyInterpreterState *interp, PyObject *func)
3382 {
3383     return func == IMPORT_FUNC(interp);
3384 }
3385 
3386 
3387 /* Import a module, either built-in, frozen, or external, and return
3388    its module object WITH INCREMENTED REFERENCE COUNT */
3389 
3390 PyObject *
PyImport_ImportModule(const char * name)3391 PyImport_ImportModule(const char *name)
3392 {
3393     PyObject *pname;
3394     PyObject *result;
3395 
3396     pname = PyUnicode_FromString(name);
3397     if (pname == NULL)
3398         return NULL;
3399     result = PyImport_Import(pname);
3400     Py_DECREF(pname);
3401     return result;
3402 }
3403 
3404 
3405 /* Import a module without blocking
3406  *
3407  * At first it tries to fetch the module from sys.modules. If the module was
3408  * never loaded before it loads it with PyImport_ImportModule() unless another
3409  * thread holds the import lock. In the latter case the function raises an
3410  * ImportError instead of blocking.
3411  *
3412  * Returns the module object with incremented ref count.
3413  */
3414 PyObject *
PyImport_ImportModuleNoBlock(const char * name)3415 PyImport_ImportModuleNoBlock(const char *name)
3416 {
3417     if (PyErr_WarnEx(PyExc_DeprecationWarning,
3418         "PyImport_ImportModuleNoBlock() is deprecated and scheduled for "
3419         "removal in Python 3.15. Use PyImport_ImportModule() instead.", 1))
3420     {
3421         return NULL;
3422     }
3423     return PyImport_ImportModule(name);
3424 }
3425 
3426 
3427 /* Remove importlib frames from the traceback,
3428  * except in Verbose mode. */
3429 static void
remove_importlib_frames(PyThreadState * tstate)3430 remove_importlib_frames(PyThreadState *tstate)
3431 {
3432     const char *importlib_filename = "<frozen importlib._bootstrap>";
3433     const char *external_filename = "<frozen importlib._bootstrap_external>";
3434     const char *remove_frames = "_call_with_frames_removed";
3435     int always_trim = 0;
3436     int in_importlib = 0;
3437     PyObject **prev_link, **outer_link = NULL;
3438     PyObject *base_tb = NULL;
3439 
3440     /* Synopsis: if it's an ImportError, we trim all importlib chunks
3441        from the traceback. We always trim chunks
3442        which end with a call to "_call_with_frames_removed". */
3443 
3444     PyObject *exc = _PyErr_GetRaisedException(tstate);
3445     if (exc == NULL || _PyInterpreterState_GetConfig(tstate->interp)->verbose) {
3446         goto done;
3447     }
3448 
3449     if (PyType_IsSubtype(Py_TYPE(exc), (PyTypeObject *) PyExc_ImportError)) {
3450         always_trim = 1;
3451     }
3452 
3453     assert(PyExceptionInstance_Check(exc));
3454     base_tb = PyException_GetTraceback(exc);
3455     prev_link = &base_tb;
3456     PyObject *tb = base_tb;
3457     while (tb != NULL) {
3458         assert(PyTraceBack_Check(tb));
3459         PyTracebackObject *traceback = (PyTracebackObject *)tb;
3460         PyObject *next = (PyObject *) traceback->tb_next;
3461         PyFrameObject *frame = traceback->tb_frame;
3462         PyCodeObject *code = PyFrame_GetCode(frame);
3463         int now_in_importlib;
3464 
3465         now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) ||
3466                            _PyUnicode_EqualToASCIIString(code->co_filename, external_filename);
3467         if (now_in_importlib && !in_importlib) {
3468             /* This is the link to this chunk of importlib tracebacks */
3469             outer_link = prev_link;
3470         }
3471         in_importlib = now_in_importlib;
3472 
3473         if (in_importlib &&
3474             (always_trim ||
3475              _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) {
3476             Py_XSETREF(*outer_link, Py_XNewRef(next));
3477             prev_link = outer_link;
3478         }
3479         else {
3480             prev_link = (PyObject **) &traceback->tb_next;
3481         }
3482         Py_DECREF(code);
3483         tb = next;
3484     }
3485     if (base_tb == NULL) {
3486         base_tb = Py_None;
3487         Py_INCREF(Py_None);
3488     }
3489     PyException_SetTraceback(exc, base_tb);
3490 done:
3491     Py_XDECREF(base_tb);
3492     _PyErr_SetRaisedException(tstate, exc);
3493 }
3494 
3495 
3496 static PyObject *
resolve_name(PyThreadState * tstate,PyObject * name,PyObject * globals,int level)3497 resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level)
3498 {
3499     PyObject *abs_name;
3500     PyObject *package = NULL;
3501     PyObject *spec = NULL;
3502     Py_ssize_t last_dot;
3503     PyObject *base;
3504     int level_up;
3505 
3506     if (globals == NULL) {
3507         _PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals");
3508         goto error;
3509     }
3510     if (!PyDict_Check(globals)) {
3511         _PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict");
3512         goto error;
3513     }
3514     if (PyDict_GetItemRef(globals, &_Py_ID(__package__), &package) < 0) {
3515         goto error;
3516     }
3517     if (package == Py_None) {
3518         Py_DECREF(package);
3519         package = NULL;
3520     }
3521     if (PyDict_GetItemRef(globals, &_Py_ID(__spec__), &spec) < 0) {
3522         goto error;
3523     }
3524 
3525     if (package != NULL) {
3526         if (!PyUnicode_Check(package)) {
3527             _PyErr_SetString(tstate, PyExc_TypeError,
3528                              "package must be a string");
3529             goto error;
3530         }
3531         else if (spec != NULL && spec != Py_None) {
3532             int equal;
3533             PyObject *parent = PyObject_GetAttr(spec, &_Py_ID(parent));
3534             if (parent == NULL) {
3535                 goto error;
3536             }
3537 
3538             equal = PyObject_RichCompareBool(package, parent, Py_EQ);
3539             Py_DECREF(parent);
3540             if (equal < 0) {
3541                 goto error;
3542             }
3543             else if (equal == 0) {
3544                 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3545                         "__package__ != __spec__.parent", 1) < 0) {
3546                     goto error;
3547                 }
3548             }
3549         }
3550     }
3551     else if (spec != NULL && spec != Py_None) {
3552         package = PyObject_GetAttr(spec, &_Py_ID(parent));
3553         if (package == NULL) {
3554             goto error;
3555         }
3556         else if (!PyUnicode_Check(package)) {
3557             _PyErr_SetString(tstate, PyExc_TypeError,
3558                              "__spec__.parent must be a string");
3559             goto error;
3560         }
3561     }
3562     else {
3563         if (PyErr_WarnEx(PyExc_ImportWarning,
3564                     "can't resolve package from __spec__ or __package__, "
3565                     "falling back on __name__ and __path__", 1) < 0) {
3566             goto error;
3567         }
3568 
3569         if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &package) < 0) {
3570             goto error;
3571         }
3572         if (package == NULL) {
3573             _PyErr_SetString(tstate, PyExc_KeyError,
3574                              "'__name__' not in globals");
3575             goto error;
3576         }
3577 
3578         if (!PyUnicode_Check(package)) {
3579             _PyErr_SetString(tstate, PyExc_TypeError,
3580                              "__name__ must be a string");
3581             goto error;
3582         }
3583 
3584         int haspath = PyDict_Contains(globals, &_Py_ID(__path__));
3585         if (haspath < 0) {
3586             goto error;
3587         }
3588         if (!haspath) {
3589             Py_ssize_t dot;
3590 
3591             dot = PyUnicode_FindChar(package, '.',
3592                                         0, PyUnicode_GET_LENGTH(package), -1);
3593             if (dot == -2) {
3594                 goto error;
3595             }
3596             else if (dot == -1) {
3597                 goto no_parent_error;
3598             }
3599             PyObject *substr = PyUnicode_Substring(package, 0, dot);
3600             if (substr == NULL) {
3601                 goto error;
3602             }
3603             Py_SETREF(package, substr);
3604         }
3605     }
3606 
3607     last_dot = PyUnicode_GET_LENGTH(package);
3608     if (last_dot == 0) {
3609         goto no_parent_error;
3610     }
3611 
3612     for (level_up = 1; level_up < level; level_up += 1) {
3613         last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
3614         if (last_dot == -2) {
3615             goto error;
3616         }
3617         else if (last_dot == -1) {
3618             _PyErr_SetString(tstate, PyExc_ImportError,
3619                              "attempted relative import beyond top-level "
3620                              "package");
3621             goto error;
3622         }
3623     }
3624 
3625     Py_XDECREF(spec);
3626     base = PyUnicode_Substring(package, 0, last_dot);
3627     Py_DECREF(package);
3628     if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) {
3629         return base;
3630     }
3631 
3632     abs_name = PyUnicode_FromFormat("%U.%U", base, name);
3633     Py_DECREF(base);
3634     return abs_name;
3635 
3636   no_parent_error:
3637     _PyErr_SetString(tstate, PyExc_ImportError,
3638                      "attempted relative import "
3639                      "with no known parent package");
3640 
3641   error:
3642     Py_XDECREF(spec);
3643     Py_XDECREF(package);
3644     return NULL;
3645 }
3646 
3647 static PyObject *
import_find_and_load(PyThreadState * tstate,PyObject * abs_name)3648 import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
3649 {
3650     PyObject *mod = NULL;
3651     PyInterpreterState *interp = tstate->interp;
3652     int import_time = _PyInterpreterState_GetConfig(interp)->import_time;
3653 #define import_level FIND_AND_LOAD(interp).import_level
3654 #define accumulated FIND_AND_LOAD(interp).accumulated
3655 
3656     PyTime_t t1 = 0, accumulated_copy = accumulated;
3657 
3658     PyObject *sys_path = PySys_GetObject("path");
3659     PyObject *sys_meta_path = PySys_GetObject("meta_path");
3660     PyObject *sys_path_hooks = PySys_GetObject("path_hooks");
3661     if (_PySys_Audit(tstate, "import", "OOOOO",
3662                      abs_name, Py_None, sys_path ? sys_path : Py_None,
3663                      sys_meta_path ? sys_meta_path : Py_None,
3664                      sys_path_hooks ? sys_path_hooks : Py_None) < 0) {
3665         return NULL;
3666     }
3667 
3668 
3669     /* XOptions is initialized after first some imports.
3670      * So we can't have negative cache before completed initialization.
3671      * Anyway, importlib._find_and_load is much slower than
3672      * _PyDict_GetItemIdWithError().
3673      */
3674     if (import_time) {
3675 #define header FIND_AND_LOAD(interp).header
3676         if (header) {
3677             fputs("import time: self [us] | cumulative | imported package\n",
3678                   stderr);
3679             header = 0;
3680         }
3681 #undef header
3682 
3683         import_level++;
3684         // ignore error: don't block import if reading the clock fails
3685         (void)PyTime_PerfCounterRaw(&t1);
3686         accumulated = 0;
3687     }
3688 
3689     if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED())
3690         PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name));
3691 
3692     mod = PyObject_CallMethodObjArgs(IMPORTLIB(interp), &_Py_ID(_find_and_load),
3693                                      abs_name, IMPORT_FUNC(interp), NULL);
3694 
3695     if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED())
3696         PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
3697                                        mod != NULL);
3698 
3699     if (import_time) {
3700         PyTime_t t2;
3701         (void)PyTime_PerfCounterRaw(&t2);
3702         PyTime_t cum = t2 - t1;
3703 
3704         import_level--;
3705         fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
3706                 (long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING),
3707                 (long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING),
3708                 import_level*2, "", PyUnicode_AsUTF8(abs_name));
3709 
3710         accumulated = accumulated_copy + cum;
3711     }
3712 
3713     return mod;
3714 #undef import_level
3715 #undef accumulated
3716 }
3717 
3718 PyObject *
PyImport_ImportModuleLevelObject(PyObject * name,PyObject * globals,PyObject * locals,PyObject * fromlist,int level)3719 PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
3720                                  PyObject *locals, PyObject *fromlist,
3721                                  int level)
3722 {
3723     PyThreadState *tstate = _PyThreadState_GET();
3724     PyObject *abs_name = NULL;
3725     PyObject *final_mod = NULL;
3726     PyObject *mod = NULL;
3727     PyObject *package = NULL;
3728     PyInterpreterState *interp = tstate->interp;
3729     int has_from;
3730 
3731     if (name == NULL) {
3732         _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name");
3733         goto error;
3734     }
3735 
3736     /* The below code is importlib.__import__() & _gcd_import(), ported to C
3737        for added performance. */
3738 
3739     if (!PyUnicode_Check(name)) {
3740         _PyErr_SetString(tstate, PyExc_TypeError,
3741                          "module name must be a string");
3742         goto error;
3743     }
3744     if (level < 0) {
3745         _PyErr_SetString(tstate, PyExc_ValueError, "level must be >= 0");
3746         goto error;
3747     }
3748 
3749     if (level > 0) {
3750         abs_name = resolve_name(tstate, name, globals, level);
3751         if (abs_name == NULL)
3752             goto error;
3753     }
3754     else {  /* level == 0 */
3755         if (PyUnicode_GET_LENGTH(name) == 0) {
3756             _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name");
3757             goto error;
3758         }
3759         abs_name = Py_NewRef(name);
3760     }
3761 
3762     mod = import_get_module(tstate, abs_name);
3763     if (mod == NULL && _PyErr_Occurred(tstate)) {
3764         goto error;
3765     }
3766 
3767     if (mod != NULL && mod != Py_None) {
3768         if (import_ensure_initialized(tstate->interp, mod, abs_name) < 0) {
3769             goto error;
3770         }
3771     }
3772     else {
3773         Py_XDECREF(mod);
3774         mod = import_find_and_load(tstate, abs_name);
3775         if (mod == NULL) {
3776             goto error;
3777         }
3778     }
3779 
3780     has_from = 0;
3781     if (fromlist != NULL && fromlist != Py_None) {
3782         has_from = PyObject_IsTrue(fromlist);
3783         if (has_from < 0)
3784             goto error;
3785     }
3786     if (!has_from) {
3787         Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3788         if (level == 0 || len > 0) {
3789             Py_ssize_t dot;
3790 
3791             dot = PyUnicode_FindChar(name, '.', 0, len, 1);
3792             if (dot == -2) {
3793                 goto error;
3794             }
3795 
3796             if (dot == -1) {
3797                 /* No dot in module name, simple exit */
3798                 final_mod = Py_NewRef(mod);
3799                 goto error;
3800             }
3801 
3802             if (level == 0) {
3803                 PyObject *front = PyUnicode_Substring(name, 0, dot);
3804                 if (front == NULL) {
3805                     goto error;
3806                 }
3807 
3808                 final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0);
3809                 Py_DECREF(front);
3810             }
3811             else {
3812                 Py_ssize_t cut_off = len - dot;
3813                 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
3814                 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
3815                                                         abs_name_len - cut_off);
3816                 if (to_return == NULL) {
3817                     goto error;
3818                 }
3819 
3820                 final_mod = import_get_module(tstate, to_return);
3821                 Py_DECREF(to_return);
3822                 if (final_mod == NULL) {
3823                     if (!_PyErr_Occurred(tstate)) {
3824                         _PyErr_Format(tstate, PyExc_KeyError,
3825                                       "%R not in sys.modules as expected",
3826                                       to_return);
3827                     }
3828                     goto error;
3829                 }
3830             }
3831         }
3832         else {
3833             final_mod = Py_NewRef(mod);
3834         }
3835     }
3836     else {
3837         int has_path = PyObject_HasAttrWithError(mod, &_Py_ID(__path__));
3838         if (has_path < 0) {
3839             goto error;
3840         }
3841         if (has_path) {
3842             final_mod = PyObject_CallMethodObjArgs(
3843                         IMPORTLIB(interp), &_Py_ID(_handle_fromlist),
3844                         mod, fromlist, IMPORT_FUNC(interp), NULL);
3845         }
3846         else {
3847             final_mod = Py_NewRef(mod);
3848         }
3849     }
3850 
3851   error:
3852     Py_XDECREF(abs_name);
3853     Py_XDECREF(mod);
3854     Py_XDECREF(package);
3855     if (final_mod == NULL) {
3856         remove_importlib_frames(tstate);
3857     }
3858     return final_mod;
3859 }
3860 
3861 PyObject *
PyImport_ImportModuleLevel(const char * name,PyObject * globals,PyObject * locals,PyObject * fromlist,int level)3862 PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
3863                            PyObject *fromlist, int level)
3864 {
3865     PyObject *nameobj, *mod;
3866     nameobj = PyUnicode_FromString(name);
3867     if (nameobj == NULL)
3868         return NULL;
3869     mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
3870                                            fromlist, level);
3871     Py_DECREF(nameobj);
3872     return mod;
3873 }
3874 
3875 
3876 /* Re-import a module of any kind and return its module object, WITH
3877    INCREMENTED REFERENCE COUNT */
3878 
3879 PyObject *
PyImport_ReloadModule(PyObject * m)3880 PyImport_ReloadModule(PyObject *m)
3881 {
3882     PyObject *reloaded_module = NULL;
3883     PyObject *importlib = PyImport_GetModule(&_Py_ID(importlib));
3884     if (importlib == NULL) {
3885         if (PyErr_Occurred()) {
3886             return NULL;
3887         }
3888 
3889         importlib = PyImport_ImportModule("importlib");
3890         if (importlib == NULL) {
3891             return NULL;
3892         }
3893     }
3894 
3895     reloaded_module = PyObject_CallMethodOneArg(importlib, &_Py_ID(reload), m);
3896     Py_DECREF(importlib);
3897     return reloaded_module;
3898 }
3899 
3900 
3901 /* Higher-level import emulator which emulates the "import" statement
3902    more accurately -- it invokes the __import__() function from the
3903    builtins of the current globals.  This means that the import is
3904    done using whatever import hooks are installed in the current
3905    environment.
3906    A dummy list ["__doc__"] is passed as the 4th argument so that
3907    e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
3908    will return <module "gencache"> instead of <module "win32com">. */
3909 
3910 PyObject *
PyImport_Import(PyObject * module_name)3911 PyImport_Import(PyObject *module_name)
3912 {
3913     PyThreadState *tstate = _PyThreadState_GET();
3914     PyObject *globals = NULL;
3915     PyObject *import = NULL;
3916     PyObject *builtins = NULL;
3917     PyObject *r = NULL;
3918 
3919     PyObject *from_list = PyList_New(0);
3920     if (from_list == NULL) {
3921         goto err;
3922     }
3923 
3924     /* Get the builtins from current globals */
3925     globals = PyEval_GetGlobals();
3926     if (globals != NULL) {
3927         Py_INCREF(globals);
3928         builtins = PyObject_GetItem(globals, &_Py_ID(__builtins__));
3929         if (builtins == NULL)
3930             goto err;
3931     }
3932     else {
3933         /* No globals -- use standard builtins, and fake globals */
3934         builtins = PyImport_ImportModuleLevel("builtins",
3935                                               NULL, NULL, NULL, 0);
3936         if (builtins == NULL) {
3937             goto err;
3938         }
3939         globals = Py_BuildValue("{OO}", &_Py_ID(__builtins__), builtins);
3940         if (globals == NULL)
3941             goto err;
3942     }
3943 
3944     /* Get the __import__ function from the builtins */
3945     if (PyDict_Check(builtins)) {
3946         import = PyObject_GetItem(builtins, &_Py_ID(__import__));
3947         if (import == NULL) {
3948             _PyErr_SetObject(tstate, PyExc_KeyError, &_Py_ID(__import__));
3949         }
3950     }
3951     else
3952         import = PyObject_GetAttr(builtins, &_Py_ID(__import__));
3953     if (import == NULL)
3954         goto err;
3955 
3956     /* Call the __import__ function with the proper argument list
3957        Always use absolute import here.
3958        Calling for side-effect of import. */
3959     r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
3960                               globals, from_list, 0, NULL);
3961     if (r == NULL)
3962         goto err;
3963     Py_DECREF(r);
3964 
3965     r = import_get_module(tstate, module_name);
3966     if (r == NULL && !_PyErr_Occurred(tstate)) {
3967         _PyErr_SetObject(tstate, PyExc_KeyError, module_name);
3968     }
3969 
3970   err:
3971     Py_XDECREF(globals);
3972     Py_XDECREF(builtins);
3973     Py_XDECREF(import);
3974     Py_XDECREF(from_list);
3975 
3976     return r;
3977 }
3978 
3979 
3980 /*********************/
3981 /* runtime lifecycle */
3982 /*********************/
3983 
3984 PyStatus
_PyImport_Init(void)3985 _PyImport_Init(void)
3986 {
3987     if (INITTAB != NULL) {
3988         return _PyStatus_ERR("global import state already initialized");
3989     }
3990 
3991     PyStatus status = _PyStatus_OK();
3992 
3993     /* Force default raw memory allocator to get a known allocator to be able
3994        to release the memory in _PyImport_Fini() */
3995     PyMemAllocatorEx old_alloc;
3996     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
3997 
3998     if (init_builtin_modules_table() != 0) {
3999         status = PyStatus_NoMemory();
4000         goto done;
4001     }
4002 
4003 done:
4004     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
4005     return status;
4006 }
4007 
4008 void
_PyImport_Fini(void)4009 _PyImport_Fini(void)
4010 {
4011     /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
4012     // XXX Should we actually leave them (mostly) intact, since we don't
4013     // ever dlclose() the module files?
4014     _extensions_cache_clear_all();
4015 
4016     /* Use the same memory allocator as _PyImport_Init(). */
4017     PyMemAllocatorEx old_alloc;
4018     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
4019 
4020     /* Free memory allocated by _PyImport_Init() */
4021     fini_builtin_modules_table();
4022 
4023     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
4024 }
4025 
4026 void
_PyImport_Fini2(void)4027 _PyImport_Fini2(void)
4028 {
4029     /* Use the same memory allocator than PyImport_ExtendInittab(). */
4030     PyMemAllocatorEx old_alloc;
4031     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
4032 
4033     // Reset PyImport_Inittab
4034     PyImport_Inittab = _PyImport_Inittab;
4035 
4036     /* Free memory allocated by PyImport_ExtendInittab() */
4037     PyMem_RawFree(inittab_copy);
4038     inittab_copy = NULL;
4039 
4040     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
4041 }
4042 
4043 
4044 /*************************/
4045 /* interpreter lifecycle */
4046 /*************************/
4047 
4048 PyStatus
_PyImport_InitCore(PyThreadState * tstate,PyObject * sysmod,int importlib)4049 _PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib)
4050 {
4051     // XXX Initialize here: interp->modules and interp->import_func.
4052     // XXX Initialize here: sys.modules and sys.meta_path.
4053 
4054     if (importlib) {
4055         /* This call sets up builtin and frozen import support */
4056         if (init_importlib(tstate, sysmod) < 0) {
4057             return _PyStatus_ERR("failed to initialize importlib");
4058         }
4059     }
4060 
4061     return _PyStatus_OK();
4062 }
4063 
4064 /* In some corner cases it is important to be sure that the import
4065    machinery has been initialized (or not cleaned up yet).  For
4066    example, see issue #4236 and PyModule_Create2(). */
4067 
4068 int
_PyImport_IsInitialized(PyInterpreterState * interp)4069 _PyImport_IsInitialized(PyInterpreterState *interp)
4070 {
4071     if (MODULES(interp) == NULL)
4072         return 0;
4073     return 1;
4074 }
4075 
4076 /* Clear the direct per-interpreter import state, if not cleared already. */
4077 void
_PyImport_ClearCore(PyInterpreterState * interp)4078 _PyImport_ClearCore(PyInterpreterState *interp)
4079 {
4080     /* interp->modules should have been cleaned up and cleared already
4081        by _PyImport_FiniCore(). */
4082     Py_CLEAR(MODULES(interp));
4083     Py_CLEAR(MODULES_BY_INDEX(interp));
4084     Py_CLEAR(IMPORTLIB(interp));
4085     Py_CLEAR(IMPORT_FUNC(interp));
4086 }
4087 
4088 void
_PyImport_FiniCore(PyInterpreterState * interp)4089 _PyImport_FiniCore(PyInterpreterState *interp)
4090 {
4091     int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
4092 
4093     if (_PySys_ClearAttrString(interp, "meta_path", verbose) < 0) {
4094         PyErr_FormatUnraisable("Exception ignored on clearing sys.meta_path");
4095     }
4096 
4097     // XXX Pull in most of finalize_modules() in pylifecycle.c.
4098 
4099     if (_PySys_ClearAttrString(interp, "modules", verbose) < 0) {
4100         PyErr_FormatUnraisable("Exception ignored on clearing sys.modules");
4101     }
4102 
4103     _PyImport_ClearCore(interp);
4104 }
4105 
4106 // XXX Add something like _PyImport_Disable() for use early in interp fini?
4107 
4108 
4109 /* "external" imports */
4110 
4111 static int
init_zipimport(PyThreadState * tstate,int verbose)4112 init_zipimport(PyThreadState *tstate, int verbose)
4113 {
4114     PyObject *path_hooks = PySys_GetObject("path_hooks");
4115     if (path_hooks == NULL) {
4116         _PyErr_SetString(tstate, PyExc_RuntimeError,
4117                          "unable to get sys.path_hooks");
4118         return -1;
4119     }
4120 
4121     if (verbose) {
4122         PySys_WriteStderr("# installing zipimport hook\n");
4123     }
4124 
4125     PyObject *zipimporter = _PyImport_GetModuleAttrString("zipimport", "zipimporter");
4126     if (zipimporter == NULL) {
4127         _PyErr_Clear(tstate); /* No zipimporter object -- okay */
4128         if (verbose) {
4129             PySys_WriteStderr("# can't import zipimport.zipimporter\n");
4130         }
4131     }
4132     else {
4133         /* sys.path_hooks.insert(0, zipimporter) */
4134         int err = PyList_Insert(path_hooks, 0, zipimporter);
4135         Py_DECREF(zipimporter);
4136         if (err < 0) {
4137             return -1;
4138         }
4139         if (verbose) {
4140             PySys_WriteStderr("# installed zipimport hook\n");
4141         }
4142     }
4143 
4144     return 0;
4145 }
4146 
4147 PyStatus
_PyImport_InitExternal(PyThreadState * tstate)4148 _PyImport_InitExternal(PyThreadState *tstate)
4149 {
4150     int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
4151 
4152     // XXX Initialize here: sys.path_hooks and sys.path_importer_cache.
4153 
4154     if (init_importlib_external(tstate->interp) != 0) {
4155         _PyErr_Print(tstate);
4156         return _PyStatus_ERR("external importer setup failed");
4157     }
4158 
4159     if (init_zipimport(tstate, verbose) != 0) {
4160         PyErr_Print();
4161         return _PyStatus_ERR("initializing zipimport failed");
4162     }
4163 
4164     return _PyStatus_OK();
4165 }
4166 
4167 void
_PyImport_FiniExternal(PyInterpreterState * interp)4168 _PyImport_FiniExternal(PyInterpreterState *interp)
4169 {
4170     int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
4171 
4172     // XXX Uninstall importlib metapath importers here?
4173 
4174     if (_PySys_ClearAttrString(interp, "path_importer_cache", verbose) < 0) {
4175         PyErr_FormatUnraisable("Exception ignored on clearing sys.path_importer_cache");
4176     }
4177     if (_PySys_ClearAttrString(interp, "path_hooks", verbose) < 0) {
4178         PyErr_FormatUnraisable("Exception ignored on clearing sys.path_hooks");
4179     }
4180 }
4181 
4182 
4183 /******************/
4184 /* module helpers */
4185 /******************/
4186 
4187 PyObject *
_PyImport_GetModuleAttr(PyObject * modname,PyObject * attrname)4188 _PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname)
4189 {
4190     PyObject *mod = PyImport_Import(modname);
4191     if (mod == NULL) {
4192         return NULL;
4193     }
4194     PyObject *result = PyObject_GetAttr(mod, attrname);
4195     Py_DECREF(mod);
4196     return result;
4197 }
4198 
4199 PyObject *
_PyImport_GetModuleAttrString(const char * modname,const char * attrname)4200 _PyImport_GetModuleAttrString(const char *modname, const char *attrname)
4201 {
4202     PyObject *pmodname = PyUnicode_FromString(modname);
4203     if (pmodname == NULL) {
4204         return NULL;
4205     }
4206     PyObject *pattrname = PyUnicode_FromString(attrname);
4207     if (pattrname == NULL) {
4208         Py_DECREF(pmodname);
4209         return NULL;
4210     }
4211     PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname);
4212     Py_DECREF(pattrname);
4213     Py_DECREF(pmodname);
4214     return result;
4215 }
4216 
4217 
4218 /**************/
4219 /* the module */
4220 /**************/
4221 
4222 /*[clinic input]
4223 _imp.lock_held
4224 
4225 Return True if the import lock is currently held, else False.
4226 
4227 On platforms without threads, return False.
4228 [clinic start generated code]*/
4229 
4230 static PyObject *
_imp_lock_held_impl(PyObject * module)4231 _imp_lock_held_impl(PyObject *module)
4232 /*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
4233 {
4234     PyInterpreterState *interp = _PyInterpreterState_GET();
4235     return PyBool_FromLong(PyMutex_IsLocked(&IMPORT_LOCK(interp).mutex));
4236 }
4237 
4238 /*[clinic input]
4239 _imp.acquire_lock
4240 
4241 Acquires the interpreter's import lock for the current thread.
4242 
4243 This lock should be used by import hooks to ensure thread-safety when importing
4244 modules. On platforms without threads, this function does nothing.
4245 [clinic start generated code]*/
4246 
4247 static PyObject *
_imp_acquire_lock_impl(PyObject * module)4248 _imp_acquire_lock_impl(PyObject *module)
4249 /*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/
4250 {
4251     PyInterpreterState *interp = _PyInterpreterState_GET();
4252     _PyImport_AcquireLock(interp);
4253     Py_RETURN_NONE;
4254 }
4255 
4256 /*[clinic input]
4257 _imp.release_lock
4258 
4259 Release the interpreter's import lock.
4260 
4261 On platforms without threads, this function does nothing.
4262 [clinic start generated code]*/
4263 
4264 static PyObject *
_imp_release_lock_impl(PyObject * module)4265 _imp_release_lock_impl(PyObject *module)
4266 /*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
4267 {
4268     PyInterpreterState *interp = _PyInterpreterState_GET();
4269     if (!_PyRecursiveMutex_IsLockedByCurrentThread(&IMPORT_LOCK(interp))) {
4270         PyErr_SetString(PyExc_RuntimeError,
4271                         "not holding the import lock");
4272         return NULL;
4273     }
4274     _PyImport_ReleaseLock(interp);
4275     Py_RETURN_NONE;
4276 }
4277 
4278 
4279 /*[clinic input]
4280 _imp._fix_co_filename
4281 
4282     code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
4283         Code object to change.
4284 
4285     path: unicode
4286         File path to use.
4287     /
4288 
4289 Changes code.co_filename to specify the passed-in file path.
4290 [clinic start generated code]*/
4291 
4292 static PyObject *
_imp__fix_co_filename_impl(PyObject * module,PyCodeObject * code,PyObject * path)4293 _imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code,
4294                            PyObject *path)
4295 /*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/
4296 
4297 {
4298     update_compiled_module(code, path);
4299 
4300     Py_RETURN_NONE;
4301 }
4302 
4303 
4304 /*[clinic input]
4305 _imp.create_builtin
4306 
4307     spec: object
4308     /
4309 
4310 Create an extension module.
4311 [clinic start generated code]*/
4312 
4313 static PyObject *
_imp_create_builtin(PyObject * module,PyObject * spec)4314 _imp_create_builtin(PyObject *module, PyObject *spec)
4315 /*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/
4316 {
4317     PyThreadState *tstate = _PyThreadState_GET();
4318 
4319     PyObject *name = PyObject_GetAttrString(spec, "name");
4320     if (name == NULL) {
4321         return NULL;
4322     }
4323 
4324     if (!PyUnicode_Check(name)) {
4325         PyErr_Format(PyExc_TypeError,
4326                      "name must be string, not %.200s",
4327                      Py_TYPE(name)->tp_name);
4328         Py_DECREF(name);
4329         return NULL;
4330     }
4331 
4332     PyObject *mod = create_builtin(tstate, name, spec);
4333     Py_DECREF(name);
4334     return mod;
4335 }
4336 
4337 
4338 /*[clinic input]
4339 _imp.extension_suffixes
4340 
4341 Returns the list of file suffixes used to identify extension modules.
4342 [clinic start generated code]*/
4343 
4344 static PyObject *
_imp_extension_suffixes_impl(PyObject * module)4345 _imp_extension_suffixes_impl(PyObject *module)
4346 /*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/
4347 {
4348     PyObject *list;
4349 
4350     list = PyList_New(0);
4351     if (list == NULL)
4352         return NULL;
4353 #ifdef HAVE_DYNAMIC_LOADING
4354     const char *suffix;
4355     unsigned int index = 0;
4356 
4357     while ((suffix = _PyImport_DynLoadFiletab[index])) {
4358         PyObject *item = PyUnicode_FromString(suffix);
4359         if (item == NULL) {
4360             Py_DECREF(list);
4361             return NULL;
4362         }
4363         if (PyList_Append(list, item) < 0) {
4364             Py_DECREF(list);
4365             Py_DECREF(item);
4366             return NULL;
4367         }
4368         Py_DECREF(item);
4369         index += 1;
4370     }
4371 #endif
4372     return list;
4373 }
4374 
4375 /*[clinic input]
4376 _imp.init_frozen
4377 
4378     name: unicode
4379     /
4380 
4381 Initializes a frozen module.
4382 [clinic start generated code]*/
4383 
4384 static PyObject *
_imp_init_frozen_impl(PyObject * module,PyObject * name)4385 _imp_init_frozen_impl(PyObject *module, PyObject *name)
4386 /*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/
4387 {
4388     PyThreadState *tstate = _PyThreadState_GET();
4389     int ret;
4390 
4391     ret = PyImport_ImportFrozenModuleObject(name);
4392     if (ret < 0)
4393         return NULL;
4394     if (ret == 0) {
4395         Py_RETURN_NONE;
4396     }
4397     return import_add_module(tstate, name);
4398 }
4399 
4400 /*[clinic input]
4401 _imp.find_frozen
4402 
4403     name: unicode
4404     /
4405     *
4406     withdata: bool = False
4407 
4408 Return info about the corresponding frozen module (if there is one) or None.
4409 
4410 The returned info (a 2-tuple):
4411 
4412  * data         the raw marshalled bytes
4413  * is_package   whether or not it is a package
4414  * origname     the originally frozen module's name, or None if not
4415                 a stdlib module (this will usually be the same as
4416                 the module's current name)
4417 [clinic start generated code]*/
4418 
4419 static PyObject *
_imp_find_frozen_impl(PyObject * module,PyObject * name,int withdata)4420 _imp_find_frozen_impl(PyObject *module, PyObject *name, int withdata)
4421 /*[clinic end generated code: output=8c1c3c7f925397a5 input=22a8847c201542fd]*/
4422 {
4423     struct frozen_info info;
4424     frozen_status status = find_frozen(name, &info);
4425     if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) {
4426         Py_RETURN_NONE;
4427     }
4428     else if (status == FROZEN_BAD_NAME) {
4429         Py_RETURN_NONE;
4430     }
4431     else if (status != FROZEN_OKAY) {
4432         set_frozen_error(status, name);
4433         return NULL;
4434     }
4435 
4436     PyObject *data = NULL;
4437     if (withdata) {
4438         data = PyMemoryView_FromMemory((char *)info.data, info.size, PyBUF_READ);
4439         if (data == NULL) {
4440             return NULL;
4441         }
4442     }
4443 
4444     PyObject *origname = NULL;
4445     if (info.origname != NULL && info.origname[0] != '\0') {
4446         origname = PyUnicode_FromString(info.origname);
4447         if (origname == NULL) {
4448             Py_XDECREF(data);
4449             return NULL;
4450         }
4451     }
4452 
4453     PyObject *result = PyTuple_Pack(3, data ? data : Py_None,
4454                                     info.is_package ? Py_True : Py_False,
4455                                     origname ? origname : Py_None);
4456     Py_XDECREF(origname);
4457     Py_XDECREF(data);
4458     return result;
4459 }
4460 
4461 /*[clinic input]
4462 _imp.get_frozen_object
4463 
4464     name: unicode
4465     data as dataobj: object = None
4466     /
4467 
4468 Create a code object for a frozen module.
4469 [clinic start generated code]*/
4470 
4471 static PyObject *
_imp_get_frozen_object_impl(PyObject * module,PyObject * name,PyObject * dataobj)4472 _imp_get_frozen_object_impl(PyObject *module, PyObject *name,
4473                             PyObject *dataobj)
4474 /*[clinic end generated code: output=54368a673a35e745 input=034bdb88f6460b7b]*/
4475 {
4476     struct frozen_info info = {0};
4477     Py_buffer buf = {0};
4478     if (PyObject_CheckBuffer(dataobj)) {
4479         if (PyObject_GetBuffer(dataobj, &buf, PyBUF_SIMPLE) != 0) {
4480             return NULL;
4481         }
4482         info.data = (const char *)buf.buf;
4483         info.size = buf.len;
4484     }
4485     else if (dataobj != Py_None) {
4486         _PyArg_BadArgument("get_frozen_object", "argument 2", "bytes", dataobj);
4487         return NULL;
4488     }
4489     else {
4490         frozen_status status = find_frozen(name, &info);
4491         if (status != FROZEN_OKAY) {
4492             set_frozen_error(status, name);
4493             return NULL;
4494         }
4495     }
4496 
4497     if (info.nameobj == NULL) {
4498         info.nameobj = name;
4499     }
4500     if (info.size == 0) {
4501         /* Does not contain executable code. */
4502         set_frozen_error(FROZEN_INVALID, name);
4503         return NULL;
4504     }
4505 
4506     PyInterpreterState *interp = _PyInterpreterState_GET();
4507     PyObject *codeobj = unmarshal_frozen_code(interp, &info);
4508     if (dataobj != Py_None) {
4509         PyBuffer_Release(&buf);
4510     }
4511     return codeobj;
4512 }
4513 
4514 /*[clinic input]
4515 _imp.is_frozen_package
4516 
4517     name: unicode
4518     /
4519 
4520 Returns True if the module name is of a frozen package.
4521 [clinic start generated code]*/
4522 
4523 static PyObject *
_imp_is_frozen_package_impl(PyObject * module,PyObject * name)4524 _imp_is_frozen_package_impl(PyObject *module, PyObject *name)
4525 /*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/
4526 {
4527     struct frozen_info info;
4528     frozen_status status = find_frozen(name, &info);
4529     if (status != FROZEN_OKAY && status != FROZEN_EXCLUDED) {
4530         set_frozen_error(status, name);
4531         return NULL;
4532     }
4533     return PyBool_FromLong(info.is_package);
4534 }
4535 
4536 /*[clinic input]
4537 _imp.is_builtin
4538 
4539     name: unicode
4540     /
4541 
4542 Returns True if the module name corresponds to a built-in module.
4543 [clinic start generated code]*/
4544 
4545 static PyObject *
_imp_is_builtin_impl(PyObject * module,PyObject * name)4546 _imp_is_builtin_impl(PyObject *module, PyObject *name)
4547 /*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/
4548 {
4549     return PyLong_FromLong(is_builtin(name));
4550 }
4551 
4552 /*[clinic input]
4553 _imp.is_frozen
4554 
4555     name: unicode
4556     /
4557 
4558 Returns True if the module name corresponds to a frozen module.
4559 [clinic start generated code]*/
4560 
4561 static PyObject *
_imp_is_frozen_impl(PyObject * module,PyObject * name)4562 _imp_is_frozen_impl(PyObject *module, PyObject *name)
4563 /*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/
4564 {
4565     struct frozen_info info;
4566     frozen_status status = find_frozen(name, &info);
4567     if (status != FROZEN_OKAY) {
4568         Py_RETURN_FALSE;
4569     }
4570     Py_RETURN_TRUE;
4571 }
4572 
4573 /*[clinic input]
4574 _imp._frozen_module_names
4575 
4576 Returns the list of available frozen modules.
4577 [clinic start generated code]*/
4578 
4579 static PyObject *
_imp__frozen_module_names_impl(PyObject * module)4580 _imp__frozen_module_names_impl(PyObject *module)
4581 /*[clinic end generated code: output=80609ef6256310a8 input=76237fbfa94460d2]*/
4582 {
4583     return list_frozen_module_names();
4584 }
4585 
4586 /*[clinic input]
4587 _imp._override_frozen_modules_for_tests
4588 
4589     override: int
4590     /
4591 
4592 (internal-only) Override PyConfig.use_frozen_modules.
4593 
4594 (-1: "off", 1: "on", 0: no override)
4595 See frozen_modules() in Lib/test/support/import_helper.py.
4596 [clinic start generated code]*/
4597 
4598 static PyObject *
_imp__override_frozen_modules_for_tests_impl(PyObject * module,int override)4599 _imp__override_frozen_modules_for_tests_impl(PyObject *module, int override)
4600 /*[clinic end generated code: output=36d5cb1594160811 input=8f1f95a3ef21aec3]*/
4601 {
4602     PyInterpreterState *interp = _PyInterpreterState_GET();
4603     OVERRIDE_FROZEN_MODULES(interp) = override;
4604     Py_RETURN_NONE;
4605 }
4606 
4607 /*[clinic input]
4608 _imp._override_multi_interp_extensions_check
4609 
4610     override: int
4611     /
4612 
4613 (internal-only) Override PyInterpreterConfig.check_multi_interp_extensions.
4614 
4615 (-1: "never", 1: "always", 0: no override)
4616 [clinic start generated code]*/
4617 
4618 static PyObject *
_imp__override_multi_interp_extensions_check_impl(PyObject * module,int override)4619 _imp__override_multi_interp_extensions_check_impl(PyObject *module,
4620                                                   int override)
4621 /*[clinic end generated code: output=3ff043af52bbf280 input=e086a2ea181f92ae]*/
4622 {
4623     PyInterpreterState *interp = _PyInterpreterState_GET();
4624     if (_Py_IsMainInterpreter(interp)) {
4625         PyErr_SetString(PyExc_RuntimeError,
4626                         "_imp._override_multi_interp_extensions_check() "
4627                         "cannot be used in the main interpreter");
4628         return NULL;
4629     }
4630 #ifdef Py_GIL_DISABLED
4631     PyErr_SetString(PyExc_RuntimeError,
4632                     "_imp._override_multi_interp_extensions_check() "
4633                     "cannot be used in the free-threaded build");
4634     return NULL;
4635 #else
4636     int oldvalue = OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp);
4637     OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp) = override;
4638     return PyLong_FromLong(oldvalue);
4639 #endif
4640 }
4641 
4642 #ifdef HAVE_DYNAMIC_LOADING
4643 
4644 /*[clinic input]
4645 _imp.create_dynamic
4646 
4647     spec: object
4648     file: object = NULL
4649     /
4650 
4651 Create an extension module.
4652 [clinic start generated code]*/
4653 
4654 static PyObject *
_imp_create_dynamic_impl(PyObject * module,PyObject * spec,PyObject * file)4655 _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
4656 /*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
4657 {
4658     PyObject *mod = NULL;
4659     PyThreadState *tstate = _PyThreadState_GET();
4660 
4661     struct _Py_ext_module_loader_info info;
4662     if (_Py_ext_module_loader_info_init_from_spec(&info, spec) < 0) {
4663         return NULL;
4664     }
4665 
4666     struct extensions_cache_value *cached = NULL;
4667     mod = import_find_extension(tstate, &info, &cached);
4668     if (mod != NULL) {
4669         assert(!_PyErr_Occurred(tstate));
4670         assert(cached != NULL);
4671         /* The module might not have md_def set in certain reload cases. */
4672         assert(_PyModule_GetDef(mod) == NULL
4673                 || cached->def == _PyModule_GetDef(mod));
4674         assert_singlephase(cached);
4675         goto finally;
4676     }
4677     else if (_PyErr_Occurred(tstate)) {
4678         goto finally;
4679     }
4680     /* Otherwise it must be multi-phase init or the first time it's loaded. */
4681 
4682     /* If the module was added to the global cache
4683      * but def->m_base.m_copy was cleared (e.g. subinterp fini)
4684      * then we have to do a little dance here. */
4685     if (cached != NULL) {
4686         assert(cached->def->m_base.m_copy == NULL);
4687         /* For now we clear the cache and move on. */
4688         _extensions_cache_delete(info.path, info.name);
4689     }
4690 
4691     if (PySys_Audit("import", "OOOOO", info.name, info.filename,
4692                     Py_None, Py_None, Py_None) < 0)
4693     {
4694         goto finally;
4695     }
4696 
4697     /* We would move this (and the fclose() below) into
4698      * _PyImport_GetModInitFunc(), but it isn't clear if the intervening
4699      * code relies on fp still being open. */
4700     FILE *fp;
4701     if (file != NULL) {
4702         fp = _Py_fopen_obj(info.filename, "r");
4703         if (fp == NULL) {
4704             goto finally;
4705         }
4706     }
4707     else {
4708         fp = NULL;
4709     }
4710 
4711     PyModInitFunction p0 = _PyImport_GetModInitFunc(&info, fp);
4712     if (p0 == NULL) {
4713         goto finally;
4714     }
4715 
4716 #ifdef Py_GIL_DISABLED
4717     // This call (and the corresponding call to _PyImport_CheckGILForModule())
4718     // would ideally be inside import_run_extension(). They are kept in the
4719     // callers for now because that would complicate the control flow inside
4720     // import_run_extension(). It should be possible to restructure
4721     // import_run_extension() to address this.
4722     _PyEval_EnableGILTransient(tstate);
4723 #endif
4724     mod = import_run_extension(
4725                     tstate, p0, &info, spec, get_modules_dict(tstate, true));
4726 #ifdef Py_GIL_DISABLED
4727     if (_PyImport_CheckGILForModule(mod, info.name) < 0) {
4728         Py_CLEAR(mod);
4729         goto finally;
4730     }
4731 #endif
4732 
4733     // XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
4734     if (fp) {
4735         fclose(fp);
4736     }
4737 
4738 finally:
4739     _Py_ext_module_loader_info_clear(&info);
4740     return mod;
4741 }
4742 
4743 /*[clinic input]
4744 _imp.exec_dynamic -> int
4745 
4746     mod: object
4747     /
4748 
4749 Initialize an extension module.
4750 [clinic start generated code]*/
4751 
4752 static int
_imp_exec_dynamic_impl(PyObject * module,PyObject * mod)4753 _imp_exec_dynamic_impl(PyObject *module, PyObject *mod)
4754 /*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/
4755 {
4756     return exec_builtin_or_dynamic(mod);
4757 }
4758 
4759 
4760 #endif /* HAVE_DYNAMIC_LOADING */
4761 
4762 /*[clinic input]
4763 _imp.exec_builtin -> int
4764 
4765     mod: object
4766     /
4767 
4768 Initialize a built-in module.
4769 [clinic start generated code]*/
4770 
4771 static int
_imp_exec_builtin_impl(PyObject * module,PyObject * mod)4772 _imp_exec_builtin_impl(PyObject *module, PyObject *mod)
4773 /*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/
4774 {
4775     return exec_builtin_or_dynamic(mod);
4776 }
4777 
4778 /*[clinic input]
4779 _imp.source_hash
4780 
4781     key: long
4782     source: Py_buffer
4783 [clinic start generated code]*/
4784 
4785 static PyObject *
_imp_source_hash_impl(PyObject * module,long key,Py_buffer * source)4786 _imp_source_hash_impl(PyObject *module, long key, Py_buffer *source)
4787 /*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/
4788 {
4789     union {
4790         uint64_t x;
4791         char data[sizeof(uint64_t)];
4792     } hash;
4793     hash.x = _Py_KeyedHash((uint64_t)key, source->buf, source->len);
4794 #if !PY_LITTLE_ENDIAN
4795     // Force to little-endian. There really ought to be a succinct standard way
4796     // to do this.
4797     for (size_t i = 0; i < sizeof(hash.data)/2; i++) {
4798         char tmp = hash.data[i];
4799         hash.data[i] = hash.data[sizeof(hash.data) - i - 1];
4800         hash.data[sizeof(hash.data) - i - 1] = tmp;
4801     }
4802 #endif
4803     return PyBytes_FromStringAndSize(hash.data, sizeof(hash.data));
4804 }
4805 
4806 
4807 PyDoc_STRVAR(doc_imp,
4808 "(Extremely) low-level import machinery bits as used by importlib.");
4809 
4810 static PyMethodDef imp_methods[] = {
4811     _IMP_EXTENSION_SUFFIXES_METHODDEF
4812     _IMP_LOCK_HELD_METHODDEF
4813     _IMP_ACQUIRE_LOCK_METHODDEF
4814     _IMP_RELEASE_LOCK_METHODDEF
4815     _IMP_FIND_FROZEN_METHODDEF
4816     _IMP_GET_FROZEN_OBJECT_METHODDEF
4817     _IMP_IS_FROZEN_PACKAGE_METHODDEF
4818     _IMP_CREATE_BUILTIN_METHODDEF
4819     _IMP_INIT_FROZEN_METHODDEF
4820     _IMP_IS_BUILTIN_METHODDEF
4821     _IMP_IS_FROZEN_METHODDEF
4822     _IMP__FROZEN_MODULE_NAMES_METHODDEF
4823     _IMP__OVERRIDE_FROZEN_MODULES_FOR_TESTS_METHODDEF
4824     _IMP__OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK_METHODDEF
4825     _IMP_CREATE_DYNAMIC_METHODDEF
4826     _IMP_EXEC_DYNAMIC_METHODDEF
4827     _IMP_EXEC_BUILTIN_METHODDEF
4828     _IMP__FIX_CO_FILENAME_METHODDEF
4829     _IMP_SOURCE_HASH_METHODDEF
4830     {NULL, NULL}  /* sentinel */
4831 };
4832 
4833 
4834 static int
imp_module_exec(PyObject * module)4835 imp_module_exec(PyObject *module)
4836 {
4837     const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode;
4838     PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
4839     if (PyModule_Add(module, "check_hash_based_pycs", pyc_mode) < 0) {
4840         return -1;
4841     }
4842 
4843     return 0;
4844 }
4845 
4846 
4847 static PyModuleDef_Slot imp_slots[] = {
4848     {Py_mod_exec, imp_module_exec},
4849     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
4850     {Py_mod_gil, Py_MOD_GIL_NOT_USED},
4851     {0, NULL}
4852 };
4853 
4854 static struct PyModuleDef imp_module = {
4855     PyModuleDef_HEAD_INIT,
4856     .m_name = "_imp",
4857     .m_doc = doc_imp,
4858     .m_size = 0,
4859     .m_methods = imp_methods,
4860     .m_slots = imp_slots,
4861 };
4862 
4863 PyMODINIT_FUNC
PyInit__imp(void)4864 PyInit__imp(void)
4865 {
4866     return PyModuleDef_Init(&imp_module);
4867 }
4868