comparison cos/python/Python/import.c @ 27:7f74363f4c82

Added some files for the python port
author windel
date Tue, 27 Dec 2011 18:59:02 +0100
parents
children
comparison
equal deleted inserted replaced
26:dcce92b1efbc 27:7f74363f4c82
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 "osdefs.h"
12 #include "importdl.h"
13
14 #ifdef HAVE_FCNTL_H
15 #include <fcntl.h>
16 #endif
17
18
19 /* Magic word to reject .pyc files generated by other Python versions.
20 It should change for each incompatible change to the bytecode.
21
22 The value of CR and LF is incorporated so if you ever read or write
23 a .pyc file in text mode the magic number will be wrong; also, the
24 Apple MPW compiler swaps their values, botching string constants.
25
26 The magic numbers must be spaced apart at least 2 values, as the
27 -U interpeter flag will cause MAGIC+1 being used. They have been
28 odd numbers for some time now.
29
30 There were a variety of old schemes for setting the magic number.
31 The current working scheme is to increment the previous value by
32 10.
33
34 Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic
35 number also includes a new "magic tag", i.e. a human readable string used
36 to represent the magic number in __pycache__ directories. When you change
37 the magic number, you must also set a new unique magic tag. Generally this
38 can be named after the Python major version of the magic number bump, but
39 it can really be anything, as long as it's different than anything else
40 that's come before. The tags are included in the following table, starting
41 with Python 3.2a0.
42
43 Known values:
44 Python 1.5: 20121
45 Python 1.5.1: 20121
46 Python 1.5.2: 20121
47 Python 1.6: 50428
48 Python 2.0: 50823
49 Python 2.0.1: 50823
50 Python 2.1: 60202
51 Python 2.1.1: 60202
52 Python 2.1.2: 60202
53 Python 2.2: 60717
54 Python 2.3a0: 62011
55 Python 2.3a0: 62021
56 Python 2.3a0: 62011 (!)
57 Python 2.4a0: 62041
58 Python 2.4a3: 62051
59 Python 2.4b1: 62061
60 Python 2.5a0: 62071
61 Python 2.5a0: 62081 (ast-branch)
62 Python 2.5a0: 62091 (with)
63 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
64 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
65 Python 2.5b3: 62111 (fix wrong code: x += yield)
66 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
67 storing constants that should have been removed)
68 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
69 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
70 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
71 Python 3000: 3000
72 3010 (removed UNARY_CONVERT)
73 3020 (added BUILD_SET)
74 3030 (added keyword-only parameters)
75 3040 (added signature annotations)
76 3050 (print becomes a function)
77 3060 (PEP 3115 metaclass syntax)
78 3061 (string literals become unicode)
79 3071 (PEP 3109 raise changes)
80 3081 (PEP 3137 make __file__ and __name__ unicode)
81 3091 (kill str8 interning)
82 3101 (merge from 2.6a0, see 62151)
83 3103 (__file__ points to source file)
84 Python 3.0a4: 3111 (WITH_CLEANUP optimization).
85 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
86 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
87 change LIST_APPEND and SET_ADD, add MAP_ADD)
88 Python 3.1a0: 3151 (optimize conditional branches:
89 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
90 Python 3.2a0: 3160 (add SETUP_WITH)
91 tag: cpython-32
92 Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR)
93 tag: cpython-32
94 Python 3.2a2 3180 (add DELETE_DEREF)
95 Python 3.3a0 3190 __class__ super closure changed
96 Python 3.3a0 3200 (__qualname__ added)
97 */
98
99 /* MAGIC must change whenever the bytecode emitted by the compiler may no
100 longer be understood by older implementations of the eval loop (usually
101 due to the addition of new opcodes)
102 TAG must change for each major Python release. The magic number will take
103 care of any bytecode changes that occur during development.
104 */
105 #define QUOTE(arg) #arg
106 #define STRIFY(name) QUOTE(name)
107 #define MAJOR STRIFY(PY_MAJOR_VERSION)
108 #define MINOR STRIFY(PY_MINOR_VERSION)
109 #define MAGIC (3200 | ((long)'\r'<<16) | ((long)'\n'<<24))
110 #define TAG "cpython-" MAJOR MINOR;
111 #define CACHEDIR "__pycache__"
112 /* Current magic word and string tag as globals. */
113 static long pyc_magic = MAGIC;
114 static const char *pyc_tag = TAG;
115 #undef QUOTE
116 #undef STRIFY
117 #undef MAJOR
118 #undef MINOR
119
120 /* See _PyImport_FixupExtensionObject() below */
121 static PyObject *extensions = NULL;
122
123 /* Function from Parser/tokenizer.c */
124 extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
125
126 /* This table is defined in config.c: */
127 extern struct _inittab _PyImport_Inittab[];
128
129 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
130
131 /* these tables define the module suffixes that Python recognizes */
132 struct filedescr * _PyImport_Filetab = NULL;
133
134 static const struct filedescr _PyImport_StandardFiletab[] = {
135 {".py", "U", PY_SOURCE},
136 {".pyc", "rb", PY_COMPILED},
137 {0, 0}
138 };
139
140 static PyObject *initstr = NULL;
141 _Py_IDENTIFIER(__path__);
142
143 /* Initialize things */
144
145 void
146 _PyImport_Init(void)
147 {
148 const struct filedescr *scan;
149 struct filedescr *filetab;
150 int countD = 0;
151 int countS = 0;
152
153 initstr = PyUnicode_InternFromString("__init__");
154 if (initstr == NULL)
155 Py_FatalError("Can't initialize import variables");
156
157 /* prepare _PyImport_Filetab: copy entries from
158 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
159 */
160 #ifdef HAVE_DYNAMIC_LOADING
161 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
162 ++countD;
163 #endif
164 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
165 ++countS;
166 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
167 if (filetab == NULL)
168 Py_FatalError("Can't initialize import file table.");
169 #ifdef HAVE_DYNAMIC_LOADING
170 memcpy(filetab, _PyImport_DynLoadFiletab,
171 countD * sizeof(struct filedescr));
172 #endif
173 memcpy(filetab + countD, _PyImport_StandardFiletab,
174 countS * sizeof(struct filedescr));
175 filetab[countD + countS].suffix = NULL;
176
177 _PyImport_Filetab = filetab;
178
179 if (Py_OptimizeFlag) {
180 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
181 for (; filetab->suffix != NULL; filetab++) {
182 if (strcmp(filetab->suffix, ".pyc") == 0)
183 filetab->suffix = ".pyo";
184 }
185 }
186 }
187
188 void
189 _PyImportHooks_Init(void)
190 {
191 PyObject *v, *path_hooks = NULL, *zimpimport;
192 int err = 0;
193
194 /* adding sys.path_hooks and sys.path_importer_cache, setting up
195 zipimport */
196 if (PyType_Ready(&PyNullImporter_Type) < 0)
197 goto error;
198
199 if (Py_VerboseFlag)
200 PySys_WriteStderr("# installing zipimport hook\n");
201
202 v = PyList_New(0);
203 if (v == NULL)
204 goto error;
205 err = PySys_SetObject("meta_path", v);
206 Py_DECREF(v);
207 if (err)
208 goto error;
209 v = PyDict_New();
210 if (v == NULL)
211 goto error;
212 err = PySys_SetObject("path_importer_cache", v);
213 Py_DECREF(v);
214 if (err)
215 goto error;
216 path_hooks = PyList_New(0);
217 if (path_hooks == NULL)
218 goto error;
219 err = PySys_SetObject("path_hooks", path_hooks);
220 if (err) {
221 error:
222 PyErr_Print();
223 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
224 "path_importer_cache, or NullImporter failed"
225 );
226 }
227
228 zimpimport = PyImport_ImportModule("zipimport");
229 if (zimpimport == NULL) {
230 PyErr_Clear(); /* No zip import module -- okay */
231 if (Py_VerboseFlag)
232 PySys_WriteStderr("# can't import zipimport\n");
233 }
234 else {
235 _Py_IDENTIFIER(zipimporter);
236 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
237 &PyId_zipimporter);
238 Py_DECREF(zimpimport);
239 if (zipimporter == NULL) {
240 PyErr_Clear(); /* No zipimporter object -- okay */
241 if (Py_VerboseFlag)
242 PySys_WriteStderr(
243 "# can't import zipimport.zipimporter\n");
244 }
245 else {
246 /* sys.path_hooks.append(zipimporter) */
247 err = PyList_Append(path_hooks, zipimporter);
248 Py_DECREF(zipimporter);
249 if (err)
250 goto error;
251 if (Py_VerboseFlag)
252 PySys_WriteStderr(
253 "# installed zipimport hook\n");
254 }
255 }
256 Py_DECREF(path_hooks);
257 }
258
259 /* Locking primitives to prevent parallel imports of the same module
260 in different threads to return with a partially loaded module.
261 These calls are serialized by the global interpreter lock. */
262
263 #ifdef WITH_THREAD
264
265 #include "pythread.h"
266
267 static PyThread_type_lock import_lock = 0;
268 static long import_lock_thread = -1;
269 static int import_lock_level = 0;
270
271 void
272 _PyImport_AcquireLock(void)
273 {
274 long me = PyThread_get_thread_ident();
275 if (me == -1)
276 return; /* Too bad */
277 if (import_lock == NULL) {
278 import_lock = PyThread_allocate_lock();
279 if (import_lock == NULL)
280 return; /* Nothing much we can do. */
281 }
282 if (import_lock_thread == me) {
283 import_lock_level++;
284 return;
285 }
286 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
287 {
288 PyThreadState *tstate = PyEval_SaveThread();
289 PyThread_acquire_lock(import_lock, 1);
290 PyEval_RestoreThread(tstate);
291 }
292 import_lock_thread = me;
293 import_lock_level = 1;
294 }
295
296 int
297 _PyImport_ReleaseLock(void)
298 {
299 long me = PyThread_get_thread_ident();
300 if (me == -1 || import_lock == NULL)
301 return 0; /* Too bad */
302 if (import_lock_thread != me)
303 return -1;
304 import_lock_level--;
305 if (import_lock_level == 0) {
306 import_lock_thread = -1;
307 PyThread_release_lock(import_lock);
308 }
309 return 1;
310 }
311
312 /* This function is called from PyOS_AfterFork to ensure that newly
313 created child processes do not share locks with the parent.
314 We now acquire the import lock around fork() calls but on some platforms
315 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
316
317 void
318 _PyImport_ReInitLock(void)
319 {
320 if (import_lock != NULL)
321 import_lock = PyThread_allocate_lock();
322 if (import_lock_level > 1) {
323 /* Forked as a side effect of import */
324 long me = PyThread_get_thread_ident();
325 PyThread_acquire_lock(import_lock, 0);
326 /* XXX: can the previous line fail? */
327 import_lock_thread = me;
328 import_lock_level--;
329 } else {
330 import_lock_thread = -1;
331 import_lock_level = 0;
332 }
333 }
334
335 #endif
336
337 static PyObject *
338 imp_lock_held(PyObject *self, PyObject *noargs)
339 {
340 #ifdef WITH_THREAD
341 return PyBool_FromLong(import_lock_thread != -1);
342 #else
343 return PyBool_FromLong(0);
344 #endif
345 }
346
347 static PyObject *
348 imp_acquire_lock(PyObject *self, PyObject *noargs)
349 {
350 #ifdef WITH_THREAD
351 _PyImport_AcquireLock();
352 #endif
353 Py_INCREF(Py_None);
354 return Py_None;
355 }
356
357 static PyObject *
358 imp_release_lock(PyObject *self, PyObject *noargs)
359 {
360 #ifdef WITH_THREAD
361 if (_PyImport_ReleaseLock() < 0) {
362 PyErr_SetString(PyExc_RuntimeError,
363 "not holding the import lock");
364 return NULL;
365 }
366 #endif
367 Py_INCREF(Py_None);
368 return Py_None;
369 }
370
371 void
372 _PyImport_Fini(void)
373 {
374 Py_XDECREF(extensions);
375 extensions = NULL;
376 PyMem_DEL(_PyImport_Filetab);
377 _PyImport_Filetab = NULL;
378 #ifdef WITH_THREAD
379 if (import_lock != NULL) {
380 PyThread_free_lock(import_lock);
381 import_lock = NULL;
382 }
383 #endif
384 }
385
386 static void
387 imp_modules_reloading_clear(void)
388 {
389 PyInterpreterState *interp = PyThreadState_Get()->interp;
390 if (interp->modules_reloading != NULL)
391 PyDict_Clear(interp->modules_reloading);
392 }
393
394 /* Helper for sys */
395
396 PyObject *
397 PyImport_GetModuleDict(void)
398 {
399 PyInterpreterState *interp = PyThreadState_GET()->interp;
400 if (interp->modules == NULL)
401 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
402 return interp->modules;
403 }
404
405
406 /* List of names to clear in sys */
407 static char* sys_deletes[] = {
408 "path", "argv", "ps1", "ps2",
409 "last_type", "last_value", "last_traceback",
410 "path_hooks", "path_importer_cache", "meta_path",
411 /* misc stuff */
412 "flags", "float_info",
413 NULL
414 };
415
416 static char* sys_files[] = {
417 "stdin", "__stdin__",
418 "stdout", "__stdout__",
419 "stderr", "__stderr__",
420 NULL
421 };
422
423
424 /* Un-initialize things, as good as we can */
425
426 void
427 PyImport_Cleanup(void)
428 {
429 Py_ssize_t pos, ndone;
430 PyObject *key, *value, *dict;
431 PyInterpreterState *interp = PyThreadState_GET()->interp;
432 PyObject *modules = interp->modules;
433
434 if (modules == NULL)
435 return; /* Already done */
436
437 /* Delete some special variables first. These are common
438 places where user values hide and people complain when their
439 destructors fail. Since the modules containing them are
440 deleted *last* of all, they would come too late in the normal
441 destruction order. Sigh. */
442
443 value = PyDict_GetItemString(modules, "builtins");
444 if (value != NULL && PyModule_Check(value)) {
445 dict = PyModule_GetDict(value);
446 if (Py_VerboseFlag)
447 PySys_WriteStderr("# clear builtins._\n");
448 PyDict_SetItemString(dict, "_", Py_None);
449 }
450 value = PyDict_GetItemString(modules, "sys");
451 if (value != NULL && PyModule_Check(value)) {
452 char **p;
453 PyObject *v;
454 dict = PyModule_GetDict(value);
455 for (p = sys_deletes; *p != NULL; p++) {
456 if (Py_VerboseFlag)
457 PySys_WriteStderr("# clear sys.%s\n", *p);
458 PyDict_SetItemString(dict, *p, Py_None);
459 }
460 for (p = sys_files; *p != NULL; p+=2) {
461 if (Py_VerboseFlag)
462 PySys_WriteStderr("# restore sys.%s\n", *p);
463 v = PyDict_GetItemString(dict, *(p+1));
464 if (v == NULL)
465 v = Py_None;
466 PyDict_SetItemString(dict, *p, v);
467 }
468 }
469
470 /* First, delete __main__ */
471 value = PyDict_GetItemString(modules, "__main__");
472 if (value != NULL && PyModule_Check(value)) {
473 if (Py_VerboseFlag)
474 PySys_WriteStderr("# cleanup __main__\n");
475 _PyModule_Clear(value);
476 PyDict_SetItemString(modules, "__main__", Py_None);
477 }
478
479 /* The special treatment of "builtins" here is because even
480 when it's not referenced as a module, its dictionary is
481 referenced by almost every module's __builtins__. Since
482 deleting a module clears its dictionary (even if there are
483 references left to it), we need to delete the "builtins"
484 module last. Likewise, we don't delete sys until the very
485 end because it is implicitly referenced (e.g. by print).
486
487 Also note that we 'delete' modules by replacing their entry
488 in the modules dict with None, rather than really deleting
489 them; this avoids a rehash of the modules dictionary and
490 also marks them as "non existent" so they won't be
491 re-imported. */
492
493 /* Next, repeatedly delete modules with a reference count of
494 one (skipping builtins and sys) and delete them */
495 do {
496 ndone = 0;
497 pos = 0;
498 while (PyDict_Next(modules, &pos, &key, &value)) {
499 if (value->ob_refcnt != 1)
500 continue;
501 if (PyUnicode_Check(key) && PyModule_Check(value)) {
502 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
503 continue;
504 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
505 continue;
506 if (Py_VerboseFlag)
507 PySys_FormatStderr(
508 "# cleanup[1] %U\n", key);
509 _PyModule_Clear(value);
510 PyDict_SetItem(modules, key, Py_None);
511 ndone++;
512 }
513 }
514 } while (ndone > 0);
515
516 /* Next, delete all modules (still skipping builtins and sys) */
517 pos = 0;
518 while (PyDict_Next(modules, &pos, &key, &value)) {
519 if (PyUnicode_Check(key) && PyModule_Check(value)) {
520 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
521 continue;
522 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
523 continue;
524 if (Py_VerboseFlag)
525 PySys_FormatStderr("# cleanup[2] %U\n", key);
526 _PyModule_Clear(value);
527 PyDict_SetItem(modules, key, Py_None);
528 }
529 }
530
531 /* Next, delete sys and builtins (in that order) */
532 value = PyDict_GetItemString(modules, "sys");
533 if (value != NULL && PyModule_Check(value)) {
534 if (Py_VerboseFlag)
535 PySys_WriteStderr("# cleanup sys\n");
536 _PyModule_Clear(value);
537 PyDict_SetItemString(modules, "sys", Py_None);
538 }
539 value = PyDict_GetItemString(modules, "builtins");
540 if (value != NULL && PyModule_Check(value)) {
541 if (Py_VerboseFlag)
542 PySys_WriteStderr("# cleanup builtins\n");
543 _PyModule_Clear(value);
544 PyDict_SetItemString(modules, "builtins", Py_None);
545 }
546
547 /* Finally, clear and delete the modules directory */
548 PyDict_Clear(modules);
549 interp->modules = NULL;
550 Py_DECREF(modules);
551 Py_CLEAR(interp->modules_reloading);
552 }
553
554
555 /* Helper for pythonrun.c -- return magic number and tag. */
556
557 long
558 PyImport_GetMagicNumber(void)
559 {
560 return pyc_magic;
561 }
562
563
564 const char *
565 PyImport_GetMagicTag(void)
566 {
567 return pyc_tag;
568 }
569
570 /* Magic for extension modules (built-in as well as dynamically
571 loaded). To prevent initializing an extension module more than
572 once, we keep a static dictionary 'extensions' keyed by module name
573 (for built-in modules) or by filename (for dynamically loaded
574 modules), containing these modules. A copy of the module's
575 dictionary is stored by calling _PyImport_FixupExtensionObject()
576 immediately after the module initialization function succeeds. A
577 copy can be retrieved from there by calling
578 _PyImport_FindExtensionObject().
579
580 Modules which do support multiple initialization set their m_size
581 field to a non-negative number (indicating the size of the
582 module-specific state). They are still recorded in the extensions
583 dictionary, to avoid loading shared libraries twice.
584 */
585
586 int
587 _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
588 PyObject *filename)
589 {
590 PyObject *modules, *dict;
591 struct PyModuleDef *def;
592 if (extensions == NULL) {
593 extensions = PyDict_New();
594 if (extensions == NULL)
595 return -1;
596 }
597 if (mod == NULL || !PyModule_Check(mod)) {
598 PyErr_BadInternalCall();
599 return -1;
600 }
601 def = PyModule_GetDef(mod);
602 if (!def) {
603 PyErr_BadInternalCall();
604 return -1;
605 }
606 modules = PyImport_GetModuleDict();
607 if (PyDict_SetItem(modules, name, mod) < 0)
608 return -1;
609 if (_PyState_AddModule(mod, def) < 0) {
610 PyDict_DelItem(modules, name);
611 return -1;
612 }
613 if (def->m_size == -1) {
614 if (def->m_base.m_copy) {
615 /* Somebody already imported the module,
616 likely under a different name.
617 XXX this should really not happen. */
618 Py_DECREF(def->m_base.m_copy);
619 def->m_base.m_copy = NULL;
620 }
621 dict = PyModule_GetDict(mod);
622 if (dict == NULL)
623 return -1;
624 def->m_base.m_copy = PyDict_Copy(dict);
625 if (def->m_base.m_copy == NULL)
626 return -1;
627 }
628 PyDict_SetItem(extensions, filename, (PyObject*)def);
629 return 0;
630 }
631
632 int
633 _PyImport_FixupBuiltin(PyObject *mod, char *name)
634 {
635 int res;
636 PyObject *nameobj;
637 nameobj = PyUnicode_InternFromString(name);
638 if (nameobj == NULL)
639 return -1;
640 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
641 Py_DECREF(nameobj);
642 return res;
643 }
644
645 PyObject *
646 _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
647 {
648 PyObject *mod, *mdict;
649 PyModuleDef* def;
650 if (extensions == NULL)
651 return NULL;
652 def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
653 if (def == NULL)
654 return NULL;
655 if (def->m_size == -1) {
656 /* Module does not support repeated initialization */
657 if (def->m_base.m_copy == NULL)
658 return NULL;
659 mod = PyImport_AddModuleObject(name);
660 if (mod == NULL)
661 return NULL;
662 mdict = PyModule_GetDict(mod);
663 if (mdict == NULL)
664 return NULL;
665 if (PyDict_Update(mdict, def->m_base.m_copy))
666 return NULL;
667 }
668 else {
669 if (def->m_base.m_init == NULL)
670 return NULL;
671 mod = def->m_base.m_init();
672 if (mod == NULL)
673 return NULL;
674 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
675 Py_DECREF(mod);
676 }
677 if (_PyState_AddModule(mod, def) < 0) {
678 PyDict_DelItem(PyImport_GetModuleDict(), name);
679 Py_DECREF(mod);
680 return NULL;
681 }
682 if (Py_VerboseFlag)
683 PySys_FormatStderr("import %U # previously loaded (%R)\n",
684 name, filename);
685 return mod;
686
687 }
688
689 PyObject *
690 _PyImport_FindBuiltin(const char *name)
691 {
692 PyObject *res, *nameobj;
693 nameobj = PyUnicode_InternFromString(name);
694 if (nameobj == NULL)
695 return NULL;
696 res = _PyImport_FindExtensionObject(nameobj, nameobj);
697 Py_DECREF(nameobj);
698 return res;
699 }
700
701 /* Get the module object corresponding to a module name.
702 First check the modules dictionary if there's one there,
703 if not, create a new one and insert it in the modules dictionary.
704 Because the former action is most common, THIS DOES NOT RETURN A
705 'NEW' REFERENCE! */
706
707 PyObject *
708 PyImport_AddModuleObject(PyObject *name)
709 {
710 PyObject *modules = PyImport_GetModuleDict();
711 PyObject *m;
712
713 if ((m = PyDict_GetItem(modules, name)) != NULL &&
714 PyModule_Check(m))
715 return m;
716 m = PyModule_NewObject(name);
717 if (m == NULL)
718 return NULL;
719 if (PyDict_SetItem(modules, name, m) != 0) {
720 Py_DECREF(m);
721 return NULL;
722 }
723 Py_DECREF(m); /* Yes, it still exists, in modules! */
724
725 return m;
726 }
727
728 PyObject *
729 PyImport_AddModule(const char *name)
730 {
731 PyObject *nameobj, *module;
732 nameobj = PyUnicode_FromString(name);
733 if (nameobj == NULL)
734 return NULL;
735 module = PyImport_AddModuleObject(nameobj);
736 Py_DECREF(nameobj);
737 return module;
738 }
739
740
741 /* Remove name from sys.modules, if it's there. */
742 static void
743 remove_module(PyObject *name)
744 {
745 PyObject *modules = PyImport_GetModuleDict();
746 if (PyDict_GetItem(modules, name) == NULL)
747 return;
748 if (PyDict_DelItem(modules, name) < 0)
749 Py_FatalError("import: deleting existing key in"
750 "sys.modules failed");
751 }
752
753 static PyObject * get_sourcefile(PyObject *filename);
754 static PyObject *make_source_pathname(PyObject *pathname);
755 static PyObject* make_compiled_pathname(PyObject *pathname, int debug);
756
757 /* Execute a code object in a module and return the module object
758 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
759 * removed from sys.modules, to avoid leaving damaged module objects
760 * in sys.modules. The caller may wish to restore the original
761 * module object (if any) in this case; PyImport_ReloadModule is an
762 * example.
763 *
764 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
765 * interface. The other two exist primarily for backward compatibility.
766 */
767 PyObject *
768 PyImport_ExecCodeModule(char *name, PyObject *co)
769 {
770 return PyImport_ExecCodeModuleWithPathnames(
771 name, co, (char *)NULL, (char *)NULL);
772 }
773
774 PyObject *
775 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
776 {
777 return PyImport_ExecCodeModuleWithPathnames(
778 name, co, pathname, (char *)NULL);
779 }
780
781 PyObject *
782 PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
783 char *cpathname)
784 {
785 PyObject *m = NULL;
786 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
787
788 nameobj = PyUnicode_FromString(name);
789 if (nameobj == NULL)
790 return NULL;
791
792 if (pathname != NULL) {
793 pathobj = PyUnicode_DecodeFSDefault(pathname);
794 if (pathobj == NULL)
795 goto error;
796 } else
797 pathobj = NULL;
798 if (cpathname != NULL) {
799 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
800 if (cpathobj == NULL)
801 goto error;
802 } else
803 cpathobj = NULL;
804 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
805 error:
806 Py_DECREF(nameobj);
807 Py_XDECREF(pathobj);
808 Py_XDECREF(cpathobj);
809 return m;
810 }
811
812 PyObject*
813 PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
814 PyObject *cpathname)
815 {
816 PyObject *modules = PyImport_GetModuleDict();
817 PyObject *m, *d, *v;
818
819 m = PyImport_AddModuleObject(name);
820 if (m == NULL)
821 return NULL;
822 /* If the module is being reloaded, we get the old module back
823 and re-use its dict to exec the new code. */
824 d = PyModule_GetDict(m);
825 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
826 if (PyDict_SetItemString(d, "__builtins__",
827 PyEval_GetBuiltins()) != 0)
828 goto error;
829 }
830 /* Remember the filename as the __file__ attribute */
831 if (pathname != NULL) {
832 v = get_sourcefile(pathname);
833 if (v == NULL)
834 PyErr_Clear();
835 }
836 else
837 v = NULL;
838 if (v == NULL) {
839 v = ((PyCodeObject *)co)->co_filename;
840 Py_INCREF(v);
841 }
842 if (PyDict_SetItemString(d, "__file__", v) != 0)
843 PyErr_Clear(); /* Not important enough to report */
844 Py_DECREF(v);
845
846 /* Remember the pyc path name as the __cached__ attribute. */
847 if (cpathname != NULL)
848 v = cpathname;
849 else
850 v = Py_None;
851 if (PyDict_SetItemString(d, "__cached__", v) != 0)
852 PyErr_Clear(); /* Not important enough to report */
853
854 v = PyEval_EvalCode(co, d, d);
855 if (v == NULL)
856 goto error;
857 Py_DECREF(v);
858
859 if ((m = PyDict_GetItem(modules, name)) == NULL) {
860 PyErr_Format(PyExc_ImportError,
861 "Loaded module %R not found in sys.modules",
862 name);
863 return NULL;
864 }
865
866 Py_INCREF(m);
867
868 return m;
869
870 error:
871 remove_module(name);
872 return NULL;
873 }
874
875
876 /* Like strrchr(string, '/') but searches for the rightmost of either SEP
877 or ALTSEP, if the latter is defined.
878 */
879 static Py_UCS4*
880 rightmost_sep(Py_UCS4 *s)
881 {
882 Py_UCS4 *found, c;
883 for (found = NULL; (c = *s); s++) {
884 if (c == SEP
885 #ifdef ALTSEP
886 || c == ALTSEP
887 #endif
888 )
889 {
890 found = s;
891 }
892 }
893 return found;
894 }
895
896 /* Like rightmost_sep, but operate on unicode objects. */
897 static Py_ssize_t
898 rightmost_sep_obj(PyObject* o, Py_ssize_t start, Py_ssize_t end)
899 {
900 Py_ssize_t found, i;
901 Py_UCS4 c;
902 for (found = -1, i = start; i < end; i++) {
903 c = PyUnicode_READ_CHAR(o, i);
904 if (c == SEP
905 #ifdef ALTSEP
906 || c == ALTSEP
907 #endif
908 )
909 {
910 found = i;
911 }
912 }
913 return found;
914 }
915
916 /* Given a pathname for a Python source file, fill a buffer with the
917 pathname for the corresponding compiled file. Return the pathname
918 for the compiled file, or NULL if there's no space in the buffer.
919 Doesn't set an exception.
920
921 foo.py -> __pycache__/foo.<tag>.pyc
922
923 pathstr is assumed to be "ready".
924 */
925
926 static PyObject*
927 make_compiled_pathname(PyObject *pathstr, int debug)
928 {
929 PyObject *result;
930 Py_ssize_t fname, ext, len, i, pos, taglen;
931 Py_ssize_t pycache_len = sizeof(CACHEDIR) - 1;
932 int kind;
933 void *data;
934 Py_UCS4 lastsep;
935
936 /* Compute the output string size. */
937 len = PyUnicode_GET_LENGTH(pathstr);
938 /* If there is no separator, this returns -1, so
939 fname will be 0. */
940 fname = rightmost_sep_obj(pathstr, 0, len) + 1;
941 /* Windows: re-use the last separator character (/ or \\) when
942 appending the __pycache__ path. */
943 if (fname > 0)
944 lastsep = PyUnicode_READ_CHAR(pathstr, fname -1);
945 else
946 lastsep = SEP;
947 ext = fname - 1;
948 for(i = fname; i < len; i++)
949 if (PyUnicode_READ_CHAR(pathstr, i) == '.')
950 ext = i + 1;
951 if (ext < fname)
952 /* No dot in filename; use entire filename */
953 ext = len;
954
955 /* result = pathstr[:fname] + "__pycache__" + SEP +
956 pathstr[fname:ext] + tag + ".py[co]" */
957 taglen = strlen(pyc_tag);
958 result = PyUnicode_New(ext + pycache_len + 1 + taglen + 4,
959 PyUnicode_MAX_CHAR_VALUE(pathstr));
960 if (!result)
961 return NULL;
962 kind = PyUnicode_KIND(result);
963 data = PyUnicode_DATA(result);
964 PyUnicode_CopyCharacters(result, 0, pathstr, 0, fname);
965 pos = fname;
966 for (i = 0; i < pycache_len; i++)
967 PyUnicode_WRITE(kind, data, pos++, CACHEDIR[i]);
968 PyUnicode_WRITE(kind, data, pos++, lastsep);
969 PyUnicode_CopyCharacters(result, pos, pathstr,
970 fname, ext - fname);
971 pos += ext - fname;
972 for (i = 0; pyc_tag[i]; i++)
973 PyUnicode_WRITE(kind, data, pos++, pyc_tag[i]);
974 PyUnicode_WRITE(kind, data, pos++, '.');
975 PyUnicode_WRITE(kind, data, pos++, 'p');
976 PyUnicode_WRITE(kind, data, pos++, 'y');
977 PyUnicode_WRITE(kind, data, pos++, debug ? 'c' : 'o');
978 return result;
979 }
980
981
982 /* Given a pathname to a Python byte compiled file, return the path to the
983 source file, if the path matches the PEP 3147 format. This does not check
984 for any file existence, however, if the pyc file name does not match PEP
985 3147 style, NULL is returned. buf must be at least as big as pathname;
986 the resulting path will always be shorter.
987
988 (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */
989
990 static PyObject*
991 make_source_pathname(PyObject *path)
992 {
993 Py_ssize_t left, right, dot0, dot1, len;
994 Py_ssize_t i, j;
995 PyObject *result;
996 int kind;
997 void *data;
998
999 len = PyUnicode_GET_LENGTH(path);
1000 if (len > MAXPATHLEN)
1001 return NULL;
1002
1003 /* Look back two slashes from the end. In between these two slashes
1004 must be the string __pycache__ or this is not a PEP 3147 style
1005 path. It's possible for there to be only one slash.
1006 */
1007 right = rightmost_sep_obj(path, 0, len);
1008 if (right == -1)
1009 return NULL;
1010 left = rightmost_sep_obj(path, 0, right);
1011 if (left == -1)
1012 left = 0;
1013 else
1014 left++;
1015 if (right-left != sizeof(CACHEDIR)-1)
1016 return NULL;
1017 for (i = 0; i < sizeof(CACHEDIR)-1; i++)
1018 if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i])
1019 return NULL;
1020
1021 /* Now verify that the path component to the right of the last slash
1022 has two dots in it.
1023 */
1024 dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1);
1025 if (dot0 < 0)
1026 return NULL;
1027 dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1);
1028 if (dot1 < 0)
1029 return NULL;
1030 /* Too many dots? */
1031 if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1)
1032 return NULL;
1033
1034 /* This is a PEP 3147 path. Start by copying everything from the
1035 start of pathname up to and including the leftmost slash. Then
1036 copy the file's basename, removing the magic tag and adding a .py
1037 suffix.
1038 */
1039 result = PyUnicode_New(left + (dot0-right) + 2,
1040 PyUnicode_MAX_CHAR_VALUE(path));
1041 if (!result)
1042 return NULL;
1043 kind = PyUnicode_KIND(result);
1044 data = PyUnicode_DATA(result);
1045 PyUnicode_CopyCharacters(result, 0, path, 0, (i = left));
1046 PyUnicode_CopyCharacters(result, left, path, right+1,
1047 (j = dot0-right));
1048 PyUnicode_WRITE(kind, data, i+j, 'p');
1049 PyUnicode_WRITE(kind, data, i+j+1, 'y');
1050 return result;
1051 }
1052
1053 /* Given a pathname for a Python source file, its time of last
1054 modification, and a pathname for a compiled file, check whether the
1055 compiled file represents the same version of the source. If so,
1056 return a FILE pointer for the compiled file, positioned just after
1057 the header; if not, return NULL.
1058 Doesn't set an exception. */
1059
1060 static FILE *
1061 check_compiled_module(PyObject *pathname, time_t mtime, PyObject *cpathname)
1062 {
1063 FILE *fp;
1064 long magic;
1065 long pyc_mtime;
1066
1067 fp = _Py_fopen(cpathname, "rb");
1068 if (fp == NULL)
1069 return NULL;
1070 magic = PyMarshal_ReadLongFromFile(fp);
1071 if (magic != pyc_magic) {
1072 if (Py_VerboseFlag)
1073 PySys_FormatStderr("# %R has bad magic\n", cpathname);
1074 fclose(fp);
1075 return NULL;
1076 }
1077 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
1078 if (pyc_mtime != mtime) {
1079 if (Py_VerboseFlag)
1080 PySys_FormatStderr("# %R has bad mtime\n", cpathname);
1081 fclose(fp);
1082 return NULL;
1083 }
1084 if (Py_VerboseFlag)
1085 PySys_FormatStderr("# %R matches %R\n", cpathname, pathname);
1086 return fp;
1087 }
1088
1089
1090 /* Read a code object from a file and check it for validity */
1091
1092 static PyCodeObject *
1093 read_compiled_module(PyObject *cpathname, FILE *fp)
1094 {
1095 PyObject *co;
1096
1097 co = PyMarshal_ReadLastObjectFromFile(fp);
1098 if (co == NULL)
1099 return NULL;
1100 if (!PyCode_Check(co)) {
1101 PyErr_Format(PyExc_ImportError,
1102 "Non-code object in %R", cpathname);
1103 Py_DECREF(co);
1104 return NULL;
1105 }
1106 return (PyCodeObject *)co;
1107 }
1108
1109
1110 /* Load a module from a compiled file, execute it, and return its
1111 module object WITH INCREMENTED REFERENCE COUNT */
1112
1113 static PyObject *
1114 load_compiled_module(PyObject *name, PyObject *cpathname, FILE *fp)
1115 {
1116 long magic;
1117 PyCodeObject *co;
1118 PyObject *m;
1119
1120 magic = PyMarshal_ReadLongFromFile(fp);
1121 if (magic != pyc_magic) {
1122 PyErr_Format(PyExc_ImportError,
1123 "Bad magic number in %R", cpathname);
1124 return NULL;
1125 }
1126 (void) PyMarshal_ReadLongFromFile(fp);
1127 co = read_compiled_module(cpathname, fp);
1128 if (co == NULL)
1129 return NULL;
1130 if (Py_VerboseFlag)
1131 PySys_FormatStderr("import %U # precompiled from %R\n",
1132 name, cpathname);
1133 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1134 cpathname, cpathname);
1135 Py_DECREF(co);
1136
1137 return m;
1138 }
1139
1140 /* Parse a source file and return the corresponding code object */
1141
1142 static PyCodeObject *
1143 parse_source_module(PyObject *pathname, FILE *fp)
1144 {
1145 PyCodeObject *co;
1146 PyObject *pathbytes;
1147 mod_ty mod;
1148 PyCompilerFlags flags;
1149 PyArena *arena;
1150
1151 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1152 if (pathbytes == NULL)
1153 return NULL;
1154
1155 arena = PyArena_New();
1156 if (arena == NULL) {
1157 Py_DECREF(pathbytes);
1158 return NULL;
1159 }
1160
1161 flags.cf_flags = 0;
1162 mod = PyParser_ASTFromFile(fp, PyBytes_AS_STRING(pathbytes), NULL,
1163 Py_file_input, 0, 0, &flags,
1164 NULL, arena);
1165 if (mod != NULL)
1166 co = PyAST_Compile(mod, PyBytes_AS_STRING(pathbytes), NULL, arena);
1167 else
1168 co = NULL;
1169 Py_DECREF(pathbytes);
1170 PyArena_Free(arena);
1171 return co;
1172 }
1173
1174 /* Write a compiled module to a file, placing the time of last
1175 modification of its source into the header.
1176 Errors are ignored, if a write error occurs an attempt is made to
1177 remove the file. */
1178
1179 static void
1180 write_compiled_module(PyCodeObject *co, PyObject *cpathname,
1181 struct stat *srcstat)
1182 {
1183 Py_UCS4 *cpathname_ucs4;
1184 FILE *fp;
1185 time_t mtime = srcstat->st_mtime;
1186 PyObject *cpathname_tmp;
1187 mode_t dirmode = (srcstat->st_mode |
1188 S_IXUSR | S_IXGRP | S_IXOTH |
1189 S_IWUSR | S_IWGRP | S_IWOTH);
1190 PyObject *dirbytes;
1191 PyObject *cpathbytes, *cpathbytes_tmp;
1192 int fd;
1193 PyObject *dirname;
1194 Py_UCS4 *dirsep;
1195 int res, ok;
1196
1197 /* Ensure that the __pycache__ directory exists. */
1198 cpathname_ucs4 = PyUnicode_AsUCS4Copy(cpathname);
1199 if (!cpathname_ucs4)
1200 return;
1201 dirsep = rightmost_sep(cpathname_ucs4);
1202 if (dirsep == NULL) {
1203 if (Py_VerboseFlag)
1204 PySys_FormatStderr("# no %s path found %R\n", CACHEDIR, cpathname);
1205 return;
1206 }
1207 dirname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
1208 cpathname_ucs4,
1209 dirsep - cpathname_ucs4);
1210 PyMem_Free(cpathname_ucs4);
1211 if (dirname == NULL) {
1212 PyErr_Clear();
1213 return;
1214 }
1215
1216 dirbytes = PyUnicode_EncodeFSDefault(dirname);
1217 if (dirbytes == NULL) {
1218 PyErr_Clear();
1219 return;
1220 }
1221 res = mkdir(PyBytes_AS_STRING(dirbytes), dirmode);
1222 Py_DECREF(dirbytes);
1223 if (0 <= res)
1224 ok = 1;
1225 else
1226 ok = (errno == EEXIST);
1227 if (!ok) {
1228 if (Py_VerboseFlag)
1229 PySys_FormatStderr("# cannot create cache dir %R\n", dirname);
1230 Py_DECREF(dirname);
1231 return;
1232 }
1233 Py_DECREF(dirname);
1234
1235 /* We first write to a tmp file and then take advantage
1236 of atomic renaming (which *should* be true even under Windows).
1237 As in importlib, we use id(something) to generate a pseudo-random
1238 filename. mkstemp() can't be used since it doesn't allow specifying
1239 the file access permissions.
1240 */
1241 cpathname_tmp = PyUnicode_FromFormat("%U.%zd",
1242 cpathname, (Py_ssize_t) co);
1243 if (cpathname_tmp == NULL) {
1244 PyErr_Clear();
1245 return;
1246 }
1247 cpathbytes_tmp = PyUnicode_EncodeFSDefault(cpathname_tmp);
1248 Py_DECREF(cpathname_tmp);
1249 if (cpathbytes_tmp == NULL) {
1250 PyErr_Clear();
1251 return;
1252 }
1253 cpathbytes = PyUnicode_EncodeFSDefault(cpathname);
1254 if (cpathbytes == NULL) {
1255 PyErr_Clear();
1256 return;
1257 }
1258 fd = open(PyBytes_AS_STRING(cpathbytes_tmp),
1259 O_CREAT | O_EXCL | O_WRONLY, 0666);
1260 if (0 <= fd)
1261 fp = fdopen(fd, "wb");
1262 else
1263 fp = NULL;
1264 if (fp == NULL) {
1265 if (Py_VerboseFlag)
1266 PySys_FormatStderr(
1267 "# can't create %R\n", cpathname);
1268 Py_DECREF(cpathbytes);
1269 Py_DECREF(cpathbytes_tmp);
1270 return;
1271 }
1272 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
1273 /* First write a 0 for mtime */
1274 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
1275 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
1276 fflush(fp);
1277 /* Now write the true mtime */
1278 fseek(fp, 4L, 0);
1279 assert(mtime < LONG_MAX);
1280 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
1281 if (fflush(fp) != 0 || ferror(fp)) {
1282 if (Py_VerboseFlag)
1283 PySys_FormatStderr("# can't write %R\n", cpathname);
1284 /* Don't keep partial file */
1285 fclose(fp);
1286 (void) unlink(PyBytes_AS_STRING(cpathbytes_tmp));
1287 Py_DECREF(cpathbytes);
1288 Py_DECREF(cpathbytes_tmp);
1289 return;
1290 }
1291 fclose(fp);
1292 /* Do a (hopefully) atomic rename */
1293 if (rename(PyBytes_AS_STRING(cpathbytes_tmp),
1294 PyBytes_AS_STRING(cpathbytes))) {
1295 if (Py_VerboseFlag)
1296 PySys_FormatStderr("# can't write %R\n", cpathname);
1297 /* Don't keep tmp file */
1298 unlink(PyBytes_AS_STRING(cpathbytes_tmp));
1299 Py_DECREF(cpathbytes);
1300 Py_DECREF(cpathbytes_tmp);
1301 return;
1302 }
1303 Py_DECREF(cpathbytes);
1304 Py_DECREF(cpathbytes_tmp);
1305 if (Py_VerboseFlag)
1306 PySys_FormatStderr("# wrote %R\n", cpathname);
1307 }
1308
1309 static void
1310 update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
1311 {
1312 PyObject *constants, *tmp;
1313 Py_ssize_t i, n;
1314
1315 if (PyUnicode_Compare(co->co_filename, oldname))
1316 return;
1317
1318 tmp = co->co_filename;
1319 co->co_filename = newname;
1320 Py_INCREF(co->co_filename);
1321 Py_DECREF(tmp);
1322
1323 constants = co->co_consts;
1324 n = PyTuple_GET_SIZE(constants);
1325 for (i = 0; i < n; i++) {
1326 tmp = PyTuple_GET_ITEM(constants, i);
1327 if (PyCode_Check(tmp))
1328 update_code_filenames((PyCodeObject *)tmp,
1329 oldname, newname);
1330 }
1331 }
1332
1333 static void
1334 update_compiled_module(PyCodeObject *co, PyObject *newname)
1335 {
1336 PyObject *oldname;
1337
1338 if (PyUnicode_Compare(co->co_filename, newname) == 0)
1339 return;
1340
1341 oldname = co->co_filename;
1342 Py_INCREF(oldname);
1343 update_code_filenames(co, oldname, newname);
1344 Py_DECREF(oldname);
1345 }
1346
1347 static PyObject *
1348 imp_fix_co_filename(PyObject *self, PyObject *args)
1349 {
1350 PyObject *co;
1351 PyObject *file_path;
1352
1353 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
1354 return NULL;
1355
1356 if (!PyCode_Check(co)) {
1357 PyErr_SetString(PyExc_TypeError,
1358 "first argument must be a code object");
1359 return NULL;
1360 }
1361
1362 if (!PyUnicode_Check(file_path)) {
1363 PyErr_SetString(PyExc_TypeError,
1364 "second argument must be a string");
1365 return NULL;
1366 }
1367
1368 update_compiled_module((PyCodeObject*)co, file_path);
1369
1370 Py_RETURN_NONE;
1371 }
1372
1373 /* Load a source module from a given file and return its module
1374 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1375 byte-compiled file, use that instead. */
1376
1377 static PyObject *
1378 load_source_module(PyObject *name, PyObject *pathname, FILE *fp)
1379 {
1380 struct stat st;
1381 FILE *fpc;
1382 PyObject *cpathname = NULL, *cpathbytes = NULL;
1383 PyCodeObject *co;
1384 PyObject *m = NULL;
1385
1386 if (fstat(fileno(fp), &st) != 0) {
1387 PyErr_Format(PyExc_RuntimeError,
1388 "unable to get file status from %R",
1389 pathname);
1390 goto error;
1391 }
1392 #if SIZEOF_TIME_T > 4
1393 /* Python's .pyc timestamp handling presumes that the timestamp fits
1394 in 4 bytes. This will be fine until sometime in the year 2038,
1395 when a 4-byte signed time_t will overflow.
1396 */
1397 if (st.st_mtime >> 32) {
1398 PyErr_SetString(PyExc_OverflowError,
1399 "modification time overflows a 4 byte field");
1400 goto error;
1401 }
1402 #endif
1403 if (PyUnicode_READY(pathname) < 0)
1404 return NULL;
1405 cpathname = make_compiled_pathname(pathname, !Py_OptimizeFlag);
1406
1407 if (cpathname != NULL)
1408 fpc = check_compiled_module(pathname, st.st_mtime, cpathname);
1409 else
1410 fpc = NULL;
1411
1412 if (fpc) {
1413 co = read_compiled_module(cpathname, fpc);
1414 fclose(fpc);
1415 if (co == NULL)
1416 goto error;
1417 update_compiled_module(co, pathname);
1418 if (Py_VerboseFlag)
1419 PySys_FormatStderr("import %U # precompiled from %R\n",
1420 name, cpathname);
1421 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1422 cpathname, cpathname);
1423 }
1424 else {
1425 co = parse_source_module(pathname, fp);
1426 if (co == NULL)
1427 goto error;
1428 if (Py_VerboseFlag)
1429 PySys_FormatStderr("import %U # from %R\n",
1430 name, pathname);
1431 if (cpathname != NULL) {
1432 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1433 if (ro == NULL || !PyObject_IsTrue(ro))
1434 write_compiled_module(co, cpathname, &st);
1435 }
1436 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1437 pathname, cpathname);
1438 }
1439 Py_DECREF(co);
1440
1441 error:
1442 Py_XDECREF(cpathbytes);
1443 Py_XDECREF(cpathname);
1444 return m;
1445 }
1446
1447 /* Get source file -> unicode or None
1448 * Returns the path to the py file if available, else the given path
1449 */
1450 static PyObject *
1451 get_sourcefile(PyObject *filename)
1452 {
1453 Py_ssize_t len;
1454 Py_UCS4 *fileuni;
1455 PyObject *py;
1456 struct stat statbuf;
1457
1458 len = PyUnicode_GET_LENGTH(filename);
1459 if (len == 0)
1460 Py_RETURN_NONE;
1461
1462 /* don't match *.pyc or *.pyo? */
1463 fileuni = PyUnicode_AsUCS4Copy(filename);
1464 if (!fileuni)
1465 return NULL;
1466 if (len < 5
1467 || fileuni[len-4] != '.'
1468 || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P')
1469 || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y'))
1470 goto unchanged;
1471
1472 /* Start by trying to turn PEP 3147 path into source path. If that
1473 * fails, just chop off the trailing character, i.e. legacy pyc path
1474 * to py.
1475 */
1476 py = make_source_pathname(filename);
1477 if (py == NULL) {
1478 PyErr_Clear();
1479 py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1);
1480 }
1481 if (py == NULL)
1482 goto error;
1483
1484 if (_Py_stat(py, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
1485 PyMem_Free(fileuni);
1486 return py;
1487 }
1488 Py_DECREF(py);
1489 goto unchanged;
1490
1491 error:
1492 PyErr_Clear();
1493 unchanged:
1494 PyMem_Free(fileuni);
1495 Py_INCREF(filename);
1496 return filename;
1497 }
1498
1499 /* Forward */
1500 static PyObject *load_module(PyObject *, FILE *, PyObject *, int, PyObject *);
1501 static struct filedescr *find_module(PyObject *, PyObject *, PyObject *,
1502 PyObject **, FILE **, PyObject **);
1503 static struct _frozen * find_frozen(PyObject *);
1504
1505 /* Load a package and return its module object WITH INCREMENTED
1506 REFERENCE COUNT */
1507
1508 static PyObject *
1509 load_package(PyObject *name, PyObject *pathname)
1510 {
1511 PyObject *m, *d, *bufobj;
1512 PyObject *file = NULL, *path_list = NULL;
1513 int err;
1514 FILE *fp = NULL;
1515 struct filedescr *fdp;
1516
1517 m = PyImport_AddModuleObject(name);
1518 if (m == NULL)
1519 return NULL;
1520 if (Py_VerboseFlag)
1521 PySys_FormatStderr("import %U # directory %R\n",
1522 name, pathname);
1523 file = get_sourcefile(pathname);
1524 if (file == NULL)
1525 return NULL;
1526 path_list = Py_BuildValue("[O]", file);
1527 if (path_list == NULL) {
1528 Py_DECREF(file);
1529 return NULL;
1530 }
1531 d = PyModule_GetDict(m);
1532 err = PyDict_SetItemString(d, "__file__", file);
1533 Py_DECREF(file);
1534 if (err == 0)
1535 err = PyDict_SetItemString(d, "__path__", path_list);
1536 if (err != 0) {
1537 Py_DECREF(path_list);
1538 return NULL;
1539 }
1540 fdp = find_module(name, initstr, path_list,
1541 &bufobj, &fp, NULL);
1542 Py_DECREF(path_list);
1543 if (fdp == NULL) {
1544 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1545 PyErr_Clear();
1546 Py_INCREF(m);
1547 return m;
1548 }
1549 else
1550 return NULL;
1551 }
1552 m = load_module(name, fp, bufobj, fdp->type, NULL);
1553 Py_XDECREF(bufobj);
1554 if (fp != NULL)
1555 fclose(fp);
1556 return m;
1557 }
1558
1559
1560 /* Helper to test for built-in module */
1561
1562 static int
1563 is_builtin(PyObject *name)
1564 {
1565 int i, cmp;
1566 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1567 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
1568 if (cmp == 0) {
1569 if (PyImport_Inittab[i].initfunc == NULL)
1570 return -1;
1571 else
1572 return 1;
1573 }
1574 }
1575 return 0;
1576 }
1577
1578
1579 /* Return an importer object for a sys.path/pkg.__path__ item 'p',
1580 possibly by fetching it from the path_importer_cache dict. If it
1581 wasn't yet cached, traverse path_hooks until a hook is found
1582 that can handle the path item. Return None if no hook could;
1583 this tells our caller it should fall back to the builtin
1584 import mechanism. Cache the result in path_importer_cache.
1585 Returns a borrowed reference. */
1586
1587 static PyObject *
1588 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1589 PyObject *p)
1590 {
1591 PyObject *importer;
1592 Py_ssize_t j, nhooks;
1593
1594 /* These conditions are the caller's responsibility: */
1595 assert(PyList_Check(path_hooks));
1596 assert(PyDict_Check(path_importer_cache));
1597
1598 nhooks = PyList_Size(path_hooks);
1599 if (nhooks < 0)
1600 return NULL; /* Shouldn't happen */
1601
1602 importer = PyDict_GetItem(path_importer_cache, p);
1603 if (importer != NULL)
1604 return importer;
1605
1606 /* set path_importer_cache[p] to None to avoid recursion */
1607 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1608 return NULL;
1609
1610 for (j = 0; j < nhooks; j++) {
1611 PyObject *hook = PyList_GetItem(path_hooks, j);
1612 if (hook == NULL)
1613 return NULL;
1614 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1615 if (importer != NULL)
1616 break;
1617
1618 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1619 return NULL;
1620 }
1621 PyErr_Clear();
1622 }
1623 if (importer == NULL) {
1624 importer = PyObject_CallFunctionObjArgs(
1625 (PyObject *)&PyNullImporter_Type, p, NULL
1626 );
1627 if (importer == NULL) {
1628 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1629 PyErr_Clear();
1630 return Py_None;
1631 }
1632 }
1633 }
1634 if (importer != NULL) {
1635 int err = PyDict_SetItem(path_importer_cache, p, importer);
1636 Py_DECREF(importer);
1637 if (err != 0)
1638 return NULL;
1639 }
1640 return importer;
1641 }
1642
1643 PyAPI_FUNC(PyObject *)
1644 PyImport_GetImporter(PyObject *path) {
1645 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1646
1647 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1648 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1649 importer = get_path_importer(path_importer_cache,
1650 path_hooks, path);
1651 }
1652 }
1653 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1654 return importer;
1655 }
1656
1657 /* Search the path (default sys.path) for a module. Return the
1658 corresponding filedescr struct, and (via return arguments) the
1659 pathname and an open file. Return NULL if the module is not found. */
1660
1661 #ifdef MS_COREDLL
1662 extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
1663 PyObject **p_path);
1664 #endif
1665
1666 /* Forward */
1667 static int case_ok(PyObject *, Py_ssize_t, PyObject *);
1668 static int find_init_module(PyObject *);
1669 static struct filedescr importhookdescr = {"", "", IMP_HOOK};
1670
1671 /* Get the path of a module: get its importer and call importer.find_module()
1672 hook, or check if the module if a package (if path/__init__.py exists).
1673
1674 -1: error: a Python error occurred
1675 0: ignore: an error occurred because of invalid data, but the error is not
1676 important enough to be reported.
1677 1: get path: module not found, but *buf contains its path
1678 2: found: *p_fd is the file descriptor (IMP_HOOK or PKG_DIRECTORY)
1679 and *buf is the path */
1680
1681 static int
1682 find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
1683 PyObject *path_hooks, PyObject *path_importer_cache,
1684 PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd)
1685 {
1686 PyObject *path_unicode, *filename = NULL;
1687 Py_ssize_t len, pos;
1688 struct stat statbuf;
1689 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1690 int result, addsep;
1691
1692 if (PyUnicode_Check(path)) {
1693 Py_INCREF(path);
1694 path_unicode = path;
1695 }
1696 else if (PyBytes_Check(path)) {
1697 path_unicode = PyUnicode_DecodeFSDefaultAndSize(
1698 PyBytes_AS_STRING(path), PyBytes_GET_SIZE(path));
1699 if (path_unicode == NULL)
1700 return -1;
1701 }
1702 else
1703 return 0;
1704
1705 if (PyUnicode_READY(path_unicode))
1706 return -1;
1707
1708 len = PyUnicode_GET_LENGTH(path_unicode);
1709 if (PyUnicode_FindChar(path_unicode, 0, 0, len, 1) != -1) {
1710 result = 0;
1711 goto out; /* path contains '\0' */
1712 }
1713
1714 /* sys.path_hooks import hook */
1715 if (p_loader != NULL) {
1716 _Py_IDENTIFIER(find_module);
1717 PyObject *importer;
1718
1719 importer = get_path_importer(path_importer_cache,
1720 path_hooks, path);
1721 if (importer == NULL) {
1722 result = -1;
1723 goto out;
1724 }
1725 /* Note: importer is a borrowed reference */
1726 if (importer != Py_None) {
1727 PyObject *loader;
1728 loader = _PyObject_CallMethodId(importer,
1729 &PyId_find_module, "O", fullname);
1730 if (loader == NULL) {
1731 result = -1; /* error */
1732 goto out;
1733 }
1734 if (loader != Py_None) {
1735 /* a loader was found */
1736 *p_loader = loader;
1737 *p_fd = &importhookdescr;
1738 result = 2;
1739 goto out;
1740 }
1741 Py_DECREF(loader);
1742 result = 0;
1743 goto out;
1744 }
1745 }
1746 /* no hook was found, use builtin import */
1747
1748 addsep = 0;
1749 if (len > 0 && PyUnicode_READ_CHAR(path_unicode, len-1) != SEP
1750 #ifdef ALTSEP
1751 && PyUnicode_READ_CHAR(path_unicode, len-1) != ALTSEP
1752 #endif
1753 )
1754 addsep = 1;
1755 filename = PyUnicode_New(len + PyUnicode_GET_LENGTH(name) + addsep,
1756 Py_MAX(PyUnicode_MAX_CHAR_VALUE(path_unicode),
1757 PyUnicode_MAX_CHAR_VALUE(name)));
1758 if (filename == NULL) {
1759 result = -1;
1760 goto out;
1761 }
1762 PyUnicode_CopyCharacters(filename, 0, path_unicode, 0, len);
1763 pos = len;
1764 if (addsep)
1765 PyUnicode_WRITE(PyUnicode_KIND(filename),
1766 PyUnicode_DATA(filename),
1767 pos++, SEP);
1768 PyUnicode_CopyCharacters(filename, pos, name, 0,
1769 PyUnicode_GET_LENGTH(name));
1770
1771 /* Check for package import (buf holds a directory name,
1772 and there's an __init__ module in that directory */
1773 #ifdef HAVE_STAT
1774 if (_Py_stat(filename, &statbuf) == 0 && /* it exists */
1775 S_ISDIR(statbuf.st_mode)) /* it's a directory */
1776 {
1777 int match;
1778
1779 match = case_ok(filename, 0, name);
1780 if (match < 0) {
1781 result = -1;
1782 goto out;
1783 }
1784 if (match) { /* case matches */
1785 if (find_init_module(filename)) { /* and has __init__.py */
1786 *p_path = filename;
1787 filename = NULL;
1788 *p_fd = &fd_package;
1789 result = 2;
1790 goto out;
1791 }
1792 else {
1793 int err;
1794 err = PyErr_WarnFormat(PyExc_ImportWarning, 1,
1795 "Not importing directory %R: missing __init__.py",
1796 filename);
1797 if (err) {
1798 result = -1;
1799 goto out;
1800 }
1801 }
1802 }
1803 }
1804 #endif
1805 *p_path = filename;
1806 filename = NULL;
1807 result = 1;
1808 out:
1809 Py_DECREF(path_unicode);
1810 Py_XDECREF(filename);
1811 return result;
1812 }
1813
1814 /* Find a module in search_path_list. For each path, try
1815 find_module_path() or try each _PyImport_Filetab suffix.
1816
1817 If the module is found, return a file descriptor, write the path in
1818 *p_filename, write the pointer to the file object into *p_fp, and (if
1819 p_loader is not NULL) the loader into *p_loader.
1820
1821 Otherwise, raise an exception and return NULL. */
1822
1823 static struct filedescr*
1824 find_module_path_list(PyObject *fullname, PyObject *name,
1825 PyObject *search_path_list, PyObject *path_hooks,
1826 PyObject *path_importer_cache,
1827 PyObject **p_path, FILE **p_fp, PyObject **p_loader)
1828 {
1829 Py_ssize_t i, npath;
1830 struct filedescr *fdp = NULL;
1831 char *filemode;
1832 FILE *fp = NULL;
1833 PyObject *prefix, *filename;
1834 int match;
1835
1836 npath = PyList_Size(search_path_list);
1837 for (i = 0; i < npath; i++) {
1838 PyObject *path;
1839 int ok;
1840
1841 path = PyList_GetItem(search_path_list, i);
1842 if (path == NULL)
1843 return NULL;
1844
1845 prefix = NULL;
1846 ok = find_module_path(fullname, name, path,
1847 path_hooks, path_importer_cache,
1848 &prefix, p_loader, &fdp);
1849 if (ok < 0)
1850 return NULL;
1851 if (ok == 0)
1852 continue;
1853 if (ok == 2) {
1854 *p_path = prefix;
1855 return fdp;
1856 }
1857
1858 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1859 struct stat statbuf;
1860
1861 filemode = fdp->mode;
1862 if (filemode[0] == 'U')
1863 filemode = "r" PY_STDIOTEXTMODE;
1864
1865 filename = PyUnicode_FromFormat("%U%s", prefix, fdp->suffix);
1866 if (filename == NULL) {
1867 Py_DECREF(prefix);
1868 return NULL;
1869 }
1870
1871 if (Py_VerboseFlag > 1)
1872 PySys_FormatStderr("# trying %R\n", filename);
1873
1874 if (_Py_stat(filename, &statbuf) != 0 || S_ISDIR(statbuf.st_mode))
1875 {
1876 /* it doesn't exist, or it's a directory */
1877 Py_DECREF(filename);
1878 continue;
1879 }
1880
1881 fp = _Py_fopen(filename, filemode);
1882 if (fp == NULL) {
1883 Py_DECREF(filename);
1884 if (PyErr_Occurred()) {
1885 Py_DECREF(prefix);
1886 return NULL;
1887 }
1888 continue;
1889 }
1890 match = case_ok(filename, -(Py_ssize_t)strlen(fdp->suffix), name);
1891 if (match < 0) {
1892 Py_DECREF(prefix);
1893 Py_DECREF(filename);
1894 return NULL;
1895 }
1896 if (match) {
1897 Py_DECREF(prefix);
1898 *p_path = filename;
1899 *p_fp = fp;
1900 return fdp;
1901 }
1902 Py_DECREF(filename);
1903
1904 fclose(fp);
1905 fp = NULL;
1906 }
1907 Py_DECREF(prefix);
1908 }
1909 PyErr_Format(PyExc_ImportError,
1910 "No module named %R", name);
1911 return NULL;
1912 }
1913
1914 /* Find a module:
1915
1916 - try find_module() of each sys.meta_path hook
1917 - try find_frozen()
1918 - try is_builtin()
1919 - try _PyWin_FindRegisteredModule() (Windows only)
1920 - otherwise, call find_module_path_list() with search_path_list (if not
1921 NULL) or sys.path
1922
1923 fullname can be NULL, but only if p_loader is NULL.
1924
1925 Return:
1926
1927 - &fd_builtin (C_BUILTIN) if it is a builtin
1928 - &fd_frozen (PY_FROZEN) if it is frozen
1929 - &fd_package (PKG_DIRECTORY) and write the filename into *p_path
1930 if it is a package
1931 - &importhookdescr (IMP_HOOK) and write the loader into *p_loader if a
1932 importer loader was found
1933 - a file descriptor (PY_SOURCE, PY_COMPILED, C_EXTENSION, PY_RESOURCE or
1934 PY_CODERESOURCE: see _PyImport_Filetab), write the filename into
1935 *p_path and the pointer to the open file into *p_fp
1936 - NULL on error
1937
1938 By default, *p_path, *p_fp and *p_loader (if set) are set to NULL.
1939 Eg. *p_path is set to NULL for a builtin package.
1940 */
1941
1942 static struct filedescr *
1943 find_module(PyObject *fullname, PyObject *name, PyObject *search_path_list,
1944 PyObject **p_path, FILE **p_fp, PyObject **p_loader)
1945 {
1946 Py_ssize_t i, npath;
1947 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1948 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1949 PyObject *path_hooks, *path_importer_cache;
1950
1951 *p_path = NULL;
1952 *p_fp = NULL;
1953 if (p_loader != NULL)
1954 *p_loader = NULL;
1955
1956 if (PyUnicode_GET_LENGTH(name) > MAXPATHLEN) {
1957 PyErr_SetString(PyExc_OverflowError,
1958 "module name is too long");
1959 return NULL;
1960 }
1961
1962 /* sys.meta_path import hook */
1963 if (p_loader != NULL) {
1964 _Py_IDENTIFIER(find_module);
1965 PyObject *meta_path;
1966
1967 meta_path = PySys_GetObject("meta_path");
1968 if (meta_path == NULL || !PyList_Check(meta_path)) {
1969 PyErr_SetString(PyExc_RuntimeError,
1970 "sys.meta_path must be a list of "
1971 "import hooks");
1972 return NULL;
1973 }
1974 Py_INCREF(meta_path); /* zap guard */
1975 npath = PyList_Size(meta_path);
1976 for (i = 0; i < npath; i++) {
1977 PyObject *loader;
1978 PyObject *hook = PyList_GetItem(meta_path, i);
1979 loader = _PyObject_CallMethodId(hook, &PyId_find_module,
1980 "OO", fullname,
1981 search_path_list != NULL ?
1982 search_path_list : Py_None);
1983 if (loader == NULL) {
1984 Py_DECREF(meta_path);
1985 return NULL; /* true error */
1986 }
1987 if (loader != Py_None) {
1988 /* a loader was found */
1989 *p_loader = loader;
1990 Py_DECREF(meta_path);
1991 return &importhookdescr;
1992 }
1993 Py_DECREF(loader);
1994 }
1995 Py_DECREF(meta_path);
1996 }
1997
1998 if (find_frozen(fullname) != NULL)
1999 return &fd_frozen;
2000
2001 if (search_path_list == NULL) {
2002 #ifdef MS_COREDLL
2003 FILE *fp;
2004 struct filedescr *fdp;
2005 #endif
2006 if (is_builtin(name))
2007 return &fd_builtin;
2008 #ifdef MS_COREDLL
2009 fp = _PyWin_FindRegisteredModule(name, &fdp, p_path);
2010 if (fp != NULL) {
2011 *p_fp = fp;
2012 return fdp;
2013 }
2014 else if (PyErr_Occurred())
2015 return NULL;
2016 #endif
2017 search_path_list = PySys_GetObject("path");
2018 }
2019
2020 if (search_path_list == NULL || !PyList_Check(search_path_list)) {
2021 PyErr_SetString(PyExc_RuntimeError,
2022 "sys.path must be a list of directory names");
2023 return NULL;
2024 }
2025
2026 path_hooks = PySys_GetObject("path_hooks");
2027 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
2028 PyErr_SetString(PyExc_RuntimeError,
2029 "sys.path_hooks must be a list of "
2030 "import hooks");
2031 return NULL;
2032 }
2033 path_importer_cache = PySys_GetObject("path_importer_cache");
2034 if (path_importer_cache == NULL ||
2035 !PyDict_Check(path_importer_cache)) {
2036 PyErr_SetString(PyExc_RuntimeError,
2037 "sys.path_importer_cache must be a dict");
2038 return NULL;
2039 }
2040
2041 return find_module_path_list(fullname, name,
2042 search_path_list, path_hooks,
2043 path_importer_cache,
2044 p_path, p_fp, p_loader);
2045 }
2046
2047 /* case_bytes(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
2048 * The arguments here are tricky, best shown by example:
2049 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
2050 * ^ ^ ^ ^
2051 * |--------------------- buf ---------------------|
2052 * |------------------- len ------------------|
2053 * |------ name -------|
2054 * |----- namelen -----|
2055 * buf is the full path, but len only counts up to (& exclusive of) the
2056 * extension. name is the module name, also exclusive of extension.
2057 *
2058 * We've already done a successful stat() or fopen() on buf, so know that
2059 * there's some match, possibly case-insensitive.
2060 *
2061 * case_bytes() is to return 1 if there's a case-sensitive match for
2062 * name, else 0. case_bytes() is also to return 1 if envar PYTHONCASEOK
2063 * exists.
2064 *
2065 * case_bytes() is used to implement case-sensitive import semantics even
2066 * on platforms with case-insensitive filesystems. It's trivial to implement
2067 * for case-sensitive filesystems. It's pretty much a cross-platform
2068 * nightmare for systems with case-insensitive filesystems.
2069 */
2070
2071 /* First we may need a pile of platform-specific header files; the sequence
2072 * of #if's here should match the sequence in the body of case_bytes().
2073 */
2074 #if defined(MS_WINDOWS)
2075 #include <windows.h>
2076
2077 #elif defined(DJGPP)
2078 #include <dir.h>
2079
2080 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
2081 #include <sys/types.h>
2082 #include <dirent.h>
2083
2084 #elif defined(PYOS_OS2)
2085 #define INCL_DOS
2086 #define INCL_DOSERRORS
2087 #define INCL_NOPMAPI
2088 #include <os2.h>
2089 #endif
2090
2091 #if defined(DJGPP) \
2092 || ((defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) \
2093 && defined(HAVE_DIRENT_H)) \
2094 || defined(PYOS_OS2)
2095 # define USE_CASE_OK_BYTES
2096 #endif
2097
2098
2099 #ifdef USE_CASE_OK_BYTES
2100 static int
2101 case_bytes(char *buf, Py_ssize_t len, Py_ssize_t namelen, const char *name)
2102 {
2103 /* Pick a platform-specific implementation; the sequence of #if's here should
2104 * match the sequence just above.
2105 */
2106
2107 /* DJGPP */
2108 #if defined(DJGPP)
2109 struct ffblk ffblk;
2110 int done;
2111
2112 if (Py_GETENV("PYTHONCASEOK") != NULL)
2113 return 1;
2114
2115 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
2116 if (done) {
2117 PyErr_Format(PyExc_NameError,
2118 "Can't find file for module %.100s\n(filename %.300s)",
2119 name, buf);
2120 return -1;
2121 }
2122 return strncmp(ffblk.ff_name, name, namelen) == 0;
2123
2124 /* new-fangled macintosh (macosx) or Cygwin */
2125 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
2126 DIR *dirp;
2127 struct dirent *dp;
2128 char dirname[MAXPATHLEN + 1];
2129 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
2130
2131 if (Py_GETENV("PYTHONCASEOK") != NULL)
2132 return 1;
2133
2134 /* Copy the dir component into dirname; substitute "." if empty */
2135 if (dirlen <= 0) {
2136 dirname[0] = '.';
2137 dirname[1] = '\0';
2138 }
2139 else {
2140 assert(dirlen <= MAXPATHLEN);
2141 memcpy(dirname, buf, dirlen);
2142 dirname[dirlen] = '\0';
2143 }
2144 /* Open the directory and search the entries for an exact match. */
2145 dirp = opendir(dirname);
2146 if (dirp) {
2147 char *nameWithExt = buf + len - namelen;
2148 while ((dp = readdir(dirp)) != NULL) {
2149 const int thislen =
2150 #ifdef _DIRENT_HAVE_D_NAMELEN
2151 dp->d_namlen;
2152 #else
2153 strlen(dp->d_name);
2154 #endif
2155 if (thislen >= namelen &&
2156 strcmp(dp->d_name, nameWithExt) == 0) {
2157 (void)closedir(dirp);
2158 return 1; /* Found */
2159 }
2160 }
2161 (void)closedir(dirp);
2162 }
2163 return 0 ; /* Not found */
2164
2165 /* OS/2 */
2166 #elif defined(PYOS_OS2)
2167 HDIR hdir = 1;
2168 ULONG srchcnt = 1;
2169 FILEFINDBUF3 ffbuf;
2170 APIRET rc;
2171
2172 if (Py_GETENV("PYTHONCASEOK") != NULL)
2173 return 1;
2174
2175 rc = DosFindFirst(buf,
2176 &hdir,
2177 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2178 &ffbuf, sizeof(ffbuf),
2179 &srchcnt,
2180 FIL_STANDARD);
2181 if (rc != NO_ERROR)
2182 return 0;
2183 return strncmp(ffbuf.achName, name, namelen) == 0;
2184
2185 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
2186 #else
2187 # error "USE_CASE_OK_BYTES is not correctly defined"
2188 #endif
2189 }
2190 #endif
2191
2192 /*
2193 * Check if a filename case matchs the name case. We've already done a
2194 * successful stat() or fopen() on buf, so know that there's some match,
2195 * possibly case-insensitive.
2196 *
2197 * case_ok() is to return 1 if there's a case-sensitive match for name, 0 if it
2198 * the filename doesn't match, or -1 on error. case_ok() is also to return 1
2199 * if envar PYTHONCASEOK exists.
2200 *
2201 * case_ok() is used to implement case-sensitive import semantics even
2202 * on platforms with case-insensitive filesystems. It's trivial to implement
2203 * for case-sensitive filesystems. It's pretty much a cross-platform
2204 * nightmare for systems with case-insensitive filesystems.
2205 */
2206
2207 static int
2208 case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
2209 {
2210 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
2211 return 1;
2212
2213 }
2214
2215 #ifdef HAVE_STAT
2216
2217 /* Helper to look for __init__.py or __init__.py[co] in potential package.
2218 Return 1 if __init__ was found, 0 if not, or -1 on error. */
2219 static int
2220 find_init_module(PyObject *directory)
2221 {
2222 struct stat statbuf;
2223 PyObject *filename;
2224 int match;
2225
2226 filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
2227 if (filename == NULL)
2228 return -1;
2229 if (_Py_stat(filename, &statbuf) == 0) {
2230 /* 3=len(".py") */
2231 match = case_ok(filename, -3, initstr);
2232 if (match < 0) {
2233 Py_DECREF(filename);
2234 return -1;
2235 }
2236 if (match) {
2237 Py_DECREF(filename);
2238 return 1;
2239 }
2240 }
2241 Py_DECREF(filename);
2242
2243 filename = PyUnicode_FromFormat("%U%c__init__.py%c",
2244 directory, SEP, Py_OptimizeFlag ? 'o' : 'c');
2245 if (filename == NULL)
2246 return -1;
2247 if (_Py_stat(filename, &statbuf) == 0) {
2248 /* 4=len(".pyc") */
2249 match = case_ok(filename, -4, initstr);
2250 if (match < 0) {
2251 Py_DECREF(filename);
2252 return -1;
2253 }
2254 if (match) {
2255 Py_DECREF(filename);
2256 return 1;
2257 }
2258 }
2259 Py_DECREF(filename);
2260 return 0;
2261 }
2262
2263 #endif /* HAVE_STAT */
2264
2265
2266 static int init_builtin(PyObject *); /* Forward */
2267
2268 static PyObject*
2269 load_builtin(PyObject *name, int type)
2270 {
2271 PyObject *m, *modules;
2272 int err;
2273
2274 if (type == C_BUILTIN)
2275 err = init_builtin(name);
2276 else
2277 err = PyImport_ImportFrozenModuleObject(name);
2278 if (err < 0)
2279 return NULL;
2280 if (err == 0) {
2281 PyErr_Format(PyExc_ImportError,
2282 "Purported %s module %R not found",
2283 type == C_BUILTIN ? "builtin" : "frozen",
2284 name);
2285 return NULL;
2286 }
2287
2288 modules = PyImport_GetModuleDict();
2289 m = PyDict_GetItem(modules, name);
2290 if (m == NULL) {
2291 PyErr_Format(
2292 PyExc_ImportError,
2293 "%s module %R not properly initialized",
2294 type == C_BUILTIN ? "builtin" : "frozen",
2295 name);
2296 return NULL;
2297 }
2298 Py_INCREF(m);
2299 return m;
2300 }
2301
2302 /* Load an external module using the default search path and return
2303 its module object WITH INCREMENTED REFERENCE COUNT */
2304
2305 static PyObject *
2306 load_module(PyObject *name, FILE *fp, PyObject *pathname, int type, PyObject *loader)
2307 {
2308 PyObject *m;
2309
2310 /* First check that there's an open file (if we need one) */
2311 switch (type) {
2312 case PY_SOURCE:
2313 case PY_COMPILED:
2314 if (fp == NULL) {
2315 PyErr_Format(PyExc_ValueError,
2316 "file object required for import (type code %d)",
2317 type);
2318 return NULL;
2319 }
2320 }
2321
2322 switch (type) {
2323
2324 case PY_SOURCE:
2325 m = load_source_module(name, pathname, fp);
2326 break;
2327
2328 case PY_COMPILED:
2329 m = load_compiled_module(name, pathname, fp);
2330 break;
2331
2332 #ifdef HAVE_DYNAMIC_LOADING
2333 case C_EXTENSION:
2334 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2335 break;
2336 #endif
2337
2338 case PKG_DIRECTORY:
2339 m = load_package(name, pathname);
2340 break;
2341
2342 case C_BUILTIN:
2343 case PY_FROZEN:
2344 m = load_builtin(name, type);
2345 break;
2346
2347 case IMP_HOOK: {
2348 _Py_IDENTIFIER(load_module);
2349 if (loader == NULL) {
2350 PyErr_SetString(PyExc_ImportError,
2351 "import hook without loader");
2352 return NULL;
2353 }
2354 m = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
2355 break;
2356 }
2357
2358 default:
2359 PyErr_Format(PyExc_ImportError,
2360 "Don't know how to import %R (type code %d)",
2361 name, type);
2362 m = NULL;
2363
2364 }
2365
2366 return m;
2367 }
2368
2369
2370 /* Initialize a built-in module.
2371 Return 1 for success, 0 if the module is not found, and -1 with
2372 an exception set if the initialization failed. */
2373
2374 static int
2375 init_builtin(PyObject *name)
2376 {
2377 struct _inittab *p;
2378
2379 if (_PyImport_FindExtensionObject(name, name) != NULL)
2380 return 1;
2381
2382 for (p = PyImport_Inittab; p->name != NULL; p++) {
2383 PyObject *mod;
2384 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
2385 if (p->initfunc == NULL) {
2386 PyErr_Format(PyExc_ImportError,
2387 "Cannot re-init internal module %R",
2388 name);
2389 return -1;
2390 }
2391 if (Py_VerboseFlag)
2392 PySys_FormatStderr("import %U # builtin\n", name);
2393 mod = (*p->initfunc)();
2394 if (mod == 0)
2395 return -1;
2396 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
2397 return -1;
2398 /* FixupExtension has put the module into sys.modules,
2399 so we can release our own reference. */
2400 Py_DECREF(mod);
2401 return 1;
2402 }
2403 }
2404 return 0;
2405 }
2406
2407
2408 /* Frozen modules */
2409
2410 static struct _frozen *
2411 find_frozen(PyObject *name)
2412 {
2413 struct _frozen *p;
2414
2415 if (name == NULL)
2416 return NULL;
2417
2418 for (p = PyImport_FrozenModules; ; p++) {
2419 if (p->name == NULL)
2420 return NULL;
2421 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
2422 break;
2423 }
2424 return p;
2425 }
2426
2427 static PyObject *
2428 get_frozen_object(PyObject *name)
2429 {
2430 struct _frozen *p = find_frozen(name);
2431 int size;
2432
2433 if (p == NULL) {
2434 PyErr_Format(PyExc_ImportError,
2435 "No such frozen object named %R",
2436 name);
2437 return NULL;
2438 }
2439 if (p->code == NULL) {
2440 PyErr_Format(PyExc_ImportError,
2441 "Excluded frozen object named %R",
2442 name);
2443 return NULL;
2444 }
2445 size = p->size;
2446 if (size < 0)
2447 size = -size;
2448 return PyMarshal_ReadObjectFromString((char *)p->code, size);
2449 }
2450
2451 static PyObject *
2452 is_frozen_package(PyObject *name)
2453 {
2454 struct _frozen *p = find_frozen(name);
2455 int size;
2456
2457 if (p == NULL) {
2458 PyErr_Format(PyExc_ImportError,
2459 "No such frozen object named %R",
2460 name);
2461 return NULL;
2462 }
2463
2464 size = p->size;
2465
2466 if (size < 0)
2467 Py_RETURN_TRUE;
2468 else
2469 Py_RETURN_FALSE;
2470 }
2471
2472
2473 /* Initialize a frozen module.
2474 Return 1 for success, 0 if the module is not found, and -1 with
2475 an exception set if the initialization failed.
2476 This function is also used from frozenmain.c */
2477
2478 int
2479 PyImport_ImportFrozenModuleObject(PyObject *name)
2480 {
2481 struct _frozen *p;
2482 PyObject *co, *m, *path;
2483 int ispackage;
2484 int size;
2485
2486 p = find_frozen(name);
2487
2488 if (p == NULL)
2489 return 0;
2490 if (p->code == NULL) {
2491 PyErr_Format(PyExc_ImportError,
2492 "Excluded frozen object named %R",
2493 name);
2494 return -1;
2495 }
2496 size = p->size;
2497 ispackage = (size < 0);
2498 if (ispackage)
2499 size = -size;
2500 if (Py_VerboseFlag)
2501 PySys_FormatStderr("import %U # frozen%s\n",
2502 name, ispackage ? " package" : "");
2503 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
2504 if (co == NULL)
2505 return -1;
2506 if (!PyCode_Check(co)) {
2507 PyErr_Format(PyExc_TypeError,
2508 "frozen object %R is not a code object",
2509 name);
2510 goto err_return;
2511 }
2512 if (ispackage) {
2513 /* Set __path__ to the package name */
2514 PyObject *d, *l;
2515 int err;
2516 m = PyImport_AddModuleObject(name);
2517 if (m == NULL)
2518 goto err_return;
2519 d = PyModule_GetDict(m);
2520 l = PyList_New(1);
2521 if (l == NULL) {
2522 goto err_return;
2523 }
2524 Py_INCREF(name);
2525 PyList_SET_ITEM(l, 0, name);
2526 err = PyDict_SetItemString(d, "__path__", l);
2527 Py_DECREF(l);
2528 if (err != 0)
2529 goto err_return;
2530 }
2531 path = PyUnicode_FromString("<frozen>");
2532 if (path == NULL)
2533 goto err_return;
2534 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
2535 Py_DECREF(path);
2536 if (m == NULL)
2537 goto err_return;
2538 Py_DECREF(co);
2539 Py_DECREF(m);
2540 return 1;
2541 err_return:
2542 Py_DECREF(co);
2543 return -1;
2544 }
2545
2546 int
2547 PyImport_ImportFrozenModule(char *name)
2548 {
2549 PyObject *nameobj;
2550 int ret;
2551 nameobj = PyUnicode_InternFromString(name);
2552 if (nameobj == NULL)
2553 return -1;
2554 ret = PyImport_ImportFrozenModuleObject(nameobj);
2555 Py_DECREF(nameobj);
2556 return ret;
2557 }
2558
2559
2560 /* Import a module, either built-in, frozen, or external, and return
2561 its module object WITH INCREMENTED REFERENCE COUNT */
2562
2563 PyObject *
2564 PyImport_ImportModule(const char *name)
2565 {
2566 PyObject *pname;
2567 PyObject *result;
2568
2569 pname = PyUnicode_FromString(name);
2570 if (pname == NULL)
2571 return NULL;
2572 result = PyImport_Import(pname);
2573 Py_DECREF(pname);
2574 return result;
2575 }
2576
2577 /* Import a module without blocking
2578 *
2579 * At first it tries to fetch the module from sys.modules. If the module was
2580 * never loaded before it loads it with PyImport_ImportModule() unless another
2581 * thread holds the import lock. In the latter case the function raises an
2582 * ImportError instead of blocking.
2583 *
2584 * Returns the module object with incremented ref count.
2585 */
2586 PyObject *
2587 PyImport_ImportModuleNoBlock(const char *name)
2588 {
2589 PyObject *nameobj, *modules, *result;
2590 #ifdef WITH_THREAD
2591 long me;
2592 #endif
2593
2594 /* Try to get the module from sys.modules[name] */
2595 modules = PyImport_GetModuleDict();
2596 if (modules == NULL)
2597 return NULL;
2598
2599 nameobj = PyUnicode_FromString(name);
2600 if (nameobj == NULL)
2601 return NULL;
2602 result = PyDict_GetItem(modules, nameobj);
2603 if (result != NULL) {
2604 Py_DECREF(nameobj);
2605 Py_INCREF(result);
2606 return result;
2607 }
2608 PyErr_Clear();
2609 #ifdef WITH_THREAD
2610 /* check the import lock
2611 * me might be -1 but I ignore the error here, the lock function
2612 * takes care of the problem */
2613 me = PyThread_get_thread_ident();
2614 if (import_lock_thread == -1 || import_lock_thread == me) {
2615 /* no thread or me is holding the lock */
2616 result = PyImport_Import(nameobj);
2617 }
2618 else {
2619 PyErr_Format(PyExc_ImportError,
2620 "Failed to import %R because the import lock"
2621 "is held by another thread.",
2622 nameobj);
2623 result = NULL;
2624 }
2625 #else
2626 result = PyImport_Import(nameobj);
2627 #endif
2628 Py_DECREF(nameobj);
2629 return result;
2630 }
2631
2632 /* Forward declarations for helper routines */
2633 static PyObject *get_parent(PyObject *globals,
2634 PyObject **p_name,
2635 int level);
2636 static PyObject *load_next(PyObject *mod, PyObject *altmod,
2637 PyObject *inputname, PyObject **p_outputname,
2638 PyObject **p_prefix);
2639 static int mark_miss(PyObject *name);
2640 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
2641 PyObject *buf, int recursive);
2642 static PyObject * import_submodule(PyObject *mod, PyObject *name,
2643 PyObject *fullname);
2644
2645 /* The Magnum Opus of dotted-name import :-) */
2646
2647 static PyObject *
2648 import_module_level(PyObject *name, PyObject *globals, PyObject *locals,
2649 PyObject *fromlist, int level)
2650 {
2651 PyObject *parent, *next, *inputname, *outputname;
2652 PyObject *head = NULL;
2653 PyObject *tail = NULL;
2654 PyObject *prefix = NULL;
2655 PyObject *result = NULL;
2656 Py_ssize_t sep, altsep;
2657
2658 if (PyUnicode_READY(name))
2659 return NULL;
2660
2661 sep = PyUnicode_FindChar(name, SEP, 0, PyUnicode_GET_LENGTH(name), 1);
2662 if (sep == -2)
2663 return NULL;
2664 #ifdef ALTSEP
2665 altsep = PyUnicode_FindChar(name, ALTSEP, 0, PyUnicode_GET_LENGTH(name), 1);
2666 if (altsep == -2)
2667 return NULL;
2668 #else
2669 altsep = -1;
2670 #endif
2671 if (sep != -1 || altsep != -1)
2672 {
2673 PyErr_SetString(PyExc_ImportError,
2674 "Import by filename is not supported.");
2675 return NULL;
2676 }
2677
2678 parent = get_parent(globals, &prefix, level);
2679 if (parent == NULL) {
2680 return NULL;
2681 }
2682
2683 if (PyUnicode_READY(prefix))
2684 return NULL;
2685
2686 head = load_next(parent, level < 0 ? Py_None : parent, name, &outputname,
2687 &prefix);
2688 if (head == NULL)
2689 goto out;
2690
2691 tail = head;
2692 Py_INCREF(tail);
2693
2694 if (outputname != NULL) {
2695 while (1) {
2696 inputname = outputname;
2697 next = load_next(tail, tail, inputname, &outputname,
2698 &prefix);
2699 Py_CLEAR(tail);
2700 Py_CLEAR(inputname);
2701 if (next == NULL)
2702 goto out;
2703 tail = next;
2704
2705 if (outputname == NULL) {
2706 break;
2707 }
2708 }
2709 }
2710 if (tail == Py_None) {
2711 /* If tail is Py_None, both get_parent and load_next found
2712 an empty module name: someone called __import__("") or
2713 doctored faulty bytecode */
2714 PyErr_SetString(PyExc_ValueError, "Empty module name");
2715 goto out;
2716 }
2717
2718 if (fromlist != NULL) {
2719 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2720 fromlist = NULL;
2721 }
2722
2723 if (fromlist == NULL) {
2724 result = head;
2725 Py_INCREF(result);
2726 goto out;
2727 }
2728
2729 if (!ensure_fromlist(tail, fromlist, prefix, 0))
2730 goto out;
2731
2732 result = tail;
2733 Py_INCREF(result);
2734 out:
2735 Py_XDECREF(head);
2736 Py_XDECREF(tail);
2737 Py_XDECREF(prefix);
2738 return result;
2739 }
2740
2741 PyObject *
2742 PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
2743 PyObject *locals, PyObject *fromlist,
2744 int level)
2745 {
2746 PyObject *mod;
2747 _PyImport_AcquireLock();
2748 mod = import_module_level(name, globals, locals, fromlist, level);
2749 if (_PyImport_ReleaseLock() < 0) {
2750 Py_XDECREF(mod);
2751 PyErr_SetString(PyExc_RuntimeError,
2752 "not holding the import lock");
2753 return NULL;
2754 }
2755 return mod;
2756 }
2757
2758 PyObject *
2759 PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
2760 PyObject *fromlist, int level)
2761 {
2762 PyObject *nameobj, *mod;
2763 nameobj = PyUnicode_FromString(name);
2764 if (nameobj == NULL)
2765 return NULL;
2766 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
2767 fromlist, level);
2768 Py_DECREF(nameobj);
2769 return mod;
2770 }
2771
2772
2773 /* Return the package that an import is being performed in. If globals comes
2774 from the module foo.bar.bat (not itself a package), this returns the
2775 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
2776 the package's entry in sys.modules is returned, as a borrowed reference.
2777
2778 The name of the returned package is returned in *p_name.
2779
2780 If globals doesn't come from a package or a module in a package, or a
2781 corresponding entry is not found in sys.modules, Py_None is returned.
2782 */
2783 static PyObject *
2784 get_parent(PyObject *globals, PyObject **p_name, int level)
2785 {
2786 PyObject *nameobj;
2787
2788 static PyObject *namestr = NULL;
2789 static PyObject *pathstr = NULL;
2790 static PyObject *pkgstr = NULL;
2791 PyObject *pkgname, *modname, *modpath, *modules, *parent;
2792 int orig_level = level;
2793
2794 if (globals == NULL || !PyDict_Check(globals) || !level)
2795 goto return_none;
2796
2797 if (namestr == NULL) {
2798 namestr = PyUnicode_InternFromString("__name__");
2799 if (namestr == NULL)
2800 return NULL;
2801 }
2802 if (pathstr == NULL) {
2803 pathstr = PyUnicode_InternFromString("__path__");
2804 if (pathstr == NULL)
2805 return NULL;
2806 }
2807 if (pkgstr == NULL) {
2808 pkgstr = PyUnicode_InternFromString("__package__");
2809 if (pkgstr == NULL)
2810 return NULL;
2811 }
2812
2813 pkgname = PyDict_GetItem(globals, pkgstr);
2814
2815 if ((pkgname != NULL) && (pkgname != Py_None)) {
2816 /* __package__ is set, so use it */
2817 if (!PyUnicode_Check(pkgname)) {
2818 PyErr_SetString(PyExc_ValueError,
2819 "__package__ set to non-string");
2820 return NULL;
2821 }
2822 if (PyUnicode_GET_LENGTH(pkgname) == 0) {
2823 if (level > 0) {
2824 PyErr_SetString(PyExc_ValueError,
2825 "Attempted relative import in non-package");
2826 return NULL;
2827 }
2828 goto return_none;
2829 }
2830 Py_INCREF(pkgname);
2831 nameobj = pkgname;
2832 } else {
2833 /* __package__ not set, so figure it out and set it */
2834 modname = PyDict_GetItem(globals, namestr);
2835 if (modname == NULL || !PyUnicode_Check(modname))
2836 goto return_none;
2837
2838 modpath = PyDict_GetItem(globals, pathstr);
2839 if (modpath != NULL) {
2840 /* __path__ is set, so modname is already the package name */
2841 int error;
2842
2843 error = PyDict_SetItem(globals, pkgstr, modname);
2844 if (error) {
2845 PyErr_SetString(PyExc_ValueError,
2846 "Could not set __package__");
2847 return NULL;
2848 }
2849 Py_INCREF(modname);
2850 nameobj = modname;
2851 } else {
2852 /* Normal module, so work out the package name if any */
2853 Py_ssize_t len;
2854 len = PyUnicode_FindChar(modname, '.',
2855 0, PyUnicode_GET_LENGTH(modname), -1);
2856 if (len == -2)
2857 return NULL;
2858 if (len < 0) {
2859 if (level > 0) {
2860 PyErr_SetString(PyExc_ValueError,
2861 "Attempted relative import in non-package");
2862 return NULL;
2863 }
2864 if (PyDict_SetItem(globals, pkgstr, Py_None)) {
2865 PyErr_SetString(PyExc_ValueError,
2866 "Could not set __package__");
2867 return NULL;
2868 }
2869 goto return_none;
2870 }
2871 pkgname = PyUnicode_Substring(modname, 0, len);
2872 if (pkgname == NULL)
2873 return NULL;
2874 if (PyDict_SetItem(globals, pkgstr, pkgname)) {
2875 Py_DECREF(pkgname);
2876 PyErr_SetString(PyExc_ValueError,
2877 "Could not set __package__");
2878 return NULL;
2879 }
2880 nameobj = pkgname;
2881 }
2882 }
2883 if (level > 1) {
2884 Py_ssize_t dot, end = PyUnicode_GET_LENGTH(nameobj);
2885 PyObject *newname;
2886 while (--level > 0) {
2887 dot = PyUnicode_FindChar(nameobj, '.', 0, end, -1);
2888 if (dot == -2) {
2889 Py_DECREF(nameobj);
2890 return NULL;
2891 }
2892 if (dot < 0) {
2893 Py_DECREF(nameobj);
2894 PyErr_SetString(PyExc_ValueError,
2895 "Attempted relative import beyond "
2896 "toplevel package");
2897 return NULL;
2898 }
2899 end = dot;
2900 }
2901 newname = PyUnicode_Substring(nameobj, 0, end);
2902 Py_DECREF(nameobj);
2903 if (newname == NULL)
2904 return NULL;
2905 nameobj = newname;
2906 }
2907
2908 modules = PyImport_GetModuleDict();
2909 parent = PyDict_GetItem(modules, nameobj);
2910 if (parent == NULL) {
2911 int err;
2912
2913 if (orig_level >= 1) {
2914 PyErr_Format(PyExc_SystemError,
2915 "Parent module %R not loaded, "
2916 "cannot perform relative import", nameobj);
2917 Py_DECREF(nameobj);
2918 return NULL;
2919 }
2920
2921 err = PyErr_WarnFormat(
2922 PyExc_RuntimeWarning, 1,
2923 "Parent module %R not found while handling absolute import",
2924 nameobj);
2925 Py_DECREF(nameobj);
2926 if (err)
2927 return NULL;
2928
2929 goto return_none;
2930 }
2931 *p_name = nameobj;
2932 return parent;
2933 /* We expect, but can't guarantee, if parent != None, that:
2934 - parent.__name__ == name
2935 - parent.__dict__ is globals
2936 If this is violated... Who cares? */
2937
2938 return_none:
2939 nameobj = PyUnicode_New(0, 0);
2940 if (nameobj == NULL)
2941 return NULL;
2942 *p_name = nameobj;
2943 return Py_None;
2944 }
2945
2946 /* altmod is either None or same as mod */
2947 static PyObject *
2948 load_next(PyObject *mod, PyObject *altmod,
2949 PyObject *inputname, PyObject **p_outputname,
2950 PyObject **p_prefix)
2951 {
2952 Py_ssize_t dot;
2953 Py_ssize_t len;
2954 PyObject *fullname, *name = NULL, *result;
2955
2956 *p_outputname = NULL;
2957
2958 len = PyUnicode_GET_LENGTH(inputname);
2959 if (len == 0) {
2960 /* completely empty module name should only happen in
2961 'from . import' (or '__import__("")')*/
2962 Py_INCREF(mod);
2963 return mod;
2964 }
2965
2966
2967 dot = PyUnicode_FindChar(inputname, '.', 0, len, 1);
2968 if (dot >= 0) {
2969 len = dot;
2970 if (len == 0) {
2971 PyErr_SetString(PyExc_ValueError,
2972 "Empty module name");
2973 goto error;
2974 }
2975 }
2976
2977 /* name = inputname[:len] */
2978 name = PyUnicode_Substring(inputname, 0, len);
2979 if (name == NULL)
2980 goto error;
2981
2982 if (PyUnicode_GET_LENGTH(*p_prefix)) {
2983 /* fullname = prefix + "." + name */
2984 fullname = PyUnicode_FromFormat("%U.%U", *p_prefix, name);
2985 if (fullname == NULL)
2986 goto error;
2987 }
2988 else {
2989 fullname = name;
2990 Py_INCREF(fullname);
2991 }
2992
2993 result = import_submodule(mod, name, fullname);
2994 Py_DECREF(*p_prefix);
2995 /* Transfer reference. */
2996 *p_prefix = fullname;
2997 if (result == Py_None && altmod != mod) {
2998 Py_DECREF(result);
2999 /* Here, altmod must be None and mod must not be None */
3000 result = import_submodule(altmod, name, name);
3001 if (result != NULL && result != Py_None) {
3002 if (mark_miss(*p_prefix) != 0) {
3003 Py_DECREF(result);
3004 goto error;
3005 }
3006 Py_DECREF(*p_prefix);
3007 *p_prefix = name;
3008 Py_INCREF(*p_prefix);
3009 }
3010 }
3011 if (result == NULL)
3012 goto error;
3013
3014 if (result == Py_None) {
3015 Py_DECREF(result);
3016 PyErr_Format(PyExc_ImportError,
3017 "No module named %R", inputname);
3018 goto error;
3019 }
3020
3021 if (dot >= 0) {
3022 *p_outputname = PyUnicode_Substring(inputname, dot+1,
3023 PyUnicode_GET_LENGTH(inputname));
3024 if (*p_outputname == NULL) {
3025 Py_DECREF(result);
3026 goto error;
3027 }
3028 }
3029
3030 Py_DECREF(name);
3031 return result;
3032
3033 error:
3034 Py_XDECREF(name);
3035 return NULL;
3036 }
3037
3038 static int
3039 mark_miss(PyObject *name)
3040 {
3041 PyObject *modules = PyImport_GetModuleDict();
3042 return PyDict_SetItem(modules, name, Py_None);
3043 }
3044
3045 static int
3046 ensure_fromlist(PyObject *mod, PyObject *fromlist, PyObject *name,
3047 int recursive)
3048 {
3049 int i;
3050 PyObject *fullname;
3051 Py_ssize_t fromlist_len;
3052
3053 if (!_PyObject_HasAttrId(mod, &PyId___path__))
3054 return 1;
3055
3056 fromlist_len = PySequence_Size(fromlist);
3057
3058 for (i = 0; i < fromlist_len; i++) {
3059 PyObject *item = PySequence_GetItem(fromlist, i);
3060 int hasit;
3061 if (item == NULL)
3062 return 0;
3063 if (!PyUnicode_Check(item)) {
3064 PyErr_SetString(PyExc_TypeError,
3065 "Item in ``from list'' not a string");
3066 Py_DECREF(item);
3067 return 0;
3068 }
3069 if (PyUnicode_READ_CHAR(item, 0) == '*') {
3070 PyObject *all;
3071 _Py_IDENTIFIER(__all__);
3072 Py_DECREF(item);
3073 /* See if the package defines __all__ */
3074 if (recursive)
3075 continue; /* Avoid endless recursion */
3076 all = _PyObject_GetAttrId(mod, &PyId___all__);
3077 if (all == NULL)
3078 PyErr_Clear();
3079 else {
3080 int ret = ensure_fromlist(mod, all, name, 1);
3081 Py_DECREF(all);
3082 if (!ret)
3083 return 0;
3084 }
3085 continue;
3086 }
3087 hasit = PyObject_HasAttr(mod, item);
3088 if (!hasit) {
3089 PyObject *submod;
3090 fullname = PyUnicode_FromFormat("%U.%U", name, item);
3091 if (fullname != NULL) {
3092 submod = import_submodule(mod, item, fullname);
3093 Py_DECREF(fullname);
3094 }
3095 else
3096 submod = NULL;
3097 Py_XDECREF(submod);
3098 if (submod == NULL) {
3099 Py_DECREF(item);
3100 return 0;
3101 }
3102 }
3103 Py_DECREF(item);
3104 }
3105
3106 return 1;
3107 }
3108
3109 static int
3110 add_submodule(PyObject *mod, PyObject *submod, PyObject *fullname,
3111 PyObject *subname, PyObject *modules)
3112 {
3113 if (mod == Py_None)
3114 return 1;
3115 /* Irrespective of the success of this load, make a
3116 reference to it in the parent package module. A copy gets
3117 saved in the modules dictionary under the full name, so get a
3118 reference from there, if need be. (The exception is when the
3119 load failed with a SyntaxError -- then there's no trace in
3120 sys.modules. In that case, of course, do nothing extra.) */
3121 if (submod == NULL) {
3122 submod = PyDict_GetItem(modules, fullname);
3123 if (submod == NULL)
3124 return 1;
3125 }
3126 if (PyModule_Check(mod)) {
3127 /* We can't use setattr here since it can give a
3128 * spurious warning if the submodule name shadows a
3129 * builtin name */
3130 PyObject *dict = PyModule_GetDict(mod);
3131 if (!dict)
3132 return 0;
3133 if (PyDict_SetItem(dict, subname, submod) < 0)
3134 return 0;
3135 }
3136 else {
3137 if (PyObject_SetAttr(mod, subname, submod) < 0)
3138 return 0;
3139 }
3140 return 1;
3141 }
3142
3143 static PyObject *
3144 import_submodule(PyObject *mod, PyObject *subname, PyObject *fullname)
3145 {
3146 PyObject *modules = PyImport_GetModuleDict();
3147 PyObject *m = NULL, *bufobj, *path_list, *loader;
3148 struct filedescr *fdp;
3149 FILE *fp;
3150
3151 /* Require:
3152 if mod == None: subname == fullname
3153 else: mod.__name__ + "." + subname == fullname
3154 */
3155
3156 if ((m = PyDict_GetItem(modules, fullname)) != NULL) {
3157 Py_INCREF(m);
3158 return m;
3159 }
3160
3161 if (mod == Py_None)
3162 path_list = NULL;
3163 else {
3164 path_list = _PyObject_GetAttrId(mod, &PyId___path__);
3165 if (path_list == NULL) {
3166 PyErr_Clear();
3167 Py_INCREF(Py_None);
3168 return Py_None;
3169 }
3170 }
3171
3172 fdp = find_module(fullname, subname, path_list,
3173 &bufobj, &fp, &loader);
3174 Py_XDECREF(path_list);
3175 if (fdp == NULL) {
3176 if (!PyErr_ExceptionMatches(PyExc_ImportError))
3177 return NULL;
3178 PyErr_Clear();
3179 Py_INCREF(Py_None);
3180 return Py_None;
3181 }
3182 m = load_module(fullname, fp, bufobj, fdp->type, loader);
3183 Py_XDECREF(bufobj);
3184 Py_XDECREF(loader);
3185 if (fp)
3186 fclose(fp);
3187 if (m == NULL)
3188 return NULL;
3189 if (!add_submodule(mod, m, fullname, subname, modules)) {
3190 Py_XDECREF(m);
3191 return NULL;
3192 }
3193 return m;
3194 }
3195
3196
3197 /* Re-import a module of any kind and return its module object, WITH
3198 INCREMENTED REFERENCE COUNT */
3199
3200 PyObject *
3201 PyImport_ReloadModule(PyObject *m)
3202 {
3203 PyInterpreterState *interp = PyThreadState_Get()->interp;
3204 PyObject *modules_reloading = interp->modules_reloading;
3205 PyObject *modules = PyImport_GetModuleDict();
3206 PyObject *path_list = NULL, *loader = NULL, *existing_m = NULL;
3207 PyObject *name, *bufobj, *subname;
3208 Py_ssize_t subname_start;
3209 struct filedescr *fdp;
3210 FILE *fp = NULL;
3211 PyObject *newm = NULL;
3212
3213 if (modules_reloading == NULL) {
3214 Py_FatalError("PyImport_ReloadModule: "
3215 "no modules_reloading dictionary!");
3216 return NULL;
3217 }
3218
3219 if (m == NULL || !PyModule_Check(m)) {
3220 PyErr_SetString(PyExc_TypeError,
3221 "reload() argument must be module");
3222 return NULL;
3223 }
3224 name = PyModule_GetNameObject(m);
3225 if (name == NULL || PyUnicode_READY(name) == -1)
3226 return NULL;
3227 if (m != PyDict_GetItem(modules, name)) {
3228 PyErr_Format(PyExc_ImportError,
3229 "reload(): module %R not in sys.modules",
3230 name);
3231 Py_DECREF(name);
3232 return NULL;
3233 }
3234 existing_m = PyDict_GetItem(modules_reloading, name);
3235 if (existing_m != NULL) {
3236 /* Due to a recursive reload, this module is already
3237 being reloaded. */
3238 Py_DECREF(name);
3239 Py_INCREF(existing_m);
3240 return existing_m;
3241 }
3242 if (PyDict_SetItem(modules_reloading, name, m) < 0) {
3243 Py_DECREF(name);
3244 return NULL;
3245 }
3246
3247 subname_start = PyUnicode_FindChar(name, '.', 0,
3248 PyUnicode_GET_LENGTH(name), -1);
3249 if (subname_start == -1) {
3250 Py_INCREF(name);
3251 subname = name;
3252 }
3253 else {
3254 PyObject *parentname, *parent;
3255 Py_ssize_t len;
3256 parentname = PyUnicode_Substring(name, 0, subname_start);
3257 if (parentname == NULL) {
3258 goto error;
3259 }
3260 parent = PyDict_GetItem(modules, parentname);
3261 if (parent == NULL) {
3262 PyErr_Format(PyExc_ImportError,
3263 "reload(): parent %R not in sys.modules",
3264 parentname);
3265 Py_DECREF(parentname);
3266 goto error;
3267 }
3268 Py_DECREF(parentname);
3269 path_list = _PyObject_GetAttrId(parent, &PyId___path__);
3270 if (path_list == NULL)
3271 PyErr_Clear();
3272 subname_start++;
3273 len = PyUnicode_GET_LENGTH(name) - (subname_start + 1);
3274 subname = PyUnicode_Substring(name, subname_start, len);
3275 }
3276 if (subname == NULL)
3277 goto error;
3278 fdp = find_module(name, subname, path_list,
3279 &bufobj, &fp, &loader);
3280 Py_DECREF(subname);
3281 Py_XDECREF(path_list);
3282
3283 if (fdp == NULL) {
3284 Py_XDECREF(loader);
3285 goto error;
3286 }
3287
3288 newm = load_module(name, fp, bufobj, fdp->type, loader);
3289 Py_XDECREF(bufobj);
3290 Py_XDECREF(loader);
3291
3292 if (fp)
3293 fclose(fp);
3294 if (newm == NULL) {
3295 /* load_module probably removed name from modules because of
3296 * the error. Put back the original module object. We're
3297 * going to return NULL in this case regardless of whether
3298 * replacing name succeeds, so the return value is ignored.
3299 */
3300 PyDict_SetItem(modules, name, m);
3301 }
3302
3303 error:
3304 imp_modules_reloading_clear();
3305 Py_DECREF(name);
3306 return newm;
3307 }
3308
3309
3310 /* Higher-level import emulator which emulates the "import" statement
3311 more accurately -- it invokes the __import__() function from the
3312 builtins of the current globals. This means that the import is
3313 done using whatever import hooks are installed in the current
3314 environment.
3315 A dummy list ["__doc__"] is passed as the 4th argument so that
3316 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
3317 will return <module "gencache"> instead of <module "win32com">. */
3318
3319 PyObject *
3320 PyImport_Import(PyObject *module_name)
3321 {
3322 static PyObject *silly_list = NULL;
3323 static PyObject *builtins_str = NULL;
3324 static PyObject *import_str = NULL;
3325 PyObject *globals = NULL;
3326 PyObject *import = NULL;
3327 PyObject *builtins = NULL;
3328 PyObject *modules = NULL;
3329 PyObject *r = NULL;
3330
3331 /* Initialize constant string objects */
3332 if (silly_list == NULL) {
3333 import_str = PyUnicode_InternFromString("__import__");
3334 if (import_str == NULL)
3335 return NULL;
3336 builtins_str = PyUnicode_InternFromString("__builtins__");
3337 if (builtins_str == NULL)
3338 return NULL;
3339 silly_list = PyList_New(0);
3340 if (silly_list == NULL)
3341 return NULL;
3342 }
3343
3344 /* Get the builtins from current globals */
3345 globals = PyEval_GetGlobals();
3346 if (globals != NULL) {
3347 Py_INCREF(globals);
3348 builtins = PyObject_GetItem(globals, builtins_str);
3349 if (builtins == NULL)
3350 goto err;
3351 }
3352 else {
3353 /* No globals -- use standard builtins, and fake globals */
3354 builtins = PyImport_ImportModuleLevel("builtins",
3355 NULL, NULL, NULL, 0);
3356 if (builtins == NULL)
3357 return NULL;
3358 globals = Py_BuildValue("{OO}", builtins_str, builtins);
3359 if (globals == NULL)
3360 goto err;
3361 }
3362
3363 /* Get the __import__ function from the builtins */
3364 if (PyDict_Check(builtins)) {
3365 import = PyObject_GetItem(builtins, import_str);
3366 if (import == NULL)
3367 PyErr_SetObject(PyExc_KeyError, import_str);
3368 }
3369 else
3370 import = PyObject_GetAttr(builtins, import_str);
3371 if (import == NULL)
3372 goto err;
3373
3374 /* Call the __import__ function with the proper argument list
3375 Always use absolute import here.
3376 Calling for side-effect of import. */
3377 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
3378 globals, silly_list, 0, NULL);
3379 if (r == NULL)
3380 goto err;
3381 Py_DECREF(r);
3382
3383 modules = PyImport_GetModuleDict();
3384 r = PyDict_GetItem(modules, module_name);
3385 if (r != NULL)
3386 Py_INCREF(r);
3387
3388 err:
3389 Py_XDECREF(globals);
3390 Py_XDECREF(builtins);
3391 Py_XDECREF(import);
3392
3393 return r;
3394 }
3395
3396
3397 /* Module 'imp' provides Python access to the primitives used for
3398 importing modules.
3399 */
3400
3401 static PyObject *
3402 imp_make_magic(long magic)
3403 {
3404 char buf[4];
3405
3406 buf[0] = (char) ((magic >> 0) & 0xff);
3407 buf[1] = (char) ((magic >> 8) & 0xff);
3408 buf[2] = (char) ((magic >> 16) & 0xff);
3409 buf[3] = (char) ((magic >> 24) & 0xff);
3410
3411 return PyBytes_FromStringAndSize(buf, 4);
3412 }
3413
3414 static PyObject *
3415 imp_get_magic(PyObject *self, PyObject *noargs)
3416 {
3417 return imp_make_magic(pyc_magic);
3418 }
3419
3420 static PyObject *
3421 imp_get_tag(PyObject *self, PyObject *noargs)
3422 {
3423 return PyUnicode_FromString(pyc_tag);
3424 }
3425
3426 static PyObject *
3427 imp_get_suffixes(PyObject *self, PyObject *noargs)
3428 {
3429 PyObject *list;
3430 struct filedescr *fdp;
3431
3432 list = PyList_New(0);
3433 if (list == NULL)
3434 return NULL;
3435 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
3436 PyObject *item = Py_BuildValue("ssi",
3437 fdp->suffix, fdp->mode, fdp->type);
3438 if (item == NULL) {
3439 Py_DECREF(list);
3440 return NULL;
3441 }
3442 if (PyList_Append(list, item) < 0) {
3443 Py_DECREF(list);
3444 Py_DECREF(item);
3445 return NULL;
3446 }
3447 Py_DECREF(item);
3448 }
3449 return list;
3450 }
3451
3452 static PyObject *
3453 call_find_module(PyObject *name, PyObject *path_list)
3454 {
3455 extern int fclose(FILE *);
3456 PyObject *fob, *ret;
3457 PyObject *pathobj;
3458 struct filedescr *fdp;
3459 FILE *fp;
3460 int fd = -1;
3461 char *found_encoding = NULL;
3462 char *encoding = NULL;
3463
3464 if (path_list == Py_None)
3465 path_list = NULL;
3466 fdp = find_module(NULL, name, path_list,
3467 &pathobj, &fp, NULL);
3468 if (fdp == NULL)
3469 return NULL;
3470 if (fp != NULL) {
3471 fd = fileno(fp);
3472 if (fd != -1)
3473 fd = dup(fd);
3474 fclose(fp);
3475 if (fd == -1) {
3476 PyErr_SetFromErrno(PyExc_OSError);
3477 return NULL;
3478 }
3479 fp = NULL;
3480 }
3481 if (fd != -1) {
3482 if (strchr(fdp->mode, 'b') == NULL) {
3483 /* PyTokenizer_FindEncodingFilename() returns PyMem_MALLOC'ed
3484 memory. */
3485 found_encoding = PyTokenizer_FindEncodingFilename(fd, pathobj);
3486 lseek(fd, 0, 0); /* Reset position */
3487 if (found_encoding == NULL && PyErr_Occurred()) {
3488 Py_XDECREF(pathobj);
3489 return NULL;
3490 }
3491 encoding = (found_encoding != NULL) ? found_encoding :
3492 (char*)PyUnicode_GetDefaultEncoding();
3493 }
3494 fob = PyFile_FromFd(fd, NULL, fdp->mode, -1,
3495 (char*)encoding, NULL, NULL, 1);
3496 if (fob == NULL) {
3497 Py_XDECREF(pathobj);
3498 close(fd);
3499 PyMem_FREE(found_encoding);
3500 return NULL;
3501 }
3502 }
3503 else {
3504 fob = Py_None;
3505 Py_INCREF(fob);
3506 }
3507 if (pathobj == NULL) {
3508 Py_INCREF(Py_None);
3509 pathobj = Py_None;
3510 }
3511 ret = Py_BuildValue("NN(ssi)",
3512 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
3513 PyMem_FREE(found_encoding);
3514
3515 return ret;
3516 }
3517
3518 static PyObject *
3519 imp_find_module(PyObject *self, PyObject *args)
3520 {
3521 PyObject *name, *path_list = NULL;
3522 if (!PyArg_ParseTuple(args, "U|O:find_module",
3523 &name, &path_list))
3524 return NULL;
3525 return call_find_module(name, path_list);
3526 }
3527
3528 static PyObject *
3529 imp_init_builtin(PyObject *self, PyObject *args)
3530 {
3531 PyObject *name;
3532 int ret;
3533 PyObject *m;
3534 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
3535 return NULL;
3536 ret = init_builtin(name);
3537 if (ret < 0)
3538 return NULL;
3539 if (ret == 0) {
3540 Py_INCREF(Py_None);
3541 return Py_None;
3542 }
3543 m = PyImport_AddModuleObject(name);
3544 Py_XINCREF(m);
3545 return m;
3546 }
3547
3548 static PyObject *
3549 imp_init_frozen(PyObject *self, PyObject *args)
3550 {
3551 PyObject *name;
3552 int ret;
3553 PyObject *m;
3554 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
3555 return NULL;
3556 ret = PyImport_ImportFrozenModuleObject(name);
3557 if (ret < 0)
3558 return NULL;
3559 if (ret == 0) {
3560 Py_INCREF(Py_None);
3561 return Py_None;
3562 }
3563 m = PyImport_AddModuleObject(name);
3564 Py_XINCREF(m);
3565 return m;
3566 }
3567
3568 static PyObject *
3569 imp_get_frozen_object(PyObject *self, PyObject *args)
3570 {
3571 PyObject *name;
3572
3573 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
3574 return NULL;
3575 return get_frozen_object(name);
3576 }
3577
3578 static PyObject *
3579 imp_is_frozen_package(PyObject *self, PyObject *args)
3580 {
3581 PyObject *name;
3582
3583 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
3584 return NULL;
3585 return is_frozen_package(name);
3586 }
3587
3588 static PyObject *
3589 imp_is_builtin(PyObject *self, PyObject *args)
3590 {
3591 PyObject *name;
3592 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
3593 return NULL;
3594 return PyLong_FromLong(is_builtin(name));
3595 }
3596
3597 static PyObject *
3598 imp_is_frozen(PyObject *self, PyObject *args)
3599 {
3600 PyObject *name;
3601 struct _frozen *p;
3602 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
3603 return NULL;
3604 p = find_frozen(name);
3605 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
3606 }
3607
3608 static FILE *
3609 get_file(PyObject *pathname, PyObject *fob, char *mode)
3610 {
3611 FILE *fp;
3612 if (mode[0] == 'U')
3613 mode = "r" PY_STDIOTEXTMODE;
3614 if (fob == NULL) {
3615 fp = _Py_fopen(pathname, mode);
3616 }
3617 else {
3618 int fd = PyObject_AsFileDescriptor(fob);
3619 if (fd == -1)
3620 return NULL;
3621 if (!_PyVerify_fd(fd))
3622 goto error;
3623 /* the FILE struct gets a new fd, so that it can be closed
3624 * independently of the file descriptor given
3625 */
3626 fd = dup(fd);
3627 if (fd == -1)
3628 goto error;
3629 fp = fdopen(fd, mode);
3630 }
3631 if (fp)
3632 return fp;
3633 error:
3634 PyErr_SetFromErrno(PyExc_IOError);
3635 return NULL;
3636 }
3637
3638 static PyObject *
3639 imp_load_compiled(PyObject *self, PyObject *args)
3640 {
3641 PyObject *name, *pathname;
3642 PyObject *fob = NULL;
3643 PyObject *m;
3644 FILE *fp;
3645 if (!PyArg_ParseTuple(args, "UO&|O:load_compiled",
3646 &name,
3647 PyUnicode_FSDecoder, &pathname,
3648 &fob))
3649 return NULL;
3650 fp = get_file(pathname, fob, "rb");
3651 if (fp == NULL) {
3652 Py_DECREF(pathname);
3653 return NULL;
3654 }
3655 m = load_compiled_module(name, pathname, fp);
3656 fclose(fp);
3657 Py_DECREF(pathname);
3658 return m;
3659 }
3660
3661 #ifdef HAVE_DYNAMIC_LOADING
3662
3663 static PyObject *
3664 imp_load_dynamic(PyObject *self, PyObject *args)
3665 {
3666 PyObject *name, *pathname, *fob = NULL, *mod;
3667 FILE *fp;
3668
3669 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
3670 &name, PyUnicode_FSDecoder, &pathname, &fob))
3671 return NULL;
3672 if (fob != NULL) {
3673 fp = get_file(NULL, fob, "r");
3674 if (fp == NULL) {
3675 Py_DECREF(pathname);
3676 return NULL;
3677 }
3678 }
3679 else
3680 fp = NULL;
3681 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
3682 Py_DECREF(pathname);
3683 if (fp)
3684 fclose(fp);
3685 return mod;
3686 }
3687
3688 #endif /* HAVE_DYNAMIC_LOADING */
3689
3690 static PyObject *
3691 imp_load_source(PyObject *self, PyObject *args)
3692 {
3693 PyObject *name, *pathname;
3694 PyObject *fob = NULL;
3695 PyObject *m;
3696 FILE *fp;
3697 if (!PyArg_ParseTuple(args, "UO&|O:load_source",
3698 &name,
3699 PyUnicode_FSDecoder, &pathname,
3700 &fob))
3701 return NULL;
3702 fp = get_file(pathname, fob, "r");
3703 if (fp == NULL) {
3704 Py_DECREF(pathname);
3705 return NULL;
3706 }
3707 m = load_source_module(name, pathname, fp);
3708 Py_DECREF(pathname);
3709 fclose(fp);
3710 return m;
3711 }
3712
3713 static PyObject *
3714 imp_load_module(PyObject *self, PyObject *args)
3715 {
3716 PyObject *name, *fob, *pathname, *pathname_obj, *ret;
3717 char *suffix; /* Unused */
3718 char *mode;
3719 int type;
3720 FILE *fp;
3721
3722 if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
3723 &name, &fob, &pathname_obj, &suffix, &mode, &type))
3724 return NULL;
3725 if (pathname_obj != Py_None) {
3726 if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
3727 return NULL;
3728 }
3729 else
3730 pathname = NULL;
3731
3732 if (*mode) {
3733 /* Mode must start with 'r' or 'U' and must not contain '+'.
3734 Implicit in this test is the assumption that the mode
3735 may contain other modifiers like 'b' or 't'. */
3736
3737 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3738 PyErr_Format(PyExc_ValueError,
3739 "invalid file open mode %.200s", mode);
3740 Py_XDECREF(pathname);
3741 return NULL;
3742 }
3743 }
3744 if (fob == Py_None)
3745 fp = NULL;
3746 else {
3747 fp = get_file(NULL, fob, mode);
3748 if (fp == NULL) {
3749 Py_XDECREF(pathname);
3750 return NULL;
3751 }
3752 }
3753 ret = load_module(name, fp, pathname, type, NULL);
3754 Py_XDECREF(pathname);
3755 if (fp)
3756 fclose(fp);
3757 return ret;
3758 }
3759
3760 static PyObject *
3761 imp_load_package(PyObject *self, PyObject *args)
3762 {
3763 PyObject *name, *pathname;
3764 PyObject * ret;
3765 if (!PyArg_ParseTuple(args, "UO&:load_package",
3766 &name, PyUnicode_FSDecoder, &pathname))
3767 return NULL;
3768 ret = load_package(name, pathname);
3769 Py_DECREF(pathname);
3770 return ret;
3771 }
3772
3773 static PyObject *
3774 imp_new_module(PyObject *self, PyObject *args)
3775 {
3776 PyObject *name;
3777 if (!PyArg_ParseTuple(args, "U:new_module", &name))
3778 return NULL;
3779 return PyModule_NewObject(name);
3780 }
3781
3782 static PyObject *
3783 imp_reload(PyObject *self, PyObject *v)
3784 {
3785 return PyImport_ReloadModule(v);
3786 }
3787
3788 PyDoc_STRVAR(doc_reload,
3789 "reload(module) -> module\n\
3790 \n\
3791 Reload the module. The module must have been successfully imported before.");
3792
3793 static PyObject *
3794 imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws)
3795 {
3796 static char *kwlist[] = {"path", "debug_override", NULL};
3797
3798 PyObject *pathname, *cpathname;
3799 PyObject *debug_override = NULL;
3800 int debug = !Py_OptimizeFlag;
3801
3802 if (!PyArg_ParseTupleAndKeywords(
3803 args, kws, "O&|O", kwlist,
3804 PyUnicode_FSDecoder, &pathname, &debug_override))
3805 return NULL;
3806
3807 if (debug_override != NULL &&
3808 (debug = PyObject_IsTrue(debug_override)) < 0) {
3809 Py_DECREF(pathname);
3810 return NULL;
3811 }
3812
3813 if (PyUnicode_READY(pathname) < 0)
3814 return NULL;
3815
3816 cpathname = make_compiled_pathname(pathname, debug);
3817 Py_DECREF(pathname);
3818
3819 if (cpathname == NULL) {
3820 PyErr_Format(PyExc_SystemError, "path buffer too short");
3821 return NULL;
3822 }
3823 return cpathname;
3824 }
3825
3826 PyDoc_STRVAR(doc_cache_from_source,
3827 "cache_from_source(path, [debug_override]) -> path\n\
3828 Given the path to a .py file, return the path to its .pyc/.pyo file.\n\
3829 \n\
3830 The .py file does not need to exist; this simply returns the path to the\n\
3831 .pyc/.pyo file calculated as if the .py file were imported. The extension\n\
3832 will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\
3833 \n\
3834 If debug_override is not None, then it must be a boolean and is taken as\n\
3835 the value of __debug__ instead.");
3836
3837 static PyObject *
3838 imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)
3839 {
3840 static char *kwlist[] = {"path", NULL};
3841 PyObject *pathname, *source;
3842
3843 if (!PyArg_ParseTupleAndKeywords(
3844 args, kws, "O&", kwlist,
3845 PyUnicode_FSDecoder, &pathname))
3846 return NULL;
3847
3848 source = make_source_pathname(pathname);
3849 if (source == NULL) {
3850 PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R",
3851 pathname);
3852 Py_DECREF(pathname);
3853 return NULL;
3854 }
3855 Py_DECREF(pathname);
3856 return source;
3857 }
3858
3859 PyDoc_STRVAR(doc_source_from_cache,
3860 "source_from_cache(path) -> path\n\
3861 Given the path to a .pyc./.pyo file, return the path to its .py file.\n\
3862 \n\
3863 The .pyc/.pyo file does not need to exist; this simply returns the path to\n\
3864 the .py file calculated to correspond to the .pyc/.pyo file. If path\n\
3865 does not conform to PEP 3147 format, ValueError will be raised.");
3866
3867 /* Doc strings */
3868
3869 PyDoc_STRVAR(doc_imp,
3870 "This module provides the components needed to build your own\n\
3871 __import__ function. Undocumented functions are obsolete.");
3872
3873 PyDoc_STRVAR(doc_find_module,
3874 "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
3875 Search for a module. If path is omitted or None, search for a\n\
3876 built-in, frozen or special module and continue search in sys.path.\n\
3877 The module name cannot contain '.'; to search for a submodule of a\n\
3878 package, pass the submodule name and the package's __path__.");
3879
3880 PyDoc_STRVAR(doc_load_module,
3881 "load_module(name, file, filename, (suffix, mode, type)) -> module\n\
3882 Load a module, given information returned by find_module().\n\
3883 The module name must include the full package name, if any.");
3884
3885 PyDoc_STRVAR(doc_get_magic,
3886 "get_magic() -> string\n\
3887 Return the magic number for .pyc or .pyo files.");
3888
3889 PyDoc_STRVAR(doc_get_tag,
3890 "get_tag() -> string\n\
3891 Return the magic tag for .pyc or .pyo files.");
3892
3893 PyDoc_STRVAR(doc_get_suffixes,
3894 "get_suffixes() -> [(suffix, mode, type), ...]\n\
3895 Return a list of (suffix, mode, type) tuples describing the files\n\
3896 that find_module() looks for.");
3897
3898 PyDoc_STRVAR(doc_new_module,
3899 "new_module(name) -> module\n\
3900 Create a new module. Do not enter it in sys.modules.\n\
3901 The module name must include the full package name, if any.");
3902
3903 PyDoc_STRVAR(doc_lock_held,
3904 "lock_held() -> boolean\n\
3905 Return True if the import lock is currently held, else False.\n\
3906 On platforms without threads, return False.");
3907
3908 PyDoc_STRVAR(doc_acquire_lock,
3909 "acquire_lock() -> None\n\
3910 Acquires the interpreter's import lock for the current thread.\n\
3911 This lock should be used by import hooks to ensure thread-safety\n\
3912 when importing modules.\n\
3913 On platforms without threads, this function does nothing.");
3914
3915 PyDoc_STRVAR(doc_release_lock,
3916 "release_lock() -> None\n\
3917 Release the interpreter's import lock.\n\
3918 On platforms without threads, this function does nothing.");
3919
3920 static PyMethodDef imp_methods[] = {
3921 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3922 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3923 {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag},
3924 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3925 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3926 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3927 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3928 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3929 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
3930 {"reload", imp_reload, METH_O, doc_reload},
3931 {"cache_from_source", (PyCFunction)imp_cache_from_source,
3932 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source},
3933 {"source_from_cache", (PyCFunction)imp_source_from_cache,
3934 METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},
3935 /* The rest are obsolete */
3936 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3937 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
3938 {"init_builtin", imp_init_builtin, METH_VARARGS},
3939 {"init_frozen", imp_init_frozen, METH_VARARGS},
3940 {"is_builtin", imp_is_builtin, METH_VARARGS},
3941 {"is_frozen", imp_is_frozen, METH_VARARGS},
3942 {"load_compiled", imp_load_compiled, METH_VARARGS},
3943 #ifdef HAVE_DYNAMIC_LOADING
3944 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
3945 #endif
3946 {"load_package", imp_load_package, METH_VARARGS},
3947 {"load_source", imp_load_source, METH_VARARGS},
3948 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
3949 {NULL, NULL} /* sentinel */
3950 };
3951
3952 static int
3953 setint(PyObject *d, char *name, int value)
3954 {
3955 PyObject *v;
3956 int err;
3957
3958 v = PyLong_FromLong((long)value);
3959 err = PyDict_SetItemString(d, name, v);
3960 Py_XDECREF(v);
3961 return err;
3962 }
3963
3964 typedef struct {
3965 PyObject_HEAD
3966 } NullImporter;
3967
3968 static int
3969 NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3970 {
3971 #ifndef MS_WINDOWS
3972 PyObject *path;
3973 struct stat statbuf;
3974 int rv;
3975
3976 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3977 return -1;
3978
3979 if (!PyArg_ParseTuple(args, "O&:NullImporter",
3980 PyUnicode_FSConverter, &path))
3981 return -1;
3982
3983 if (PyBytes_GET_SIZE(path) == 0) {
3984 Py_DECREF(path);
3985 PyErr_SetString(PyExc_ImportError, "empty pathname");
3986 return -1;
3987 }
3988
3989 rv = stat(PyBytes_AS_STRING(path), &statbuf);
3990 Py_DECREF(path);
3991 if (rv == 0) {
3992 /* it exists */
3993 if (S_ISDIR(statbuf.st_mode)) {
3994 /* it's a directory */
3995 PyErr_SetString(PyExc_ImportError, "existing directory");
3996 return -1;
3997 }
3998 }
3999 #else /* MS_WINDOWS */
4000 PyObject *pathobj;
4001 DWORD rv;
4002 wchar_t *path;
4003
4004 if (!_PyArg_NoKeywords("NullImporter()", kwds))
4005 return -1;
4006
4007 if (!PyArg_ParseTuple(args, "U:NullImporter",
4008 &pathobj))
4009 return -1;
4010
4011 if (PyUnicode_GET_LENGTH(pathobj) == 0) {
4012 PyErr_SetString(PyExc_ImportError, "empty pathname");
4013 return -1;
4014 }
4015
4016 path = PyUnicode_AsWideCharString(pathobj, NULL);
4017 if (path == NULL)
4018 return -1;
4019 /* see issue1293 and issue3677:
4020 * stat() on Windows doesn't recognise paths like
4021 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
4022 */
4023 rv = GetFileAttributesW(path);
4024 PyMem_Free(path);
4025 if (rv != INVALID_FILE_ATTRIBUTES) {
4026 /* it exists */
4027 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
4028 /* it's a directory */
4029 PyErr_SetString(PyExc_ImportError, "existing directory");
4030 return -1;
4031 }
4032 }
4033 #endif
4034 return 0;
4035 }
4036
4037 static PyObject *
4038 NullImporter_find_module(NullImporter *self, PyObject *args)
4039 {
4040 Py_RETURN_NONE;
4041 }
4042
4043 static PyMethodDef NullImporter_methods[] = {
4044 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
4045 "Always return None"
4046 },
4047 {NULL} /* Sentinel */
4048 };
4049
4050
4051 PyTypeObject PyNullImporter_Type = {
4052 PyVarObject_HEAD_INIT(NULL, 0)
4053 "imp.NullImporter", /*tp_name*/
4054 sizeof(NullImporter), /*tp_basicsize*/
4055 0, /*tp_itemsize*/
4056 0, /*tp_dealloc*/
4057 0, /*tp_print*/
4058 0, /*tp_getattr*/
4059 0, /*tp_setattr*/
4060 0, /*tp_reserved*/
4061 0, /*tp_repr*/
4062 0, /*tp_as_number*/
4063 0, /*tp_as_sequence*/
4064 0, /*tp_as_mapping*/
4065 0, /*tp_hash */
4066 0, /*tp_call*/
4067 0, /*tp_str*/
4068 0, /*tp_getattro*/
4069 0, /*tp_setattro*/
4070 0, /*tp_as_buffer*/
4071 Py_TPFLAGS_DEFAULT, /*tp_flags*/
4072 "Null importer object", /* tp_doc */
4073 0, /* tp_traverse */
4074 0, /* tp_clear */
4075 0, /* tp_richcompare */
4076 0, /* tp_weaklistoffset */
4077 0, /* tp_iter */
4078 0, /* tp_iternext */
4079 NullImporter_methods, /* tp_methods */
4080 0, /* tp_members */
4081 0, /* tp_getset */
4082 0, /* tp_base */
4083 0, /* tp_dict */
4084 0, /* tp_descr_get */
4085 0, /* tp_descr_set */
4086 0, /* tp_dictoffset */
4087 (initproc)NullImporter_init, /* tp_init */
4088 0, /* tp_alloc */
4089 PyType_GenericNew /* tp_new */
4090 };
4091
4092 static struct PyModuleDef impmodule = {
4093 PyModuleDef_HEAD_INIT,
4094 "imp",
4095 doc_imp,
4096 0,
4097 imp_methods,
4098 NULL,
4099 NULL,
4100 NULL,
4101 NULL
4102 };
4103
4104 PyMODINIT_FUNC
4105 PyInit_imp(void)
4106 {
4107 PyObject *m, *d;
4108
4109 if (PyType_Ready(&PyNullImporter_Type) < 0)
4110 return NULL;
4111
4112 m = PyModule_Create(&impmodule);
4113 if (m == NULL)
4114 goto failure;
4115 d = PyModule_GetDict(m);
4116 if (d == NULL)
4117 goto failure;
4118
4119 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
4120 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
4121 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
4122 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
4123 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
4124 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
4125 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
4126 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
4127 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
4128 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
4129
4130 Py_INCREF(&PyNullImporter_Type);
4131 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
4132 return m;
4133 failure:
4134 Py_XDECREF(m);
4135 return NULL;
4136 }
4137
4138
4139 /* API for embedding applications that want to add their own entries
4140 to the table of built-in modules. This should normally be called
4141 *before* Py_Initialize(). When the table resize fails, -1 is
4142 returned and the existing table is unchanged.
4143
4144 After a similar function by Just van Rossum. */
4145
4146 int
4147 PyImport_ExtendInittab(struct _inittab *newtab)
4148 {
4149 static struct _inittab *our_copy = NULL;
4150 struct _inittab *p;
4151 int i, n;
4152
4153 /* Count the number of entries in both tables */
4154 for (n = 0; newtab[n].name != NULL; n++)
4155 ;
4156 if (n == 0)
4157 return 0; /* Nothing to do */
4158 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
4159 ;
4160
4161 /* Allocate new memory for the combined table */
4162 p = our_copy;
4163 PyMem_RESIZE(p, struct _inittab, i+n+1);
4164 if (p == NULL)
4165 return -1;
4166
4167 /* Copy the tables into the new memory */
4168 if (our_copy != PyImport_Inittab)
4169 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
4170 PyImport_Inittab = our_copy = p;
4171 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
4172
4173 return 0;
4174 }
4175
4176 /* Shorthand to add a single entry given a name and a function */
4177
4178 int
4179 PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
4180 {
4181 struct _inittab newtab[2];
4182
4183 memset(newtab, '\0', sizeof newtab);
4184
4185 newtab[0].name = (char *)name;
4186 newtab[0].initfunc = initfunc;
4187
4188 return PyImport_ExtendInittab(newtab);
4189 }
4190