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