Tagging 3.0b1
[python/dscho.git] / Python / import.c
blob14cda6e2728ce6d98ebc2cc707227b91a3d64741
2 /* Module definition and import implementation */
4 #include "Python.h"
6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */
8 #include "pyarena.h"
9 #include "pythonrun.h"
10 #include "errcode.h"
11 #include "marshal.h"
12 #include "code.h"
13 #include "compile.h"
14 #include "eval.h"
15 #include "osdefs.h"
16 #include "importdl.h"
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
25 #ifdef MS_WINDOWS
26 /* for stat.st_mode */
27 typedef unsigned short mode_t;
28 #endif
30 extern time_t PyOS_GetLastModificationTime(char *, FILE *);
31 /* In getmtime.c */
33 /* Magic word to reject .pyc files generated by other Python versions.
34 It should change for each incompatible change to the bytecode.
36 The value of CR and LF is incorporated so if you ever read or write
37 a .pyc file in text mode the magic number will be wrong; also, the
38 Apple MPW compiler swaps their values, botching string constants.
40 The magic numbers must be spaced apart at least 2 values, as the
41 -U interpeter flag will cause MAGIC+1 being used. They have been
42 odd numbers for some time now.
44 There were a variety of old schemes for setting the magic number.
45 The current working scheme is to increment the previous value by
46 10.
48 Known values:
49 Python 1.5: 20121
50 Python 1.5.1: 20121
51 Python 1.5.2: 20121
52 Python 1.6: 50428
53 Python 2.0: 50823
54 Python 2.0.1: 50823
55 Python 2.1: 60202
56 Python 2.1.1: 60202
57 Python 2.1.2: 60202
58 Python 2.2: 60717
59 Python 2.3a0: 62011
60 Python 2.3a0: 62021
61 Python 2.3a0: 62011 (!)
62 Python 2.4a0: 62041
63 Python 2.4a3: 62051
64 Python 2.4b1: 62061
65 Python 2.5a0: 62071
66 Python 2.5a0: 62081 (ast-branch)
67 Python 2.5a0: 62091 (with)
68 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
69 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
70 Python 2.5b3: 62111 (fix wrong code: x += yield)
71 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
72 storing constants that should have been removed)
73 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
74 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
75 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
76 Python 3000: 3000
77 3010 (removed UNARY_CONVERT)
78 3020 (added BUILD_SET)
79 3030 (added keyword-only parameters)
80 3040 (added signature annotations)
81 3050 (print becomes a function)
82 3060 (PEP 3115 metaclass syntax)
83 3070 (PEP 3109 raise changes)
84 3080 (PEP 3137 make __file__ and __name__ unicode)
85 3090 (kill str8 interning)
86 3100 (merge from 2.6a0, see 62151)
87 3102 (__file__ points to source file)
88 Python 3.0a4: 3110 (WITH_CLEANUP optimization).
89 Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT)
91 #define MAGIC (3130 | ((long)'\r'<<16) | ((long)'\n'<<24))
93 /* Magic word as global; note that _PyImport_Init() can change the
94 value of this global to accommodate for alterations of how the
95 compiler works which are enabled by command line switches. */
96 static long pyc_magic = MAGIC;
98 /* See _PyImport_FixupExtension() below */
99 static PyObject *extensions = NULL;
101 /* This table is defined in config.c: */
102 extern struct _inittab _PyImport_Inittab[];
104 /* Method from Parser/tokenizer.c */
105 extern char * PyTokenizer_FindEncoding(int);
107 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
109 /* these tables define the module suffixes that Python recognizes */
110 struct filedescr * _PyImport_Filetab = NULL;
112 static const struct filedescr _PyImport_StandardFiletab[] = {
113 {".py", "U", PY_SOURCE},
114 #ifdef MS_WINDOWS
115 {".pyw", "U", PY_SOURCE},
116 #endif
117 {".pyc", "rb", PY_COMPILED},
118 {0, 0}
122 /* Initialize things */
124 void
125 _PyImport_Init(void)
127 const struct filedescr *scan;
128 struct filedescr *filetab;
129 int countD = 0;
130 int countS = 0;
132 /* prepare _PyImport_Filetab: copy entries from
133 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
135 #ifdef HAVE_DYNAMIC_LOADING
136 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
137 ++countD;
138 #endif
139 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
140 ++countS;
141 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
142 if (filetab == NULL)
143 Py_FatalError("Can't initialize import file table.");
144 #ifdef HAVE_DYNAMIC_LOADING
145 memcpy(filetab, _PyImport_DynLoadFiletab,
146 countD * sizeof(struct filedescr));
147 #endif
148 memcpy(filetab + countD, _PyImport_StandardFiletab,
149 countS * sizeof(struct filedescr));
150 filetab[countD + countS].suffix = NULL;
152 _PyImport_Filetab = filetab;
154 if (Py_OptimizeFlag) {
155 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
156 for (; filetab->suffix != NULL; filetab++) {
157 if (strcmp(filetab->suffix, ".pyc") == 0)
158 filetab->suffix = ".pyo";
163 /* Fix the pyc_magic so that byte compiled code created
164 using the all-Unicode method doesn't interfere with
165 code created in normal operation mode. */
166 pyc_magic = MAGIC + 1;
170 void
171 _PyImportHooks_Init(void)
173 PyObject *v, *path_hooks = NULL, *zimpimport;
174 int err = 0;
176 /* adding sys.path_hooks and sys.path_importer_cache, setting up
177 zipimport */
178 if (PyType_Ready(&PyNullImporter_Type) < 0)
179 goto error;
181 if (Py_VerboseFlag)
182 PySys_WriteStderr("# installing zipimport hook\n");
184 v = PyList_New(0);
185 if (v == NULL)
186 goto error;
187 err = PySys_SetObject("meta_path", v);
188 Py_DECREF(v);
189 if (err)
190 goto error;
191 v = PyDict_New();
192 if (v == NULL)
193 goto error;
194 err = PySys_SetObject("path_importer_cache", v);
195 Py_DECREF(v);
196 if (err)
197 goto error;
198 path_hooks = PyList_New(0);
199 if (path_hooks == NULL)
200 goto error;
201 err = PySys_SetObject("path_hooks", path_hooks);
202 if (err) {
203 error:
204 PyErr_Print();
205 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
206 "path_importer_cache, or NullImporter failed"
210 zimpimport = PyImport_ImportModule("zipimport");
211 if (zimpimport == NULL) {
212 PyErr_Clear(); /* No zip import module -- okay */
213 if (Py_VerboseFlag)
214 PySys_WriteStderr("# can't import zipimport\n");
216 else {
217 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
218 "zipimporter");
219 Py_DECREF(zimpimport);
220 if (zipimporter == NULL) {
221 PyErr_Clear(); /* No zipimporter object -- okay */
222 if (Py_VerboseFlag)
223 PySys_WriteStderr(
224 "# can't import zipimport.zipimporter\n");
226 else {
227 /* sys.path_hooks.append(zipimporter) */
228 err = PyList_Append(path_hooks, zipimporter);
229 Py_DECREF(zipimporter);
230 if (err)
231 goto error;
232 if (Py_VerboseFlag)
233 PySys_WriteStderr(
234 "# installed zipimport hook\n");
237 Py_DECREF(path_hooks);
240 void
241 _PyImport_Fini(void)
243 Py_XDECREF(extensions);
244 extensions = NULL;
245 PyMem_DEL(_PyImport_Filetab);
246 _PyImport_Filetab = NULL;
250 /* Locking primitives to prevent parallel imports of the same module
251 in different threads to return with a partially loaded module.
252 These calls are serialized by the global interpreter lock. */
254 #ifdef WITH_THREAD
256 #include "pythread.h"
258 static PyThread_type_lock import_lock = 0;
259 static long import_lock_thread = -1;
260 static int import_lock_level = 0;
262 static void
263 lock_import(void)
265 long me = PyThread_get_thread_ident();
266 if (me == -1)
267 return; /* Too bad */
268 if (import_lock == NULL) {
269 import_lock = PyThread_allocate_lock();
270 if (import_lock == NULL)
271 return; /* Nothing much we can do. */
273 if (import_lock_thread == me) {
274 import_lock_level++;
275 return;
277 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
279 PyThreadState *tstate = PyEval_SaveThread();
280 PyThread_acquire_lock(import_lock, 1);
281 PyEval_RestoreThread(tstate);
283 import_lock_thread = me;
284 import_lock_level = 1;
287 static int
288 unlock_import(void)
290 long me = PyThread_get_thread_ident();
291 if (me == -1 || import_lock == NULL)
292 return 0; /* Too bad */
293 if (import_lock_thread != me)
294 return -1;
295 import_lock_level--;
296 if (import_lock_level == 0) {
297 import_lock_thread = -1;
298 PyThread_release_lock(import_lock);
300 return 1;
303 /* This function is called from PyOS_AfterFork to ensure that newly
304 created child processes do not share locks with the parent. */
306 void
307 _PyImport_ReInitLock(void)
309 #ifdef _AIX
310 if (import_lock != NULL)
311 import_lock = PyThread_allocate_lock();
312 #endif
315 #else
317 #define lock_import()
318 #define unlock_import() 0
320 #endif
322 static PyObject *
323 imp_lock_held(PyObject *self, PyObject *noargs)
325 #ifdef WITH_THREAD
326 return PyBool_FromLong(import_lock_thread != -1);
327 #else
328 return PyBool_FromLong(0);
329 #endif
332 static PyObject *
333 imp_acquire_lock(PyObject *self, PyObject *noargs)
335 #ifdef WITH_THREAD
336 lock_import();
337 #endif
338 Py_INCREF(Py_None);
339 return Py_None;
342 static PyObject *
343 imp_release_lock(PyObject *self, PyObject *noargs)
345 #ifdef WITH_THREAD
346 if (unlock_import() < 0) {
347 PyErr_SetString(PyExc_RuntimeError,
348 "not holding the import lock");
349 return NULL;
351 #endif
352 Py_INCREF(Py_None);
353 return Py_None;
356 static void
357 imp_modules_reloading_clear(void)
359 PyInterpreterState *interp = PyThreadState_Get()->interp;
360 if (interp->modules_reloading != NULL)
361 PyDict_Clear(interp->modules_reloading);
364 /* Helper for sys */
366 PyObject *
367 PyImport_GetModuleDict(void)
369 PyInterpreterState *interp = PyThreadState_GET()->interp;
370 if (interp->modules == NULL)
371 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
372 return interp->modules;
376 /* List of names to clear in sys */
377 static char* sys_deletes[] = {
378 "path", "argv", "ps1", "ps2",
379 "last_type", "last_value", "last_traceback",
380 "path_hooks", "path_importer_cache", "meta_path",
381 /* misc stuff */
382 "flags", "float_info",
383 NULL
386 static char* sys_files[] = {
387 "stdin", "__stdin__",
388 "stdout", "__stdout__",
389 "stderr", "__stderr__",
390 NULL
394 /* Un-initialize things, as good as we can */
396 void
397 PyImport_Cleanup(void)
399 Py_ssize_t pos, ndone;
400 char *name;
401 PyObject *key, *value, *dict;
402 PyInterpreterState *interp = PyThreadState_GET()->interp;
403 PyObject *modules = interp->modules;
405 if (modules == NULL)
406 return; /* Already done */
408 /* Delete some special variables first. These are common
409 places where user values hide and people complain when their
410 destructors fail. Since the modules containing them are
411 deleted *last* of all, they would come too late in the normal
412 destruction order. Sigh. */
414 value = PyDict_GetItemString(modules, "builtins");
415 if (value != NULL && PyModule_Check(value)) {
416 dict = PyModule_GetDict(value);
417 if (Py_VerboseFlag)
418 PySys_WriteStderr("# clear builtins._\n");
419 PyDict_SetItemString(dict, "_", Py_None);
421 value = PyDict_GetItemString(modules, "sys");
422 if (value != NULL && PyModule_Check(value)) {
423 char **p;
424 PyObject *v;
425 dict = PyModule_GetDict(value);
426 for (p = sys_deletes; *p != NULL; p++) {
427 if (Py_VerboseFlag)
428 PySys_WriteStderr("# clear sys.%s\n", *p);
429 PyDict_SetItemString(dict, *p, Py_None);
431 for (p = sys_files; *p != NULL; p+=2) {
432 if (Py_VerboseFlag)
433 PySys_WriteStderr("# restore sys.%s\n", *p);
434 v = PyDict_GetItemString(dict, *(p+1));
435 if (v == NULL)
436 v = Py_None;
437 PyDict_SetItemString(dict, *p, v);
441 /* First, delete __main__ */
442 value = PyDict_GetItemString(modules, "__main__");
443 if (value != NULL && PyModule_Check(value)) {
444 if (Py_VerboseFlag)
445 PySys_WriteStderr("# cleanup __main__\n");
446 _PyModule_Clear(value);
447 PyDict_SetItemString(modules, "__main__", Py_None);
450 /* The special treatment of "builtins" here is because even
451 when it's not referenced as a module, its dictionary is
452 referenced by almost every module's __builtins__. Since
453 deleting a module clears its dictionary (even if there are
454 references left to it), we need to delete the "builtins"
455 module last. Likewise, we don't delete sys until the very
456 end because it is implicitly referenced (e.g. by print).
458 Also note that we 'delete' modules by replacing their entry
459 in the modules dict with None, rather than really deleting
460 them; this avoids a rehash of the modules dictionary and
461 also marks them as "non existent" so they won't be
462 re-imported. */
464 /* Next, repeatedly delete modules with a reference count of
465 one (skipping builtins and sys) and delete them */
466 do {
467 ndone = 0;
468 pos = 0;
469 while (PyDict_Next(modules, &pos, &key, &value)) {
470 if (value->ob_refcnt != 1)
471 continue;
472 if (PyUnicode_Check(key) && PyModule_Check(value)) {
473 name = PyUnicode_AsString(key);
474 if (strcmp(name, "builtins") == 0)
475 continue;
476 if (strcmp(name, "sys") == 0)
477 continue;
478 if (Py_VerboseFlag)
479 PySys_WriteStderr(
480 "# cleanup[1] %s\n", name);
481 _PyModule_Clear(value);
482 PyDict_SetItem(modules, key, Py_None);
483 ndone++;
486 } while (ndone > 0);
488 /* Next, delete all modules (still skipping builtins and sys) */
489 pos = 0;
490 while (PyDict_Next(modules, &pos, &key, &value)) {
491 if (PyUnicode_Check(key) && PyModule_Check(value)) {
492 name = PyUnicode_AsString(key);
493 if (strcmp(name, "builtins") == 0)
494 continue;
495 if (strcmp(name, "sys") == 0)
496 continue;
497 if (Py_VerboseFlag)
498 PySys_WriteStderr("# cleanup[2] %s\n", name);
499 _PyModule_Clear(value);
500 PyDict_SetItem(modules, key, Py_None);
504 /* Next, delete sys and builtins (in that order) */
505 value = PyDict_GetItemString(modules, "sys");
506 if (value != NULL && PyModule_Check(value)) {
507 if (Py_VerboseFlag)
508 PySys_WriteStderr("# cleanup sys\n");
509 _PyModule_Clear(value);
510 PyDict_SetItemString(modules, "sys", Py_None);
512 value = PyDict_GetItemString(modules, "builtins");
513 if (value != NULL && PyModule_Check(value)) {
514 if (Py_VerboseFlag)
515 PySys_WriteStderr("# cleanup builtins\n");
516 _PyModule_Clear(value);
517 PyDict_SetItemString(modules, "builtins", Py_None);
520 /* Finally, clear and delete the modules directory */
521 PyDict_Clear(modules);
522 interp->modules = NULL;
523 Py_DECREF(modules);
524 Py_CLEAR(interp->modules_reloading);
528 /* Helper for pythonrun.c -- return magic number */
530 long
531 PyImport_GetMagicNumber(void)
533 return pyc_magic;
537 /* Magic for extension modules (built-in as well as dynamically
538 loaded). To prevent initializing an extension module more than
539 once, we keep a static dictionary 'extensions' keyed by module name
540 (for built-in modules) or by filename (for dynamically loaded
541 modules), containing these modules. A copy of the module's
542 dictionary is stored by calling _PyImport_FixupExtension()
543 immediately after the module initialization function succeeds. A
544 copy can be retrieved from there by calling
545 _PyImport_FindExtension().
547 Modules which do support multiple multiple initialization set
548 their m_size field to a non-negative number (indicating the size
549 of the module-specific state). They are still recorded in the
550 extensions dictionary, to avoid loading shared libraries twice.
554 _PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
556 PyObject *modules, *dict;
557 struct PyModuleDef *def;
558 if (extensions == NULL) {
559 extensions = PyDict_New();
560 if (extensions == NULL)
561 return -1;
563 if (mod == NULL || !PyModule_Check(mod)) {
564 PyErr_BadInternalCall();
565 return -1;
567 def = PyModule_GetDef(mod);
568 if (!def) {
569 PyErr_BadInternalCall();
570 return -1;
572 modules = PyImport_GetModuleDict();
573 if (PyDict_SetItemString(modules, name, mod) < 0)
574 return -1;
575 if (_PyState_AddModule(mod, def) < 0) {
576 PyDict_DelItemString(modules, name);
577 return -1;
579 if (def->m_size == -1) {
580 if (def->m_base.m_copy) {
581 /* Somebody already imported the module,
582 likely under a different name.
583 XXX this should really not happen. */
584 Py_DECREF(def->m_base.m_copy);
585 def->m_base.m_copy = NULL;
587 dict = PyModule_GetDict(mod);
588 if (dict == NULL)
589 return -1;
590 def->m_base.m_copy = PyDict_Copy(dict);
591 if (def->m_base.m_copy == NULL)
592 return -1;
594 PyDict_SetItemString(extensions, filename, (PyObject*)def);
595 return 0;
598 PyObject *
599 _PyImport_FindExtension(char *name, char *filename)
601 PyObject *mod, *mdict;
602 PyModuleDef* def;
603 if (extensions == NULL)
604 return NULL;
605 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
606 if (def == NULL)
607 return NULL;
608 if (def->m_size == -1) {
609 /* Module does not support repeated initialization */
610 if (def->m_base.m_copy == NULL)
611 return NULL;
612 mod = PyImport_AddModule(name);
613 if (mod == NULL)
614 return NULL;
615 Py_INCREF(mod);
616 mdict = PyModule_GetDict(mod);
617 if (mdict == NULL)
618 return NULL;
619 if (PyDict_Update(mdict, def->m_base.m_copy))
620 return NULL;
622 else {
623 if (def->m_base.m_init == NULL)
624 return NULL;
625 mod = def->m_base.m_init();
626 if (mod == NULL)
627 return NULL;
628 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
630 if (_PyState_AddModule(mod, def) < 0) {
631 PyDict_DelItemString(PyImport_GetModuleDict(), name);
632 Py_DECREF(mod);
633 return NULL;
635 if (Py_VerboseFlag)
636 PySys_WriteStderr("import %s # previously loaded (%s)\n",
637 name, filename);
638 return mod;
643 /* Get the module object corresponding to a module name.
644 First check the modules dictionary if there's one there,
645 if not, create a new one and insert it in the modules dictionary.
646 Because the former action is most common, THIS DOES NOT RETURN A
647 'NEW' REFERENCE! */
649 PyObject *
650 PyImport_AddModule(const char *name)
652 PyObject *modules = PyImport_GetModuleDict();
653 PyObject *m;
655 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
656 PyModule_Check(m))
657 return m;
658 m = PyModule_New(name);
659 if (m == NULL)
660 return NULL;
661 if (PyDict_SetItemString(modules, name, m) != 0) {
662 Py_DECREF(m);
663 return NULL;
665 Py_DECREF(m); /* Yes, it still exists, in modules! */
667 return m;
670 /* Remove name from sys.modules, if it's there. */
671 static void
672 _RemoveModule(const char *name)
674 PyObject *modules = PyImport_GetModuleDict();
675 if (PyDict_GetItemString(modules, name) == NULL)
676 return;
677 if (PyDict_DelItemString(modules, name) < 0)
678 Py_FatalError("import: deleting existing key in"
679 "sys.modules failed");
682 static PyObject * get_sourcefile(const char *file);
684 /* Execute a code object in a module and return the module object
685 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
686 * removed from sys.modules, to avoid leaving damaged module objects
687 * in sys.modules. The caller may wish to restore the original
688 * module object (if any) in this case; PyImport_ReloadModule is an
689 * example.
691 PyObject *
692 PyImport_ExecCodeModule(char *name, PyObject *co)
694 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
697 PyObject *
698 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
700 PyObject *modules = PyImport_GetModuleDict();
701 PyObject *m, *d, *v;
703 m = PyImport_AddModule(name);
704 if (m == NULL)
705 return NULL;
706 /* If the module is being reloaded, we get the old module back
707 and re-use its dict to exec the new code. */
708 d = PyModule_GetDict(m);
709 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
710 if (PyDict_SetItemString(d, "__builtins__",
711 PyEval_GetBuiltins()) != 0)
712 goto error;
714 /* Remember the filename as the __file__ attribute */
715 v = NULL;
716 if (pathname != NULL) {
717 v = get_sourcefile(pathname);
718 if (v == NULL)
719 PyErr_Clear();
721 if (v == NULL) {
722 v = ((PyCodeObject *)co)->co_filename;
723 Py_INCREF(v);
725 if (PyDict_SetItemString(d, "__file__", v) != 0)
726 PyErr_Clear(); /* Not important enough to report */
727 Py_DECREF(v);
729 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
730 if (v == NULL)
731 goto error;
732 Py_DECREF(v);
734 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
735 PyErr_Format(PyExc_ImportError,
736 "Loaded module %.200s not found in sys.modules",
737 name);
738 return NULL;
741 Py_INCREF(m);
743 return m;
745 error:
746 _RemoveModule(name);
747 return NULL;
751 /* Given a pathname for a Python source file, fill a buffer with the
752 pathname for the corresponding compiled file. Return the pathname
753 for the compiled file, or NULL if there's no space in the buffer.
754 Doesn't set an exception. */
756 static char *
757 make_compiled_pathname(char *pathname, char *buf, size_t buflen)
759 size_t len = strlen(pathname);
760 if (len+2 > buflen)
761 return NULL;
763 #ifdef MS_WINDOWS
764 /* Treat .pyw as if it were .py. The case of ".pyw" must match
765 that used in _PyImport_StandardFiletab. */
766 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
767 --len; /* pretend 'w' isn't there */
768 #endif
769 memcpy(buf, pathname, len);
770 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
771 buf[len+1] = '\0';
773 return buf;
777 /* Given a pathname for a Python source file, its time of last
778 modification, and a pathname for a compiled file, check whether the
779 compiled file represents the same version of the source. If so,
780 return a FILE pointer for the compiled file, positioned just after
781 the header; if not, return NULL.
782 Doesn't set an exception. */
784 static FILE *
785 check_compiled_module(char *pathname, time_t mtime, char *cpathname)
787 FILE *fp;
788 long magic;
789 long pyc_mtime;
791 fp = fopen(cpathname, "rb");
792 if (fp == NULL)
793 return NULL;
794 magic = PyMarshal_ReadLongFromFile(fp);
795 if (magic != pyc_magic) {
796 if (Py_VerboseFlag)
797 PySys_WriteStderr("# %s has bad magic\n", cpathname);
798 fclose(fp);
799 return NULL;
801 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
802 if (pyc_mtime != mtime) {
803 if (Py_VerboseFlag)
804 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
805 fclose(fp);
806 return NULL;
808 if (Py_VerboseFlag)
809 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
810 return fp;
814 /* Read a code object from a file and check it for validity */
816 static PyCodeObject *
817 read_compiled_module(char *cpathname, FILE *fp)
819 PyObject *co;
821 co = PyMarshal_ReadLastObjectFromFile(fp);
822 if (co == NULL)
823 return NULL;
824 if (!PyCode_Check(co)) {
825 PyErr_Format(PyExc_ImportError,
826 "Non-code object in %.200s", cpathname);
827 Py_DECREF(co);
828 return NULL;
830 return (PyCodeObject *)co;
834 /* Load a module from a compiled file, execute it, and return its
835 module object WITH INCREMENTED REFERENCE COUNT */
837 static PyObject *
838 load_compiled_module(char *name, char *cpathname, FILE *fp)
840 long magic;
841 PyCodeObject *co;
842 PyObject *m;
844 magic = PyMarshal_ReadLongFromFile(fp);
845 if (magic != pyc_magic) {
846 PyErr_Format(PyExc_ImportError,
847 "Bad magic number in %.200s", cpathname);
848 return NULL;
850 (void) PyMarshal_ReadLongFromFile(fp);
851 co = read_compiled_module(cpathname, fp);
852 if (co == NULL)
853 return NULL;
854 if (Py_VerboseFlag)
855 PySys_WriteStderr("import %s # precompiled from %s\n",
856 name, cpathname);
857 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
858 Py_DECREF(co);
860 return m;
863 /* Parse a source file and return the corresponding code object */
865 static PyCodeObject *
866 parse_source_module(const char *pathname, FILE *fp)
868 PyCodeObject *co = NULL;
869 mod_ty mod;
870 PyCompilerFlags flags;
871 PyArena *arena = PyArena_New();
872 if (arena == NULL)
873 return NULL;
875 flags.cf_flags = 0;
876 mod = PyParser_ASTFromFile(fp, pathname, NULL,
877 Py_file_input, 0, 0, &flags,
878 NULL, arena);
879 if (mod) {
880 co = PyAST_Compile(mod, pathname, NULL, arena);
882 PyArena_Free(arena);
883 return co;
887 /* Helper to open a bytecode file for writing in exclusive mode */
889 static FILE *
890 open_exclusive(char *filename, mode_t mode)
892 #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
893 /* Use O_EXCL to avoid a race condition when another process tries to
894 write the same file. When that happens, our open() call fails,
895 which is just fine (since it's only a cache).
896 XXX If the file exists and is writable but the directory is not
897 writable, the file will never be written. Oh well.
899 int fd;
900 (void) unlink(filename);
901 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
902 #ifdef O_BINARY
903 |O_BINARY /* necessary for Windows */
904 #endif
905 #ifdef __VMS
906 , mode, "ctxt=bin", "shr=nil"
907 #else
908 , mode
909 #endif
911 if (fd < 0)
912 return NULL;
913 return fdopen(fd, "wb");
914 #else
915 /* Best we can do -- on Windows this can't happen anyway */
916 return fopen(filename, "wb");
917 #endif
921 /* Write a compiled module to a file, placing the time of last
922 modification of its source into the header.
923 Errors are ignored, if a write error occurs an attempt is made to
924 remove the file. */
926 static void
927 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
929 FILE *fp;
930 time_t mtime = srcstat->st_mtime;
931 mode_t mode = srcstat->st_mode;
933 fp = open_exclusive(cpathname, mode);
934 if (fp == NULL) {
935 if (Py_VerboseFlag)
936 PySys_WriteStderr(
937 "# can't create %s\n", cpathname);
938 return;
940 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
941 /* First write a 0 for mtime */
942 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
943 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
944 if (fflush(fp) != 0 || ferror(fp)) {
945 if (Py_VerboseFlag)
946 PySys_WriteStderr("# can't write %s\n", cpathname);
947 /* Don't keep partial file */
948 fclose(fp);
949 (void) unlink(cpathname);
950 return;
952 /* Now write the true mtime */
953 fseek(fp, 4L, 0);
954 assert(mtime < LONG_MAX);
955 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
956 fflush(fp);
957 fclose(fp);
958 if (Py_VerboseFlag)
959 PySys_WriteStderr("# wrote %s\n", cpathname);
963 /* Load a source module from a given file and return its module
964 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
965 byte-compiled file, use that instead. */
967 static PyObject *
968 load_source_module(char *name, char *pathname, FILE *fp)
970 struct stat st;
971 FILE *fpc;
972 char buf[MAXPATHLEN+1];
973 char *cpathname;
974 PyCodeObject *co;
975 PyObject *m;
977 if (fstat(fileno(fp), &st) != 0) {
978 PyErr_Format(PyExc_RuntimeError,
979 "unable to get file status from '%s'",
980 pathname);
981 return NULL;
983 #if SIZEOF_TIME_T > 4
984 /* Python's .pyc timestamp handling presumes that the timestamp fits
985 in 4 bytes. This will be fine until sometime in the year 2038,
986 when a 4-byte signed time_t will overflow.
988 if (st.st_mtime >> 32) {
989 PyErr_SetString(PyExc_OverflowError,
990 "modification time overflows a 4 byte field");
991 return NULL;
993 #endif
994 cpathname = make_compiled_pathname(pathname, buf,
995 (size_t)MAXPATHLEN + 1);
996 if (cpathname != NULL &&
997 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
998 co = read_compiled_module(cpathname, fpc);
999 fclose(fpc);
1000 if (co == NULL)
1001 return NULL;
1002 if (Py_VerboseFlag)
1003 PySys_WriteStderr("import %s # precompiled from %s\n",
1004 name, cpathname);
1005 pathname = cpathname;
1007 else {
1008 co = parse_source_module(pathname, fp);
1009 if (co == NULL)
1010 return NULL;
1011 if (Py_VerboseFlag)
1012 PySys_WriteStderr("import %s # from %s\n",
1013 name, pathname);
1014 if (cpathname) {
1015 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1016 if (ro == NULL || !PyObject_IsTrue(ro))
1017 write_compiled_module(co, cpathname, &st);
1020 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
1021 Py_DECREF(co);
1023 return m;
1026 /* Get source file -> unicode or None
1027 * Returns the path to the py file if available, else the given path
1029 static PyObject *
1030 get_sourcefile(const char *file)
1032 char py[MAXPATHLEN + 1];
1033 Py_ssize_t len;
1034 PyObject *u;
1035 struct stat statbuf;
1037 if (!file || !*file) {
1038 Py_RETURN_NONE;
1041 len = strlen(file);
1042 /* match '*.py?' */
1043 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
1044 return PyUnicode_DecodeFSDefault(file);
1047 strncpy(py, file, len-1);
1048 py[len-1] = '\0';
1049 if (stat(py, &statbuf) == 0 &&
1050 S_ISREG(statbuf.st_mode)) {
1051 u = PyUnicode_DecodeFSDefault(py);
1053 else {
1054 u = PyUnicode_DecodeFSDefault(file);
1056 return u;
1059 /* Forward */
1060 static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1061 static struct filedescr *find_module(char *, char *, PyObject *,
1062 char *, size_t, FILE **, PyObject **);
1063 static struct _frozen * find_frozen(char *);
1065 /* Load a package and return its module object WITH INCREMENTED
1066 REFERENCE COUNT */
1068 static PyObject *
1069 load_package(char *name, char *pathname)
1071 PyObject *m, *d;
1072 PyObject *file = NULL;
1073 PyObject *path = NULL;
1074 int err;
1075 char buf[MAXPATHLEN+1];
1076 FILE *fp = NULL;
1077 struct filedescr *fdp;
1079 m = PyImport_AddModule(name);
1080 if (m == NULL)
1081 return NULL;
1082 if (Py_VerboseFlag)
1083 PySys_WriteStderr("import %s # directory %s\n",
1084 name, pathname);
1085 d = PyModule_GetDict(m);
1086 file = get_sourcefile(pathname);
1087 if (file == NULL)
1088 goto error;
1089 path = Py_BuildValue("[O]", file);
1090 if (path == NULL)
1091 goto error;
1092 err = PyDict_SetItemString(d, "__file__", file);
1093 if (err == 0)
1094 err = PyDict_SetItemString(d, "__path__", path);
1095 if (err != 0)
1096 goto error;
1097 buf[0] = '\0';
1098 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
1099 if (fdp == NULL) {
1100 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1101 PyErr_Clear();
1102 Py_INCREF(m);
1104 else
1105 m = NULL;
1106 goto cleanup;
1108 m = load_module(name, fp, buf, fdp->type, NULL);
1109 if (fp != NULL)
1110 fclose(fp);
1111 goto cleanup;
1113 error:
1114 m = NULL;
1115 cleanup:
1116 Py_XDECREF(path);
1117 Py_XDECREF(file);
1118 return m;
1122 /* Helper to test for built-in module */
1124 static int
1125 is_builtin(char *name)
1127 int i;
1128 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1129 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1130 if (PyImport_Inittab[i].initfunc == NULL)
1131 return -1;
1132 else
1133 return 1;
1136 return 0;
1140 /* Return an importer object for a sys.path/pkg.__path__ item 'p',
1141 possibly by fetching it from the path_importer_cache dict. If it
1142 wasn't yet cached, traverse path_hooks until a hook is found
1143 that can handle the path item. Return None if no hook could;
1144 this tells our caller it should fall back to the builtin
1145 import mechanism. Cache the result in path_importer_cache.
1146 Returns a borrowed reference. */
1148 static PyObject *
1149 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1150 PyObject *p)
1152 PyObject *importer;
1153 Py_ssize_t j, nhooks;
1155 /* These conditions are the caller's responsibility: */
1156 assert(PyList_Check(path_hooks));
1157 assert(PyDict_Check(path_importer_cache));
1159 nhooks = PyList_Size(path_hooks);
1160 if (nhooks < 0)
1161 return NULL; /* Shouldn't happen */
1163 importer = PyDict_GetItem(path_importer_cache, p);
1164 if (importer != NULL)
1165 return importer;
1167 /* set path_importer_cache[p] to None to avoid recursion */
1168 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1169 return NULL;
1171 for (j = 0; j < nhooks; j++) {
1172 PyObject *hook = PyList_GetItem(path_hooks, j);
1173 if (hook == NULL)
1174 return NULL;
1175 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1176 if (importer != NULL)
1177 break;
1179 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1180 return NULL;
1182 PyErr_Clear();
1184 if (importer == NULL) {
1185 importer = PyObject_CallFunctionObjArgs(
1186 (PyObject *)&PyNullImporter_Type, p, NULL
1188 if (importer == NULL) {
1189 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1190 PyErr_Clear();
1191 return Py_None;
1195 if (importer != NULL) {
1196 int err = PyDict_SetItem(path_importer_cache, p, importer);
1197 Py_DECREF(importer);
1198 if (err != 0)
1199 return NULL;
1201 return importer;
1204 PyAPI_FUNC(PyObject *)
1205 PyImport_GetImporter(PyObject *path) {
1206 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1208 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1209 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1210 importer = get_path_importer(path_importer_cache,
1211 path_hooks, path);
1214 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1215 return importer;
1218 /* Search the path (default sys.path) for a module. Return the
1219 corresponding filedescr struct, and (via return arguments) the
1220 pathname and an open file. Return NULL if the module is not found. */
1222 #ifdef MS_COREDLL
1223 extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1224 char *, Py_ssize_t);
1225 #endif
1227 static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
1228 static int find_init_module(char *); /* Forward */
1229 static struct filedescr importhookdescr = {"", "", IMP_HOOK};
1231 static struct filedescr *
1232 find_module(char *fullname, char *subname, PyObject *path, char *buf,
1233 size_t buflen, FILE **p_fp, PyObject **p_loader)
1235 Py_ssize_t i, npath;
1236 size_t len, namelen;
1237 struct filedescr *fdp = NULL;
1238 char *filemode;
1239 FILE *fp = NULL;
1240 PyObject *path_hooks, *path_importer_cache;
1241 struct stat statbuf;
1242 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1243 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1244 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1245 char name[MAXPATHLEN+1];
1246 #if defined(PYOS_OS2)
1247 size_t saved_len;
1248 size_t saved_namelen;
1249 char *saved_buf = NULL;
1250 #endif
1251 if (p_loader != NULL)
1252 *p_loader = NULL;
1254 if (strlen(subname) > MAXPATHLEN) {
1255 PyErr_SetString(PyExc_OverflowError,
1256 "module name is too long");
1257 return NULL;
1259 strcpy(name, subname);
1261 /* sys.meta_path import hook */
1262 if (p_loader != NULL) {
1263 PyObject *meta_path;
1265 meta_path = PySys_GetObject("meta_path");
1266 if (meta_path == NULL || !PyList_Check(meta_path)) {
1267 PyErr_SetString(PyExc_ImportError,
1268 "sys.meta_path must be a list of "
1269 "import hooks");
1270 return NULL;
1272 Py_INCREF(meta_path); /* zap guard */
1273 npath = PyList_Size(meta_path);
1274 for (i = 0; i < npath; i++) {
1275 PyObject *loader;
1276 PyObject *hook = PyList_GetItem(meta_path, i);
1277 loader = PyObject_CallMethod(hook, "find_module",
1278 "sO", fullname,
1279 path != NULL ?
1280 path : Py_None);
1281 if (loader == NULL) {
1282 Py_DECREF(meta_path);
1283 return NULL; /* true error */
1285 if (loader != Py_None) {
1286 /* a loader was found */
1287 *p_loader = loader;
1288 Py_DECREF(meta_path);
1289 return &importhookdescr;
1291 Py_DECREF(loader);
1293 Py_DECREF(meta_path);
1296 if (path != NULL && PyUnicode_Check(path)) {
1297 /* The only type of submodule allowed inside a "frozen"
1298 package are other frozen modules or packages. */
1299 char *p = PyUnicode_AsString(path);
1300 if (strlen(p) + 1 + strlen(name) >= (size_t)buflen) {
1301 PyErr_SetString(PyExc_ImportError,
1302 "full frozen module name too long");
1303 return NULL;
1305 strcpy(buf, p);
1306 strcat(buf, ".");
1307 strcat(buf, name);
1308 strcpy(name, buf);
1309 if (find_frozen(name) != NULL) {
1310 strcpy(buf, name);
1311 return &fd_frozen;
1313 PyErr_Format(PyExc_ImportError,
1314 "No frozen submodule named %.200s", name);
1315 return NULL;
1317 if (path == NULL) {
1318 if (is_builtin(name)) {
1319 strcpy(buf, name);
1320 return &fd_builtin;
1322 if ((find_frozen(name)) != NULL) {
1323 strcpy(buf, name);
1324 return &fd_frozen;
1327 #ifdef MS_COREDLL
1328 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1329 if (fp != NULL) {
1330 *p_fp = fp;
1331 return fdp;
1333 #endif
1334 path = PySys_GetObject("path");
1336 if (path == NULL || !PyList_Check(path)) {
1337 PyErr_SetString(PyExc_ImportError,
1338 "sys.path must be a list of directory names");
1339 return NULL;
1342 path_hooks = PySys_GetObject("path_hooks");
1343 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1344 PyErr_SetString(PyExc_ImportError,
1345 "sys.path_hooks must be a list of "
1346 "import hooks");
1347 return NULL;
1349 path_importer_cache = PySys_GetObject("path_importer_cache");
1350 if (path_importer_cache == NULL ||
1351 !PyDict_Check(path_importer_cache)) {
1352 PyErr_SetString(PyExc_ImportError,
1353 "sys.path_importer_cache must be a dict");
1354 return NULL;
1357 npath = PyList_Size(path);
1358 namelen = strlen(name);
1359 for (i = 0; i < npath; i++) {
1360 PyObject *v = PyList_GetItem(path, i);
1361 PyObject *origv = v;
1362 const char *base;
1363 Py_ssize_t size;
1364 if (!v)
1365 return NULL;
1366 if (PyUnicode_Check(v)) {
1367 v = PyUnicode_AsEncodedString(v,
1368 Py_FileSystemDefaultEncoding, NULL);
1369 if (v == NULL)
1370 return NULL;
1372 else if (!PyBytes_Check(v))
1373 continue;
1374 else
1375 Py_INCREF(v);
1377 base = PyBytes_AS_STRING(v);
1378 size = PyBytes_GET_SIZE(v);
1379 len = size;
1380 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1381 Py_DECREF(v);
1382 continue; /* Too long */
1384 strcpy(buf, base);
1385 Py_DECREF(v);
1387 if (strlen(buf) != len) {
1388 continue; /* v contains '\0' */
1391 /* sys.path_hooks import hook */
1392 if (p_loader != NULL) {
1393 PyObject *importer;
1395 importer = get_path_importer(path_importer_cache,
1396 path_hooks, origv);
1397 if (importer == NULL) {
1398 return NULL;
1400 /* Note: importer is a borrowed reference */
1401 if (importer != Py_None) {
1402 PyObject *loader;
1403 loader = PyObject_CallMethod(importer,
1404 "find_module",
1405 "s", fullname);
1406 if (loader == NULL)
1407 return NULL; /* error */
1408 if (loader != Py_None) {
1409 /* a loader was found */
1410 *p_loader = loader;
1411 return &importhookdescr;
1413 Py_DECREF(loader);
1414 continue;
1417 /* no hook was found, use builtin import */
1419 if (len > 0 && buf[len-1] != SEP
1420 #ifdef ALTSEP
1421 && buf[len-1] != ALTSEP
1422 #endif
1424 buf[len++] = SEP;
1425 strcpy(buf+len, name);
1426 len += namelen;
1428 /* Check for package import (buf holds a directory name,
1429 and there's an __init__ module in that directory */
1430 #ifdef HAVE_STAT
1431 if (stat(buf, &statbuf) == 0 && /* it exists */
1432 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1433 case_ok(buf, len, namelen, name)) { /* case matches */
1434 if (find_init_module(buf)) { /* and has __init__.py */
1435 return &fd_package;
1437 else {
1438 char warnstr[MAXPATHLEN+80];
1439 sprintf(warnstr, "Not importing directory "
1440 "'%.*s': missing __init__.py",
1441 MAXPATHLEN, buf);
1442 if (PyErr_WarnEx(PyExc_ImportWarning,
1443 warnstr, 1)) {
1444 return NULL;
1448 #endif
1449 #if defined(PYOS_OS2)
1450 /* take a snapshot of the module spec for restoration
1451 * after the 8 character DLL hackery
1453 saved_buf = strdup(buf);
1454 saved_len = len;
1455 saved_namelen = namelen;
1456 #endif /* PYOS_OS2 */
1457 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1458 #if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
1459 /* OS/2 limits DLLs to 8 character names (w/o
1460 extension)
1461 * so if the name is longer than that and its a
1462 * dynamically loaded module we're going to try,
1463 * truncate the name before trying
1465 if (strlen(subname) > 8) {
1466 /* is this an attempt to load a C extension? */
1467 const struct filedescr *scan;
1468 scan = _PyImport_DynLoadFiletab;
1469 while (scan->suffix != NULL) {
1470 if (!strcmp(scan->suffix, fdp->suffix))
1471 break;
1472 else
1473 scan++;
1475 if (scan->suffix != NULL) {
1476 /* yes, so truncate the name */
1477 namelen = 8;
1478 len -= strlen(subname) - namelen;
1479 buf[len] = '\0';
1482 #endif /* PYOS_OS2 */
1483 strcpy(buf+len, fdp->suffix);
1484 if (Py_VerboseFlag > 1)
1485 PySys_WriteStderr("# trying %s\n", buf);
1486 filemode = fdp->mode;
1487 if (filemode[0] == 'U')
1488 filemode = "r" PY_STDIOTEXTMODE;
1489 fp = fopen(buf, filemode);
1490 if (fp != NULL) {
1491 if (case_ok(buf, len, namelen, name))
1492 break;
1493 else { /* continue search */
1494 fclose(fp);
1495 fp = NULL;
1498 #if defined(PYOS_OS2)
1499 /* restore the saved snapshot */
1500 strcpy(buf, saved_buf);
1501 len = saved_len;
1502 namelen = saved_namelen;
1503 #endif
1505 #if defined(PYOS_OS2)
1506 /* don't need/want the module name snapshot anymore */
1507 if (saved_buf)
1509 free(saved_buf);
1510 saved_buf = NULL;
1512 #endif
1513 if (fp != NULL)
1514 break;
1516 if (fp == NULL) {
1517 PyErr_Format(PyExc_ImportError,
1518 "No module named %.200s", name);
1519 return NULL;
1521 *p_fp = fp;
1522 return fdp;
1525 /* Helpers for main.c
1526 * Find the source file corresponding to a named module
1528 struct filedescr *
1529 _PyImport_FindModule(const char *name, PyObject *path, char *buf,
1530 size_t buflen, FILE **p_fp, PyObject **p_loader)
1532 return find_module((char *) name, (char *) name, path,
1533 buf, buflen, p_fp, p_loader);
1536 PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1538 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1541 /* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
1542 * The arguments here are tricky, best shown by example:
1543 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1544 * ^ ^ ^ ^
1545 * |--------------------- buf ---------------------|
1546 * |------------------- len ------------------|
1547 * |------ name -------|
1548 * |----- namelen -----|
1549 * buf is the full path, but len only counts up to (& exclusive of) the
1550 * extension. name is the module name, also exclusive of extension.
1552 * We've already done a successful stat() or fopen() on buf, so know that
1553 * there's some match, possibly case-insensitive.
1555 * case_ok() is to return 1 if there's a case-sensitive match for
1556 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1557 * exists.
1559 * case_ok() is used to implement case-sensitive import semantics even
1560 * on platforms with case-insensitive filesystems. It's trivial to implement
1561 * for case-sensitive filesystems. It's pretty much a cross-platform
1562 * nightmare for systems with case-insensitive filesystems.
1565 /* First we may need a pile of platform-specific header files; the sequence
1566 * of #if's here should match the sequence in the body of case_ok().
1568 #if defined(MS_WINDOWS)
1569 #include <windows.h>
1571 #elif defined(DJGPP)
1572 #include <dir.h>
1574 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
1575 #include <sys/types.h>
1576 #include <dirent.h>
1578 #elif defined(PYOS_OS2)
1579 #define INCL_DOS
1580 #define INCL_DOSERRORS
1581 #define INCL_NOPMAPI
1582 #include <os2.h>
1583 #endif
1585 static int
1586 case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
1588 /* Pick a platform-specific implementation; the sequence of #if's here should
1589 * match the sequence just above.
1592 /* MS_WINDOWS */
1593 #if defined(MS_WINDOWS)
1594 WIN32_FIND_DATA data;
1595 HANDLE h;
1597 if (Py_GETENV("PYTHONCASEOK") != NULL)
1598 return 1;
1600 h = FindFirstFile(buf, &data);
1601 if (h == INVALID_HANDLE_VALUE) {
1602 PyErr_Format(PyExc_NameError,
1603 "Can't find file for module %.100s\n(filename %.300s)",
1604 name, buf);
1605 return 0;
1607 FindClose(h);
1608 return strncmp(data.cFileName, name, namelen) == 0;
1610 /* DJGPP */
1611 #elif defined(DJGPP)
1612 struct ffblk ffblk;
1613 int done;
1615 if (Py_GETENV("PYTHONCASEOK") != NULL)
1616 return 1;
1618 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1619 if (done) {
1620 PyErr_Format(PyExc_NameError,
1621 "Can't find file for module %.100s\n(filename %.300s)",
1622 name, buf);
1623 return 0;
1625 return strncmp(ffblk.ff_name, name, namelen) == 0;
1627 /* new-fangled macintosh (macosx) or Cygwin */
1628 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
1629 DIR *dirp;
1630 struct dirent *dp;
1631 char dirname[MAXPATHLEN + 1];
1632 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
1634 if (Py_GETENV("PYTHONCASEOK") != NULL)
1635 return 1;
1637 /* Copy the dir component into dirname; substitute "." if empty */
1638 if (dirlen <= 0) {
1639 dirname[0] = '.';
1640 dirname[1] = '\0';
1642 else {
1643 assert(dirlen <= MAXPATHLEN);
1644 memcpy(dirname, buf, dirlen);
1645 dirname[dirlen] = '\0';
1647 /* Open the directory and search the entries for an exact match. */
1648 dirp = opendir(dirname);
1649 if (dirp) {
1650 char *nameWithExt = buf + len - namelen;
1651 while ((dp = readdir(dirp)) != NULL) {
1652 const int thislen =
1653 #ifdef _DIRENT_HAVE_D_NAMELEN
1654 dp->d_namlen;
1655 #else
1656 strlen(dp->d_name);
1657 #endif
1658 if (thislen >= namelen &&
1659 strcmp(dp->d_name, nameWithExt) == 0) {
1660 (void)closedir(dirp);
1661 return 1; /* Found */
1664 (void)closedir(dirp);
1666 return 0 ; /* Not found */
1668 /* OS/2 */
1669 #elif defined(PYOS_OS2)
1670 HDIR hdir = 1;
1671 ULONG srchcnt = 1;
1672 FILEFINDBUF3 ffbuf;
1673 APIRET rc;
1675 if (Py_GETENV("PYTHONCASEOK") != NULL)
1676 return 1;
1678 rc = DosFindFirst(buf,
1679 &hdir,
1680 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1681 &ffbuf, sizeof(ffbuf),
1682 &srchcnt,
1683 FIL_STANDARD);
1684 if (rc != NO_ERROR)
1685 return 0;
1686 return strncmp(ffbuf.achName, name, namelen) == 0;
1688 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1689 #else
1690 return 1;
1692 #endif
1696 #ifdef HAVE_STAT
1697 /* Helper to look for __init__.py or __init__.py[co] in potential package */
1698 static int
1699 find_init_module(char *buf)
1701 const size_t save_len = strlen(buf);
1702 size_t i = save_len;
1703 char *pname; /* pointer to start of __init__ */
1704 struct stat statbuf;
1706 /* For calling case_ok(buf, len, namelen, name):
1707 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1708 * ^ ^ ^ ^
1709 * |--------------------- buf ---------------------|
1710 * |------------------- len ------------------|
1711 * |------ name -------|
1712 * |----- namelen -----|
1714 if (save_len + 13 >= MAXPATHLEN)
1715 return 0;
1716 buf[i++] = SEP;
1717 pname = buf + i;
1718 strcpy(pname, "__init__.py");
1719 if (stat(buf, &statbuf) == 0) {
1720 if (case_ok(buf,
1721 save_len + 9, /* len("/__init__") */
1722 8, /* len("__init__") */
1723 pname)) {
1724 buf[save_len] = '\0';
1725 return 1;
1728 i += strlen(pname);
1729 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
1730 if (stat(buf, &statbuf) == 0) {
1731 if (case_ok(buf,
1732 save_len + 9, /* len("/__init__") */
1733 8, /* len("__init__") */
1734 pname)) {
1735 buf[save_len] = '\0';
1736 return 1;
1739 buf[save_len] = '\0';
1740 return 0;
1743 #endif /* HAVE_STAT */
1746 static int init_builtin(char *); /* Forward */
1748 /* Load an external module using the default search path and return
1749 its module object WITH INCREMENTED REFERENCE COUNT */
1751 static PyObject *
1752 load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
1754 PyObject *modules;
1755 PyObject *m;
1756 int err;
1758 /* First check that there's an open file (if we need one) */
1759 switch (type) {
1760 case PY_SOURCE:
1761 case PY_COMPILED:
1762 if (fp == NULL) {
1763 PyErr_Format(PyExc_ValueError,
1764 "file object required for import (type code %d)",
1765 type);
1766 return NULL;
1770 switch (type) {
1772 case PY_SOURCE:
1773 m = load_source_module(name, buf, fp);
1774 break;
1776 case PY_COMPILED:
1777 m = load_compiled_module(name, buf, fp);
1778 break;
1780 #ifdef HAVE_DYNAMIC_LOADING
1781 case C_EXTENSION:
1782 m = _PyImport_LoadDynamicModule(name, buf, fp);
1783 break;
1784 #endif
1786 case PKG_DIRECTORY:
1787 m = load_package(name, buf);
1788 break;
1790 case C_BUILTIN:
1791 case PY_FROZEN:
1792 if (buf != NULL && buf[0] != '\0')
1793 name = buf;
1794 if (type == C_BUILTIN)
1795 err = init_builtin(name);
1796 else
1797 err = PyImport_ImportFrozenModule(name);
1798 if (err < 0)
1799 return NULL;
1800 if (err == 0) {
1801 PyErr_Format(PyExc_ImportError,
1802 "Purported %s module %.200s not found",
1803 type == C_BUILTIN ?
1804 "builtin" : "frozen",
1805 name);
1806 return NULL;
1808 modules = PyImport_GetModuleDict();
1809 m = PyDict_GetItemString(modules, name);
1810 if (m == NULL) {
1811 PyErr_Format(
1812 PyExc_ImportError,
1813 "%s module %.200s not properly initialized",
1814 type == C_BUILTIN ?
1815 "builtin" : "frozen",
1816 name);
1817 return NULL;
1819 Py_INCREF(m);
1820 break;
1822 case IMP_HOOK: {
1823 if (loader == NULL) {
1824 PyErr_SetString(PyExc_ImportError,
1825 "import hook without loader");
1826 return NULL;
1828 m = PyObject_CallMethod(loader, "load_module", "s", name);
1829 break;
1832 default:
1833 PyErr_Format(PyExc_ImportError,
1834 "Don't know how to import %.200s (type code %d)",
1835 name, type);
1836 m = NULL;
1840 return m;
1844 /* Initialize a built-in module.
1845 Return 1 for success, 0 if the module is not found, and -1 with
1846 an exception set if the initialization failed. */
1848 static int
1849 init_builtin(char *name)
1851 struct _inittab *p;
1853 if (_PyImport_FindExtension(name, name) != NULL)
1854 return 1;
1856 for (p = PyImport_Inittab; p->name != NULL; p++) {
1857 PyObject *mod;
1858 if (strcmp(name, p->name) == 0) {
1859 if (p->initfunc == NULL) {
1860 PyErr_Format(PyExc_ImportError,
1861 "Cannot re-init internal module %.200s",
1862 name);
1863 return -1;
1865 if (Py_VerboseFlag)
1866 PySys_WriteStderr("import %s # builtin\n", name);
1867 mod = (*p->initfunc)();
1868 if (mod == 0)
1869 return -1;
1870 if (_PyImport_FixupExtension(mod, name, name) < 0)
1871 return -1;
1872 /* FixupExtension has put the module into sys.modules,
1873 so we can release our own reference. */
1874 Py_DECREF(mod);
1875 return 1;
1878 return 0;
1882 /* Frozen modules */
1884 static struct _frozen *
1885 find_frozen(char *name)
1887 struct _frozen *p;
1889 for (p = PyImport_FrozenModules; ; p++) {
1890 if (p->name == NULL)
1891 return NULL;
1892 if (strcmp(p->name, name) == 0)
1893 break;
1895 return p;
1898 static PyObject *
1899 get_frozen_object(char *name)
1901 struct _frozen *p = find_frozen(name);
1902 int size;
1904 if (p == NULL) {
1905 PyErr_Format(PyExc_ImportError,
1906 "No such frozen object named %.200s",
1907 name);
1908 return NULL;
1910 if (p->code == NULL) {
1911 PyErr_Format(PyExc_ImportError,
1912 "Excluded frozen object named %.200s",
1913 name);
1914 return NULL;
1916 size = p->size;
1917 if (size < 0)
1918 size = -size;
1919 return PyMarshal_ReadObjectFromString((char *)p->code, size);
1922 /* Initialize a frozen module.
1923 Return 1 for succes, 0 if the module is not found, and -1 with
1924 an exception set if the initialization failed.
1925 This function is also used from frozenmain.c */
1928 PyImport_ImportFrozenModule(char *name)
1930 struct _frozen *p = find_frozen(name);
1931 PyObject *co;
1932 PyObject *m;
1933 int ispackage;
1934 int size;
1936 if (p == NULL)
1937 return 0;
1938 if (p->code == NULL) {
1939 PyErr_Format(PyExc_ImportError,
1940 "Excluded frozen object named %.200s",
1941 name);
1942 return -1;
1944 size = p->size;
1945 ispackage = (size < 0);
1946 if (ispackage)
1947 size = -size;
1948 if (Py_VerboseFlag)
1949 PySys_WriteStderr("import %s # frozen%s\n",
1950 name, ispackage ? " package" : "");
1951 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1952 if (co == NULL)
1953 return -1;
1954 if (!PyCode_Check(co)) {
1955 PyErr_Format(PyExc_TypeError,
1956 "frozen object %.200s is not a code object",
1957 name);
1958 goto err_return;
1960 if (ispackage) {
1961 /* Set __path__ to the package name */
1962 PyObject *d, *s;
1963 int err;
1964 m = PyImport_AddModule(name);
1965 if (m == NULL)
1966 goto err_return;
1967 d = PyModule_GetDict(m);
1968 s = PyUnicode_InternFromString(name);
1969 if (s == NULL)
1970 goto err_return;
1971 err = PyDict_SetItemString(d, "__path__", s);
1972 Py_DECREF(s);
1973 if (err != 0)
1974 goto err_return;
1976 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
1977 if (m == NULL)
1978 goto err_return;
1979 Py_DECREF(co);
1980 Py_DECREF(m);
1981 return 1;
1982 err_return:
1983 Py_DECREF(co);
1984 return -1;
1988 /* Import a module, either built-in, frozen, or external, and return
1989 its module object WITH INCREMENTED REFERENCE COUNT */
1991 PyObject *
1992 PyImport_ImportModule(const char *name)
1994 PyObject *pname;
1995 PyObject *result;
1997 pname = PyUnicode_FromString(name);
1998 if (pname == NULL)
1999 return NULL;
2000 result = PyImport_Import(pname);
2001 Py_DECREF(pname);
2002 return result;
2005 /* Import a module without blocking
2007 * At first it tries to fetch the module from sys.modules. If the module was
2008 * never loaded before it loads it with PyImport_ImportModule() unless another
2009 * thread holds the import lock. In the latter case the function raises an
2010 * ImportError instead of blocking.
2012 * Returns the module object with incremented ref count.
2014 PyObject *
2015 PyImport_ImportModuleNoBlock(const char *name)
2017 PyObject *result;
2018 PyObject *modules;
2019 long me;
2021 /* Try to get the module from sys.modules[name] */
2022 modules = PyImport_GetModuleDict();
2023 if (modules == NULL)
2024 return NULL;
2026 result = PyDict_GetItemString(modules, name);
2027 if (result != NULL) {
2028 Py_INCREF(result);
2029 return result;
2031 else {
2032 PyErr_Clear();
2035 /* check the import lock
2036 * me might be -1 but I ignore the error here, the lock function
2037 * takes care of the problem */
2038 me = PyThread_get_thread_ident();
2039 if (import_lock_thread == -1 || import_lock_thread == me) {
2040 /* no thread or me is holding the lock */
2041 return PyImport_ImportModule(name);
2043 else {
2044 PyErr_Format(PyExc_ImportError,
2045 "Failed to import %.200s because the import lock"
2046 "is held by another thread.",
2047 name);
2048 return NULL;
2052 /* Forward declarations for helper routines */
2053 static PyObject *get_parent(PyObject *globals, char *buf,
2054 Py_ssize_t *p_buflen, int level);
2055 static PyObject *load_next(PyObject *mod, PyObject *altmod,
2056 char **p_name, char *buf, Py_ssize_t *p_buflen);
2057 static int mark_miss(char *name);
2058 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
2059 char *buf, Py_ssize_t buflen, int recursive);
2060 static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
2062 /* The Magnum Opus of dotted-name import :-) */
2064 static PyObject *
2065 import_module_level(char *name, PyObject *globals, PyObject *locals,
2066 PyObject *fromlist, int level)
2068 char buf[MAXPATHLEN+1];
2069 Py_ssize_t buflen = 0;
2070 PyObject *parent, *head, *next, *tail;
2072 if (strchr(name, '/') != NULL
2073 #ifdef MS_WINDOWS
2074 || strchr(name, '\\') != NULL
2075 #endif
2077 PyErr_SetString(PyExc_ImportError,
2078 "Import by filename is not supported.");
2079 return NULL;
2082 parent = get_parent(globals, buf, &buflen, level);
2083 if (parent == NULL)
2084 return NULL;
2086 head = load_next(parent, Py_None, &name, buf, &buflen);
2087 if (head == NULL)
2088 return NULL;
2090 tail = head;
2091 Py_INCREF(tail);
2092 while (name) {
2093 next = load_next(tail, tail, &name, buf, &buflen);
2094 Py_DECREF(tail);
2095 if (next == NULL) {
2096 Py_DECREF(head);
2097 return NULL;
2099 tail = next;
2101 if (tail == Py_None) {
2102 /* If tail is Py_None, both get_parent and load_next found
2103 an empty module name: someone called __import__("") or
2104 doctored faulty bytecode */
2105 Py_DECREF(tail);
2106 Py_DECREF(head);
2107 PyErr_SetString(PyExc_ValueError,
2108 "Empty module name");
2109 return NULL;
2112 if (fromlist != NULL) {
2113 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2114 fromlist = NULL;
2117 if (fromlist == NULL) {
2118 Py_DECREF(tail);
2119 return head;
2122 Py_DECREF(head);
2123 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
2124 Py_DECREF(tail);
2125 return NULL;
2128 return tail;
2131 PyObject *
2132 PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2133 PyObject *fromlist, int level)
2135 PyObject *result;
2136 lock_import();
2137 result = import_module_level(name, globals, locals, fromlist, level);
2138 if (unlock_import() < 0) {
2139 Py_XDECREF(result);
2140 PyErr_SetString(PyExc_RuntimeError,
2141 "not holding the import lock");
2142 return NULL;
2144 return result;
2147 /* Return the package that an import is being performed in. If globals comes
2148 from the module foo.bar.bat (not itself a package), this returns the
2149 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
2150 the package's entry in sys.modules is returned, as a borrowed reference.
2152 The *name* of the returned package is returned in buf, with the length of
2153 the name in *p_buflen.
2155 If globals doesn't come from a package or a module in a package, or a
2156 corresponding entry is not found in sys.modules, Py_None is returned.
2158 static PyObject *
2159 get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
2161 static PyObject *namestr = NULL;
2162 static PyObject *pathstr = NULL;
2163 static PyObject *pkgstr = NULL;
2164 PyObject *pkgname, *modname, *modpath, *modules, *parent;
2166 if (globals == NULL || !PyDict_Check(globals) || !level)
2167 return Py_None;
2169 if (namestr == NULL) {
2170 namestr = PyUnicode_InternFromString("__name__");
2171 if (namestr == NULL)
2172 return NULL;
2174 if (pathstr == NULL) {
2175 pathstr = PyUnicode_InternFromString("__path__");
2176 if (pathstr == NULL)
2177 return NULL;
2179 if (pkgstr == NULL) {
2180 pkgstr = PyUnicode_InternFromString("__package__");
2181 if (pkgstr == NULL)
2182 return NULL;
2185 *buf = '\0';
2186 *p_buflen = 0;
2187 pkgname = PyDict_GetItem(globals, pkgstr);
2189 if ((pkgname != NULL) && (pkgname != Py_None)) {
2190 /* __package__ is set, so use it */
2191 char *pkgname_str;
2192 Py_ssize_t len;
2194 if (!PyUnicode_Check(pkgname)) {
2195 PyErr_SetString(PyExc_ValueError,
2196 "__package__ set to non-string");
2197 return NULL;
2199 pkgname_str = PyUnicode_AsStringAndSize(pkgname, &len);
2200 if (len == 0) {
2201 if (level > 0) {
2202 PyErr_SetString(PyExc_ValueError,
2203 "Attempted relative import in non-package");
2204 return NULL;
2206 return Py_None;
2208 if (len > MAXPATHLEN) {
2209 PyErr_SetString(PyExc_ValueError,
2210 "Package name too long");
2211 return NULL;
2213 strcpy(buf, pkgname_str);
2214 } else {
2215 /* __package__ not set, so figure it out and set it */
2216 modname = PyDict_GetItem(globals, namestr);
2217 if (modname == NULL || !PyUnicode_Check(modname))
2218 return Py_None;
2220 modpath = PyDict_GetItem(globals, pathstr);
2221 if (modpath != NULL) {
2222 /* __path__ is set, so modname is already the package name */
2223 char *modname_str;
2224 Py_ssize_t len;
2225 int error;
2227 modname_str = PyUnicode_AsStringAndSize(modname, &len);
2228 if (len > MAXPATHLEN) {
2229 PyErr_SetString(PyExc_ValueError,
2230 "Module name too long");
2231 return NULL;
2233 strcpy(buf, modname_str);
2234 error = PyDict_SetItem(globals, pkgstr, modname);
2235 if (error) {
2236 PyErr_SetString(PyExc_ValueError,
2237 "Could not set __package__");
2238 return NULL;
2240 } else {
2241 /* Normal module, so work out the package name if any */
2242 char *start = PyUnicode_AsString(modname);
2243 char *lastdot = strrchr(start, '.');
2244 size_t len;
2245 int error;
2246 if (lastdot == NULL && level > 0) {
2247 PyErr_SetString(PyExc_ValueError,
2248 "Attempted relative import in non-package");
2249 return NULL;
2251 if (lastdot == NULL) {
2252 error = PyDict_SetItem(globals, pkgstr, Py_None);
2253 if (error) {
2254 PyErr_SetString(PyExc_ValueError,
2255 "Could not set __package__");
2256 return NULL;
2258 return Py_None;
2260 len = lastdot - start;
2261 if (len >= MAXPATHLEN) {
2262 PyErr_SetString(PyExc_ValueError,
2263 "Module name too long");
2264 return NULL;
2266 strncpy(buf, start, len);
2267 buf[len] = '\0';
2268 pkgname = PyUnicode_FromString(buf);
2269 if (pkgname == NULL) {
2270 return NULL;
2272 error = PyDict_SetItem(globals, pkgstr, pkgname);
2273 Py_DECREF(pkgname);
2274 if (error) {
2275 PyErr_SetString(PyExc_ValueError,
2276 "Could not set __package__");
2277 return NULL;
2281 while (--level > 0) {
2282 char *dot = strrchr(buf, '.');
2283 if (dot == NULL) {
2284 PyErr_SetString(PyExc_ValueError,
2285 "Attempted relative import beyond "
2286 "toplevel package");
2287 return NULL;
2289 *dot = '\0';
2291 *p_buflen = strlen(buf);
2293 modules = PyImport_GetModuleDict();
2294 parent = PyDict_GetItemString(modules, buf);
2295 if (parent == NULL)
2296 PyErr_Format(PyExc_SystemError,
2297 "Parent module '%.200s' not loaded", buf);
2298 return parent;
2299 /* We expect, but can't guarantee, if parent != None, that:
2300 - parent.__name__ == buf
2301 - parent.__dict__ is globals
2302 If this is violated... Who cares? */
2305 /* altmod is either None or same as mod */
2306 static PyObject *
2307 load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2308 Py_ssize_t *p_buflen)
2310 char *name = *p_name;
2311 char *dot = strchr(name, '.');
2312 size_t len;
2313 char *p;
2314 PyObject *result;
2316 if (strlen(name) == 0) {
2317 /* completely empty module name should only happen in
2318 'from . import' (or '__import__("")')*/
2319 Py_INCREF(mod);
2320 *p_name = NULL;
2321 return mod;
2324 if (dot == NULL) {
2325 *p_name = NULL;
2326 len = strlen(name);
2328 else {
2329 *p_name = dot+1;
2330 len = dot-name;
2332 if (len == 0) {
2333 PyErr_SetString(PyExc_ValueError,
2334 "Empty module name");
2335 return NULL;
2338 p = buf + *p_buflen;
2339 if (p != buf)
2340 *p++ = '.';
2341 if (p+len-buf >= MAXPATHLEN) {
2342 PyErr_SetString(PyExc_ValueError,
2343 "Module name too long");
2344 return NULL;
2346 strncpy(p, name, len);
2347 p[len] = '\0';
2348 *p_buflen = p+len-buf;
2350 result = import_submodule(mod, p, buf);
2351 if (result == Py_None && altmod != mod) {
2352 Py_DECREF(result);
2353 /* Here, altmod must be None and mod must not be None */
2354 result = import_submodule(altmod, p, p);
2355 if (result != NULL && result != Py_None) {
2356 if (mark_miss(buf) != 0) {
2357 Py_DECREF(result);
2358 return NULL;
2360 strncpy(buf, name, len);
2361 buf[len] = '\0';
2362 *p_buflen = len;
2365 if (result == NULL)
2366 return NULL;
2368 if (result == Py_None) {
2369 Py_DECREF(result);
2370 PyErr_Format(PyExc_ImportError,
2371 "No module named %.200s", name);
2372 return NULL;
2375 return result;
2378 static int
2379 mark_miss(char *name)
2381 PyObject *modules = PyImport_GetModuleDict();
2382 return PyDict_SetItemString(modules, name, Py_None);
2385 static int
2386 ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
2387 int recursive)
2389 int i;
2391 if (!PyObject_HasAttrString(mod, "__path__"))
2392 return 1;
2394 for (i = 0; ; i++) {
2395 PyObject *item = PySequence_GetItem(fromlist, i);
2396 int hasit;
2397 if (item == NULL) {
2398 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2399 PyErr_Clear();
2400 return 1;
2402 return 0;
2404 if (!PyUnicode_Check(item)) {
2405 PyErr_SetString(PyExc_TypeError,
2406 "Item in ``from list'' not a string");
2407 Py_DECREF(item);
2408 return 0;
2410 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
2411 PyObject *all;
2412 Py_DECREF(item);
2413 /* See if the package defines __all__ */
2414 if (recursive)
2415 continue; /* Avoid endless recursion */
2416 all = PyObject_GetAttrString(mod, "__all__");
2417 if (all == NULL)
2418 PyErr_Clear();
2419 else {
2420 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
2421 Py_DECREF(all);
2422 if (!ret)
2423 return 0;
2425 continue;
2427 hasit = PyObject_HasAttr(mod, item);
2428 if (!hasit) {
2429 PyObject *item8;
2430 char *subname;
2431 PyObject *submod;
2432 char *p;
2433 if (!Py_FileSystemDefaultEncoding) {
2434 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2435 PyUnicode_GetSize(item),
2436 NULL);
2437 } else {
2438 item8 = PyUnicode_AsEncodedString(item,
2439 Py_FileSystemDefaultEncoding, NULL);
2441 if (!item8) {
2442 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2443 return 0;
2445 subname = PyBytes_AS_STRING(item8);
2446 if (buflen + strlen(subname) >= MAXPATHLEN) {
2447 PyErr_SetString(PyExc_ValueError,
2448 "Module name too long");
2449 Py_DECREF(item);
2450 return 0;
2452 p = buf + buflen;
2453 *p++ = '.';
2454 strcpy(p, subname);
2455 submod = import_submodule(mod, subname, buf);
2456 Py_DECREF(item8);
2457 Py_XDECREF(submod);
2458 if (submod == NULL) {
2459 Py_DECREF(item);
2460 return 0;
2463 Py_DECREF(item);
2466 /* NOTREACHED */
2469 static int
2470 add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2471 PyObject *modules)
2473 if (mod == Py_None)
2474 return 1;
2475 /* Irrespective of the success of this load, make a
2476 reference to it in the parent package module. A copy gets
2477 saved in the modules dictionary under the full name, so get a
2478 reference from there, if need be. (The exception is when the
2479 load failed with a SyntaxError -- then there's no trace in
2480 sys.modules. In that case, of course, do nothing extra.) */
2481 if (submod == NULL) {
2482 submod = PyDict_GetItemString(modules, fullname);
2483 if (submod == NULL)
2484 return 1;
2486 if (PyModule_Check(mod)) {
2487 /* We can't use setattr here since it can give a
2488 * spurious warning if the submodule name shadows a
2489 * builtin name */
2490 PyObject *dict = PyModule_GetDict(mod);
2491 if (!dict)
2492 return 0;
2493 if (PyDict_SetItemString(dict, subname, submod) < 0)
2494 return 0;
2496 else {
2497 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2498 return 0;
2500 return 1;
2503 static PyObject *
2504 import_submodule(PyObject *mod, char *subname, char *fullname)
2506 PyObject *modules = PyImport_GetModuleDict();
2507 PyObject *m = NULL;
2509 /* Require:
2510 if mod == None: subname == fullname
2511 else: mod.__name__ + "." + subname == fullname
2514 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
2515 Py_INCREF(m);
2517 else {
2518 PyObject *path, *loader = NULL;
2519 char buf[MAXPATHLEN+1];
2520 struct filedescr *fdp;
2521 FILE *fp = NULL;
2523 if (mod == Py_None)
2524 path = NULL;
2525 else {
2526 path = PyObject_GetAttrString(mod, "__path__");
2527 if (path == NULL) {
2528 PyErr_Clear();
2529 Py_INCREF(Py_None);
2530 return Py_None;
2534 buf[0] = '\0';
2535 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2536 &fp, &loader);
2537 Py_XDECREF(path);
2538 if (fdp == NULL) {
2539 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2540 return NULL;
2541 PyErr_Clear();
2542 Py_INCREF(Py_None);
2543 return Py_None;
2545 m = load_module(fullname, fp, buf, fdp->type, loader);
2546 Py_XDECREF(loader);
2547 if (fp)
2548 fclose(fp);
2549 if (!add_submodule(mod, m, fullname, subname, modules)) {
2550 Py_XDECREF(m);
2551 m = NULL;
2555 return m;
2559 /* Re-import a module of any kind and return its module object, WITH
2560 INCREMENTED REFERENCE COUNT */
2562 PyObject *
2563 PyImport_ReloadModule(PyObject *m)
2565 PyInterpreterState *interp = PyThreadState_Get()->interp;
2566 PyObject *modules_reloading = interp->modules_reloading;
2567 PyObject *modules = PyImport_GetModuleDict();
2568 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
2569 char *name, *subname;
2570 char buf[MAXPATHLEN+1];
2571 struct filedescr *fdp;
2572 FILE *fp = NULL;
2573 PyObject *newm;
2575 if (modules_reloading == NULL) {
2576 Py_FatalError("PyImport_ReloadModule: "
2577 "no modules_reloading dictionary!");
2578 return NULL;
2581 if (m == NULL || !PyModule_Check(m)) {
2582 PyErr_SetString(PyExc_TypeError,
2583 "reload() argument must be module");
2584 return NULL;
2586 name = (char*)PyModule_GetName(m);
2587 if (name == NULL)
2588 return NULL;
2589 if (m != PyDict_GetItemString(modules, name)) {
2590 PyErr_Format(PyExc_ImportError,
2591 "reload(): module %.200s not in sys.modules",
2592 name);
2593 return NULL;
2595 existing_m = PyDict_GetItemString(modules_reloading, name);
2596 if (existing_m != NULL) {
2597 /* Due to a recursive reload, this module is already
2598 being reloaded. */
2599 Py_INCREF(existing_m);
2600 return existing_m;
2602 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2603 return NULL;
2605 subname = strrchr(name, '.');
2606 if (subname == NULL)
2607 subname = name;
2608 else {
2609 PyObject *parentname, *parent;
2610 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
2611 if (parentname == NULL) {
2612 imp_modules_reloading_clear();
2613 return NULL;
2615 parent = PyDict_GetItem(modules, parentname);
2616 if (parent == NULL) {
2617 PyErr_Format(PyExc_ImportError,
2618 "reload(): parent %.200s not in sys.modules",
2619 PyUnicode_AsString(parentname));
2620 Py_DECREF(parentname);
2621 imp_modules_reloading_clear();
2622 return NULL;
2624 Py_DECREF(parentname);
2625 subname++;
2626 path = PyObject_GetAttrString(parent, "__path__");
2627 if (path == NULL)
2628 PyErr_Clear();
2630 buf[0] = '\0';
2631 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
2632 Py_XDECREF(path);
2634 if (fdp == NULL) {
2635 Py_XDECREF(loader);
2636 imp_modules_reloading_clear();
2637 return NULL;
2640 newm = load_module(name, fp, buf, fdp->type, loader);
2641 Py_XDECREF(loader);
2643 if (fp)
2644 fclose(fp);
2645 if (newm == NULL) {
2646 /* load_module probably removed name from modules because of
2647 * the error. Put back the original module object. We're
2648 * going to return NULL in this case regardless of whether
2649 * replacing name succeeds, so the return value is ignored.
2651 PyDict_SetItemString(modules, name, m);
2653 imp_modules_reloading_clear();
2654 return newm;
2658 /* Higher-level import emulator which emulates the "import" statement
2659 more accurately -- it invokes the __import__() function from the
2660 builtins of the current globals. This means that the import is
2661 done using whatever import hooks are installed in the current
2662 environment, e.g. by "rexec".
2663 A dummy list ["__doc__"] is passed as the 4th argument so that
2664 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
2665 will return <module "gencache"> instead of <module "win32com">. */
2667 PyObject *
2668 PyImport_Import(PyObject *module_name)
2670 static PyObject *silly_list = NULL;
2671 static PyObject *builtins_str = NULL;
2672 static PyObject *import_str = NULL;
2673 PyObject *globals = NULL;
2674 PyObject *import = NULL;
2675 PyObject *builtins = NULL;
2676 PyObject *r = NULL;
2678 /* Initialize constant string objects */
2679 if (silly_list == NULL) {
2680 import_str = PyUnicode_InternFromString("__import__");
2681 if (import_str == NULL)
2682 return NULL;
2683 builtins_str = PyUnicode_InternFromString("__builtins__");
2684 if (builtins_str == NULL)
2685 return NULL;
2686 silly_list = Py_BuildValue("[s]", "__doc__");
2687 if (silly_list == NULL)
2688 return NULL;
2691 /* Get the builtins from current globals */
2692 globals = PyEval_GetGlobals();
2693 if (globals != NULL) {
2694 Py_INCREF(globals);
2695 builtins = PyObject_GetItem(globals, builtins_str);
2696 if (builtins == NULL)
2697 goto err;
2699 else {
2700 /* No globals -- use standard builtins, and fake globals */
2701 PyErr_Clear();
2703 builtins = PyImport_ImportModuleLevel("builtins",
2704 NULL, NULL, NULL, 0);
2705 if (builtins == NULL)
2706 return NULL;
2707 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2708 if (globals == NULL)
2709 goto err;
2712 /* Get the __import__ function from the builtins */
2713 if (PyDict_Check(builtins)) {
2714 import = PyObject_GetItem(builtins, import_str);
2715 if (import == NULL)
2716 PyErr_SetObject(PyExc_KeyError, import_str);
2718 else
2719 import = PyObject_GetAttr(builtins, import_str);
2720 if (import == NULL)
2721 goto err;
2723 /* Call the __import__ function with the proper argument list
2724 * Always use absolute import here. */
2725 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2726 globals, silly_list, 0, NULL);
2728 err:
2729 Py_XDECREF(globals);
2730 Py_XDECREF(builtins);
2731 Py_XDECREF(import);
2733 return r;
2737 /* Module 'imp' provides Python access to the primitives used for
2738 importing modules.
2741 static PyObject *
2742 imp_get_magic(PyObject *self, PyObject *noargs)
2744 char buf[4];
2746 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2747 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2748 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2749 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
2751 return PyBytes_FromStringAndSize(buf, 4);
2754 static PyObject *
2755 imp_get_suffixes(PyObject *self, PyObject *noargs)
2757 PyObject *list;
2758 struct filedescr *fdp;
2760 list = PyList_New(0);
2761 if (list == NULL)
2762 return NULL;
2763 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2764 PyObject *item = Py_BuildValue("ssi",
2765 fdp->suffix, fdp->mode, fdp->type);
2766 if (item == NULL) {
2767 Py_DECREF(list);
2768 return NULL;
2770 if (PyList_Append(list, item) < 0) {
2771 Py_DECREF(list);
2772 Py_DECREF(item);
2773 return NULL;
2775 Py_DECREF(item);
2777 return list;
2780 static PyObject *
2781 call_find_module(char *name, PyObject *path)
2783 extern int fclose(FILE *);
2784 PyObject *fob, *ret;
2785 struct filedescr *fdp;
2786 char pathname[MAXPATHLEN+1];
2787 FILE *fp = NULL;
2788 int fd = -1;
2789 char *found_encoding = NULL;
2790 char *encoding = NULL;
2792 pathname[0] = '\0';
2793 if (path == Py_None)
2794 path = NULL;
2795 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
2796 if (fdp == NULL)
2797 return NULL;
2798 if (fp != NULL) {
2799 fd = fileno(fp);
2800 if (fd != -1)
2801 fd = dup(fd);
2802 fclose(fp);
2803 fp = NULL;
2805 if (fd != -1) {
2806 if (strchr(fdp->mode, 'b') == NULL) {
2807 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2808 memory. */
2809 found_encoding = PyTokenizer_FindEncoding(fd);
2810 lseek(fd, 0, 0); /* Reset position */
2811 encoding = (found_encoding != NULL) ? found_encoding :
2812 (char*)PyUnicode_GetDefaultEncoding();
2814 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
2815 (char*)encoding, NULL, NULL, 1);
2816 if (fob == NULL) {
2817 close(fd);
2818 PyMem_FREE(found_encoding);
2819 return NULL;
2822 else {
2823 fob = Py_None;
2824 Py_INCREF(fob);
2826 ret = Py_BuildValue("Os(ssi)",
2827 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
2828 Py_DECREF(fob);
2829 PyMem_FREE(found_encoding);
2831 return ret;
2834 static PyObject *
2835 imp_find_module(PyObject *self, PyObject *args)
2837 char *name;
2838 PyObject *path = NULL;
2839 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
2840 return NULL;
2841 return call_find_module(name, path);
2844 static PyObject *
2845 imp_init_builtin(PyObject *self, PyObject *args)
2847 char *name;
2848 int ret;
2849 PyObject *m;
2850 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2851 return NULL;
2852 ret = init_builtin(name);
2853 if (ret < 0)
2854 return NULL;
2855 if (ret == 0) {
2856 Py_INCREF(Py_None);
2857 return Py_None;
2859 m = PyImport_AddModule(name);
2860 Py_XINCREF(m);
2861 return m;
2864 static PyObject *
2865 imp_init_frozen(PyObject *self, PyObject *args)
2867 char *name;
2868 int ret;
2869 PyObject *m;
2870 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2871 return NULL;
2872 ret = PyImport_ImportFrozenModule(name);
2873 if (ret < 0)
2874 return NULL;
2875 if (ret == 0) {
2876 Py_INCREF(Py_None);
2877 return Py_None;
2879 m = PyImport_AddModule(name);
2880 Py_XINCREF(m);
2881 return m;
2884 static PyObject *
2885 imp_get_frozen_object(PyObject *self, PyObject *args)
2887 char *name;
2889 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2890 return NULL;
2891 return get_frozen_object(name);
2894 static PyObject *
2895 imp_is_builtin(PyObject *self, PyObject *args)
2897 char *name;
2898 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2899 return NULL;
2900 return PyLong_FromLong(is_builtin(name));
2903 static PyObject *
2904 imp_is_frozen(PyObject *self, PyObject *args)
2906 char *name;
2907 struct _frozen *p;
2908 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2909 return NULL;
2910 p = find_frozen(name);
2911 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
2914 static FILE *
2915 get_file(char *pathname, PyObject *fob, char *mode)
2917 FILE *fp;
2918 if (mode[0] == 'U')
2919 mode = "r" PY_STDIOTEXTMODE;
2920 if (fob == NULL) {
2921 fp = fopen(pathname, mode);
2923 else {
2924 int fd = PyObject_AsFileDescriptor(fob);
2925 if (fd == -1)
2926 return NULL;
2927 /* XXX This will leak a FILE struct. Fix this!!!!
2928 (But it doesn't leak a file descrioptor!) */
2929 fp = fdopen(fd, mode);
2931 if (fp == NULL)
2932 PyErr_SetFromErrno(PyExc_IOError);
2933 return fp;
2936 static PyObject *
2937 imp_load_compiled(PyObject *self, PyObject *args)
2939 char *name;
2940 char *pathname;
2941 PyObject *fob = NULL;
2942 PyObject *m;
2943 FILE *fp;
2944 if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
2945 &name, &pathname, &fob))
2946 return NULL;
2947 fp = get_file(pathname, fob, "rb");
2948 if (fp == NULL)
2949 return NULL;
2950 m = load_compiled_module(name, pathname, fp);
2951 if (fob == NULL)
2952 fclose(fp);
2953 return m;
2956 #ifdef HAVE_DYNAMIC_LOADING
2958 static PyObject *
2959 imp_load_dynamic(PyObject *self, PyObject *args)
2961 char *name;
2962 char *pathname;
2963 PyObject *fob = NULL;
2964 PyObject *m;
2965 FILE *fp = NULL;
2966 if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
2967 &name, &pathname, &fob))
2968 return NULL;
2969 if (fob) {
2970 fp = get_file(pathname, fob, "r");
2971 if (fp == NULL)
2972 return NULL;
2974 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2975 return m;
2978 #endif /* HAVE_DYNAMIC_LOADING */
2980 static PyObject *
2981 imp_load_source(PyObject *self, PyObject *args)
2983 char *name;
2984 char *pathname;
2985 PyObject *fob = NULL;
2986 PyObject *m;
2987 FILE *fp;
2988 if (!PyArg_ParseTuple(args, "ss|O:load_source",
2989 &name, &pathname, &fob))
2990 return NULL;
2991 fp = get_file(pathname, fob, "r");
2992 if (fp == NULL)
2993 return NULL;
2994 m = load_source_module(name, pathname, fp);
2995 if (fob == NULL)
2996 fclose(fp);
2997 return m;
3000 static PyObject *
3001 imp_load_module(PyObject *self, PyObject *args)
3003 char *name;
3004 PyObject *fob;
3005 char *pathname;
3006 char *suffix; /* Unused */
3007 char *mode;
3008 int type;
3009 FILE *fp;
3011 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
3012 &name, &fob, &pathname,
3013 &suffix, &mode, &type))
3014 return NULL;
3015 if (*mode) {
3016 /* Mode must start with 'r' or 'U' and must not contain '+'.
3017 Implicit in this test is the assumption that the mode
3018 may contain other modifiers like 'b' or 't'. */
3020 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3021 PyErr_Format(PyExc_ValueError,
3022 "invalid file open mode %.200s", mode);
3023 return NULL;
3026 if (fob == Py_None)
3027 fp = NULL;
3028 else {
3029 fp = get_file(NULL, fob, mode);
3030 if (fp == NULL)
3031 return NULL;
3033 return load_module(name, fp, pathname, type, NULL);
3036 static PyObject *
3037 imp_load_package(PyObject *self, PyObject *args)
3039 char *name;
3040 char *pathname;
3041 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
3042 return NULL;
3043 return load_package(name, pathname);
3046 static PyObject *
3047 imp_new_module(PyObject *self, PyObject *args)
3049 char *name;
3050 if (!PyArg_ParseTuple(args, "s:new_module", &name))
3051 return NULL;
3052 return PyModule_New(name);
3055 static PyObject *
3056 imp_reload(PyObject *self, PyObject *v)
3058 return PyImport_ReloadModule(v);
3061 PyDoc_STRVAR(doc_reload,
3062 "reload(module) -> module\n\
3064 Reload the module. The module must have been successfully imported before.");
3066 /* Doc strings */
3068 PyDoc_STRVAR(doc_imp,
3069 "This module provides the components needed to build your own\n\
3070 __import__ function. Undocumented functions are obsolete.");
3072 PyDoc_STRVAR(doc_find_module,
3073 "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
3074 Search for a module. If path is omitted or None, search for a\n\
3075 built-in, frozen or special module and continue search in sys.path.\n\
3076 The module name cannot contain '.'; to search for a submodule of a\n\
3077 package, pass the submodule name and the package's __path__.");
3079 PyDoc_STRVAR(doc_load_module,
3080 "load_module(name, file, filename, (suffix, mode, type)) -> module\n\
3081 Load a module, given information returned by find_module().\n\
3082 The module name must include the full package name, if any.");
3084 PyDoc_STRVAR(doc_get_magic,
3085 "get_magic() -> string\n\
3086 Return the magic number for .pyc or .pyo files.");
3088 PyDoc_STRVAR(doc_get_suffixes,
3089 "get_suffixes() -> [(suffix, mode, type), ...]\n\
3090 Return a list of (suffix, mode, type) tuples describing the files\n\
3091 that find_module() looks for.");
3093 PyDoc_STRVAR(doc_new_module,
3094 "new_module(name) -> module\n\
3095 Create a new module. Do not enter it in sys.modules.\n\
3096 The module name must include the full package name, if any.");
3098 PyDoc_STRVAR(doc_lock_held,
3099 "lock_held() -> boolean\n\
3100 Return True if the import lock is currently held, else False.\n\
3101 On platforms without threads, return False.");
3103 PyDoc_STRVAR(doc_acquire_lock,
3104 "acquire_lock() -> None\n\
3105 Acquires the interpreter's import lock for the current thread.\n\
3106 This lock should be used by import hooks to ensure thread-safety\n\
3107 when importing modules.\n\
3108 On platforms without threads, this function does nothing.");
3110 PyDoc_STRVAR(doc_release_lock,
3111 "release_lock() -> None\n\
3112 Release the interpreter's import lock.\n\
3113 On platforms without threads, this function does nothing.");
3115 static PyMethodDef imp_methods[] = {
3116 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3117 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3118 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3119 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3120 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3121 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3122 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3123 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
3124 {"reload", imp_reload, METH_O, doc_reload},
3125 /* The rest are obsolete */
3126 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3127 {"init_builtin", imp_init_builtin, METH_VARARGS},
3128 {"init_frozen", imp_init_frozen, METH_VARARGS},
3129 {"is_builtin", imp_is_builtin, METH_VARARGS},
3130 {"is_frozen", imp_is_frozen, METH_VARARGS},
3131 {"load_compiled", imp_load_compiled, METH_VARARGS},
3132 #ifdef HAVE_DYNAMIC_LOADING
3133 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
3134 #endif
3135 {"load_package", imp_load_package, METH_VARARGS},
3136 {"load_source", imp_load_source, METH_VARARGS},
3137 {NULL, NULL} /* sentinel */
3140 static int
3141 setint(PyObject *d, char *name, int value)
3143 PyObject *v;
3144 int err;
3146 v = PyLong_FromLong((long)value);
3147 err = PyDict_SetItemString(d, name, v);
3148 Py_XDECREF(v);
3149 return err;
3152 typedef struct {
3153 PyObject_HEAD
3154 } NullImporter;
3156 static int
3157 NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3159 char *path;
3160 Py_ssize_t pathlen;
3162 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3163 return -1;
3165 if (!PyArg_ParseTuple(args, "es:NullImporter",
3166 Py_FileSystemDefaultEncoding, &path))
3167 return -1;
3169 pathlen = strlen(path);
3170 if (pathlen == 0) {
3171 PyErr_SetString(PyExc_ImportError, "empty pathname");
3172 return -1;
3173 } else {
3174 struct stat statbuf;
3175 int rv;
3177 rv = stat(path, &statbuf);
3178 #ifdef MS_WINDOWS
3179 /* MS Windows stat() chokes on paths like C:\path\. Try to
3180 * recover *one* time by stripping off a trailing slash or
3181 * backslash. http://bugs.python.org/issue1293
3183 if (rv != 0 && pathlen <= MAXPATHLEN &&
3184 (path[pathlen-1] == '/' || path[pathlen-1] == '\\')) {
3185 char mangled[MAXPATHLEN+1];
3187 strcpy(mangled, path);
3188 mangled[pathlen-1] = '\0';
3189 rv = stat(mangled, &statbuf);
3191 #endif
3192 if (rv == 0) {
3193 /* it exists */
3194 if (S_ISDIR(statbuf.st_mode)) {
3195 /* it's a directory */
3196 PyErr_SetString(PyExc_ImportError,
3197 "existing directory");
3198 return -1;
3202 return 0;
3205 static PyObject *
3206 NullImporter_find_module(NullImporter *self, PyObject *args)
3208 Py_RETURN_NONE;
3211 static PyMethodDef NullImporter_methods[] = {
3212 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3213 "Always return None"
3215 {NULL} /* Sentinel */
3219 PyTypeObject PyNullImporter_Type = {
3220 PyVarObject_HEAD_INIT(NULL, 0)
3221 "imp.NullImporter", /*tp_name*/
3222 sizeof(NullImporter), /*tp_basicsize*/
3223 0, /*tp_itemsize*/
3224 0, /*tp_dealloc*/
3225 0, /*tp_print*/
3226 0, /*tp_getattr*/
3227 0, /*tp_setattr*/
3228 0, /*tp_compare*/
3229 0, /*tp_repr*/
3230 0, /*tp_as_number*/
3231 0, /*tp_as_sequence*/
3232 0, /*tp_as_mapping*/
3233 0, /*tp_hash */
3234 0, /*tp_call*/
3235 0, /*tp_str*/
3236 0, /*tp_getattro*/
3237 0, /*tp_setattro*/
3238 0, /*tp_as_buffer*/
3239 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3240 "Null importer object", /* tp_doc */
3241 0, /* tp_traverse */
3242 0, /* tp_clear */
3243 0, /* tp_richcompare */
3244 0, /* tp_weaklistoffset */
3245 0, /* tp_iter */
3246 0, /* tp_iternext */
3247 NullImporter_methods, /* tp_methods */
3248 0, /* tp_members */
3249 0, /* tp_getset */
3250 0, /* tp_base */
3251 0, /* tp_dict */
3252 0, /* tp_descr_get */
3253 0, /* tp_descr_set */
3254 0, /* tp_dictoffset */
3255 (initproc)NullImporter_init, /* tp_init */
3256 0, /* tp_alloc */
3257 PyType_GenericNew /* tp_new */
3260 static struct PyModuleDef impmodule = {
3261 PyModuleDef_HEAD_INIT,
3262 "imp",
3263 doc_imp,
3265 imp_methods,
3266 NULL,
3267 NULL,
3268 NULL,
3269 NULL
3272 PyMODINIT_FUNC
3273 PyInit_imp(void)
3275 PyObject *m, *d;
3277 if (PyType_Ready(&PyNullImporter_Type) < 0)
3278 return NULL;
3280 m = PyModule_Create(&impmodule);
3281 if (m == NULL)
3282 goto failure;
3283 d = PyModule_GetDict(m);
3284 if (d == NULL)
3285 goto failure;
3287 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3288 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3289 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3290 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3291 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3292 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3293 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3294 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
3295 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
3296 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
3298 Py_INCREF(&PyNullImporter_Type);
3299 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
3300 return m;
3301 failure:
3302 Py_XDECREF(m);
3303 return NULL;
3308 /* API for embedding applications that want to add their own entries
3309 to the table of built-in modules. This should normally be called
3310 *before* Py_Initialize(). When the table resize fails, -1 is
3311 returned and the existing table is unchanged.
3313 After a similar function by Just van Rossum. */
3316 PyImport_ExtendInittab(struct _inittab *newtab)
3318 static struct _inittab *our_copy = NULL;
3319 struct _inittab *p;
3320 int i, n;
3322 /* Count the number of entries in both tables */
3323 for (n = 0; newtab[n].name != NULL; n++)
3325 if (n == 0)
3326 return 0; /* Nothing to do */
3327 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3330 /* Allocate new memory for the combined table */
3331 p = our_copy;
3332 PyMem_RESIZE(p, struct _inittab, i+n+1);
3333 if (p == NULL)
3334 return -1;
3336 /* Copy the tables into the new memory */
3337 if (our_copy != PyImport_Inittab)
3338 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3339 PyImport_Inittab = our_copy = p;
3340 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3342 return 0;
3345 /* Shorthand to add a single entry given a name and a function */
3348 PyImport_AppendInittab(char *name, PyObject* (*initfunc)(void))
3350 struct _inittab newtab[2];
3352 memset(newtab, '\0', sizeof newtab);
3354 newtab[0].name = name;
3355 newtab[0].initfunc = initfunc;
3357 return PyImport_ExtendInittab(newtab);
3360 #ifdef __cplusplus
3362 #endif