Line data Source code
1 :
2 : /* Module definition and import implementation */
3 :
4 : #include "Python.h"
5 :
6 : #include "Python-ast.h"
7 : #undef Yield /* undefine macro conflicting with winbase.h */
8 : #include "errcode.h"
9 : #include "marshal.h"
10 : #include "code.h"
11 : #include "frameobject.h"
12 : #include "osdefs.h"
13 : #include "importdl.h"
14 :
15 : #ifdef HAVE_FCNTL_H
16 : #include <fcntl.h>
17 : #endif
18 : #ifdef __cplusplus
19 : extern "C" {
20 : #endif
21 :
22 : #ifdef MS_WINDOWS
23 : /* for stat.st_mode */
24 : typedef unsigned short mode_t;
25 : /* for _mkdir */
26 : #include <direct.h>
27 : #endif
28 :
29 :
30 : #define CACHEDIR "__pycache__"
31 :
32 : /* See _PyImport_FixupExtensionObject() below */
33 : static PyObject *extensions = NULL;
34 :
35 : /* Function from Parser/tokenizer.c */
36 : extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
37 :
38 : /* This table is defined in config.c: */
39 : extern struct _inittab _PyImport_Inittab[];
40 :
41 : struct _inittab *PyImport_Inittab = _PyImport_Inittab;
42 :
43 : static PyObject *initstr = NULL;
44 :
45 : /* Initialize things */
46 :
47 : void
48 1 : _PyImport_Init(void)
49 : {
50 1 : initstr = PyUnicode_InternFromString("__init__");
51 1 : if (initstr == NULL)
52 0 : Py_FatalError("Can't initialize import variables");
53 1 : }
54 :
55 : void
56 1 : _PyImportHooks_Init(void)
57 : {
58 1 : PyObject *v, *path_hooks = NULL;
59 1 : int err = 0;
60 :
61 : /* adding sys.path_hooks and sys.path_importer_cache */
62 1 : v = PyList_New(0);
63 1 : if (v == NULL)
64 0 : goto error;
65 1 : err = PySys_SetObject("meta_path", v);
66 1 : Py_DECREF(v);
67 1 : if (err)
68 0 : goto error;
69 1 : v = PyDict_New();
70 1 : if (v == NULL)
71 0 : goto error;
72 1 : err = PySys_SetObject("path_importer_cache", v);
73 1 : Py_DECREF(v);
74 1 : if (err)
75 0 : goto error;
76 1 : path_hooks = PyList_New(0);
77 1 : if (path_hooks == NULL)
78 0 : goto error;
79 1 : err = PySys_SetObject("path_hooks", path_hooks);
80 1 : if (err) {
81 : error:
82 0 : PyErr_Print();
83 0 : Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
84 : "or path_importer_cache failed");
85 : }
86 1 : Py_DECREF(path_hooks);
87 1 : }
88 :
89 : void
90 1 : _PyImportZip_Init(void)
91 : {
92 : PyObject *path_hooks, *zimpimport;
93 1 : int err = 0;
94 :
95 1 : path_hooks = PySys_GetObject("path_hooks");
96 1 : if (path_hooks == NULL)
97 0 : goto error;
98 :
99 1 : if (Py_VerboseFlag)
100 0 : PySys_WriteStderr("# installing zipimport hook\n");
101 :
102 1 : zimpimport = PyImport_ImportModule("zipimport");
103 1 : if (zimpimport == NULL) {
104 0 : PyErr_Clear(); /* No zip import module -- okay */
105 0 : if (Py_VerboseFlag)
106 0 : PySys_WriteStderr("# can't import zipimport\n");
107 : }
108 : else {
109 : _Py_IDENTIFIER(zipimporter);
110 1 : PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
111 : &PyId_zipimporter);
112 1 : Py_DECREF(zimpimport);
113 1 : if (zipimporter == NULL) {
114 0 : PyErr_Clear(); /* No zipimporter object -- okay */
115 0 : if (Py_VerboseFlag)
116 0 : PySys_WriteStderr(
117 : "# can't import zipimport.zipimporter\n");
118 : }
119 : else {
120 : /* sys.path_hooks.insert(0, zipimporter) */
121 1 : err = PyList_Insert(path_hooks, 0, zipimporter);
122 1 : Py_DECREF(zipimporter);
123 1 : if (err < 0) {
124 0 : goto error;
125 : }
126 1 : if (Py_VerboseFlag)
127 0 : PySys_WriteStderr(
128 : "# installed zipimport hook\n");
129 : }
130 : }
131 :
132 2 : return;
133 :
134 : error:
135 0 : PyErr_Print();
136 0 : Py_FatalError("initializing zipimport failed");
137 : }
138 :
139 : /* Locking primitives to prevent parallel imports of the same module
140 : in different threads to return with a partially loaded module.
141 : These calls are serialized by the global interpreter lock. */
142 :
143 : #ifdef WITH_THREAD
144 :
145 : #include "pythread.h"
146 :
147 : static PyThread_type_lock import_lock = 0;
148 : static long import_lock_thread = -1;
149 : static int import_lock_level = 0;
150 :
151 : void
152 552 : _PyImport_AcquireLock(void)
153 : {
154 552 : long me = PyThread_get_thread_ident();
155 552 : if (me == -1)
156 0 : return; /* Too bad */
157 552 : if (import_lock == NULL) {
158 1 : import_lock = PyThread_allocate_lock();
159 1 : if (import_lock == NULL)
160 0 : return; /* Nothing much we can do. */
161 : }
162 552 : if (import_lock_thread == me) {
163 0 : import_lock_level++;
164 0 : return;
165 : }
166 552 : if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
167 : {
168 0 : PyThreadState *tstate = PyEval_SaveThread();
169 0 : PyThread_acquire_lock(import_lock, 1);
170 0 : PyEval_RestoreThread(tstate);
171 : }
172 552 : import_lock_thread = me;
173 552 : import_lock_level = 1;
174 : }
175 :
176 : int
177 552 : _PyImport_ReleaseLock(void)
178 : {
179 552 : long me = PyThread_get_thread_ident();
180 552 : if (me == -1 || import_lock == NULL)
181 0 : return 0; /* Too bad */
182 552 : if (import_lock_thread != me)
183 0 : return -1;
184 552 : import_lock_level--;
185 552 : if (import_lock_level == 0) {
186 552 : import_lock_thread = -1;
187 552 : PyThread_release_lock(import_lock);
188 : }
189 552 : return 1;
190 : }
191 :
192 : /* This function is called from PyOS_AfterFork to ensure that newly
193 : created child processes do not share locks with the parent.
194 : We now acquire the import lock around fork() calls but on some platforms
195 : (Solaris 9 and earlier? see isue7242) that still left us with problems. */
196 :
197 : void
198 0 : _PyImport_ReInitLock(void)
199 : {
200 0 : if (import_lock != NULL)
201 0 : import_lock = PyThread_allocate_lock();
202 0 : if (import_lock_level > 1) {
203 : /* Forked as a side effect of import */
204 0 : long me = PyThread_get_thread_ident();
205 0 : PyThread_acquire_lock(import_lock, 0);
206 : /* XXX: can the previous line fail? */
207 0 : import_lock_thread = me;
208 0 : import_lock_level--;
209 : } else {
210 0 : import_lock_thread = -1;
211 0 : import_lock_level = 0;
212 : }
213 0 : }
214 :
215 : #endif
216 :
217 : static PyObject *
218 0 : imp_lock_held(PyObject *self, PyObject *noargs)
219 : {
220 : #ifdef WITH_THREAD
221 0 : return PyBool_FromLong(import_lock_thread != -1);
222 : #else
223 : return PyBool_FromLong(0);
224 : #endif
225 : }
226 :
227 : static PyObject *
228 220 : imp_acquire_lock(PyObject *self, PyObject *noargs)
229 : {
230 : #ifdef WITH_THREAD
231 220 : _PyImport_AcquireLock();
232 : #endif
233 220 : Py_INCREF(Py_None);
234 220 : return Py_None;
235 : }
236 :
237 : static PyObject *
238 364 : imp_release_lock(PyObject *self, PyObject *noargs)
239 : {
240 : #ifdef WITH_THREAD
241 364 : if (_PyImport_ReleaseLock() < 0) {
242 0 : PyErr_SetString(PyExc_RuntimeError,
243 : "not holding the import lock");
244 0 : return NULL;
245 : }
246 : #endif
247 364 : Py_INCREF(Py_None);
248 364 : return Py_None;
249 : }
250 :
251 : void
252 0 : _PyImport_Fini(void)
253 : {
254 0 : Py_XDECREF(extensions);
255 0 : extensions = NULL;
256 : #ifdef WITH_THREAD
257 0 : if (import_lock != NULL) {
258 0 : PyThread_free_lock(import_lock);
259 0 : import_lock = NULL;
260 : }
261 : #endif
262 0 : }
263 :
264 : /* Helper for sys */
265 :
266 : PyObject *
267 1147 : PyImport_GetModuleDict(void)
268 : {
269 1147 : PyInterpreterState *interp = PyThreadState_GET()->interp;
270 1147 : if (interp->modules == NULL)
271 0 : Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
272 1147 : return interp->modules;
273 : }
274 :
275 :
276 : /* List of names to clear in sys */
277 : static char* sys_deletes[] = {
278 : "path", "argv", "ps1", "ps2",
279 : "last_type", "last_value", "last_traceback",
280 : "path_hooks", "path_importer_cache", "meta_path",
281 : /* misc stuff */
282 : "flags", "float_info",
283 : NULL
284 : };
285 :
286 : static char* sys_files[] = {
287 : "stdin", "__stdin__",
288 : "stdout", "__stdout__",
289 : "stderr", "__stderr__",
290 : NULL
291 : };
292 :
293 :
294 : /* Un-initialize things, as good as we can */
295 :
296 : void
297 0 : PyImport_Cleanup(void)
298 : {
299 : Py_ssize_t pos, ndone;
300 : PyObject *key, *value, *dict;
301 0 : PyInterpreterState *interp = PyThreadState_GET()->interp;
302 0 : PyObject *modules = interp->modules;
303 :
304 0 : if (modules == NULL)
305 0 : return; /* Already done */
306 :
307 : /* Delete some special variables first. These are common
308 : places where user values hide and people complain when their
309 : destructors fail. Since the modules containing them are
310 : deleted *last* of all, they would come too late in the normal
311 : destruction order. Sigh. */
312 :
313 0 : value = PyDict_GetItemString(modules, "builtins");
314 0 : if (value != NULL && PyModule_Check(value)) {
315 0 : dict = PyModule_GetDict(value);
316 0 : if (Py_VerboseFlag)
317 0 : PySys_WriteStderr("# clear builtins._\n");
318 0 : PyDict_SetItemString(dict, "_", Py_None);
319 : }
320 0 : value = PyDict_GetItemString(modules, "sys");
321 0 : if (value != NULL && PyModule_Check(value)) {
322 : char **p;
323 : PyObject *v;
324 0 : dict = PyModule_GetDict(value);
325 0 : for (p = sys_deletes; *p != NULL; p++) {
326 0 : if (Py_VerboseFlag)
327 0 : PySys_WriteStderr("# clear sys.%s\n", *p);
328 0 : PyDict_SetItemString(dict, *p, Py_None);
329 : }
330 0 : for (p = sys_files; *p != NULL; p+=2) {
331 0 : if (Py_VerboseFlag)
332 0 : PySys_WriteStderr("# restore sys.%s\n", *p);
333 0 : v = PyDict_GetItemString(dict, *(p+1));
334 0 : if (v == NULL)
335 0 : v = Py_None;
336 0 : PyDict_SetItemString(dict, *p, v);
337 : }
338 : }
339 :
340 : /* First, delete __main__ */
341 0 : value = PyDict_GetItemString(modules, "__main__");
342 0 : if (value != NULL && PyModule_Check(value)) {
343 0 : if (Py_VerboseFlag)
344 0 : PySys_WriteStderr("# cleanup __main__\n");
345 0 : _PyModule_Clear(value);
346 0 : PyDict_SetItemString(modules, "__main__", Py_None);
347 : }
348 :
349 : /* The special treatment of "builtins" here is because even
350 : when it's not referenced as a module, its dictionary is
351 : referenced by almost every module's __builtins__. Since
352 : deleting a module clears its dictionary (even if there are
353 : references left to it), we need to delete the "builtins"
354 : module last. Likewise, we don't delete sys until the very
355 : end because it is implicitly referenced (e.g. by print).
356 :
357 : Also note that we 'delete' modules by replacing their entry
358 : in the modules dict with None, rather than really deleting
359 : them; this avoids a rehash of the modules dictionary and
360 : also marks them as "non existent" so they won't be
361 : re-imported. */
362 :
363 : /* Next, repeatedly delete modules with a reference count of
364 : one (skipping builtins and sys) and delete them */
365 : do {
366 0 : ndone = 0;
367 0 : pos = 0;
368 0 : while (PyDict_Next(modules, &pos, &key, &value)) {
369 0 : if (value->ob_refcnt != 1)
370 0 : continue;
371 0 : if (PyUnicode_Check(key) && PyModule_Check(value)) {
372 0 : if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
373 0 : continue;
374 0 : if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
375 0 : continue;
376 0 : if (Py_VerboseFlag)
377 0 : PySys_FormatStderr(
378 : "# cleanup[1] %U\n", key);
379 0 : _PyModule_Clear(value);
380 0 : PyDict_SetItem(modules, key, Py_None);
381 0 : ndone++;
382 : }
383 : }
384 0 : } while (ndone > 0);
385 :
386 : /* Next, delete all modules (still skipping builtins and sys) */
387 0 : pos = 0;
388 0 : while (PyDict_Next(modules, &pos, &key, &value)) {
389 0 : if (PyUnicode_Check(key) && PyModule_Check(value)) {
390 0 : if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
391 0 : continue;
392 0 : if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
393 0 : continue;
394 0 : if (Py_VerboseFlag)
395 0 : PySys_FormatStderr("# cleanup[2] %U\n", key);
396 0 : _PyModule_Clear(value);
397 0 : PyDict_SetItem(modules, key, Py_None);
398 : }
399 : }
400 :
401 : /* Next, delete sys and builtins (in that order) */
402 0 : value = PyDict_GetItemString(modules, "sys");
403 0 : if (value != NULL && PyModule_Check(value)) {
404 0 : if (Py_VerboseFlag)
405 0 : PySys_WriteStderr("# cleanup sys\n");
406 0 : _PyModule_Clear(value);
407 0 : PyDict_SetItemString(modules, "sys", Py_None);
408 : }
409 0 : value = PyDict_GetItemString(modules, "builtins");
410 0 : if (value != NULL && PyModule_Check(value)) {
411 0 : if (Py_VerboseFlag)
412 0 : PySys_WriteStderr("# cleanup builtins\n");
413 0 : _PyModule_Clear(value);
414 0 : PyDict_SetItemString(modules, "builtins", Py_None);
415 : }
416 :
417 : /* Finally, clear and delete the modules directory */
418 0 : PyDict_Clear(modules);
419 0 : interp->modules = NULL;
420 0 : Py_DECREF(modules);
421 : }
422 :
423 :
424 : /* Helper for pythonrun.c -- return magic number and tag. */
425 :
426 : long
427 0 : PyImport_GetMagicNumber(void)
428 : {
429 : long res;
430 0 : PyInterpreterState *interp = PyThreadState_Get()->interp;
431 0 : PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
432 : "_RAW_MAGIC_NUMBER");
433 0 : if (pyc_magic == NULL)
434 0 : return -1;
435 0 : res = PyLong_AsLong(pyc_magic);
436 0 : Py_DECREF(pyc_magic);
437 0 : return res;
438 : }
439 :
440 :
441 : extern const char * _PySys_ImplCacheTag;
442 :
443 : const char *
444 0 : PyImport_GetMagicTag(void)
445 : {
446 0 : return _PySys_ImplCacheTag;
447 : }
448 :
449 :
450 : /* Magic for extension modules (built-in as well as dynamically
451 : loaded). To prevent initializing an extension module more than
452 : once, we keep a static dictionary 'extensions' keyed by module name
453 : (for built-in modules) or by filename (for dynamically loaded
454 : modules), containing these modules. A copy of the module's
455 : dictionary is stored by calling _PyImport_FixupExtensionObject()
456 : immediately after the module initialization function succeeds. A
457 : copy can be retrieved from there by calling
458 : _PyImport_FindExtensionObject().
459 :
460 : Modules which do support multiple initialization set their m_size
461 : field to a non-negative number (indicating the size of the
462 : module-specific state). They are still recorded in the extensions
463 : dictionary, to avoid loading shared libraries twice.
464 : */
465 :
466 : int
467 23 : _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
468 : PyObject *filename)
469 : {
470 : PyObject *modules, *dict;
471 : struct PyModuleDef *def;
472 23 : if (extensions == NULL) {
473 1 : extensions = PyDict_New();
474 1 : if (extensions == NULL)
475 0 : return -1;
476 : }
477 23 : if (mod == NULL || !PyModule_Check(mod)) {
478 0 : PyErr_BadInternalCall();
479 0 : return -1;
480 : }
481 23 : def = PyModule_GetDef(mod);
482 23 : if (!def) {
483 0 : PyErr_BadInternalCall();
484 0 : return -1;
485 : }
486 23 : modules = PyImport_GetModuleDict();
487 23 : if (PyDict_SetItem(modules, name, mod) < 0)
488 0 : return -1;
489 23 : if (_PyState_AddModule(mod, def) < 0) {
490 0 : PyDict_DelItem(modules, name);
491 0 : return -1;
492 : }
493 23 : if (def->m_size == -1) {
494 20 : if (def->m_base.m_copy) {
495 : /* Somebody already imported the module,
496 : likely under a different name.
497 : XXX this should really not happen. */
498 0 : Py_DECREF(def->m_base.m_copy);
499 0 : def->m_base.m_copy = NULL;
500 : }
501 20 : dict = PyModule_GetDict(mod);
502 20 : if (dict == NULL)
503 0 : return -1;
504 20 : def->m_base.m_copy = PyDict_Copy(dict);
505 20 : if (def->m_base.m_copy == NULL)
506 0 : return -1;
507 : }
508 23 : PyDict_SetItem(extensions, filename, (PyObject*)def);
509 23 : return 0;
510 : }
511 :
512 : int
513 3 : _PyImport_FixupBuiltin(PyObject *mod, char *name)
514 : {
515 : int res;
516 : PyObject *nameobj;
517 3 : nameobj = PyUnicode_InternFromString(name);
518 3 : if (nameobj == NULL)
519 0 : return -1;
520 3 : res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
521 3 : Py_DECREF(nameobj);
522 3 : return res;
523 : }
524 :
525 : PyObject *
526 20 : _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
527 : {
528 : PyObject *mod, *mdict;
529 : PyModuleDef* def;
530 20 : if (extensions == NULL)
531 0 : return NULL;
532 20 : def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
533 20 : if (def == NULL)
534 20 : return NULL;
535 0 : if (def->m_size == -1) {
536 : /* Module does not support repeated initialization */
537 0 : if (def->m_base.m_copy == NULL)
538 0 : return NULL;
539 0 : mod = PyImport_AddModuleObject(name);
540 0 : if (mod == NULL)
541 0 : return NULL;
542 0 : mdict = PyModule_GetDict(mod);
543 0 : if (mdict == NULL)
544 0 : return NULL;
545 0 : if (PyDict_Update(mdict, def->m_base.m_copy))
546 0 : return NULL;
547 : }
548 : else {
549 0 : if (def->m_base.m_init == NULL)
550 0 : return NULL;
551 0 : mod = def->m_base.m_init();
552 0 : if (mod == NULL)
553 0 : return NULL;
554 0 : PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
555 0 : Py_DECREF(mod);
556 : }
557 0 : if (_PyState_AddModule(mod, def) < 0) {
558 0 : PyDict_DelItem(PyImport_GetModuleDict(), name);
559 0 : Py_DECREF(mod);
560 0 : return NULL;
561 : }
562 0 : if (Py_VerboseFlag)
563 0 : PySys_FormatStderr("import %U # previously loaded (%R)\n",
564 : name, filename);
565 0 : return mod;
566 :
567 : }
568 :
569 : PyObject *
570 0 : _PyImport_FindBuiltin(const char *name)
571 : {
572 : PyObject *res, *nameobj;
573 0 : nameobj = PyUnicode_InternFromString(name);
574 0 : if (nameobj == NULL)
575 0 : return NULL;
576 0 : res = _PyImport_FindExtensionObject(nameobj, nameobj);
577 0 : Py_DECREF(nameobj);
578 0 : return res;
579 : }
580 :
581 : /* Get the module object corresponding to a module name.
582 : First check the modules dictionary if there's one there,
583 : if not, create a new one and insert it in the modules dictionary.
584 : Because the former action is most common, THIS DOES NOT RETURN A
585 : 'NEW' REFERENCE! */
586 :
587 : PyObject *
588 1110 : PyImport_AddModuleObject(PyObject *name)
589 : {
590 1110 : PyObject *modules = PyImport_GetModuleDict();
591 : PyObject *m;
592 :
593 2218 : if ((m = PyDict_GetItem(modules, name)) != NULL &&
594 1108 : PyModule_Check(m))
595 1108 : return m;
596 2 : m = PyModule_NewObject(name);
597 2 : if (m == NULL)
598 0 : return NULL;
599 2 : if (PyDict_SetItem(modules, name, m) != 0) {
600 0 : Py_DECREF(m);
601 0 : return NULL;
602 : }
603 2 : Py_DECREF(m); /* Yes, it still exists, in modules! */
604 :
605 2 : return m;
606 : }
607 :
608 : PyObject *
609 1093 : PyImport_AddModule(const char *name)
610 : {
611 : PyObject *nameobj, *module;
612 1093 : nameobj = PyUnicode_FromString(name);
613 1093 : if (nameobj == NULL)
614 0 : return NULL;
615 1093 : module = PyImport_AddModuleObject(nameobj);
616 1093 : Py_DECREF(nameobj);
617 1093 : return module;
618 : }
619 :
620 :
621 : /* Remove name from sys.modules, if it's there. */
622 : static void
623 0 : remove_module(PyObject *name)
624 : {
625 0 : PyObject *modules = PyImport_GetModuleDict();
626 0 : if (PyDict_GetItem(modules, name) == NULL)
627 0 : return;
628 0 : if (PyDict_DelItem(modules, name) < 0)
629 0 : Py_FatalError("import: deleting existing key in"
630 : "sys.modules failed");
631 : }
632 :
633 :
634 : /* Execute a code object in a module and return the module object
635 : * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
636 : * removed from sys.modules, to avoid leaving damaged module objects
637 : * in sys.modules. The caller may wish to restore the original
638 : * module object (if any) in this case; PyImport_ReloadModule is an
639 : * example.
640 : *
641 : * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
642 : * interface. The other two exist primarily for backward compatibility.
643 : */
644 : PyObject *
645 0 : PyImport_ExecCodeModule(char *name, PyObject *co)
646 : {
647 0 : return PyImport_ExecCodeModuleWithPathnames(
648 : name, co, (char *)NULL, (char *)NULL);
649 : }
650 :
651 : PyObject *
652 0 : PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
653 : {
654 0 : return PyImport_ExecCodeModuleWithPathnames(
655 : name, co, pathname, (char *)NULL);
656 : }
657 :
658 : PyObject *
659 0 : PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
660 : char *cpathname)
661 : {
662 0 : PyObject *m = NULL;
663 0 : PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
664 :
665 0 : nameobj = PyUnicode_FromString(name);
666 0 : if (nameobj == NULL)
667 0 : return NULL;
668 :
669 0 : if (cpathname != NULL) {
670 0 : cpathobj = PyUnicode_DecodeFSDefault(cpathname);
671 0 : if (cpathobj == NULL)
672 0 : goto error;
673 : }
674 : else
675 0 : cpathobj = NULL;
676 :
677 0 : if (pathname != NULL) {
678 0 : pathobj = PyUnicode_DecodeFSDefault(pathname);
679 0 : if (pathobj == NULL)
680 0 : goto error;
681 : }
682 0 : else if (cpathobj != NULL) {
683 0 : PyInterpreterState *interp = PyThreadState_GET()->interp;
684 : _Py_IDENTIFIER(_get_sourcefile);
685 :
686 0 : if (interp == NULL) {
687 0 : Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
688 : "no interpreter!");
689 : }
690 :
691 0 : pathobj = _PyObject_CallMethodObjIdArgs(interp->importlib,
692 : &PyId__get_sourcefile, cpathobj,
693 : NULL);
694 0 : if (pathobj == NULL)
695 0 : PyErr_Clear();
696 : }
697 : else
698 0 : pathobj = NULL;
699 :
700 0 : m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
701 : error:
702 0 : Py_DECREF(nameobj);
703 0 : Py_XDECREF(pathobj);
704 0 : Py_XDECREF(cpathobj);
705 0 : return m;
706 : }
707 :
708 : PyObject*
709 1 : PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
710 : PyObject *cpathname)
711 : {
712 1 : PyObject *modules = PyImport_GetModuleDict();
713 : PyObject *m, *d, *v;
714 :
715 1 : m = PyImport_AddModuleObject(name);
716 1 : if (m == NULL)
717 0 : return NULL;
718 : /* If the module is being reloaded, we get the old module back
719 : and re-use its dict to exec the new code. */
720 1 : d = PyModule_GetDict(m);
721 1 : if (PyDict_GetItemString(d, "__builtins__") == NULL) {
722 1 : if (PyDict_SetItemString(d, "__builtins__",
723 : PyEval_GetBuiltins()) != 0)
724 0 : goto error;
725 : }
726 1 : if (pathname != NULL) {
727 1 : v = pathname;
728 : }
729 : else {
730 0 : v = ((PyCodeObject *)co)->co_filename;
731 : }
732 1 : Py_INCREF(v);
733 1 : if (PyDict_SetItemString(d, "__file__", v) != 0)
734 0 : PyErr_Clear(); /* Not important enough to report */
735 1 : Py_DECREF(v);
736 :
737 : /* Remember the pyc path name as the __cached__ attribute. */
738 1 : if (cpathname != NULL)
739 0 : v = cpathname;
740 : else
741 1 : v = Py_None;
742 1 : if (PyDict_SetItemString(d, "__cached__", v) != 0)
743 0 : PyErr_Clear(); /* Not important enough to report */
744 :
745 1 : v = PyEval_EvalCode(co, d, d);
746 1 : if (v == NULL)
747 0 : goto error;
748 1 : Py_DECREF(v);
749 :
750 1 : if ((m = PyDict_GetItem(modules, name)) == NULL) {
751 0 : PyErr_Format(PyExc_ImportError,
752 : "Loaded module %R not found in sys.modules",
753 : name);
754 0 : return NULL;
755 : }
756 :
757 1 : Py_INCREF(m);
758 :
759 1 : return m;
760 :
761 : error:
762 0 : remove_module(name);
763 0 : return NULL;
764 : }
765 :
766 :
767 : static void
768 0 : update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
769 : {
770 : PyObject *constants, *tmp;
771 : Py_ssize_t i, n;
772 :
773 0 : if (PyUnicode_Compare(co->co_filename, oldname))
774 0 : return;
775 :
776 0 : tmp = co->co_filename;
777 0 : co->co_filename = newname;
778 0 : Py_INCREF(co->co_filename);
779 0 : Py_DECREF(tmp);
780 :
781 0 : constants = co->co_consts;
782 0 : n = PyTuple_GET_SIZE(constants);
783 0 : for (i = 0; i < n; i++) {
784 0 : tmp = PyTuple_GET_ITEM(constants, i);
785 0 : if (PyCode_Check(tmp))
786 0 : update_code_filenames((PyCodeObject *)tmp,
787 : oldname, newname);
788 : }
789 : }
790 :
791 : static void
792 45 : update_compiled_module(PyCodeObject *co, PyObject *newname)
793 : {
794 : PyObject *oldname;
795 :
796 45 : if (PyUnicode_Compare(co->co_filename, newname) == 0)
797 90 : return;
798 :
799 0 : oldname = co->co_filename;
800 0 : Py_INCREF(oldname);
801 0 : update_code_filenames(co, oldname, newname);
802 0 : Py_DECREF(oldname);
803 : }
804 :
805 : static PyObject *
806 45 : imp_fix_co_filename(PyObject *self, PyObject *args)
807 : {
808 : PyObject *co;
809 : PyObject *file_path;
810 :
811 45 : if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
812 0 : return NULL;
813 :
814 45 : if (!PyCode_Check(co)) {
815 0 : PyErr_SetString(PyExc_TypeError,
816 : "first argument must be a code object");
817 0 : return NULL;
818 : }
819 :
820 45 : if (!PyUnicode_Check(file_path)) {
821 0 : PyErr_SetString(PyExc_TypeError,
822 : "second argument must be a string");
823 0 : return NULL;
824 : }
825 :
826 45 : update_compiled_module((PyCodeObject*)co, file_path);
827 :
828 45 : Py_RETURN_NONE;
829 : }
830 :
831 :
832 : /* Forward */
833 : static struct _frozen * find_frozen(PyObject *);
834 :
835 :
836 : /* Helper to test for built-in module */
837 :
838 : static int
839 75 : is_builtin(PyObject *name)
840 : {
841 : int i, cmp;
842 2264 : for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
843 2199 : cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
844 2199 : if (cmp == 0) {
845 10 : if (PyImport_Inittab[i].initfunc == NULL)
846 0 : return -1;
847 : else
848 10 : return 1;
849 : }
850 : }
851 65 : return 0;
852 : }
853 :
854 :
855 : /* Return an importer object for a sys.path/pkg.__path__ item 'p',
856 : possibly by fetching it from the path_importer_cache dict. If it
857 : wasn't yet cached, traverse path_hooks until a hook is found
858 : that can handle the path item. Return None if no hook could;
859 : this tells our caller it should fall back to the builtin
860 : import mechanism. Cache the result in path_importer_cache.
861 : Returns a borrowed reference. */
862 :
863 : static PyObject *
864 0 : get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
865 : PyObject *p)
866 : {
867 : PyObject *importer;
868 : Py_ssize_t j, nhooks;
869 :
870 : /* These conditions are the caller's responsibility: */
871 : assert(PyList_Check(path_hooks));
872 : assert(PyDict_Check(path_importer_cache));
873 :
874 0 : nhooks = PyList_Size(path_hooks);
875 0 : if (nhooks < 0)
876 0 : return NULL; /* Shouldn't happen */
877 :
878 0 : importer = PyDict_GetItem(path_importer_cache, p);
879 0 : if (importer != NULL)
880 0 : return importer;
881 :
882 : /* set path_importer_cache[p] to None to avoid recursion */
883 0 : if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
884 0 : return NULL;
885 :
886 0 : for (j = 0; j < nhooks; j++) {
887 0 : PyObject *hook = PyList_GetItem(path_hooks, j);
888 0 : if (hook == NULL)
889 0 : return NULL;
890 0 : importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
891 0 : if (importer != NULL)
892 0 : break;
893 :
894 0 : if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
895 0 : return NULL;
896 : }
897 0 : PyErr_Clear();
898 : }
899 0 : if (importer == NULL) {
900 0 : return Py_None;
901 : }
902 0 : if (importer != NULL) {
903 0 : int err = PyDict_SetItem(path_importer_cache, p, importer);
904 0 : Py_DECREF(importer);
905 0 : if (err != 0)
906 0 : return NULL;
907 : }
908 0 : return importer;
909 : }
910 :
911 : PyAPI_FUNC(PyObject *)
912 0 : PyImport_GetImporter(PyObject *path) {
913 0 : PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
914 :
915 0 : if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
916 0 : if ((path_hooks = PySys_GetObject("path_hooks"))) {
917 0 : importer = get_path_importer(path_importer_cache,
918 : path_hooks, path);
919 : }
920 : }
921 0 : Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
922 0 : return importer;
923 : }
924 :
925 :
926 : static int init_builtin(PyObject *); /* Forward */
927 :
928 : /* Initialize a built-in module.
929 : Return 1 for success, 0 if the module is not found, and -1 with
930 : an exception set if the initialization failed. */
931 :
932 : static int
933 16 : init_builtin(PyObject *name)
934 : {
935 : struct _inittab *p;
936 :
937 16 : if (_PyImport_FindExtensionObject(name, name) != NULL)
938 0 : return 1;
939 :
940 199 : for (p = PyImport_Inittab; p->name != NULL; p++) {
941 : PyObject *mod;
942 : PyModuleDef *def;
943 199 : if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
944 16 : if (p->initfunc == NULL) {
945 0 : PyErr_Format(PyExc_ImportError,
946 : "Cannot re-init internal module %R",
947 : name);
948 0 : return -1;
949 : }
950 16 : mod = (*p->initfunc)();
951 16 : if (mod == 0)
952 0 : return -1;
953 : /* Remember pointer to module init function. */
954 16 : def = PyModule_GetDef(mod);
955 16 : def->m_base.m_init = p->initfunc;
956 16 : if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
957 0 : return -1;
958 : /* FixupExtension has put the module into sys.modules,
959 : so we can release our own reference. */
960 16 : Py_DECREF(mod);
961 16 : return 1;
962 : }
963 : }
964 0 : return 0;
965 : }
966 :
967 :
968 : /* Frozen modules */
969 :
970 : static struct _frozen *
971 71 : find_frozen(PyObject *name)
972 : {
973 : struct _frozen *p;
974 :
975 71 : if (name == NULL)
976 0 : return NULL;
977 :
978 351 : for (p = PyImport_FrozenModules; ; p++) {
979 351 : if (p->name == NULL)
980 70 : return NULL;
981 281 : if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
982 1 : break;
983 280 : }
984 1 : return p;
985 : }
986 :
987 : static PyObject *
988 0 : get_frozen_object(PyObject *name)
989 : {
990 0 : struct _frozen *p = find_frozen(name);
991 : int size;
992 :
993 0 : if (p == NULL) {
994 0 : PyErr_Format(PyExc_ImportError,
995 : "No such frozen object named %R",
996 : name);
997 0 : return NULL;
998 : }
999 0 : if (p->code == NULL) {
1000 0 : PyErr_Format(PyExc_ImportError,
1001 : "Excluded frozen object named %R",
1002 : name);
1003 0 : return NULL;
1004 : }
1005 0 : size = p->size;
1006 0 : if (size < 0)
1007 0 : size = -size;
1008 0 : return PyMarshal_ReadObjectFromString((char *)p->code, size);
1009 : }
1010 :
1011 : static PyObject *
1012 0 : is_frozen_package(PyObject *name)
1013 : {
1014 0 : struct _frozen *p = find_frozen(name);
1015 : int size;
1016 :
1017 0 : if (p == NULL) {
1018 0 : PyErr_Format(PyExc_ImportError,
1019 : "No such frozen object named %R",
1020 : name);
1021 0 : return NULL;
1022 : }
1023 :
1024 0 : size = p->size;
1025 :
1026 0 : if (size < 0)
1027 0 : Py_RETURN_TRUE;
1028 : else
1029 0 : Py_RETURN_FALSE;
1030 : }
1031 :
1032 :
1033 : /* Initialize a frozen module.
1034 : Return 1 for success, 0 if the module is not found, and -1 with
1035 : an exception set if the initialization failed.
1036 : This function is also used from frozenmain.c */
1037 :
1038 : int
1039 1 : PyImport_ImportFrozenModuleObject(PyObject *name)
1040 : {
1041 : struct _frozen *p;
1042 : PyObject *co, *m, *path;
1043 : int ispackage;
1044 : int size;
1045 :
1046 1 : p = find_frozen(name);
1047 :
1048 1 : if (p == NULL)
1049 0 : return 0;
1050 1 : if (p->code == NULL) {
1051 0 : PyErr_Format(PyExc_ImportError,
1052 : "Excluded frozen object named %R",
1053 : name);
1054 0 : return -1;
1055 : }
1056 1 : size = p->size;
1057 1 : ispackage = (size < 0);
1058 1 : if (ispackage)
1059 0 : size = -size;
1060 1 : co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1061 1 : if (co == NULL)
1062 0 : return -1;
1063 1 : if (!PyCode_Check(co)) {
1064 0 : PyErr_Format(PyExc_TypeError,
1065 : "frozen object %R is not a code object",
1066 : name);
1067 0 : goto err_return;
1068 : }
1069 1 : if (ispackage) {
1070 : /* Set __path__ to the package name */
1071 : PyObject *d, *l;
1072 : int err;
1073 0 : m = PyImport_AddModuleObject(name);
1074 0 : if (m == NULL)
1075 0 : goto err_return;
1076 0 : d = PyModule_GetDict(m);
1077 0 : l = PyList_New(1);
1078 0 : if (l == NULL) {
1079 0 : goto err_return;
1080 : }
1081 0 : Py_INCREF(name);
1082 0 : PyList_SET_ITEM(l, 0, name);
1083 0 : err = PyDict_SetItemString(d, "__path__", l);
1084 0 : Py_DECREF(l);
1085 0 : if (err != 0)
1086 0 : goto err_return;
1087 : }
1088 1 : path = PyUnicode_FromString("<frozen>");
1089 1 : if (path == NULL)
1090 0 : goto err_return;
1091 1 : m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1092 1 : Py_DECREF(path);
1093 1 : if (m == NULL)
1094 0 : goto err_return;
1095 1 : Py_DECREF(co);
1096 1 : Py_DECREF(m);
1097 1 : return 1;
1098 : err_return:
1099 0 : Py_DECREF(co);
1100 0 : return -1;
1101 : }
1102 :
1103 : int
1104 1 : PyImport_ImportFrozenModule(char *name)
1105 : {
1106 : PyObject *nameobj;
1107 : int ret;
1108 1 : nameobj = PyUnicode_InternFromString(name);
1109 1 : if (nameobj == NULL)
1110 0 : return -1;
1111 1 : ret = PyImport_ImportFrozenModuleObject(nameobj);
1112 1 : Py_DECREF(nameobj);
1113 1 : return ret;
1114 : }
1115 :
1116 :
1117 : /* Import a module, either built-in, frozen, or external, and return
1118 : its module object WITH INCREMENTED REFERENCE COUNT */
1119 :
1120 : PyObject *
1121 11 : PyImport_ImportModule(const char *name)
1122 : {
1123 : PyObject *pname;
1124 : PyObject *result;
1125 :
1126 11 : pname = PyUnicode_FromString(name);
1127 11 : if (pname == NULL)
1128 0 : return NULL;
1129 11 : result = PyImport_Import(pname);
1130 11 : Py_DECREF(pname);
1131 11 : return result;
1132 : }
1133 :
1134 : /* Import a module without blocking
1135 : *
1136 : * At first it tries to fetch the module from sys.modules. If the module was
1137 : * never loaded before it loads it with PyImport_ImportModule() unless another
1138 : * thread holds the import lock. In the latter case the function raises an
1139 : * ImportError instead of blocking.
1140 : *
1141 : * Returns the module object with incremented ref count.
1142 : */
1143 : PyObject *
1144 1 : PyImport_ImportModuleNoBlock(const char *name)
1145 : {
1146 1 : return PyImport_ImportModule(name);
1147 : }
1148 :
1149 :
1150 : /* Remove importlib frames from the traceback,
1151 : * except in Verbose mode. */
1152 : static void
1153 77 : remove_importlib_frames(void)
1154 : {
1155 77 : const char *importlib_filename = "<frozen importlib._bootstrap>";
1156 77 : const char *remove_frames = "_call_with_frames_removed";
1157 77 : int always_trim = 0;
1158 77 : int in_importlib = 0;
1159 : PyObject *exception, *value, *base_tb, *tb;
1160 77 : PyObject **prev_link, **outer_link = NULL;
1161 :
1162 : /* Synopsis: if it's an ImportError, we trim all importlib chunks
1163 : from the traceback. We always trim chunks
1164 : which end with a call to "_call_with_frames_removed". */
1165 :
1166 77 : PyErr_Fetch(&exception, &value, &base_tb);
1167 77 : if (!exception || Py_VerboseFlag)
1168 : goto done;
1169 77 : if (PyType_IsSubtype((PyTypeObject *) exception,
1170 : (PyTypeObject *) PyExc_ImportError))
1171 77 : always_trim = 1;
1172 :
1173 77 : prev_link = &base_tb;
1174 77 : tb = base_tb;
1175 486 : while (tb != NULL) {
1176 332 : PyTracebackObject *traceback = (PyTracebackObject *)tb;
1177 332 : PyObject *next = (PyObject *) traceback->tb_next;
1178 332 : PyFrameObject *frame = traceback->tb_frame;
1179 332 : PyCodeObject *code = frame->f_code;
1180 : int now_in_importlib;
1181 :
1182 : assert(PyTraceBack_Check(tb));
1183 664 : now_in_importlib = (PyUnicode_CompareWithASCIIString(
1184 : code->co_filename,
1185 332 : importlib_filename) == 0);
1186 332 : if (now_in_importlib && !in_importlib) {
1187 : /* This is the link to this chunk of importlib tracebacks */
1188 77 : outer_link = prev_link;
1189 : }
1190 332 : in_importlib = now_in_importlib;
1191 :
1192 332 : if (in_importlib &&
1193 0 : (always_trim ||
1194 0 : PyUnicode_CompareWithASCIIString(code->co_name,
1195 210 : remove_frames) == 0)) {
1196 210 : PyObject *tmp = *outer_link;
1197 210 : *outer_link = next;
1198 210 : Py_XINCREF(next);
1199 210 : Py_DECREF(tmp);
1200 210 : prev_link = outer_link;
1201 : }
1202 : else {
1203 122 : prev_link = (PyObject **) &traceback->tb_next;
1204 : }
1205 332 : tb = next;
1206 : }
1207 : done:
1208 77 : PyErr_Restore(exception, value, base_tb);
1209 77 : }
1210 :
1211 :
1212 : PyObject *
1213 332 : PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1214 : PyObject *locals, PyObject *given_fromlist,
1215 : int level)
1216 : {
1217 : _Py_IDENTIFIER(__import__);
1218 : _Py_IDENTIFIER(__initializing__);
1219 : _Py_IDENTIFIER(__package__);
1220 : _Py_IDENTIFIER(__path__);
1221 : _Py_IDENTIFIER(__name__);
1222 : _Py_IDENTIFIER(_find_and_load);
1223 : _Py_IDENTIFIER(_handle_fromlist);
1224 : _Py_IDENTIFIER(_lock_unlock_module);
1225 : _Py_static_string(single_dot, ".");
1226 332 : PyObject *abs_name = NULL;
1227 332 : PyObject *builtins_import = NULL;
1228 332 : PyObject *final_mod = NULL;
1229 332 : PyObject *mod = NULL;
1230 332 : PyObject *package = NULL;
1231 332 : PyObject *globals = NULL;
1232 332 : PyObject *fromlist = NULL;
1233 332 : PyInterpreterState *interp = PyThreadState_GET()->interp;
1234 :
1235 : /* Make sure to use default values so as to not have
1236 : PyObject_CallMethodObjArgs() truncate the parameter list because of a
1237 : NULL argument. */
1238 332 : if (given_globals == NULL) {
1239 77 : globals = PyDict_New();
1240 77 : if (globals == NULL) {
1241 0 : goto error;
1242 : }
1243 : }
1244 : else {
1245 : /* Only have to care what given_globals is if it will be used
1246 : for something. */
1247 255 : if (level > 0 && !PyDict_Check(given_globals)) {
1248 0 : PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1249 0 : goto error;
1250 : }
1251 255 : globals = given_globals;
1252 255 : Py_INCREF(globals);
1253 : }
1254 :
1255 332 : if (given_fromlist == NULL) {
1256 76 : fromlist = PyList_New(0);
1257 76 : if (fromlist == NULL) {
1258 0 : goto error;
1259 : }
1260 : }
1261 : else {
1262 256 : fromlist = given_fromlist;
1263 256 : Py_INCREF(fromlist);
1264 : }
1265 332 : if (name == NULL) {
1266 0 : PyErr_SetString(PyExc_ValueError, "Empty module name");
1267 0 : goto error;
1268 : }
1269 :
1270 : /* The below code is importlib.__import__() & _gcd_import(), ported to C
1271 : for added performance. */
1272 :
1273 332 : if (!PyUnicode_Check(name)) {
1274 0 : PyErr_SetString(PyExc_TypeError, "module name must be a string");
1275 0 : goto error;
1276 : }
1277 332 : else if (PyUnicode_READY(name) < 0) {
1278 0 : goto error;
1279 : }
1280 332 : if (level < 0) {
1281 0 : PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1282 0 : goto error;
1283 : }
1284 332 : else if (level > 0) {
1285 11 : package = _PyDict_GetItemId(globals, &PyId___package__);
1286 11 : if (package != NULL && package != Py_None) {
1287 11 : Py_INCREF(package);
1288 22 : if (!PyUnicode_Check(package)) {
1289 0 : PyErr_SetString(PyExc_TypeError, "package must be a string");
1290 0 : goto error;
1291 : }
1292 : }
1293 : else {
1294 0 : package = _PyDict_GetItemId(globals, &PyId___name__);
1295 0 : if (package == NULL) {
1296 0 : PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
1297 0 : goto error;
1298 : }
1299 0 : else if (!PyUnicode_Check(package)) {
1300 0 : PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1301 : }
1302 0 : Py_INCREF(package);
1303 :
1304 0 : if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
1305 0 : PyObject *partition = NULL;
1306 0 : PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1307 0 : if (borrowed_dot == NULL) {
1308 0 : goto error;
1309 : }
1310 0 : partition = PyUnicode_RPartition(package, borrowed_dot);
1311 0 : Py_DECREF(package);
1312 0 : if (partition == NULL) {
1313 0 : goto error;
1314 : }
1315 0 : package = PyTuple_GET_ITEM(partition, 0);
1316 0 : Py_INCREF(package);
1317 0 : Py_DECREF(partition);
1318 : }
1319 : }
1320 :
1321 11 : if (PyDict_GetItem(interp->modules, package) == NULL) {
1322 0 : PyErr_Format(PyExc_SystemError,
1323 : "Parent module %R not loaded, cannot perform relative "
1324 : "import", package);
1325 0 : goto error;
1326 : }
1327 : }
1328 : else { /* level == 0 */
1329 321 : if (PyUnicode_GET_LENGTH(name) == 0) {
1330 0 : PyErr_SetString(PyExc_ValueError, "Empty module name");
1331 0 : goto error;
1332 : }
1333 321 : package = Py_None;
1334 321 : Py_INCREF(package);
1335 : }
1336 :
1337 332 : if (level > 0) {
1338 11 : Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1339 11 : PyObject *base = NULL;
1340 11 : int level_up = 1;
1341 :
1342 11 : for (level_up = 1; level_up < level; level_up += 1) {
1343 0 : last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1344 0 : if (last_dot == -2) {
1345 0 : goto error;
1346 : }
1347 0 : else if (last_dot == -1) {
1348 0 : PyErr_SetString(PyExc_ValueError,
1349 : "attempted relative import beyond top-level "
1350 : "package");
1351 0 : goto error;
1352 : }
1353 : }
1354 11 : base = PyUnicode_Substring(package, 0, last_dot);
1355 11 : if (PyUnicode_GET_LENGTH(name) > 0) {
1356 10 : PyObject *borrowed_dot, *seq = NULL;
1357 :
1358 10 : borrowed_dot = _PyUnicode_FromId(&single_dot);
1359 10 : seq = PyTuple_Pack(2, base, name);
1360 10 : Py_DECREF(base);
1361 10 : if (borrowed_dot == NULL || seq == NULL) {
1362 : goto error;
1363 : }
1364 :
1365 10 : abs_name = PyUnicode_Join(borrowed_dot, seq);
1366 10 : Py_DECREF(seq);
1367 10 : if (abs_name == NULL) {
1368 0 : goto error;
1369 : }
1370 : }
1371 : else {
1372 1 : abs_name = base;
1373 : }
1374 : }
1375 : else {
1376 321 : abs_name = name;
1377 321 : Py_INCREF(abs_name);
1378 : }
1379 :
1380 : #ifdef WITH_THREAD
1381 332 : _PyImport_AcquireLock();
1382 : #endif
1383 : /* From this point forward, goto error_with_unlock! */
1384 332 : if (PyDict_Check(globals)) {
1385 332 : builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1386 : }
1387 332 : if (builtins_import == NULL) {
1388 332 : builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1389 332 : if (builtins_import == NULL) {
1390 0 : Py_FatalError("__import__ missing");
1391 : }
1392 : }
1393 332 : Py_INCREF(builtins_import);
1394 :
1395 332 : mod = PyDict_GetItem(interp->modules, abs_name);
1396 332 : if (mod == Py_None) {
1397 0 : PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1398 : "None in sys.modules", abs_name);
1399 0 : if (msg != NULL) {
1400 0 : PyErr_SetImportError(msg, abs_name, NULL);
1401 0 : Py_DECREF(msg);
1402 : }
1403 0 : mod = NULL;
1404 0 : goto error_with_unlock;
1405 : }
1406 332 : else if (mod != NULL) {
1407 : PyObject *value;
1408 195 : int initializing = 0;
1409 :
1410 195 : Py_INCREF(mod);
1411 : /* Optimization: only call _bootstrap._lock_unlock_module() if
1412 : __initializing__ is true.
1413 : NOTE: because of this, __initializing__ must be set *before*
1414 : stuffing the new module in sys.modules.
1415 : */
1416 195 : value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1417 195 : if (value == NULL)
1418 130 : PyErr_Clear();
1419 : else {
1420 65 : initializing = PyObject_IsTrue(value);
1421 65 : Py_DECREF(value);
1422 65 : if (initializing == -1)
1423 0 : PyErr_Clear();
1424 : }
1425 195 : if (initializing > 0) {
1426 : /* _bootstrap._lock_unlock_module() releases the import lock */
1427 7 : value = _PyObject_CallMethodObjIdArgs(interp->importlib,
1428 : &PyId__lock_unlock_module, abs_name,
1429 : NULL);
1430 7 : if (value == NULL)
1431 0 : goto error;
1432 7 : Py_DECREF(value);
1433 : }
1434 : else {
1435 : #ifdef WITH_THREAD
1436 188 : if (_PyImport_ReleaseLock() < 0) {
1437 0 : PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1438 0 : goto error;
1439 : }
1440 : #endif
1441 : }
1442 : }
1443 : else {
1444 : /* _bootstrap._find_and_load() releases the import lock */
1445 137 : mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
1446 : &PyId__find_and_load, abs_name,
1447 : builtins_import, NULL);
1448 137 : if (mod == NULL) {
1449 77 : goto error;
1450 : }
1451 : }
1452 : /* From now on we don't hold the import lock anymore. */
1453 :
1454 255 : if (PyObject_Not(fromlist)) {
1455 192 : if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1456 186 : PyObject *front = NULL;
1457 186 : PyObject *partition = NULL;
1458 186 : PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1459 :
1460 186 : if (borrowed_dot == NULL) {
1461 0 : goto error;
1462 : }
1463 :
1464 186 : partition = PyUnicode_Partition(name, borrowed_dot);
1465 186 : if (partition == NULL) {
1466 0 : goto error;
1467 : }
1468 :
1469 186 : if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1470 : /* No dot in module name, simple exit */
1471 180 : Py_DECREF(partition);
1472 180 : final_mod = mod;
1473 180 : Py_INCREF(mod);
1474 180 : goto error;
1475 : }
1476 :
1477 6 : front = PyTuple_GET_ITEM(partition, 0);
1478 6 : Py_INCREF(front);
1479 6 : Py_DECREF(partition);
1480 :
1481 6 : if (level == 0) {
1482 6 : final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
1483 6 : Py_DECREF(front);
1484 : }
1485 : else {
1486 0 : Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1487 0 : PyUnicode_GET_LENGTH(front);
1488 0 : Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
1489 0 : PyObject *to_return = PyUnicode_Substring(abs_name, 0,
1490 : abs_name_len - cut_off);
1491 0 : Py_DECREF(front);
1492 0 : if (to_return == NULL) {
1493 0 : goto error;
1494 : }
1495 :
1496 0 : final_mod = PyDict_GetItem(interp->modules, to_return);
1497 0 : if (final_mod == NULL) {
1498 0 : PyErr_Format(PyExc_KeyError,
1499 : "%R not in sys.modules as expected",
1500 : to_return);
1501 : }
1502 : else {
1503 0 : Py_INCREF(final_mod);
1504 : }
1505 0 : Py_DECREF(to_return);
1506 : }
1507 : }
1508 : else {
1509 0 : final_mod = mod;
1510 0 : Py_INCREF(mod);
1511 : }
1512 : }
1513 : else {
1514 69 : final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
1515 : &PyId__handle_fromlist, mod,
1516 : fromlist, builtins_import,
1517 : NULL);
1518 : }
1519 75 : goto error;
1520 :
1521 : error_with_unlock:
1522 : #ifdef WITH_THREAD
1523 0 : if (_PyImport_ReleaseLock() < 0) {
1524 0 : PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1525 : }
1526 : #endif
1527 : error:
1528 332 : Py_XDECREF(abs_name);
1529 332 : Py_XDECREF(builtins_import);
1530 332 : Py_XDECREF(mod);
1531 332 : Py_XDECREF(package);
1532 332 : Py_XDECREF(globals);
1533 332 : Py_XDECREF(fromlist);
1534 332 : if (final_mod == NULL)
1535 77 : remove_importlib_frames();
1536 332 : return final_mod;
1537 : }
1538 :
1539 : PyObject *
1540 10 : PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
1541 : PyObject *fromlist, int level)
1542 : {
1543 : PyObject *nameobj, *mod;
1544 10 : nameobj = PyUnicode_FromString(name);
1545 10 : if (nameobj == NULL)
1546 0 : return NULL;
1547 10 : mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1548 : fromlist, level);
1549 10 : Py_DECREF(nameobj);
1550 10 : return mod;
1551 : }
1552 :
1553 :
1554 : /* Re-import a module of any kind and return its module object, WITH
1555 : INCREMENTED REFERENCE COUNT */
1556 :
1557 : PyObject *
1558 0 : PyImport_ReloadModule(PyObject *m)
1559 : {
1560 : _Py_IDENTIFIER(reload);
1561 0 : PyObject *reloaded_module = NULL;
1562 0 : PyObject *modules = PyImport_GetModuleDict();
1563 0 : PyObject *imp = PyDict_GetItemString(modules, "imp");
1564 0 : if (imp == NULL) {
1565 0 : imp = PyImport_ImportModule("imp");
1566 0 : if (imp == NULL) {
1567 0 : return NULL;
1568 : }
1569 : }
1570 : else {
1571 0 : Py_INCREF(imp);
1572 : }
1573 :
1574 0 : reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1575 0 : Py_DECREF(imp);
1576 0 : return reloaded_module;
1577 : }
1578 :
1579 :
1580 : /* Higher-level import emulator which emulates the "import" statement
1581 : more accurately -- it invokes the __import__() function from the
1582 : builtins of the current globals. This means that the import is
1583 : done using whatever import hooks are installed in the current
1584 : environment.
1585 : A dummy list ["__doc__"] is passed as the 4th argument so that
1586 : e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
1587 : will return <module "gencache"> instead of <module "win32com">. */
1588 :
1589 : PyObject *
1590 11 : PyImport_Import(PyObject *module_name)
1591 : {
1592 : static PyObject *silly_list = NULL;
1593 : static PyObject *builtins_str = NULL;
1594 : static PyObject *import_str = NULL;
1595 11 : PyObject *globals = NULL;
1596 11 : PyObject *import = NULL;
1597 11 : PyObject *builtins = NULL;
1598 11 : PyObject *modules = NULL;
1599 11 : PyObject *r = NULL;
1600 :
1601 : /* Initialize constant string objects */
1602 11 : if (silly_list == NULL) {
1603 1 : import_str = PyUnicode_InternFromString("__import__");
1604 1 : if (import_str == NULL)
1605 0 : return NULL;
1606 1 : builtins_str = PyUnicode_InternFromString("__builtins__");
1607 1 : if (builtins_str == NULL)
1608 0 : return NULL;
1609 1 : silly_list = PyList_New(0);
1610 1 : if (silly_list == NULL)
1611 0 : return NULL;
1612 : }
1613 :
1614 : /* Get the builtins from current globals */
1615 11 : globals = PyEval_GetGlobals();
1616 11 : if (globals != NULL) {
1617 1 : Py_INCREF(globals);
1618 1 : builtins = PyObject_GetItem(globals, builtins_str);
1619 1 : if (builtins == NULL)
1620 0 : goto err;
1621 : }
1622 : else {
1623 : /* No globals -- use standard builtins, and fake globals */
1624 10 : builtins = PyImport_ImportModuleLevel("builtins",
1625 : NULL, NULL, NULL, 0);
1626 10 : if (builtins == NULL)
1627 0 : return NULL;
1628 10 : globals = Py_BuildValue("{OO}", builtins_str, builtins);
1629 10 : if (globals == NULL)
1630 0 : goto err;
1631 : }
1632 :
1633 : /* Get the __import__ function from the builtins */
1634 11 : if (PyDict_Check(builtins)) {
1635 1 : import = PyObject_GetItem(builtins, import_str);
1636 1 : if (import == NULL)
1637 0 : PyErr_SetObject(PyExc_KeyError, import_str);
1638 : }
1639 : else
1640 10 : import = PyObject_GetAttr(builtins, import_str);
1641 11 : if (import == NULL)
1642 0 : goto err;
1643 :
1644 : /* Call the __import__ function with the proper argument list
1645 : Always use absolute import here.
1646 : Calling for side-effect of import. */
1647 11 : r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1648 : globals, silly_list, 0, NULL);
1649 11 : if (r == NULL)
1650 0 : goto err;
1651 11 : Py_DECREF(r);
1652 :
1653 11 : modules = PyImport_GetModuleDict();
1654 11 : r = PyDict_GetItem(modules, module_name);
1655 11 : if (r != NULL)
1656 11 : Py_INCREF(r);
1657 :
1658 : err:
1659 11 : Py_XDECREF(globals);
1660 11 : Py_XDECREF(builtins);
1661 11 : Py_XDECREF(import);
1662 :
1663 11 : return r;
1664 : }
1665 :
1666 : static PyObject *
1667 2 : imp_extension_suffixes(PyObject *self, PyObject *noargs)
1668 : {
1669 : PyObject *list;
1670 : const char *suffix;
1671 2 : unsigned int index = 0;
1672 :
1673 2 : list = PyList_New(0);
1674 2 : if (list == NULL)
1675 0 : return NULL;
1676 : #ifdef HAVE_DYNAMIC_LOADING
1677 10 : while ((suffix = _PyImport_DynLoadFiletab[index])) {
1678 6 : PyObject *item = PyUnicode_FromString(suffix);
1679 6 : if (item == NULL) {
1680 0 : Py_DECREF(list);
1681 0 : return NULL;
1682 : }
1683 6 : if (PyList_Append(list, item) < 0) {
1684 0 : Py_DECREF(list);
1685 0 : Py_DECREF(item);
1686 0 : return NULL;
1687 : }
1688 6 : Py_DECREF(item);
1689 6 : index += 1;
1690 : }
1691 : #endif
1692 2 : return list;
1693 : }
1694 :
1695 : static PyObject *
1696 16 : imp_init_builtin(PyObject *self, PyObject *args)
1697 : {
1698 : PyObject *name;
1699 : int ret;
1700 : PyObject *m;
1701 16 : if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
1702 0 : return NULL;
1703 16 : ret = init_builtin(name);
1704 16 : if (ret < 0)
1705 0 : return NULL;
1706 16 : if (ret == 0) {
1707 0 : Py_INCREF(Py_None);
1708 0 : return Py_None;
1709 : }
1710 16 : m = PyImport_AddModuleObject(name);
1711 16 : Py_XINCREF(m);
1712 16 : return m;
1713 : }
1714 :
1715 : static PyObject *
1716 0 : imp_init_frozen(PyObject *self, PyObject *args)
1717 : {
1718 : PyObject *name;
1719 : int ret;
1720 : PyObject *m;
1721 0 : if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
1722 0 : return NULL;
1723 0 : ret = PyImport_ImportFrozenModuleObject(name);
1724 0 : if (ret < 0)
1725 0 : return NULL;
1726 0 : if (ret == 0) {
1727 0 : Py_INCREF(Py_None);
1728 0 : return Py_None;
1729 : }
1730 0 : m = PyImport_AddModuleObject(name);
1731 0 : Py_XINCREF(m);
1732 0 : return m;
1733 : }
1734 :
1735 : static PyObject *
1736 0 : imp_get_frozen_object(PyObject *self, PyObject *args)
1737 : {
1738 : PyObject *name;
1739 :
1740 0 : if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
1741 0 : return NULL;
1742 0 : return get_frozen_object(name);
1743 : }
1744 :
1745 : static PyObject *
1746 0 : imp_is_frozen_package(PyObject *self, PyObject *args)
1747 : {
1748 : PyObject *name;
1749 :
1750 0 : if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
1751 0 : return NULL;
1752 0 : return is_frozen_package(name);
1753 : }
1754 :
1755 : static PyObject *
1756 75 : imp_is_builtin(PyObject *self, PyObject *args)
1757 : {
1758 : PyObject *name;
1759 75 : if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
1760 0 : return NULL;
1761 75 : return PyLong_FromLong(is_builtin(name));
1762 : }
1763 :
1764 : static PyObject *
1765 70 : imp_is_frozen(PyObject *self, PyObject *args)
1766 : {
1767 : PyObject *name;
1768 : struct _frozen *p;
1769 70 : if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
1770 0 : return NULL;
1771 70 : p = find_frozen(name);
1772 70 : return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
1773 : }
1774 :
1775 : #ifdef HAVE_DYNAMIC_LOADING
1776 :
1777 : static PyObject *
1778 4 : imp_load_dynamic(PyObject *self, PyObject *args)
1779 : {
1780 4 : PyObject *name, *pathname, *fob = NULL, *mod;
1781 : FILE *fp;
1782 :
1783 4 : if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1784 : &name, PyUnicode_FSDecoder, &pathname, &fob))
1785 0 : return NULL;
1786 4 : if (fob != NULL) {
1787 0 : fp = _Py_fopen(pathname, "r");
1788 0 : if (fp == NULL) {
1789 0 : Py_DECREF(pathname);
1790 0 : if (!PyErr_Occurred())
1791 0 : PyErr_SetFromErrno(PyExc_IOError);
1792 0 : return NULL;
1793 : }
1794 : }
1795 : else
1796 4 : fp = NULL;
1797 4 : mod = _PyImport_LoadDynamicModule(name, pathname, fp);
1798 4 : Py_DECREF(pathname);
1799 4 : if (fp)
1800 0 : fclose(fp);
1801 4 : return mod;
1802 : }
1803 :
1804 : #endif /* HAVE_DYNAMIC_LOADING */
1805 :
1806 :
1807 : /* Doc strings */
1808 :
1809 : PyDoc_STRVAR(doc_imp,
1810 : "(Extremely) low-level import machinery bits as used by importlib and imp.");
1811 :
1812 : PyDoc_STRVAR(doc_extension_suffixes,
1813 : "extension_suffixes() -> list of strings\n\
1814 : Returns the list of file suffixes used to identify extension modules.");
1815 :
1816 : PyDoc_STRVAR(doc_lock_held,
1817 : "lock_held() -> boolean\n\
1818 : Return True if the import lock is currently held, else False.\n\
1819 : On platforms without threads, return False.");
1820 :
1821 : PyDoc_STRVAR(doc_acquire_lock,
1822 : "acquire_lock() -> None\n\
1823 : Acquires the interpreter's import lock for the current thread.\n\
1824 : This lock should be used by import hooks to ensure thread-safety\n\
1825 : when importing modules.\n\
1826 : On platforms without threads, this function does nothing.");
1827 :
1828 : PyDoc_STRVAR(doc_release_lock,
1829 : "release_lock() -> None\n\
1830 : Release the interpreter's import lock.\n\
1831 : On platforms without threads, this function does nothing.");
1832 :
1833 : static PyMethodDef imp_methods[] = {
1834 : {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1835 : doc_extension_suffixes},
1836 : {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1837 : {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1838 : {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
1839 : {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1840 : {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1841 : {"init_builtin", imp_init_builtin, METH_VARARGS},
1842 : {"init_frozen", imp_init_frozen, METH_VARARGS},
1843 : {"is_builtin", imp_is_builtin, METH_VARARGS},
1844 : {"is_frozen", imp_is_frozen, METH_VARARGS},
1845 : #ifdef HAVE_DYNAMIC_LOADING
1846 : {"load_dynamic", imp_load_dynamic, METH_VARARGS},
1847 : #endif
1848 : {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
1849 : {NULL, NULL} /* sentinel */
1850 : };
1851 :
1852 :
1853 : static struct PyModuleDef impmodule = {
1854 : PyModuleDef_HEAD_INIT,
1855 : "_imp",
1856 : doc_imp,
1857 : 0,
1858 : imp_methods,
1859 : NULL,
1860 : NULL,
1861 : NULL,
1862 : NULL
1863 : };
1864 :
1865 : PyMODINIT_FUNC
1866 1 : PyInit_imp(void)
1867 : {
1868 : PyObject *m, *d;
1869 :
1870 1 : m = PyModule_Create(&impmodule);
1871 1 : if (m == NULL)
1872 0 : goto failure;
1873 1 : d = PyModule_GetDict(m);
1874 1 : if (d == NULL)
1875 0 : goto failure;
1876 :
1877 1 : return m;
1878 : failure:
1879 0 : Py_XDECREF(m);
1880 0 : return NULL;
1881 : }
1882 :
1883 :
1884 : /* API for embedding applications that want to add their own entries
1885 : to the table of built-in modules. This should normally be called
1886 : *before* Py_Initialize(). When the table resize fails, -1 is
1887 : returned and the existing table is unchanged.
1888 :
1889 : After a similar function by Just van Rossum. */
1890 :
1891 : int
1892 1 : PyImport_ExtendInittab(struct _inittab *newtab)
1893 : {
1894 : static struct _inittab *our_copy = NULL;
1895 : struct _inittab *p;
1896 : int i, n;
1897 :
1898 : /* Count the number of entries in both tables */
1899 1 : for (n = 0; newtab[n].name != NULL; n++)
1900 : ;
1901 1 : if (n == 0)
1902 0 : return 0; /* Nothing to do */
1903 1 : for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1904 : ;
1905 :
1906 : /* Allocate new memory for the combined table */
1907 1 : p = our_copy;
1908 1 : PyMem_RESIZE(p, struct _inittab, i+n+1);
1909 1 : if (p == NULL)
1910 0 : return -1;
1911 :
1912 : /* Copy the tables into the new memory */
1913 1 : if (our_copy != PyImport_Inittab)
1914 1 : memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1915 1 : PyImport_Inittab = our_copy = p;
1916 1 : memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
1917 :
1918 1 : return 0;
1919 : }
1920 :
1921 : /* Shorthand to add a single entry given a name and a function */
1922 :
1923 : int
1924 1 : PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
1925 : {
1926 : struct _inittab newtab[2];
1927 :
1928 1 : memset(newtab, '\0', sizeof newtab);
1929 :
1930 1 : newtab[0].name = (char *)name;
1931 1 : newtab[0].initfunc = initfunc;
1932 :
1933 1 : return PyImport_ExtendInittab(newtab);
1934 : }
1935 :
1936 : #ifdef __cplusplus
1937 : }
1938 : #endif
|