This commit was manufactured by cvs2svn to create tag 'r201'.
[python/dscho.git] / Python / import.c
blob203fe41051f7c573f76fb919d5c96cd81a55ca40
2 /* Module definition and import implementation */
4 #include "Python.h"
6 #include "node.h"
7 #include "token.h"
8 #include "errcode.h"
9 #include "marshal.h"
10 #include "compile.h"
11 #include "eval.h"
12 #include "osdefs.h"
13 #include "importdl.h"
14 #ifdef macintosh
15 #include "macglue.h"
16 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
22 /* We expect that stat exists on most systems.
23 It's confirmed on Unix, Mac and Windows.
24 If you don't have it, add #define DONT_HAVE_STAT to your config.h. */
25 #ifndef DONT_HAVE_STAT
26 #define HAVE_STAT
28 #ifndef DONT_HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
32 #ifndef DONT_HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #elif defined(HAVE_STAT_H)
35 #include <stat.h>
36 #endif
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif
42 #if defined(PYCC_VACPP)
43 /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
44 #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
45 #endif
47 #ifndef S_ISDIR
48 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
49 #endif
51 #endif
54 extern time_t PyOS_GetLastModificationTime(char *, FILE *);
55 /* In getmtime.c */
57 /* Magic word to reject .pyc files generated by other Python versions */
58 /* Change for each incompatible change */
59 /* The value of CR and LF is incorporated so if you ever read or write
60 a .pyc file in text mode the magic number will be wrong; also, the
61 Apple MPW compiler swaps their values, botching string constants */
62 /* XXX Perhaps the magic number should be frozen and a version field
63 added to the .pyc file header? */
64 /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
65 #define MAGIC (50823 | ((long)'\r'<<16) | ((long)'\n'<<24))
67 /* Magic word as global; note that _PyImport_Init() can change the
68 value of this global to accommodate for alterations of how the
69 compiler works which are enabled by command line switches. */
70 static long pyc_magic = MAGIC;
72 /* See _PyImport_FixupExtension() below */
73 static PyObject *extensions = NULL;
75 /* This table is defined in config.c: */
76 extern struct _inittab _PyImport_Inittab[];
78 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
80 /* these tables define the module suffixes that Python recognizes */
81 struct filedescr * _PyImport_Filetab = NULL;
82 static const struct filedescr _PyImport_StandardFiletab[] = {
83 {".py", "r", PY_SOURCE},
84 {".pyc", "rb", PY_COMPILED},
85 {0, 0}
88 /* Initialize things */
90 void
91 _PyImport_Init(void)
93 const struct filedescr *scan;
94 struct filedescr *filetab;
95 int countD = 0;
96 int countS = 0;
98 /* prepare _PyImport_Filetab: copy entries from
99 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
101 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
102 ++countD;
103 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
104 ++countS;
105 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
106 memcpy(filetab, _PyImport_DynLoadFiletab,
107 countD * sizeof(struct filedescr));
108 memcpy(filetab + countD, _PyImport_StandardFiletab,
109 countS * sizeof(struct filedescr));
110 filetab[countD + countS].suffix = NULL;
112 _PyImport_Filetab = filetab;
114 if (Py_OptimizeFlag) {
115 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
116 for (; filetab->suffix != NULL; filetab++) {
117 if (strcmp(filetab->suffix, ".pyc") == 0)
118 filetab->suffix = ".pyo";
122 if (Py_UnicodeFlag) {
123 /* Fix the pyc_magic so that byte compiled code created
124 using the all-Unicode method doesn't interfere with
125 code created in normal operation mode. */
126 pyc_magic = MAGIC + 1;
130 void
131 _PyImport_Fini(void)
133 Py_XDECREF(extensions);
134 extensions = NULL;
135 PyMem_DEL(_PyImport_Filetab);
136 _PyImport_Filetab = NULL;
140 /* Locking primitives to prevent parallel imports of the same module
141 in different threads to return with a partially loaded module.
142 These calls are serialized by the global interpreter lock. */
144 #ifdef WITH_THREAD
146 #include "pythread.h"
148 static PyThread_type_lock import_lock = 0;
149 static long import_lock_thread = -1;
150 static int import_lock_level = 0;
152 static void
153 lock_import(void)
155 long me = PyThread_get_thread_ident();
156 if (me == -1)
157 return; /* Too bad */
158 if (import_lock == NULL)
159 import_lock = PyThread_allocate_lock();
160 if (import_lock_thread == me) {
161 import_lock_level++;
162 return;
164 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
165 PyThreadState *tstate = PyEval_SaveThread();
166 PyThread_acquire_lock(import_lock, 1);
167 PyEval_RestoreThread(tstate);
169 import_lock_thread = me;
170 import_lock_level = 1;
173 static void
174 unlock_import(void)
176 long me = PyThread_get_thread_ident();
177 if (me == -1)
178 return; /* Too bad */
179 if (import_lock_thread != me)
180 Py_FatalError("unlock_import: not holding the import lock");
181 import_lock_level--;
182 if (import_lock_level == 0) {
183 import_lock_thread = -1;
184 PyThread_release_lock(import_lock);
188 #else
190 #define lock_import()
191 #define unlock_import()
193 #endif
195 /* Helper for sys */
197 PyObject *
198 PyImport_GetModuleDict(void)
200 PyInterpreterState *interp = PyThreadState_Get()->interp;
201 if (interp->modules == NULL)
202 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
203 return interp->modules;
207 /* List of names to clear in sys */
208 static char* sys_deletes[] = {
209 "path", "argv", "ps1", "ps2", "exitfunc",
210 "exc_type", "exc_value", "exc_traceback",
211 "last_type", "last_value", "last_traceback",
212 NULL
215 static char* sys_files[] = {
216 "stdin", "__stdin__",
217 "stdout", "__stdout__",
218 "stderr", "__stderr__",
219 NULL
223 /* Un-initialize things, as good as we can */
225 void
226 PyImport_Cleanup(void)
228 int pos, ndone;
229 char *name;
230 PyObject *key, *value, *dict;
231 PyInterpreterState *interp = PyThreadState_Get()->interp;
232 PyObject *modules = interp->modules;
234 if (modules == NULL)
235 return; /* Already done */
237 /* Delete some special variables first. These are common
238 places where user values hide and people complain when their
239 destructors fail. Since the modules containing them are
240 deleted *last* of all, they would come too late in the normal
241 destruction order. Sigh. */
243 value = PyDict_GetItemString(modules, "__builtin__");
244 if (value != NULL && PyModule_Check(value)) {
245 dict = PyModule_GetDict(value);
246 if (Py_VerboseFlag)
247 PySys_WriteStderr("# clear __builtin__._\n");
248 PyDict_SetItemString(dict, "_", Py_None);
250 value = PyDict_GetItemString(modules, "sys");
251 if (value != NULL && PyModule_Check(value)) {
252 char **p;
253 PyObject *v;
254 dict = PyModule_GetDict(value);
255 for (p = sys_deletes; *p != NULL; p++) {
256 if (Py_VerboseFlag)
257 PySys_WriteStderr("# clear sys.%s\n", *p);
258 PyDict_SetItemString(dict, *p, Py_None);
260 for (p = sys_files; *p != NULL; p+=2) {
261 if (Py_VerboseFlag)
262 PySys_WriteStderr("# restore sys.%s\n", *p);
263 v = PyDict_GetItemString(dict, *(p+1));
264 if (v == NULL)
265 v = Py_None;
266 PyDict_SetItemString(dict, *p, v);
270 /* First, delete __main__ */
271 value = PyDict_GetItemString(modules, "__main__");
272 if (value != NULL && PyModule_Check(value)) {
273 if (Py_VerboseFlag)
274 PySys_WriteStderr("# cleanup __main__\n");
275 _PyModule_Clear(value);
276 PyDict_SetItemString(modules, "__main__", Py_None);
279 /* The special treatment of __builtin__ here is because even
280 when it's not referenced as a module, its dictionary is
281 referenced by almost every module's __builtins__. Since
282 deleting a module clears its dictionary (even if there are
283 references left to it), we need to delete the __builtin__
284 module last. Likewise, we don't delete sys until the very
285 end because it is implicitly referenced (e.g. by print).
287 Also note that we 'delete' modules by replacing their entry
288 in the modules dict with None, rather than really deleting
289 them; this avoids a rehash of the modules dictionary and
290 also marks them as "non existent" so they won't be
291 re-imported. */
293 /* Next, repeatedly delete modules with a reference count of
294 one (skipping __builtin__ and sys) and delete them */
295 do {
296 ndone = 0;
297 pos = 0;
298 while (PyDict_Next(modules, &pos, &key, &value)) {
299 if (value->ob_refcnt != 1)
300 continue;
301 if (PyString_Check(key) && PyModule_Check(value)) {
302 name = PyString_AS_STRING(key);
303 if (strcmp(name, "__builtin__") == 0)
304 continue;
305 if (strcmp(name, "sys") == 0)
306 continue;
307 if (Py_VerboseFlag)
308 PySys_WriteStderr(
309 "# cleanup[1] %s\n", name);
310 _PyModule_Clear(value);
311 PyDict_SetItem(modules, key, Py_None);
312 ndone++;
315 } while (ndone > 0);
317 /* Next, delete all modules (still skipping __builtin__ and sys) */
318 pos = 0;
319 while (PyDict_Next(modules, &pos, &key, &value)) {
320 if (PyString_Check(key) && PyModule_Check(value)) {
321 name = PyString_AS_STRING(key);
322 if (strcmp(name, "__builtin__") == 0)
323 continue;
324 if (strcmp(name, "sys") == 0)
325 continue;
326 if (Py_VerboseFlag)
327 PySys_WriteStderr("# cleanup[2] %s\n", name);
328 _PyModule_Clear(value);
329 PyDict_SetItem(modules, key, Py_None);
333 /* Next, delete sys and __builtin__ (in that order) */
334 value = PyDict_GetItemString(modules, "sys");
335 if (value != NULL && PyModule_Check(value)) {
336 if (Py_VerboseFlag)
337 PySys_WriteStderr("# cleanup sys\n");
338 _PyModule_Clear(value);
339 PyDict_SetItemString(modules, "sys", Py_None);
341 value = PyDict_GetItemString(modules, "__builtin__");
342 if (value != NULL && PyModule_Check(value)) {
343 if (Py_VerboseFlag)
344 PySys_WriteStderr("# cleanup __builtin__\n");
345 _PyModule_Clear(value);
346 PyDict_SetItemString(modules, "__builtin__", Py_None);
349 /* Finally, clear and delete the modules directory */
350 PyDict_Clear(modules);
351 interp->modules = NULL;
352 Py_DECREF(modules);
356 /* Helper for pythonrun.c -- return magic number */
358 long
359 PyImport_GetMagicNumber(void)
361 return pyc_magic;
365 /* Magic for extension modules (built-in as well as dynamically
366 loaded). To prevent initializing an extension module more than
367 once, we keep a static dictionary 'extensions' keyed by module name
368 (for built-in modules) or by filename (for dynamically loaded
369 modules), containing these modules. A copy od the module's
370 dictionary is stored by calling _PyImport_FixupExtension()
371 immediately after the module initialization function succeeds. A
372 copy can be retrieved from there by calling
373 _PyImport_FindExtension(). */
375 PyObject *
376 _PyImport_FixupExtension(char *name, char *filename)
378 PyObject *modules, *mod, *dict, *copy;
379 if (extensions == NULL) {
380 extensions = PyDict_New();
381 if (extensions == NULL)
382 return NULL;
384 modules = PyImport_GetModuleDict();
385 mod = PyDict_GetItemString(modules, name);
386 if (mod == NULL || !PyModule_Check(mod)) {
387 PyErr_Format(PyExc_SystemError,
388 "_PyImport_FixupExtension: module %.200s not loaded", name);
389 return NULL;
391 dict = PyModule_GetDict(mod);
392 if (dict == NULL)
393 return NULL;
394 copy = PyObject_CallMethod(dict, "copy", "");
395 if (copy == NULL)
396 return NULL;
397 PyDict_SetItemString(extensions, filename, copy);
398 Py_DECREF(copy);
399 return copy;
402 PyObject *
403 _PyImport_FindExtension(char *name, char *filename)
405 PyObject *dict, *mod, *mdict, *result;
406 if (extensions == NULL)
407 return NULL;
408 dict = PyDict_GetItemString(extensions, filename);
409 if (dict == NULL)
410 return NULL;
411 mod = PyImport_AddModule(name);
412 if (mod == NULL)
413 return NULL;
414 mdict = PyModule_GetDict(mod);
415 if (mdict == NULL)
416 return NULL;
417 result = PyObject_CallMethod(mdict, "update", "O", dict);
418 if (result == NULL)
419 return NULL;
420 Py_DECREF(result);
421 if (Py_VerboseFlag)
422 PySys_WriteStderr("import %s # previously loaded (%s)\n",
423 name, filename);
424 return mod;
428 /* Get the module object corresponding to a module name.
429 First check the modules dictionary if there's one there,
430 if not, create a new one and insert in in the modules dictionary.
431 Because the former action is most common, THIS DOES NOT RETURN A
432 'NEW' REFERENCE! */
434 PyObject *
435 PyImport_AddModule(char *name)
437 PyObject *modules = PyImport_GetModuleDict();
438 PyObject *m;
440 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
441 PyModule_Check(m))
442 return m;
443 m = PyModule_New(name);
444 if (m == NULL)
445 return NULL;
446 if (PyDict_SetItemString(modules, name, m) != 0) {
447 Py_DECREF(m);
448 return NULL;
450 Py_DECREF(m); /* Yes, it still exists, in modules! */
452 return m;
456 /* Execute a code object in a module and return the module object
457 WITH INCREMENTED REFERENCE COUNT */
459 PyObject *
460 PyImport_ExecCodeModule(char *name, PyObject *co)
462 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
465 PyObject *
466 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
468 PyObject *modules = PyImport_GetModuleDict();
469 PyObject *m, *d, *v;
471 m = PyImport_AddModule(name);
472 if (m == NULL)
473 return NULL;
474 d = PyModule_GetDict(m);
475 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
476 if (PyDict_SetItemString(d, "__builtins__",
477 PyEval_GetBuiltins()) != 0)
478 return NULL;
480 /* Remember the filename as the __file__ attribute */
481 v = NULL;
482 if (pathname != NULL) {
483 v = PyString_FromString(pathname);
484 if (v == NULL)
485 PyErr_Clear();
487 if (v == NULL) {
488 v = ((PyCodeObject *)co)->co_filename;
489 Py_INCREF(v);
491 if (PyDict_SetItemString(d, "__file__", v) != 0)
492 PyErr_Clear(); /* Not important enough to report */
493 Py_DECREF(v);
495 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
496 if (v == NULL)
497 return NULL;
498 Py_DECREF(v);
500 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
501 PyErr_Format(PyExc_ImportError,
502 "Loaded module %.200s not found in sys.modules",
503 name);
504 return NULL;
507 Py_INCREF(m);
509 return m;
513 /* Given a pathname for a Python source file, fill a buffer with the
514 pathname for the corresponding compiled file. Return the pathname
515 for the compiled file, or NULL if there's no space in the buffer.
516 Doesn't set an exception. */
518 static char *
519 make_compiled_pathname(char *pathname, char *buf, size_t buflen)
521 size_t len;
523 len = strlen(pathname);
524 if (len+2 > buflen)
525 return NULL;
526 strcpy(buf, pathname);
527 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
529 return buf;
533 /* Given a pathname for a Python source file, its time of last
534 modification, and a pathname for a compiled file, check whether the
535 compiled file represents the same version of the source. If so,
536 return a FILE pointer for the compiled file, positioned just after
537 the header; if not, return NULL.
538 Doesn't set an exception. */
540 static FILE *
541 check_compiled_module(char *pathname, long mtime, char *cpathname)
543 FILE *fp;
544 long magic;
545 long pyc_mtime;
547 fp = fopen(cpathname, "rb");
548 if (fp == NULL)
549 return NULL;
550 magic = PyMarshal_ReadLongFromFile(fp);
551 if (magic != pyc_magic) {
552 if (Py_VerboseFlag)
553 PySys_WriteStderr("# %s has bad magic\n", cpathname);
554 fclose(fp);
555 return NULL;
557 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
558 if (pyc_mtime != mtime) {
559 if (Py_VerboseFlag)
560 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
561 fclose(fp);
562 return NULL;
564 if (Py_VerboseFlag)
565 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
566 return fp;
570 /* Read a code object from a file and check it for validity */
572 static PyCodeObject *
573 read_compiled_module(char *cpathname, FILE *fp)
575 PyObject *co;
577 co = PyMarshal_ReadObjectFromFile(fp);
578 /* Ugly: rd_object() may return NULL with or without error */
579 if (co == NULL || !PyCode_Check(co)) {
580 if (!PyErr_Occurred())
581 PyErr_Format(PyExc_ImportError,
582 "Non-code object in %.200s", cpathname);
583 Py_XDECREF(co);
584 return NULL;
586 return (PyCodeObject *)co;
590 /* Load a module from a compiled file, execute it, and return its
591 module object WITH INCREMENTED REFERENCE COUNT */
593 static PyObject *
594 load_compiled_module(char *name, char *cpathname, FILE *fp)
596 long magic;
597 PyCodeObject *co;
598 PyObject *m;
600 magic = PyMarshal_ReadLongFromFile(fp);
601 if (magic != pyc_magic) {
602 PyErr_Format(PyExc_ImportError,
603 "Bad magic number in %.200s", cpathname);
604 return NULL;
606 (void) PyMarshal_ReadLongFromFile(fp);
607 co = read_compiled_module(cpathname, fp);
608 if (co == NULL)
609 return NULL;
610 if (Py_VerboseFlag)
611 PySys_WriteStderr("import %s # precompiled from %s\n",
612 name, cpathname);
613 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
614 Py_DECREF(co);
616 return m;
619 /* Parse a source file and return the corresponding code object */
621 static PyCodeObject *
622 parse_source_module(char *pathname, FILE *fp)
624 PyCodeObject *co;
625 node *n;
627 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
628 if (n == NULL)
629 return NULL;
630 co = PyNode_Compile(n, pathname);
631 PyNode_Free(n);
633 return co;
637 /* Helper to open a bytecode file for writing in exclusive mode */
639 static FILE *
640 open_exclusive(char *filename)
642 #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
643 /* Use O_EXCL to avoid a race condition when another process tries to
644 write the same file. When that happens, our open() call fails,
645 which is just fine (since it's only a cache).
646 XXX If the file exists and is writable but the directory is not
647 writable, the file will never be written. Oh well.
649 int fd;
650 (void) unlink(filename);
651 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
652 #ifdef O_BINARY
653 |O_BINARY /* necessary for Windows */
654 #endif
656 , 0666);
657 if (fd < 0)
658 return NULL;
659 return fdopen(fd, "wb");
660 #else
661 /* Best we can do -- on Windows this can't happen anyway */
662 return fopen(filename, "wb");
663 #endif
667 /* Write a compiled module to a file, placing the time of last
668 modification of its source into the header.
669 Errors are ignored, if a write error occurs an attempt is made to
670 remove the file. */
672 static void
673 write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
675 FILE *fp;
677 fp = open_exclusive(cpathname);
678 if (fp == NULL) {
679 if (Py_VerboseFlag)
680 PySys_WriteStderr(
681 "# can't create %s\n", cpathname);
682 return;
684 PyMarshal_WriteLongToFile(pyc_magic, fp);
685 /* First write a 0 for mtime */
686 PyMarshal_WriteLongToFile(0L, fp);
687 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
688 if (ferror(fp)) {
689 if (Py_VerboseFlag)
690 PySys_WriteStderr("# can't write %s\n", cpathname);
691 /* Don't keep partial file */
692 fclose(fp);
693 (void) unlink(cpathname);
694 return;
696 /* Now write the true mtime */
697 fseek(fp, 4L, 0);
698 PyMarshal_WriteLongToFile(mtime, fp);
699 fflush(fp);
700 fclose(fp);
701 if (Py_VerboseFlag)
702 PySys_WriteStderr("# wrote %s\n", cpathname);
703 #ifdef macintosh
704 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
705 #endif
709 /* Load a source module from a given file and return its module
710 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
711 byte-compiled file, use that instead. */
713 static PyObject *
714 load_source_module(char *name, char *pathname, FILE *fp)
716 time_t mtime;
717 FILE *fpc;
718 char buf[MAXPATHLEN+1];
719 char *cpathname;
720 PyCodeObject *co;
721 PyObject *m;
723 mtime = PyOS_GetLastModificationTime(pathname, fp);
724 if (mtime == -1)
725 return NULL;
726 #if SIZEOF_TIME_T > 4
727 /* Python's .pyc timestamp handling presumes that the timestamp fits
728 in 4 bytes. This will be fine until sometime in the year 2038,
729 when a 4-byte signed time_t will overflow.
731 if (mtime >> 32) {
732 PyErr_SetString(PyExc_OverflowError,
733 "modification time overflows a 4 bytes");
734 return NULL;
736 #endif
737 cpathname = make_compiled_pathname(pathname, buf, (size_t)MAXPATHLEN+1);
738 if (cpathname != NULL &&
739 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
740 co = read_compiled_module(cpathname, fpc);
741 fclose(fpc);
742 if (co == NULL)
743 return NULL;
744 if (Py_VerboseFlag)
745 PySys_WriteStderr("import %s # precompiled from %s\n",
746 name, cpathname);
747 pathname = cpathname;
749 else {
750 co = parse_source_module(pathname, fp);
751 if (co == NULL)
752 return NULL;
753 if (Py_VerboseFlag)
754 PySys_WriteStderr("import %s # from %s\n",
755 name, pathname);
756 write_compiled_module(co, cpathname, mtime);
758 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
759 Py_DECREF(co);
761 return m;
765 /* Forward */
766 static PyObject *load_module(char *, FILE *, char *, int);
767 static struct filedescr *find_module(char *, PyObject *,
768 char *, size_t, FILE **);
769 static struct _frozen *find_frozen(char *name);
771 /* Load a package and return its module object WITH INCREMENTED
772 REFERENCE COUNT */
774 static PyObject *
775 load_package(char *name, char *pathname)
777 PyObject *m, *d, *file, *path;
778 int err;
779 char buf[MAXPATHLEN+1];
780 FILE *fp = NULL;
781 struct filedescr *fdp;
783 m = PyImport_AddModule(name);
784 if (m == NULL)
785 return NULL;
786 if (Py_VerboseFlag)
787 PySys_WriteStderr("import %s # directory %s\n",
788 name, pathname);
789 d = PyModule_GetDict(m);
790 file = PyString_FromString(pathname);
791 if (file == NULL)
792 return NULL;
793 path = Py_BuildValue("[O]", file);
794 if (path == NULL) {
795 Py_DECREF(file);
796 return NULL;
798 err = PyDict_SetItemString(d, "__file__", file);
799 if (err == 0)
800 err = PyDict_SetItemString(d, "__path__", path);
801 if (err != 0) {
802 m = NULL;
803 goto cleanup;
805 buf[0] = '\0';
806 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
807 if (fdp == NULL) {
808 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
809 PyErr_Clear();
811 else
812 m = NULL;
813 goto cleanup;
815 m = load_module(name, fp, buf, fdp->type);
816 if (fp != NULL)
817 fclose(fp);
818 cleanup:
819 Py_XDECREF(path);
820 Py_XDECREF(file);
821 return m;
825 /* Helper to test for built-in module */
827 static int
828 is_builtin(char *name)
830 int i;
831 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
832 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
833 if (PyImport_Inittab[i].initfunc == NULL)
834 return -1;
835 else
836 return 1;
839 return 0;
843 /* Search the path (default sys.path) for a module. Return the
844 corresponding filedescr struct, and (via return arguments) the
845 pathname and an open file. Return NULL if the module is not found. */
847 #ifdef MS_COREDLL
848 extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
849 char *, int);
850 #endif
852 #ifdef CHECK_IMPORT_CASE
853 static int check_case(char *, int, int, char *);
854 #endif
856 static int find_init_module(char *); /* Forward */
858 static struct filedescr *
859 find_module(char *realname, PyObject *path, char *buf, size_t buflen,
860 FILE **p_fp)
862 int i, npath;
863 size_t len, namelen;
864 struct _frozen *f;
865 struct filedescr *fdp = NULL;
866 FILE *fp = NULL;
867 struct stat statbuf;
868 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
869 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
870 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
871 char name[MAXPATHLEN+1];
873 if (strlen(realname) > MAXPATHLEN) {
874 PyErr_SetString(PyExc_OverflowError, "module name is too long");
875 return NULL;
877 strcpy(name, realname);
879 if (path != NULL && PyString_Check(path)) {
880 /* Submodule of "frozen" package:
881 Set name to the fullname, path to NULL
882 and continue as "usual" */
883 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
884 PyErr_SetString(PyExc_ImportError,
885 "full frozen module name too long");
886 return NULL;
888 strcpy(buf, PyString_AsString(path));
889 strcat(buf, ".");
890 strcat(buf, name);
891 strcpy(name, buf);
892 path = NULL;
894 if (path == NULL) {
895 if (is_builtin(name)) {
896 strcpy(buf, name);
897 return &fd_builtin;
899 if ((f = find_frozen(name)) != NULL) {
900 strcpy(buf, name);
901 return &fd_frozen;
904 #ifdef MS_COREDLL
905 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
906 if (fp != NULL) {
907 *p_fp = fp;
908 return fdp;
910 #endif
911 path = PySys_GetObject("path");
913 if (path == NULL || !PyList_Check(path)) {
914 PyErr_SetString(PyExc_ImportError,
915 "sys.path must be a list of directory names");
916 return NULL;
918 npath = PyList_Size(path);
919 namelen = strlen(name);
920 for (i = 0; i < npath; i++) {
921 PyObject *v = PyList_GetItem(path, i);
922 if (!PyString_Check(v))
923 continue;
924 len = PyString_Size(v);
925 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
926 continue; /* Too long */
927 strcpy(buf, PyString_AsString(v));
928 if (strlen(buf) != len)
929 continue; /* v contains '\0' */
930 #ifdef macintosh
931 #ifdef INTERN_STRINGS
933 ** Speedup: each sys.path item is interned, and
934 ** FindResourceModule remembers which items refer to
935 ** folders (so we don't have to bother trying to look
936 ** into them for resources).
938 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
939 v = PyList_GET_ITEM(path, i);
940 #endif
941 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
942 static struct filedescr resfiledescr =
943 {"", "", PY_RESOURCE};
945 return &resfiledescr;
947 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
948 static struct filedescr resfiledescr =
949 {"", "", PY_CODERESOURCE};
951 return &resfiledescr;
953 #endif
954 if (len > 0 && buf[len-1] != SEP
955 #ifdef ALTSEP
956 && buf[len-1] != ALTSEP
957 #endif
959 buf[len++] = SEP;
960 #ifdef IMPORT_8x3_NAMES
961 /* see if we are searching in directory dos-8x3 */
962 if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){
963 int j;
964 char ch; /* limit name to 8 lower-case characters */
965 for (j = 0; (ch = name[j]) && j < 8; j++)
966 if (isupper(ch))
967 buf[len++] = tolower(ch);
968 else
969 buf[len++] = ch;
971 else /* Not in dos-8x3, use the full name */
972 #endif
974 strcpy(buf+len, name);
975 len += namelen;
977 #ifdef HAVE_STAT
978 if (stat(buf, &statbuf) == 0) {
979 if (S_ISDIR(statbuf.st_mode)) {
980 if (find_init_module(buf)) {
981 #ifdef CHECK_IMPORT_CASE
982 if (!check_case(buf, len, namelen,
983 name))
984 return NULL;
985 #endif
986 return &fd_package;
990 #else
991 /* XXX How are you going to test for directories? */
992 #endif
993 #ifdef macintosh
994 fdp = PyMac_FindModuleExtension(buf, &len, name);
995 if (fdp)
996 fp = fopen(buf, fdp->mode);
997 #else
998 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
999 strcpy(buf+len, fdp->suffix);
1000 if (Py_VerboseFlag > 1)
1001 PySys_WriteStderr("# trying %s\n", buf);
1002 fp = fopen(buf, fdp->mode);
1003 if (fp != NULL)
1004 break;
1006 #endif /* !macintosh */
1007 if (fp != NULL)
1008 break;
1010 if (fp == NULL) {
1011 PyErr_Format(PyExc_ImportError,
1012 "No module named %.200s", name);
1013 return NULL;
1015 #ifdef CHECK_IMPORT_CASE
1016 if (!check_case(buf, len, namelen, name)) {
1017 fclose(fp);
1018 return NULL;
1020 #endif
1022 *p_fp = fp;
1023 return fdp;
1026 #ifdef CHECK_IMPORT_CASE
1028 #ifdef MS_WIN32
1029 #include <windows.h>
1030 #include <ctype.h>
1032 static int
1033 allcaps8x3(char *s)
1035 /* Return 1 if s is an 8.3 filename in ALLCAPS */
1036 char c;
1037 char *dot = strchr(s, '.');
1038 char *end = strchr(s, '\0');
1039 if (dot != NULL) {
1040 if (dot-s > 8)
1041 return 0; /* More than 8 before '.' */
1042 if (end-dot > 4)
1043 return 0; /* More than 3 after '.' */
1044 end = strchr(dot+1, '.');
1045 if (end != NULL)
1046 return 0; /* More than one dot */
1048 else if (end-s > 8)
1049 return 0; /* More than 8 and no dot */
1050 while ((c = *s++)) {
1051 if (islower(c))
1052 return 0;
1054 return 1;
1057 static int
1058 check_case(char *buf, int len, int namelen, char *name)
1060 WIN32_FIND_DATA data;
1061 HANDLE h;
1062 if (getenv("PYTHONCASEOK") != NULL)
1063 return 1;
1064 h = FindFirstFile(buf, &data);
1065 if (h == INVALID_HANDLE_VALUE) {
1066 PyErr_Format(PyExc_NameError,
1067 "Can't find file for module %.100s\n(filename %.300s)",
1068 name, buf);
1069 return 0;
1071 FindClose(h);
1072 if (allcaps8x3(data.cFileName)) {
1073 /* Skip the test if the filename is ALL.CAPS. This can
1074 happen in certain circumstances beyond our control,
1075 e.g. when software is installed under NT on a FAT
1076 filesystem and then the same FAT filesystem is used
1077 under Windows 95. */
1078 return 1;
1080 if (strncmp(data.cFileName, name, namelen) != 0) {
1081 strcpy(buf+len-namelen, data.cFileName);
1082 PyErr_Format(PyExc_NameError,
1083 "Case mismatch for module name %.100s\n(filename %.300s)",
1084 name, buf);
1085 return 0;
1087 return 1;
1089 #endif /* MS_WIN32 */
1091 #ifdef macintosh
1092 #include <TextUtils.h>
1093 #ifdef USE_GUSI1
1094 #include "TFileSpec.h" /* for Path2FSSpec() */
1095 #endif
1096 static int
1097 check_case(char *buf, int len, int namelen, char *name)
1099 FSSpec fss;
1100 OSErr err;
1101 #ifndef USE_GUSI1
1102 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1103 #else
1104 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1105 the way, which is fine for all directories, but here we need
1106 the original name of the alias file (say, Dlg.ppc.slb, not
1107 toolboxmodules.ppc.slb). */
1108 char *colon;
1109 err = Path2FSSpec(buf, &fss);
1110 if (err == noErr) {
1111 colon = strrchr(buf, ':'); /* find filename */
1112 if (colon != NULL)
1113 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1114 Pstring(colon+1), &fss);
1115 else
1116 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1117 fss.name, &fss);
1119 #endif
1120 if (err) {
1121 PyErr_Format(PyExc_NameError,
1122 "Can't find file for module %.100s\n(filename %.300s)",
1123 name, buf);
1124 return 0;
1126 if ( namelen > fss.name[0] || strncmp(name, (char *)fss.name+1, namelen) != 0 ) {
1127 PyErr_Format(PyExc_NameError,
1128 "Case mismatch for module name %.100s\n(filename %.300s)",
1129 name, fss.name);
1130 return 0;
1132 return 1;
1134 #endif /* macintosh */
1136 #ifdef DJGPP
1137 #include <dir.h>
1139 static int
1140 check_case(char *buf, int len, int namelen, char *name)
1142 struct ffblk ffblk;
1143 int done;
1145 if (getenv("PYTHONCASEOK") != NULL)
1146 return 1;
1147 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1148 if (done) {
1149 PyErr_Format(PyExc_NameError,
1150 "Can't find file for module %.100s\n(filename %.300s)",
1151 name, buf);
1152 return 0;
1155 if (strncmp(ffblk.ff_name, name, namelen) != 0) {
1156 strcpy(buf+len-namelen, ffblk.ff_name);
1157 PyErr_Format(PyExc_NameError,
1158 "Case mismatch for module name %.100s\n(filename %.300s)",
1159 name, buf);
1160 return 0;
1162 return 1;
1164 #endif
1166 #endif /* CHECK_IMPORT_CASE */
1168 #ifdef HAVE_STAT
1169 /* Helper to look for __init__.py or __init__.py[co] in potential package */
1170 static int
1171 find_init_module(char *buf)
1173 size_t save_len = strlen(buf);
1174 size_t i = save_len;
1175 struct stat statbuf;
1177 if (save_len + 13 >= MAXPATHLEN)
1178 return 0;
1179 buf[i++] = SEP;
1180 strcpy(buf+i, "__init__.py");
1181 if (stat(buf, &statbuf) == 0) {
1182 buf[save_len] = '\0';
1183 return 1;
1185 i += strlen(buf+i);
1186 if (Py_OptimizeFlag)
1187 strcpy(buf+i, "o");
1188 else
1189 strcpy(buf+i, "c");
1190 if (stat(buf, &statbuf) == 0) {
1191 buf[save_len] = '\0';
1192 return 1;
1194 buf[save_len] = '\0';
1195 return 0;
1197 #endif /* HAVE_STAT */
1200 static int init_builtin(char *); /* Forward */
1202 /* Load an external module using the default search path and return
1203 its module object WITH INCREMENTED REFERENCE COUNT */
1205 static PyObject *
1206 load_module(char *name, FILE *fp, char *buf, int type)
1208 PyObject *modules;
1209 PyObject *m;
1210 int err;
1212 /* First check that there's an open file (if we need one) */
1213 switch (type) {
1214 case PY_SOURCE:
1215 case PY_COMPILED:
1216 if (fp == NULL) {
1217 PyErr_Format(PyExc_ValueError,
1218 "file object required for import (type code %d)",
1219 type);
1220 return NULL;
1224 switch (type) {
1226 case PY_SOURCE:
1227 m = load_source_module(name, buf, fp);
1228 break;
1230 case PY_COMPILED:
1231 m = load_compiled_module(name, buf, fp);
1232 break;
1234 #ifdef HAVE_DYNAMIC_LOADING
1235 case C_EXTENSION:
1236 m = _PyImport_LoadDynamicModule(name, buf, fp);
1237 break;
1238 #endif
1240 #ifdef macintosh
1241 case PY_RESOURCE:
1242 m = PyMac_LoadResourceModule(name, buf);
1243 break;
1244 case PY_CODERESOURCE:
1245 m = PyMac_LoadCodeResourceModule(name, buf);
1246 break;
1247 #endif
1249 case PKG_DIRECTORY:
1250 m = load_package(name, buf);
1251 break;
1253 case C_BUILTIN:
1254 case PY_FROZEN:
1255 if (buf != NULL && buf[0] != '\0')
1256 name = buf;
1257 if (type == C_BUILTIN)
1258 err = init_builtin(name);
1259 else
1260 err = PyImport_ImportFrozenModule(name);
1261 if (err < 0)
1262 return NULL;
1263 if (err == 0) {
1264 PyErr_Format(PyExc_ImportError,
1265 "Purported %s module %.200s not found",
1266 type == C_BUILTIN ?
1267 "builtin" : "frozen",
1268 name);
1269 return NULL;
1271 modules = PyImport_GetModuleDict();
1272 m = PyDict_GetItemString(modules, name);
1273 if (m == NULL) {
1274 PyErr_Format(
1275 PyExc_ImportError,
1276 "%s module %.200s not properly initialized",
1277 type == C_BUILTIN ?
1278 "builtin" : "frozen",
1279 name);
1280 return NULL;
1282 Py_INCREF(m);
1283 break;
1285 default:
1286 PyErr_Format(PyExc_ImportError,
1287 "Don't know how to import %.200s (type code %d)",
1288 name, type);
1289 m = NULL;
1293 return m;
1297 /* Initialize a built-in module.
1298 Return 1 for succes, 0 if the module is not found, and -1 with
1299 an exception set if the initialization failed. */
1301 static int
1302 init_builtin(char *name)
1304 struct _inittab *p;
1305 PyObject *mod;
1307 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1308 return 1;
1310 for (p = PyImport_Inittab; p->name != NULL; p++) {
1311 if (strcmp(name, p->name) == 0) {
1312 if (p->initfunc == NULL) {
1313 PyErr_Format(PyExc_ImportError,
1314 "Cannot re-init internal module %.200s",
1315 name);
1316 return -1;
1318 if (Py_VerboseFlag)
1319 PySys_WriteStderr("import %s # builtin\n", name);
1320 (*p->initfunc)();
1321 if (PyErr_Occurred())
1322 return -1;
1323 if (_PyImport_FixupExtension(name, name) == NULL)
1324 return -1;
1325 return 1;
1328 return 0;
1332 /* Frozen modules */
1334 static struct _frozen *
1335 find_frozen(char *name)
1337 struct _frozen *p;
1339 for (p = PyImport_FrozenModules; ; p++) {
1340 if (p->name == NULL)
1341 return NULL;
1342 if (strcmp(p->name, name) == 0)
1343 break;
1345 return p;
1348 static PyObject *
1349 get_frozen_object(char *name)
1351 struct _frozen *p = find_frozen(name);
1352 int size;
1354 if (p == NULL) {
1355 PyErr_Format(PyExc_ImportError,
1356 "No such frozen object named %.200s",
1357 name);
1358 return NULL;
1360 size = p->size;
1361 if (size < 0)
1362 size = -size;
1363 return PyMarshal_ReadObjectFromString((char *)p->code, size);
1366 /* Initialize a frozen module.
1367 Return 1 for succes, 0 if the module is not found, and -1 with
1368 an exception set if the initialization failed.
1369 This function is also used from frozenmain.c */
1372 PyImport_ImportFrozenModule(char *name)
1374 struct _frozen *p = find_frozen(name);
1375 PyObject *co;
1376 PyObject *m;
1377 int ispackage;
1378 int size;
1380 if (p == NULL)
1381 return 0;
1382 size = p->size;
1383 ispackage = (size < 0);
1384 if (ispackage)
1385 size = -size;
1386 if (Py_VerboseFlag)
1387 PySys_WriteStderr("import %s # frozen%s\n",
1388 name, ispackage ? " package" : "");
1389 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1390 if (co == NULL)
1391 return -1;
1392 if (!PyCode_Check(co)) {
1393 Py_DECREF(co);
1394 PyErr_Format(PyExc_TypeError,
1395 "frozen object %.200s is not a code object",
1396 name);
1397 return -1;
1399 if (ispackage) {
1400 /* Set __path__ to the package name */
1401 PyObject *d, *s;
1402 int err;
1403 m = PyImport_AddModule(name);
1404 if (m == NULL)
1405 return -1;
1406 d = PyModule_GetDict(m);
1407 s = PyString_InternFromString(name);
1408 if (s == NULL)
1409 return -1;
1410 err = PyDict_SetItemString(d, "__path__", s);
1411 Py_DECREF(s);
1412 if (err != 0)
1413 return err;
1415 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
1416 Py_DECREF(co);
1417 if (m == NULL)
1418 return -1;
1419 Py_DECREF(m);
1420 return 1;
1424 /* Import a module, either built-in, frozen, or external, and return
1425 its module object WITH INCREMENTED REFERENCE COUNT */
1427 PyObject *
1428 PyImport_ImportModule(char *name)
1430 static PyObject *fromlist = NULL;
1431 if (fromlist == NULL && strchr(name, '.') != NULL) {
1432 fromlist = Py_BuildValue("(s)", "*");
1433 if (fromlist == NULL)
1434 return NULL;
1436 return PyImport_ImportModuleEx(name, NULL, NULL, fromlist);
1439 /* Forward declarations for helper routines */
1440 static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1441 static PyObject *load_next(PyObject *mod, PyObject *altmod,
1442 char **p_name, char *buf, int *p_buflen);
1443 static int mark_miss(char *name);
1444 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1445 char *buf, int buflen, int recursive);
1446 static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
1448 /* The Magnum Opus of dotted-name import :-) */
1450 static PyObject *
1451 import_module_ex(char *name, PyObject *globals, PyObject *locals,
1452 PyObject *fromlist)
1454 char buf[MAXPATHLEN+1];
1455 int buflen = 0;
1456 PyObject *parent, *head, *next, *tail;
1458 parent = get_parent(globals, buf, &buflen);
1459 if (parent == NULL)
1460 return NULL;
1462 head = load_next(parent, Py_None, &name, buf, &buflen);
1463 if (head == NULL)
1464 return NULL;
1466 tail = head;
1467 Py_INCREF(tail);
1468 while (name) {
1469 next = load_next(tail, tail, &name, buf, &buflen);
1470 Py_DECREF(tail);
1471 if (next == NULL) {
1472 Py_DECREF(head);
1473 return NULL;
1475 tail = next;
1478 if (fromlist != NULL) {
1479 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1480 fromlist = NULL;
1483 if (fromlist == NULL) {
1484 Py_DECREF(tail);
1485 return head;
1488 Py_DECREF(head);
1489 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
1490 Py_DECREF(tail);
1491 return NULL;
1494 return tail;
1497 PyObject *
1498 PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1499 PyObject *fromlist)
1501 PyObject *result;
1502 lock_import();
1503 result = import_module_ex(name, globals, locals, fromlist);
1504 unlock_import();
1505 return result;
1508 static PyObject *
1509 get_parent(PyObject *globals, char *buf, int *p_buflen)
1511 static PyObject *namestr = NULL;
1512 static PyObject *pathstr = NULL;
1513 PyObject *modname, *modpath, *modules, *parent;
1515 if (globals == NULL || !PyDict_Check(globals))
1516 return Py_None;
1518 if (namestr == NULL) {
1519 namestr = PyString_InternFromString("__name__");
1520 if (namestr == NULL)
1521 return NULL;
1523 if (pathstr == NULL) {
1524 pathstr = PyString_InternFromString("__path__");
1525 if (pathstr == NULL)
1526 return NULL;
1529 *buf = '\0';
1530 *p_buflen = 0;
1531 modname = PyDict_GetItem(globals, namestr);
1532 if (modname == NULL || !PyString_Check(modname))
1533 return Py_None;
1535 modpath = PyDict_GetItem(globals, pathstr);
1536 if (modpath != NULL) {
1537 int len = PyString_GET_SIZE(modname);
1538 if (len > MAXPATHLEN) {
1539 PyErr_SetString(PyExc_ValueError,
1540 "Module name too long");
1541 return NULL;
1543 strcpy(buf, PyString_AS_STRING(modname));
1544 *p_buflen = len;
1546 else {
1547 char *start = PyString_AS_STRING(modname);
1548 char *lastdot = strrchr(start, '.');
1549 size_t len;
1550 if (lastdot == NULL)
1551 return Py_None;
1552 len = lastdot - start;
1553 if (len >= MAXPATHLEN) {
1554 PyErr_SetString(PyExc_ValueError,
1555 "Module name too long");
1556 return NULL;
1558 strncpy(buf, start, len);
1559 buf[len] = '\0';
1560 *p_buflen = len;
1563 modules = PyImport_GetModuleDict();
1564 parent = PyDict_GetItemString(modules, buf);
1565 if (parent == NULL)
1566 parent = Py_None;
1567 return parent;
1568 /* We expect, but can't guarantee, if parent != None, that:
1569 - parent.__name__ == buf
1570 - parent.__dict__ is globals
1571 If this is violated... Who cares? */
1574 /* altmod is either None or same as mod */
1575 static PyObject *
1576 load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1577 int *p_buflen)
1579 char *name = *p_name;
1580 char *dot = strchr(name, '.');
1581 size_t len;
1582 char *p;
1583 PyObject *result;
1585 if (dot == NULL) {
1586 *p_name = NULL;
1587 len = strlen(name);
1589 else {
1590 *p_name = dot+1;
1591 len = dot-name;
1593 if (len == 0) {
1594 PyErr_SetString(PyExc_ValueError,
1595 "Empty module name");
1596 return NULL;
1599 p = buf + *p_buflen;
1600 if (p != buf)
1601 *p++ = '.';
1602 if (p+len-buf >= MAXPATHLEN) {
1603 PyErr_SetString(PyExc_ValueError,
1604 "Module name too long");
1605 return NULL;
1607 strncpy(p, name, len);
1608 p[len] = '\0';
1609 *p_buflen = p+len-buf;
1611 result = import_submodule(mod, p, buf);
1612 if (result == Py_None && altmod != mod) {
1613 Py_DECREF(result);
1614 /* Here, altmod must be None and mod must not be None */
1615 result = import_submodule(altmod, p, p);
1616 if (result != NULL && result != Py_None) {
1617 if (mark_miss(buf) != 0) {
1618 Py_DECREF(result);
1619 return NULL;
1621 strncpy(buf, name, len);
1622 buf[len] = '\0';
1623 *p_buflen = len;
1626 if (result == NULL)
1627 return NULL;
1629 if (result == Py_None) {
1630 Py_DECREF(result);
1631 PyErr_Format(PyExc_ImportError,
1632 "No module named %.200s", name);
1633 return NULL;
1636 return result;
1639 static int
1640 mark_miss(char *name)
1642 PyObject *modules = PyImport_GetModuleDict();
1643 return PyDict_SetItemString(modules, name, Py_None);
1646 static int
1647 ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1648 int recursive)
1650 int i;
1652 if (!PyObject_HasAttrString(mod, "__path__"))
1653 return 1;
1655 for (i = 0; ; i++) {
1656 PyObject *item = PySequence_GetItem(fromlist, i);
1657 int hasit;
1658 if (item == NULL) {
1659 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1660 PyErr_Clear();
1661 return 1;
1663 return 0;
1665 if (!PyString_Check(item)) {
1666 PyErr_SetString(PyExc_TypeError,
1667 "Item in ``from list'' not a string");
1668 Py_DECREF(item);
1669 return 0;
1671 if (PyString_AS_STRING(item)[0] == '*') {
1672 PyObject *all;
1673 Py_DECREF(item);
1674 /* See if the package defines __all__ */
1675 if (recursive)
1676 continue; /* Avoid endless recursion */
1677 all = PyObject_GetAttrString(mod, "__all__");
1678 if (all == NULL)
1679 PyErr_Clear();
1680 else {
1681 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1682 return 0;
1683 Py_DECREF(all);
1685 continue;
1687 hasit = PyObject_HasAttr(mod, item);
1688 if (!hasit) {
1689 char *subname = PyString_AS_STRING(item);
1690 PyObject *submod;
1691 char *p;
1692 if (buflen + strlen(subname) >= MAXPATHLEN) {
1693 PyErr_SetString(PyExc_ValueError,
1694 "Module name too long");
1695 Py_DECREF(item);
1696 return 0;
1698 p = buf + buflen;
1699 *p++ = '.';
1700 strcpy(p, subname);
1701 submod = import_submodule(mod, subname, buf);
1702 Py_XDECREF(submod);
1703 if (submod == NULL) {
1704 Py_DECREF(item);
1705 return 0;
1708 Py_DECREF(item);
1711 /* NOTREACHED */
1714 static PyObject *
1715 import_submodule(PyObject *mod, char *subname, char *fullname)
1717 PyObject *modules = PyImport_GetModuleDict();
1718 PyObject *m;
1720 /* Require:
1721 if mod == None: subname == fullname
1722 else: mod.__name__ + "." + subname == fullname
1725 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
1726 Py_INCREF(m);
1728 else {
1729 PyObject *path;
1730 char buf[MAXPATHLEN+1];
1731 struct filedescr *fdp;
1732 FILE *fp = NULL;
1734 if (mod == Py_None)
1735 path = NULL;
1736 else {
1737 path = PyObject_GetAttrString(mod, "__path__");
1738 if (path == NULL) {
1739 PyErr_Clear();
1740 Py_INCREF(Py_None);
1741 return Py_None;
1745 buf[0] = '\0';
1746 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1747 Py_XDECREF(path);
1748 if (fdp == NULL) {
1749 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1750 return NULL;
1751 PyErr_Clear();
1752 Py_INCREF(Py_None);
1753 return Py_None;
1755 m = load_module(fullname, fp, buf, fdp->type);
1756 if (fp)
1757 fclose(fp);
1758 if (m != NULL && mod != Py_None) {
1759 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1760 Py_DECREF(m);
1761 m = NULL;
1766 return m;
1770 /* Re-import a module of any kind and return its module object, WITH
1771 INCREMENTED REFERENCE COUNT */
1773 PyObject *
1774 PyImport_ReloadModule(PyObject *m)
1776 PyObject *modules = PyImport_GetModuleDict();
1777 PyObject *path = NULL;
1778 char *name, *subname;
1779 char buf[MAXPATHLEN+1];
1780 struct filedescr *fdp;
1781 FILE *fp = NULL;
1783 if (m == NULL || !PyModule_Check(m)) {
1784 PyErr_SetString(PyExc_TypeError,
1785 "reload() argument must be module");
1786 return NULL;
1788 name = PyModule_GetName(m);
1789 if (name == NULL)
1790 return NULL;
1791 if (m != PyDict_GetItemString(modules, name)) {
1792 PyErr_Format(PyExc_ImportError,
1793 "reload(): module %.200s not in sys.modules",
1794 name);
1795 return NULL;
1797 subname = strrchr(name, '.');
1798 if (subname == NULL)
1799 subname = name;
1800 else {
1801 PyObject *parentname, *parent;
1802 parentname = PyString_FromStringAndSize(name, (subname-name));
1803 if (parentname == NULL)
1804 return NULL;
1805 parent = PyDict_GetItem(modules, parentname);
1806 Py_DECREF(parentname);
1807 if (parent == NULL) {
1808 PyErr_Format(PyExc_ImportError,
1809 "reload(): parent %.200s not in sys.modules",
1810 name);
1811 return NULL;
1813 subname++;
1814 path = PyObject_GetAttrString(parent, "__path__");
1815 if (path == NULL)
1816 PyErr_Clear();
1818 buf[0] = '\0';
1819 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1820 Py_XDECREF(path);
1821 if (fdp == NULL)
1822 return NULL;
1823 m = load_module(name, fp, buf, fdp->type);
1824 if (fp)
1825 fclose(fp);
1826 return m;
1830 /* Higher-level import emulator which emulates the "import" statement
1831 more accurately -- it invokes the __import__() function from the
1832 builtins of the current globals. This means that the import is
1833 done using whatever import hooks are installed in the current
1834 environment, e.g. by "rexec".
1835 A dummy list ["__doc__"] is passed as the 4th argument so that
1836 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1837 will return <module "gencache"> instead of <module "win32com">. */
1839 PyObject *
1840 PyImport_Import(PyObject *module_name)
1842 static PyObject *silly_list = NULL;
1843 static PyObject *builtins_str = NULL;
1844 static PyObject *import_str = NULL;
1845 PyObject *globals = NULL;
1846 PyObject *import = NULL;
1847 PyObject *builtins = NULL;
1848 PyObject *r = NULL;
1850 /* Initialize constant string objects */
1851 if (silly_list == NULL) {
1852 import_str = PyString_InternFromString("__import__");
1853 if (import_str == NULL)
1854 return NULL;
1855 builtins_str = PyString_InternFromString("__builtins__");
1856 if (builtins_str == NULL)
1857 return NULL;
1858 silly_list = Py_BuildValue("[s]", "__doc__");
1859 if (silly_list == NULL)
1860 return NULL;
1863 /* Get the builtins from current globals */
1864 globals = PyEval_GetGlobals();
1865 if(globals != NULL) {
1866 Py_INCREF(globals);
1867 builtins = PyObject_GetItem(globals, builtins_str);
1868 if (builtins == NULL)
1869 goto err;
1871 else {
1872 /* No globals -- use standard builtins, and fake globals */
1873 PyErr_Clear();
1875 builtins = PyImport_ImportModuleEx("__builtin__",
1876 NULL, NULL, NULL);
1877 if (builtins == NULL)
1878 return NULL;
1879 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1880 if (globals == NULL)
1881 goto err;
1884 /* Get the __import__ function from the builtins */
1885 if (PyDict_Check(builtins))
1886 import=PyObject_GetItem(builtins, import_str);
1887 else
1888 import=PyObject_GetAttr(builtins, import_str);
1889 if (import == NULL)
1890 goto err;
1892 /* Call the _import__ function with the proper argument list */
1893 r = PyObject_CallFunction(import, "OOOO",
1894 module_name, globals, globals, silly_list);
1896 err:
1897 Py_XDECREF(globals);
1898 Py_XDECREF(builtins);
1899 Py_XDECREF(import);
1901 return r;
1905 /* Module 'imp' provides Python access to the primitives used for
1906 importing modules.
1909 static PyObject *
1910 imp_get_magic(PyObject *self, PyObject *args)
1912 char buf[4];
1914 if (!PyArg_ParseTuple(args, ":get_magic"))
1915 return NULL;
1916 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1917 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1918 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1919 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
1921 return PyString_FromStringAndSize(buf, 4);
1924 static PyObject *
1925 imp_get_suffixes(PyObject *self, PyObject *args)
1927 PyObject *list;
1928 struct filedescr *fdp;
1930 if (!PyArg_ParseTuple(args, ":get_suffixes"))
1931 return NULL;
1932 list = PyList_New(0);
1933 if (list == NULL)
1934 return NULL;
1935 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1936 PyObject *item = Py_BuildValue("ssi",
1937 fdp->suffix, fdp->mode, fdp->type);
1938 if (item == NULL) {
1939 Py_DECREF(list);
1940 return NULL;
1942 if (PyList_Append(list, item) < 0) {
1943 Py_DECREF(list);
1944 Py_DECREF(item);
1945 return NULL;
1947 Py_DECREF(item);
1949 return list;
1952 static PyObject *
1953 call_find_module(char *name, PyObject *path)
1955 extern int fclose(FILE *);
1956 PyObject *fob, *ret;
1957 struct filedescr *fdp;
1958 char pathname[MAXPATHLEN+1];
1959 FILE *fp = NULL;
1961 pathname[0] = '\0';
1962 if (path == Py_None)
1963 path = NULL;
1964 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
1965 if (fdp == NULL)
1966 return NULL;
1967 if (fp != NULL) {
1968 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
1969 if (fob == NULL) {
1970 fclose(fp);
1971 return NULL;
1974 else {
1975 fob = Py_None;
1976 Py_INCREF(fob);
1978 ret = Py_BuildValue("Os(ssi)",
1979 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
1980 Py_DECREF(fob);
1981 return ret;
1984 static PyObject *
1985 imp_find_module(PyObject *self, PyObject *args)
1987 char *name;
1988 PyObject *path = NULL;
1989 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
1990 return NULL;
1991 return call_find_module(name, path);
1994 static PyObject *
1995 imp_init_builtin(PyObject *self, PyObject *args)
1997 char *name;
1998 int ret;
1999 PyObject *m;
2000 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2001 return NULL;
2002 ret = init_builtin(name);
2003 if (ret < 0)
2004 return NULL;
2005 if (ret == 0) {
2006 Py_INCREF(Py_None);
2007 return Py_None;
2009 m = PyImport_AddModule(name);
2010 Py_XINCREF(m);
2011 return m;
2014 static PyObject *
2015 imp_init_frozen(PyObject *self, PyObject *args)
2017 char *name;
2018 int ret;
2019 PyObject *m;
2020 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2021 return NULL;
2022 ret = PyImport_ImportFrozenModule(name);
2023 if (ret < 0)
2024 return NULL;
2025 if (ret == 0) {
2026 Py_INCREF(Py_None);
2027 return Py_None;
2029 m = PyImport_AddModule(name);
2030 Py_XINCREF(m);
2031 return m;
2034 static PyObject *
2035 imp_get_frozen_object(PyObject *self, PyObject *args)
2037 char *name;
2039 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2040 return NULL;
2041 return get_frozen_object(name);
2044 static PyObject *
2045 imp_is_builtin(PyObject *self, PyObject *args)
2047 char *name;
2048 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2049 return NULL;
2050 return PyInt_FromLong(is_builtin(name));
2053 static PyObject *
2054 imp_is_frozen(PyObject *self, PyObject *args)
2056 char *name;
2057 struct _frozen *p;
2058 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2059 return NULL;
2060 p = find_frozen(name);
2061 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
2064 static FILE *
2065 get_file(char *pathname, PyObject *fob, char *mode)
2067 FILE *fp;
2068 if (fob == NULL) {
2069 fp = fopen(pathname, mode);
2070 if (fp == NULL)
2071 PyErr_SetFromErrno(PyExc_IOError);
2073 else {
2074 fp = PyFile_AsFile(fob);
2075 if (fp == NULL)
2076 PyErr_SetString(PyExc_ValueError,
2077 "bad/closed file object");
2079 return fp;
2082 static PyObject *
2083 imp_load_compiled(PyObject *self, PyObject *args)
2085 char *name;
2086 char *pathname;
2087 PyObject *fob = NULL;
2088 PyObject *m;
2089 FILE *fp;
2090 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
2091 &PyFile_Type, &fob))
2092 return NULL;
2093 fp = get_file(pathname, fob, "rb");
2094 if (fp == NULL)
2095 return NULL;
2096 m = load_compiled_module(name, pathname, fp);
2097 if (fob == NULL)
2098 fclose(fp);
2099 return m;
2102 #ifdef HAVE_DYNAMIC_LOADING
2104 static PyObject *
2105 imp_load_dynamic(PyObject *self, PyObject *args)
2107 char *name;
2108 char *pathname;
2109 PyObject *fob = NULL;
2110 PyObject *m;
2111 FILE *fp = NULL;
2112 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
2113 &PyFile_Type, &fob))
2114 return NULL;
2115 if (fob) {
2116 fp = get_file(pathname, fob, "r");
2117 if (fp == NULL)
2118 return NULL;
2120 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2121 return m;
2124 #endif /* HAVE_DYNAMIC_LOADING */
2126 static PyObject *
2127 imp_load_source(PyObject *self, PyObject *args)
2129 char *name;
2130 char *pathname;
2131 PyObject *fob = NULL;
2132 PyObject *m;
2133 FILE *fp;
2134 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
2135 &PyFile_Type, &fob))
2136 return NULL;
2137 fp = get_file(pathname, fob, "r");
2138 if (fp == NULL)
2139 return NULL;
2140 m = load_source_module(name, pathname, fp);
2141 if (fob == NULL)
2142 fclose(fp);
2143 return m;
2146 #ifdef macintosh
2147 static PyObject *
2148 imp_load_resource(PyObject *self, PyObject *args)
2150 char *name;
2151 char *pathname;
2152 PyObject *m;
2154 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
2155 return NULL;
2156 m = PyMac_LoadResourceModule(name, pathname);
2157 return m;
2159 #endif /* macintosh */
2161 static PyObject *
2162 imp_load_module(PyObject *self, PyObject *args)
2164 char *name;
2165 PyObject *fob;
2166 char *pathname;
2167 char *suffix; /* Unused */
2168 char *mode;
2169 int type;
2170 FILE *fp;
2172 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
2173 &name, &fob, &pathname,
2174 &suffix, &mode, &type))
2175 return NULL;
2176 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2177 PyErr_Format(PyExc_ValueError,
2178 "invalid file open mode %.200s", mode);
2179 return NULL;
2181 if (fob == Py_None)
2182 fp = NULL;
2183 else {
2184 if (!PyFile_Check(fob)) {
2185 PyErr_SetString(PyExc_ValueError,
2186 "load_module arg#2 should be a file or None");
2187 return NULL;
2189 fp = get_file(pathname, fob, mode);
2190 if (fp == NULL)
2191 return NULL;
2193 return load_module(name, fp, pathname, type);
2196 static PyObject *
2197 imp_load_package(PyObject *self, PyObject *args)
2199 char *name;
2200 char *pathname;
2201 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
2202 return NULL;
2203 return load_package(name, pathname);
2206 static PyObject *
2207 imp_new_module(PyObject *self, PyObject *args)
2209 char *name;
2210 if (!PyArg_ParseTuple(args, "s:new_module", &name))
2211 return NULL;
2212 return PyModule_New(name);
2215 /* Doc strings */
2217 static char doc_imp[] = "\
2218 This module provides the components needed to build your own\n\
2219 __import__ function. Undocumented functions are obsolete.\n\
2222 static char doc_find_module[] = "\
2223 find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2224 Search for a module. If path is omitted or None, search for a\n\
2225 built-in, frozen or special module and continue search in sys.path.\n\
2226 The module name cannot contain '.'; to search for a submodule of a\n\
2227 package, pass the submodule name and the package's __path__.\
2230 static char doc_load_module[] = "\
2231 load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2232 Load a module, given information returned by find_module().\n\
2233 The module name must include the full package name, if any.\
2236 static char doc_get_magic[] = "\
2237 get_magic() -> string\n\
2238 Return the magic number for .pyc or .pyo files.\
2241 static char doc_get_suffixes[] = "\
2242 get_suffixes() -> [(suffix, mode, type), ...]\n\
2243 Return a list of (suffix, mode, type) tuples describing the files\n\
2244 that find_module() looks for.\
2247 static char doc_new_module[] = "\
2248 new_module(name) -> module\n\
2249 Create a new module. Do not enter it in sys.modules.\n\
2250 The module name must include the full package name, if any.\
2253 static PyMethodDef imp_methods[] = {
2254 {"find_module", imp_find_module, 1, doc_find_module},
2255 {"get_magic", imp_get_magic, 1, doc_get_magic},
2256 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2257 {"load_module", imp_load_module, 1, doc_load_module},
2258 {"new_module", imp_new_module, 1, doc_new_module},
2259 /* The rest are obsolete */
2260 {"get_frozen_object", imp_get_frozen_object, 1},
2261 {"init_builtin", imp_init_builtin, 1},
2262 {"init_frozen", imp_init_frozen, 1},
2263 {"is_builtin", imp_is_builtin, 1},
2264 {"is_frozen", imp_is_frozen, 1},
2265 {"load_compiled", imp_load_compiled, 1},
2266 #ifdef HAVE_DYNAMIC_LOADING
2267 {"load_dynamic", imp_load_dynamic, 1},
2268 #endif
2269 {"load_package", imp_load_package, 1},
2270 #ifdef macintosh
2271 {"load_resource", imp_load_resource, 1},
2272 #endif
2273 {"load_source", imp_load_source, 1},
2274 {NULL, NULL} /* sentinel */
2277 static int
2278 setint(PyObject *d, char *name, int value)
2280 PyObject *v;
2281 int err;
2283 v = PyInt_FromLong((long)value);
2284 err = PyDict_SetItemString(d, name, v);
2285 Py_XDECREF(v);
2286 return err;
2289 void
2290 initimp(void)
2292 PyObject *m, *d;
2294 m = Py_InitModule4("imp", imp_methods, doc_imp,
2295 NULL, PYTHON_API_VERSION);
2296 d = PyModule_GetDict(m);
2298 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2299 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2300 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2301 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2302 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2303 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2304 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2305 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
2306 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
2308 failure:
2313 /* API for embedding applications that want to add their own entries
2314 to the table of built-in modules. This should normally be called
2315 *before* Py_Initialize(). When the table resize fails, -1 is
2316 returned and the existing table is unchanged.
2318 After a similar function by Just van Rossum. */
2321 PyImport_ExtendInittab(struct _inittab *newtab)
2323 static struct _inittab *our_copy = NULL;
2324 struct _inittab *p;
2325 int i, n;
2327 /* Count the number of entries in both tables */
2328 for (n = 0; newtab[n].name != NULL; n++)
2330 if (n == 0)
2331 return 0; /* Nothing to do */
2332 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2335 /* Allocate new memory for the combined table */
2336 p = our_copy;
2337 PyMem_RESIZE(p, struct _inittab, i+n+1);
2338 if (p == NULL)
2339 return -1;
2341 /* Copy the tables into the new memory */
2342 if (our_copy != PyImport_Inittab)
2343 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2344 PyImport_Inittab = our_copy = p;
2345 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2347 return 0;
2350 /* Shorthand to add a single entry given a name and a function */
2353 PyImport_AppendInittab(char *name, void (*initfunc)(void))
2355 struct _inittab newtab[2];
2357 memset(newtab, '\0', sizeof newtab);
2359 newtab[0].name = name;
2360 newtab[0].initfunc = initfunc;
2362 return PyImport_ExtendInittab(newtab);