This commit was manufactured by cvs2svn to create tag 'r212'.
[python/dscho.git] / Python / import.c
blob3d55fe4c33c3ab7057bfac7100d38c9f20773663
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 #ifdef HAVE_FCNTL_H
23 #include <fcntl.h>
24 #endif
26 #if defined(PYCC_VACPP)
27 /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
28 #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
29 #endif
31 #ifndef S_ISDIR
32 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
33 #endif
35 extern time_t PyOS_GetLastModificationTime(char *, FILE *);
36 /* In getmtime.c */
38 /* Magic word to reject .pyc files generated by other Python versions */
39 /* Change for each incompatible change */
40 /* The value of CR and LF is incorporated so if you ever read or write
41 a .pyc file in text mode the magic number will be wrong; also, the
42 Apple MPW compiler swaps their values, botching string constants */
43 /* XXX Perhaps the magic number should be frozen and a version field
44 added to the .pyc file header? */
45 /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
46 #define MAGIC (60202 | ((long)'\r'<<16) | ((long)'\n'<<24))
48 /* Magic word as global; note that _PyImport_Init() can change the
49 value of this global to accommodate for alterations of how the
50 compiler works which are enabled by command line switches. */
51 static long pyc_magic = MAGIC;
53 /* See _PyImport_FixupExtension() below */
54 static PyObject *extensions = NULL;
56 /* This table is defined in config.c: */
57 extern struct _inittab _PyImport_Inittab[];
59 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
61 /* these tables define the module suffixes that Python recognizes */
62 struct filedescr * _PyImport_Filetab = NULL;
64 #ifdef RISCOS
65 static const struct filedescr _PyImport_StandardFiletab[] = {
66 {"/py", "r", PY_SOURCE},
67 {"/pyc", "rb", PY_COMPILED},
68 {0, 0}
70 #else
71 static const struct filedescr _PyImport_StandardFiletab[] = {
72 {".py", "r", PY_SOURCE},
73 {".pyc", "rb", PY_COMPILED},
74 {0, 0}
76 #endif
78 /* Initialize things */
80 void
81 _PyImport_Init(void)
83 const struct filedescr *scan;
84 struct filedescr *filetab;
85 int countD = 0;
86 int countS = 0;
88 /* prepare _PyImport_Filetab: copy entries from
89 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
91 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
92 ++countD;
93 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
94 ++countS;
95 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
96 memcpy(filetab, _PyImport_DynLoadFiletab,
97 countD * sizeof(struct filedescr));
98 memcpy(filetab + countD, _PyImport_StandardFiletab,
99 countS * sizeof(struct filedescr));
100 filetab[countD + countS].suffix = NULL;
102 _PyImport_Filetab = filetab;
104 if (Py_OptimizeFlag) {
105 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
106 for (; filetab->suffix != NULL; filetab++) {
107 #ifndef RISCOS
108 if (strcmp(filetab->suffix, ".pyc") == 0)
109 filetab->suffix = ".pyo";
110 #else
111 if (strcmp(filetab->suffix, "/pyc") == 0)
112 filetab->suffix = "/pyo";
113 #endif
117 if (Py_UnicodeFlag) {
118 /* Fix the pyc_magic so that byte compiled code created
119 using the all-Unicode method doesn't interfere with
120 code created in normal operation mode. */
121 pyc_magic = MAGIC + 1;
125 void
126 _PyImport_Fini(void)
128 Py_XDECREF(extensions);
129 extensions = NULL;
130 PyMem_DEL(_PyImport_Filetab);
131 _PyImport_Filetab = NULL;
135 /* Locking primitives to prevent parallel imports of the same module
136 in different threads to return with a partially loaded module.
137 These calls are serialized by the global interpreter lock. */
139 #ifdef WITH_THREAD
141 #include "pythread.h"
143 static PyThread_type_lock import_lock = 0;
144 static long import_lock_thread = -1;
145 static int import_lock_level = 0;
147 static void
148 lock_import(void)
150 long me = PyThread_get_thread_ident();
151 if (me == -1)
152 return; /* Too bad */
153 if (import_lock == NULL)
154 import_lock = PyThread_allocate_lock();
155 if (import_lock_thread == me) {
156 import_lock_level++;
157 return;
159 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
160 PyThreadState *tstate = PyEval_SaveThread();
161 PyThread_acquire_lock(import_lock, 1);
162 PyEval_RestoreThread(tstate);
164 import_lock_thread = me;
165 import_lock_level = 1;
168 static void
169 unlock_import(void)
171 long me = PyThread_get_thread_ident();
172 if (me == -1)
173 return; /* Too bad */
174 if (import_lock_thread != me)
175 Py_FatalError("unlock_import: not holding the import lock");
176 import_lock_level--;
177 if (import_lock_level == 0) {
178 import_lock_thread = -1;
179 PyThread_release_lock(import_lock);
183 #else
185 #define lock_import()
186 #define unlock_import()
188 #endif
190 /* Helper for sys */
192 PyObject *
193 PyImport_GetModuleDict(void)
195 PyInterpreterState *interp = PyThreadState_Get()->interp;
196 if (interp->modules == NULL)
197 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
198 return interp->modules;
202 /* List of names to clear in sys */
203 static char* sys_deletes[] = {
204 "path", "argv", "ps1", "ps2", "exitfunc",
205 "exc_type", "exc_value", "exc_traceback",
206 "last_type", "last_value", "last_traceback",
207 NULL
210 static char* sys_files[] = {
211 "stdin", "__stdin__",
212 "stdout", "__stdout__",
213 "stderr", "__stderr__",
214 NULL
218 /* Un-initialize things, as good as we can */
220 void
221 PyImport_Cleanup(void)
223 int pos, ndone;
224 char *name;
225 PyObject *key, *value, *dict;
226 PyInterpreterState *interp = PyThreadState_Get()->interp;
227 PyObject *modules = interp->modules;
229 if (modules == NULL)
230 return; /* Already done */
232 /* Delete some special variables first. These are common
233 places where user values hide and people complain when their
234 destructors fail. Since the modules containing them are
235 deleted *last* of all, they would come too late in the normal
236 destruction order. Sigh. */
238 value = PyDict_GetItemString(modules, "__builtin__");
239 if (value != NULL && PyModule_Check(value)) {
240 dict = PyModule_GetDict(value);
241 if (Py_VerboseFlag)
242 PySys_WriteStderr("# clear __builtin__._\n");
243 PyDict_SetItemString(dict, "_", Py_None);
245 value = PyDict_GetItemString(modules, "sys");
246 if (value != NULL && PyModule_Check(value)) {
247 char **p;
248 PyObject *v;
249 dict = PyModule_GetDict(value);
250 for (p = sys_deletes; *p != NULL; p++) {
251 if (Py_VerboseFlag)
252 PySys_WriteStderr("# clear sys.%s\n", *p);
253 PyDict_SetItemString(dict, *p, Py_None);
255 for (p = sys_files; *p != NULL; p+=2) {
256 if (Py_VerboseFlag)
257 PySys_WriteStderr("# restore sys.%s\n", *p);
258 v = PyDict_GetItemString(dict, *(p+1));
259 if (v == NULL)
260 v = Py_None;
261 PyDict_SetItemString(dict, *p, v);
265 /* First, delete __main__ */
266 value = PyDict_GetItemString(modules, "__main__");
267 if (value != NULL && PyModule_Check(value)) {
268 if (Py_VerboseFlag)
269 PySys_WriteStderr("# cleanup __main__\n");
270 _PyModule_Clear(value);
271 PyDict_SetItemString(modules, "__main__", Py_None);
274 /* The special treatment of __builtin__ here is because even
275 when it's not referenced as a module, its dictionary is
276 referenced by almost every module's __builtins__. Since
277 deleting a module clears its dictionary (even if there are
278 references left to it), we need to delete the __builtin__
279 module last. Likewise, we don't delete sys until the very
280 end because it is implicitly referenced (e.g. by print).
282 Also note that we 'delete' modules by replacing their entry
283 in the modules dict with None, rather than really deleting
284 them; this avoids a rehash of the modules dictionary and
285 also marks them as "non existent" so they won't be
286 re-imported. */
288 /* Next, repeatedly delete modules with a reference count of
289 one (skipping __builtin__ and sys) and delete them */
290 do {
291 ndone = 0;
292 pos = 0;
293 while (PyDict_Next(modules, &pos, &key, &value)) {
294 if (value->ob_refcnt != 1)
295 continue;
296 if (PyString_Check(key) && PyModule_Check(value)) {
297 name = PyString_AS_STRING(key);
298 if (strcmp(name, "__builtin__") == 0)
299 continue;
300 if (strcmp(name, "sys") == 0)
301 continue;
302 if (Py_VerboseFlag)
303 PySys_WriteStderr(
304 "# cleanup[1] %s\n", name);
305 _PyModule_Clear(value);
306 PyDict_SetItem(modules, key, Py_None);
307 ndone++;
310 } while (ndone > 0);
312 /* Next, delete all modules (still skipping __builtin__ and sys) */
313 pos = 0;
314 while (PyDict_Next(modules, &pos, &key, &value)) {
315 if (PyString_Check(key) && PyModule_Check(value)) {
316 name = PyString_AS_STRING(key);
317 if (strcmp(name, "__builtin__") == 0)
318 continue;
319 if (strcmp(name, "sys") == 0)
320 continue;
321 if (Py_VerboseFlag)
322 PySys_WriteStderr("# cleanup[2] %s\n", name);
323 _PyModule_Clear(value);
324 PyDict_SetItem(modules, key, Py_None);
328 /* Next, delete sys and __builtin__ (in that order) */
329 value = PyDict_GetItemString(modules, "sys");
330 if (value != NULL && PyModule_Check(value)) {
331 if (Py_VerboseFlag)
332 PySys_WriteStderr("# cleanup sys\n");
333 _PyModule_Clear(value);
334 PyDict_SetItemString(modules, "sys", Py_None);
336 value = PyDict_GetItemString(modules, "__builtin__");
337 if (value != NULL && PyModule_Check(value)) {
338 if (Py_VerboseFlag)
339 PySys_WriteStderr("# cleanup __builtin__\n");
340 _PyModule_Clear(value);
341 PyDict_SetItemString(modules, "__builtin__", Py_None);
344 /* Finally, clear and delete the modules directory */
345 PyDict_Clear(modules);
346 interp->modules = NULL;
347 Py_DECREF(modules);
351 /* Helper for pythonrun.c -- return magic number */
353 long
354 PyImport_GetMagicNumber(void)
356 return pyc_magic;
360 /* Magic for extension modules (built-in as well as dynamically
361 loaded). To prevent initializing an extension module more than
362 once, we keep a static dictionary 'extensions' keyed by module name
363 (for built-in modules) or by filename (for dynamically loaded
364 modules), containing these modules. A copy od the module's
365 dictionary is stored by calling _PyImport_FixupExtension()
366 immediately after the module initialization function succeeds. A
367 copy can be retrieved from there by calling
368 _PyImport_FindExtension(). */
370 PyObject *
371 _PyImport_FixupExtension(char *name, char *filename)
373 PyObject *modules, *mod, *dict, *copy;
374 if (extensions == NULL) {
375 extensions = PyDict_New();
376 if (extensions == NULL)
377 return NULL;
379 modules = PyImport_GetModuleDict();
380 mod = PyDict_GetItemString(modules, name);
381 if (mod == NULL || !PyModule_Check(mod)) {
382 PyErr_Format(PyExc_SystemError,
383 "_PyImport_FixupExtension: module %.200s not loaded", name);
384 return NULL;
386 dict = PyModule_GetDict(mod);
387 if (dict == NULL)
388 return NULL;
389 copy = PyObject_CallMethod(dict, "copy", "");
390 if (copy == NULL)
391 return NULL;
392 PyDict_SetItemString(extensions, filename, copy);
393 Py_DECREF(copy);
394 return copy;
397 PyObject *
398 _PyImport_FindExtension(char *name, char *filename)
400 PyObject *dict, *mod, *mdict, *result;
401 if (extensions == NULL)
402 return NULL;
403 dict = PyDict_GetItemString(extensions, filename);
404 if (dict == NULL)
405 return NULL;
406 mod = PyImport_AddModule(name);
407 if (mod == NULL)
408 return NULL;
409 mdict = PyModule_GetDict(mod);
410 if (mdict == NULL)
411 return NULL;
412 result = PyObject_CallMethod(mdict, "update", "O", dict);
413 if (result == NULL)
414 return NULL;
415 Py_DECREF(result);
416 if (Py_VerboseFlag)
417 PySys_WriteStderr("import %s # previously loaded (%s)\n",
418 name, filename);
419 return mod;
423 /* Get the module object corresponding to a module name.
424 First check the modules dictionary if there's one there,
425 if not, create a new one and insert in in the modules dictionary.
426 Because the former action is most common, THIS DOES NOT RETURN A
427 'NEW' REFERENCE! */
429 PyObject *
430 PyImport_AddModule(char *name)
432 PyObject *modules = PyImport_GetModuleDict();
433 PyObject *m;
435 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
436 PyModule_Check(m))
437 return m;
438 m = PyModule_New(name);
439 if (m == NULL)
440 return NULL;
441 if (PyDict_SetItemString(modules, name, m) != 0) {
442 Py_DECREF(m);
443 return NULL;
445 Py_DECREF(m); /* Yes, it still exists, in modules! */
447 return m;
451 /* Execute a code object in a module and return the module object
452 WITH INCREMENTED REFERENCE COUNT */
454 PyObject *
455 PyImport_ExecCodeModule(char *name, PyObject *co)
457 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
460 PyObject *
461 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
463 PyObject *modules = PyImport_GetModuleDict();
464 PyObject *m, *d, *v;
466 m = PyImport_AddModule(name);
467 if (m == NULL)
468 return NULL;
469 d = PyModule_GetDict(m);
470 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
471 if (PyDict_SetItemString(d, "__builtins__",
472 PyEval_GetBuiltins()) != 0)
473 return NULL;
475 /* Remember the filename as the __file__ attribute */
476 v = NULL;
477 if (pathname != NULL) {
478 v = PyString_FromString(pathname);
479 if (v == NULL)
480 PyErr_Clear();
482 if (v == NULL) {
483 v = ((PyCodeObject *)co)->co_filename;
484 Py_INCREF(v);
486 if (PyDict_SetItemString(d, "__file__", v) != 0)
487 PyErr_Clear(); /* Not important enough to report */
488 Py_DECREF(v);
490 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
491 if (v == NULL)
492 return NULL;
493 Py_DECREF(v);
495 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
496 PyErr_Format(PyExc_ImportError,
497 "Loaded module %.200s not found in sys.modules",
498 name);
499 return NULL;
502 Py_INCREF(m);
504 return m;
508 /* Given a pathname for a Python source file, fill a buffer with the
509 pathname for the corresponding compiled file. Return the pathname
510 for the compiled file, or NULL if there's no space in the buffer.
511 Doesn't set an exception. */
513 static char *
514 make_compiled_pathname(char *pathname, char *buf, size_t buflen)
516 size_t len;
518 len = strlen(pathname);
519 if (len+2 > buflen)
520 return NULL;
521 strcpy(buf, pathname);
522 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
524 return buf;
528 /* Given a pathname for a Python source file, its time of last
529 modification, and a pathname for a compiled file, check whether the
530 compiled file represents the same version of the source. If so,
531 return a FILE pointer for the compiled file, positioned just after
532 the header; if not, return NULL.
533 Doesn't set an exception. */
535 static FILE *
536 check_compiled_module(char *pathname, long mtime, char *cpathname)
538 FILE *fp;
539 long magic;
540 long pyc_mtime;
542 fp = fopen(cpathname, "rb");
543 if (fp == NULL)
544 return NULL;
545 magic = PyMarshal_ReadLongFromFile(fp);
546 if (magic != pyc_magic) {
547 if (Py_VerboseFlag)
548 PySys_WriteStderr("# %s has bad magic\n", cpathname);
549 fclose(fp);
550 return NULL;
552 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
553 if (pyc_mtime != mtime) {
554 if (Py_VerboseFlag)
555 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
556 fclose(fp);
557 return NULL;
559 if (Py_VerboseFlag)
560 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
561 return fp;
565 /* Read a code object from a file and check it for validity */
567 static PyCodeObject *
568 read_compiled_module(char *cpathname, FILE *fp)
570 PyObject *co;
572 co = PyMarshal_ReadLastObjectFromFile(fp);
573 /* Ugly: rd_object() may return NULL with or without error */
574 if (co == NULL || !PyCode_Check(co)) {
575 if (!PyErr_Occurred())
576 PyErr_Format(PyExc_ImportError,
577 "Non-code object in %.200s", cpathname);
578 Py_XDECREF(co);
579 return NULL;
581 return (PyCodeObject *)co;
585 /* Load a module from a compiled file, execute it, and return its
586 module object WITH INCREMENTED REFERENCE COUNT */
588 static PyObject *
589 load_compiled_module(char *name, char *cpathname, FILE *fp)
591 long magic;
592 PyCodeObject *co;
593 PyObject *m;
595 magic = PyMarshal_ReadLongFromFile(fp);
596 if (magic != pyc_magic) {
597 PyErr_Format(PyExc_ImportError,
598 "Bad magic number in %.200s", cpathname);
599 return NULL;
601 (void) PyMarshal_ReadLongFromFile(fp);
602 co = read_compiled_module(cpathname, fp);
603 if (co == NULL)
604 return NULL;
605 if (Py_VerboseFlag)
606 PySys_WriteStderr("import %s # precompiled from %s\n",
607 name, cpathname);
608 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
609 Py_DECREF(co);
611 return m;
614 /* Parse a source file and return the corresponding code object */
616 static PyCodeObject *
617 parse_source_module(char *pathname, FILE *fp)
619 PyCodeObject *co;
620 node *n;
622 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
623 if (n == NULL)
624 return NULL;
625 co = PyNode_Compile(n, pathname);
626 PyNode_Free(n);
628 return co;
632 /* Helper to open a bytecode file for writing in exclusive mode */
634 static FILE *
635 open_exclusive(char *filename)
637 #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
638 /* Use O_EXCL to avoid a race condition when another process tries to
639 write the same file. When that happens, our open() call fails,
640 which is just fine (since it's only a cache).
641 XXX If the file exists and is writable but the directory is not
642 writable, the file will never be written. Oh well.
644 int fd;
645 (void) unlink(filename);
646 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
647 #ifdef O_BINARY
648 |O_BINARY /* necessary for Windows */
649 #endif
651 , 0666);
652 if (fd < 0)
653 return NULL;
654 return fdopen(fd, "wb");
655 #else
656 /* Best we can do -- on Windows this can't happen anyway */
657 return fopen(filename, "wb");
658 #endif
662 /* Write a compiled module to a file, placing the time of last
663 modification of its source into the header.
664 Errors are ignored, if a write error occurs an attempt is made to
665 remove the file. */
667 static void
668 write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
670 FILE *fp;
672 fp = open_exclusive(cpathname);
673 if (fp == NULL) {
674 if (Py_VerboseFlag)
675 PySys_WriteStderr(
676 "# can't create %s\n", cpathname);
677 return;
679 PyMarshal_WriteLongToFile(pyc_magic, fp);
680 /* First write a 0 for mtime */
681 PyMarshal_WriteLongToFile(0L, fp);
682 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
683 if (ferror(fp)) {
684 if (Py_VerboseFlag)
685 PySys_WriteStderr("# can't write %s\n", cpathname);
686 /* Don't keep partial file */
687 fclose(fp);
688 (void) unlink(cpathname);
689 return;
691 /* Now write the true mtime */
692 fseek(fp, 4L, 0);
693 PyMarshal_WriteLongToFile(mtime, fp);
694 fflush(fp);
695 fclose(fp);
696 if (Py_VerboseFlag)
697 PySys_WriteStderr("# wrote %s\n", cpathname);
698 #ifdef macintosh
699 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
700 #endif
704 /* Load a source module from a given file and return its module
705 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
706 byte-compiled file, use that instead. */
708 static PyObject *
709 load_source_module(char *name, char *pathname, FILE *fp)
711 time_t mtime;
712 FILE *fpc;
713 char buf[MAXPATHLEN+1];
714 char *cpathname;
715 PyCodeObject *co;
716 PyObject *m;
718 mtime = PyOS_GetLastModificationTime(pathname, fp);
719 if (mtime == (time_t)(-1))
720 return NULL;
721 #if SIZEOF_TIME_T > 4
722 /* Python's .pyc timestamp handling presumes that the timestamp fits
723 in 4 bytes. This will be fine until sometime in the year 2038,
724 when a 4-byte signed time_t will overflow.
726 if (mtime >> 32) {
727 PyErr_SetString(PyExc_OverflowError,
728 "modification time overflows a 4 byte field");
729 return NULL;
731 #endif
732 cpathname = make_compiled_pathname(pathname, buf,
733 (size_t)MAXPATHLEN + 1);
734 if (cpathname != NULL &&
735 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
736 co = read_compiled_module(cpathname, fpc);
737 fclose(fpc);
738 if (co == NULL)
739 return NULL;
740 if (Py_VerboseFlag)
741 PySys_WriteStderr("import %s # precompiled from %s\n",
742 name, cpathname);
743 pathname = cpathname;
745 else {
746 co = parse_source_module(pathname, fp);
747 if (co == NULL)
748 return NULL;
749 if (Py_VerboseFlag)
750 PySys_WriteStderr("import %s # from %s\n",
751 name, pathname);
752 write_compiled_module(co, cpathname, mtime);
754 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
755 Py_DECREF(co);
757 return m;
761 /* Forward */
762 static PyObject *load_module(char *, FILE *, char *, int);
763 static struct filedescr *find_module(char *, PyObject *,
764 char *, size_t, FILE **);
765 static struct _frozen *find_frozen(char *name);
767 /* Load a package and return its module object WITH INCREMENTED
768 REFERENCE COUNT */
770 static PyObject *
771 load_package(char *name, char *pathname)
773 PyObject *m, *d, *file, *path;
774 int err;
775 char buf[MAXPATHLEN+1];
776 FILE *fp = NULL;
777 struct filedescr *fdp;
779 m = PyImport_AddModule(name);
780 if (m == NULL)
781 return NULL;
782 if (Py_VerboseFlag)
783 PySys_WriteStderr("import %s # directory %s\n",
784 name, pathname);
785 d = PyModule_GetDict(m);
786 file = PyString_FromString(pathname);
787 if (file == NULL)
788 return NULL;
789 path = Py_BuildValue("[O]", file);
790 if (path == NULL) {
791 Py_DECREF(file);
792 return NULL;
794 err = PyDict_SetItemString(d, "__file__", file);
795 if (err == 0)
796 err = PyDict_SetItemString(d, "__path__", path);
797 if (err != 0) {
798 m = NULL;
799 goto cleanup;
801 buf[0] = '\0';
802 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
803 if (fdp == NULL) {
804 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
805 PyErr_Clear();
807 else
808 m = NULL;
809 goto cleanup;
811 m = load_module(name, fp, buf, fdp->type);
812 if (fp != NULL)
813 fclose(fp);
814 cleanup:
815 Py_XDECREF(path);
816 Py_XDECREF(file);
817 return m;
821 /* Helper to test for built-in module */
823 static int
824 is_builtin(char *name)
826 int i;
827 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
828 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
829 if (PyImport_Inittab[i].initfunc == NULL)
830 return -1;
831 else
832 return 1;
835 return 0;
839 /* Search the path (default sys.path) for a module. Return the
840 corresponding filedescr struct, and (via return arguments) the
841 pathname and an open file. Return NULL if the module is not found. */
843 #ifdef MS_COREDLL
844 extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
845 char *, int);
846 #endif
848 static int case_ok(char *, int, int, char *);
849 static int find_init_module(char *); /* Forward */
851 static struct filedescr *
852 find_module(char *realname, PyObject *path, char *buf, size_t buflen,
853 FILE **p_fp)
855 int i, npath;
856 size_t len, namelen;
857 struct _frozen *f;
858 struct filedescr *fdp = NULL;
859 FILE *fp = NULL;
860 #ifndef RISCOS
861 struct stat statbuf;
862 #endif
863 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
864 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
865 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
866 char name[MAXPATHLEN+1];
868 if (strlen(realname) > MAXPATHLEN) {
869 PyErr_SetString(PyExc_OverflowError, "module name is too long");
870 return NULL;
872 strcpy(name, realname);
874 if (path != NULL && PyString_Check(path)) {
875 /* Submodule of "frozen" package:
876 Set name to the fullname, path to NULL
877 and continue as "usual" */
878 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
879 PyErr_SetString(PyExc_ImportError,
880 "full frozen module name too long");
881 return NULL;
883 strcpy(buf, PyString_AsString(path));
884 strcat(buf, ".");
885 strcat(buf, name);
886 strcpy(name, buf);
887 path = NULL;
889 if (path == NULL) {
890 if (is_builtin(name)) {
891 strcpy(buf, name);
892 return &fd_builtin;
894 if ((f = find_frozen(name)) != NULL) {
895 strcpy(buf, name);
896 return &fd_frozen;
899 #ifdef MS_COREDLL
900 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
901 if (fp != NULL) {
902 *p_fp = fp;
903 return fdp;
905 #endif
906 path = PySys_GetObject("path");
908 if (path == NULL || !PyList_Check(path)) {
909 PyErr_SetString(PyExc_ImportError,
910 "sys.path must be a list of directory names");
911 return NULL;
913 npath = PyList_Size(path);
914 namelen = strlen(name);
915 for (i = 0; i < npath; i++) {
916 PyObject *v = PyList_GetItem(path, i);
917 if (!PyString_Check(v))
918 continue;
919 len = PyString_Size(v);
920 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
921 continue; /* Too long */
922 strcpy(buf, PyString_AsString(v));
923 if (strlen(buf) != len)
924 continue; /* v contains '\0' */
925 #ifdef macintosh
926 #ifdef INTERN_STRINGS
928 ** Speedup: each sys.path item is interned, and
929 ** FindResourceModule remembers which items refer to
930 ** folders (so we don't have to bother trying to look
931 ** into them for resources).
933 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
934 v = PyList_GET_ITEM(path, i);
935 #endif
936 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
937 static struct filedescr resfiledescr =
938 {"", "", PY_RESOURCE};
940 return &resfiledescr;
942 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
943 static struct filedescr resfiledescr =
944 {"", "", PY_CODERESOURCE};
946 return &resfiledescr;
948 #endif
949 if (len > 0 && buf[len-1] != SEP
950 #ifdef ALTSEP
951 && buf[len-1] != ALTSEP
952 #endif
954 buf[len++] = SEP;
955 strcpy(buf+len, name);
956 len += namelen;
958 /* Check for package import (buf holds a directory name,
959 and there's an __init__ module in that directory */
960 #ifdef HAVE_STAT
961 if (stat(buf, &statbuf) == 0 && /* it exists */
962 S_ISDIR(statbuf.st_mode) && /* it's a directory */
963 find_init_module(buf) && /* it has __init__.py */
964 case_ok(buf, len, namelen, name)) /* and case matches */
965 return &fd_package;
966 #else
967 /* XXX How are you going to test for directories? */
968 #ifdef RISCOS
970 static struct filedescr fd = {"", "", PKG_DIRECTORY};
971 if (isdir(buf)) {
972 if (find_init_module(buf))
973 return &fd;
976 #endif
977 #endif
978 #ifdef macintosh
979 fdp = PyMac_FindModuleExtension(buf, &len, name);
980 if (fdp) {
981 #else
982 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
983 strcpy(buf+len, fdp->suffix);
984 if (Py_VerboseFlag > 1)
985 PySys_WriteStderr("# trying %s\n", buf);
986 #endif /* !macintosh */
987 fp = fopen(buf, fdp->mode);
988 if (fp != NULL) {
989 if (case_ok(buf, len, namelen, name))
990 break;
991 else { /* continue search */
992 fclose(fp);
993 fp = NULL;
997 if (fp != NULL)
998 break;
1000 if (fp == NULL) {
1001 PyErr_Format(PyExc_ImportError,
1002 "No module named %.200s", name);
1003 return NULL;
1005 *p_fp = fp;
1006 return fdp;
1009 /* case_ok(char* buf, int len, int namelen, char* name)
1010 * The arguments here are tricky, best shown by example:
1011 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1012 * ^ ^ ^ ^
1013 * |--------------------- buf ---------------------|
1014 * |------------------- len ------------------|
1015 * |------ name -------|
1016 * |----- namelen -----|
1017 * buf is the full path, but len only counts up to (& exclusive of) the
1018 * extension. name is the module name, also exclusive of extension.
1020 * We've already done a successful stat() or fopen() on buf, so know that
1021 * there's some match, possibly case-insensitive.
1023 * case_ok() is to return 1 if there's a case-sensitive match for
1024 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1025 * exists.
1027 * case_ok() is used to implement case-sensitive import semantics even
1028 * on platforms with case-insensitive filesystems. It's trivial to implement
1029 * for case-sensitive filesystems. It's pretty much a cross-platform
1030 * nightmare for systems with case-insensitive filesystems.
1033 /* First we may need a pile of platform-specific header files; the sequence
1034 * of #if's here should match the sequence in the body of case_ok().
1036 #if defined(MS_WIN32) || defined(__CYGWIN__)
1037 #include <windows.h>
1038 #ifdef __CYGWIN__
1039 #include <sys/cygwin.h>
1040 #endif
1042 #elif defined(DJGPP)
1043 #include <dir.h>
1045 #elif defined(macintosh)
1046 #include <TextUtils.h>
1047 #ifdef USE_GUSI1
1048 #include "TFileSpec.h" /* for Path2FSSpec() */
1049 #endif
1051 #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
1052 #include <sys/types.h>
1053 #include <dirent.h>
1055 #endif
1057 static int
1058 case_ok(char *buf, int len, int namelen, char *name)
1060 /* Pick a platform-specific implementation; the sequence of #if's here should
1061 * match the sequence just above.
1064 /* MS_WIN32 || __CYGWIN__ */
1065 #if defined(MS_WIN32) || defined(__CYGWIN__)
1066 WIN32_FIND_DATA data;
1067 HANDLE h;
1068 #ifdef __CYGWIN__
1069 char tempbuf[MAX_PATH];
1070 #endif
1072 if (getenv("PYTHONCASEOK") != NULL)
1073 return 1;
1075 #ifdef __CYGWIN__
1076 cygwin32_conv_to_win32_path(buf, tempbuf);
1077 h = FindFirstFile(tempbuf, &data);
1078 #else
1079 h = FindFirstFile(buf, &data);
1080 #endif
1081 if (h == INVALID_HANDLE_VALUE) {
1082 PyErr_Format(PyExc_NameError,
1083 "Can't find file for module %.100s\n(filename %.300s)",
1084 name, buf);
1085 return 0;
1087 FindClose(h);
1088 return strncmp(data.cFileName, name, namelen) == 0;
1090 /* DJGPP */
1091 #elif defined(DJGPP)
1092 struct ffblk ffblk;
1093 int done;
1095 if (getenv("PYTHONCASEOK") != NULL)
1096 return 1;
1098 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1099 if (done) {
1100 PyErr_Format(PyExc_NameError,
1101 "Can't find file for module %.100s\n(filename %.300s)",
1102 name, buf);
1103 return 0;
1105 return strncmp(ffblk.ff_name, name, namelen) == 0;
1107 /* macintosh */
1108 #elif defined(macintosh)
1109 FSSpec fss;
1110 OSErr err;
1112 if (getenv("PYTHONCASEOK") != NULL)
1113 return 1;
1115 #ifndef USE_GUSI1
1116 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1117 #else
1118 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1119 the way, which is fine for all directories, but here we need
1120 the original name of the alias file (say, Dlg.ppc.slb, not
1121 toolboxmodules.ppc.slb). */
1122 char *colon;
1123 err = Path2FSSpec(buf, &fss);
1124 if (err == noErr) {
1125 colon = strrchr(buf, ':'); /* find filename */
1126 if (colon != NULL)
1127 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1128 Pstring(colon+1), &fss);
1129 else
1130 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1131 fss.name, &fss);
1133 #endif
1134 if (err) {
1135 PyErr_Format(PyExc_NameError,
1136 "Can't find file for module %.100s\n(filename %.300s)",
1137 name, buf);
1138 return 0;
1140 return fss.name[0] >= namelen &&
1141 strncmp(name, (char *)fss.name+1, namelen) == 0;
1143 /* new-fangled macintosh (macosx) */
1144 #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
1145 DIR *dirp;
1146 struct dirent *dp;
1147 char dirname[MAXPATHLEN + 1];
1148 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
1150 if (getenv("PYTHONCASEOK") != NULL)
1151 return 1;
1153 /* Copy the dir component into dirname; substitute "." if empty */
1154 if (dirlen <= 0) {
1155 dirname[0] = '.';
1156 dirname[1] = '\0';
1158 else {
1159 assert(dirlen <= MAXPATHLEN);
1160 memcpy(dirname, buf, dirlen);
1161 dirname[dirlen] = '\0';
1163 /* Open the directory and search the entries for an exact match. */
1164 dirp = opendir(dirname);
1165 if (dirp) {
1166 char *nameWithExt = buf + len - namelen;
1167 while ((dp = readdir(dirp)) != NULL) {
1168 const int thislen =
1169 #ifdef _DIRENT_HAVE_D_NAMELEN
1170 dp->d_namlen;
1171 #else
1172 strlen(dp->d_name);
1173 #endif
1174 if (thislen >= namelen &&
1175 strcmp(dp->d_name, nameWithExt) == 0) {
1176 (void)closedir(dirp);
1177 return 1; /* Found */
1180 (void)closedir(dirp);
1182 return 0 ; /* Not found */
1184 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1185 #else
1186 return 1;
1188 #endif
1192 #ifdef HAVE_STAT
1193 /* Helper to look for __init__.py or __init__.py[co] in potential package */
1194 static int
1195 find_init_module(char *buf)
1197 const size_t save_len = strlen(buf);
1198 size_t i = save_len;
1199 char *pname; /* pointer to start of __init__ */
1200 struct stat statbuf;
1202 /* For calling case_ok(buf, len, namelen, name):
1203 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1204 * ^ ^ ^ ^
1205 * |--------------------- buf ---------------------|
1206 * |------------------- len ------------------|
1207 * |------ name -------|
1208 * |----- namelen -----|
1210 if (save_len + 13 >= MAXPATHLEN)
1211 return 0;
1212 buf[i++] = SEP;
1213 pname = buf + i;
1214 strcpy(pname, "__init__.py");
1215 if (stat(buf, &statbuf) == 0) {
1216 if (case_ok(buf,
1217 save_len + 9, /* len("/__init__") */
1218 8, /* len("__init__") */
1219 pname)) {
1220 buf[save_len] = '\0';
1221 return 1;
1224 i += strlen(pname);
1225 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
1226 if (stat(buf, &statbuf) == 0) {
1227 if (case_ok(buf,
1228 save_len + 9, /* len("/__init__") */
1229 8, /* len("__init__") */
1230 pname)) {
1231 buf[save_len] = '\0';
1232 return 1;
1235 buf[save_len] = '\0';
1236 return 0;
1239 #else
1241 #ifdef RISCOS
1242 static int
1243 find_init_module(buf)
1244 char *buf;
1246 int save_len = strlen(buf);
1247 int i = save_len;
1249 if (save_len + 13 >= MAXPATHLEN)
1250 return 0;
1251 buf[i++] = SEP;
1252 strcpy(buf+i, "__init__/py");
1253 if (isfile(buf)) {
1254 buf[save_len] = '\0';
1255 return 1;
1258 if (Py_OptimizeFlag)
1259 strcpy(buf+i, "o");
1260 else
1261 strcpy(buf+i, "c");
1262 if (isfile(buf)) {
1263 buf[save_len] = '\0';
1264 return 1;
1266 buf[save_len] = '\0';
1267 return 0;
1269 #endif /*RISCOS*/
1271 #endif /* HAVE_STAT */
1274 static int init_builtin(char *); /* Forward */
1276 /* Load an external module using the default search path and return
1277 its module object WITH INCREMENTED REFERENCE COUNT */
1279 static PyObject *
1280 load_module(char *name, FILE *fp, char *buf, int type)
1282 PyObject *modules;
1283 PyObject *m;
1284 int err;
1286 /* First check that there's an open file (if we need one) */
1287 switch (type) {
1288 case PY_SOURCE:
1289 case PY_COMPILED:
1290 if (fp == NULL) {
1291 PyErr_Format(PyExc_ValueError,
1292 "file object required for import (type code %d)",
1293 type);
1294 return NULL;
1298 switch (type) {
1300 case PY_SOURCE:
1301 m = load_source_module(name, buf, fp);
1302 break;
1304 case PY_COMPILED:
1305 m = load_compiled_module(name, buf, fp);
1306 break;
1308 #ifdef HAVE_DYNAMIC_LOADING
1309 case C_EXTENSION:
1310 m = _PyImport_LoadDynamicModule(name, buf, fp);
1311 break;
1312 #endif
1314 #ifdef macintosh
1315 case PY_RESOURCE:
1316 m = PyMac_LoadResourceModule(name, buf);
1317 break;
1318 case PY_CODERESOURCE:
1319 m = PyMac_LoadCodeResourceModule(name, buf);
1320 break;
1321 #endif
1323 case PKG_DIRECTORY:
1324 m = load_package(name, buf);
1325 break;
1327 case C_BUILTIN:
1328 case PY_FROZEN:
1329 if (buf != NULL && buf[0] != '\0')
1330 name = buf;
1331 if (type == C_BUILTIN)
1332 err = init_builtin(name);
1333 else
1334 err = PyImport_ImportFrozenModule(name);
1335 if (err < 0)
1336 return NULL;
1337 if (err == 0) {
1338 PyErr_Format(PyExc_ImportError,
1339 "Purported %s module %.200s not found",
1340 type == C_BUILTIN ?
1341 "builtin" : "frozen",
1342 name);
1343 return NULL;
1345 modules = PyImport_GetModuleDict();
1346 m = PyDict_GetItemString(modules, name);
1347 if (m == NULL) {
1348 PyErr_Format(
1349 PyExc_ImportError,
1350 "%s module %.200s not properly initialized",
1351 type == C_BUILTIN ?
1352 "builtin" : "frozen",
1353 name);
1354 return NULL;
1356 Py_INCREF(m);
1357 break;
1359 default:
1360 PyErr_Format(PyExc_ImportError,
1361 "Don't know how to import %.200s (type code %d)",
1362 name, type);
1363 m = NULL;
1367 return m;
1371 /* Initialize a built-in module.
1372 Return 1 for succes, 0 if the module is not found, and -1 with
1373 an exception set if the initialization failed. */
1375 static int
1376 init_builtin(char *name)
1378 struct _inittab *p;
1379 PyObject *mod;
1381 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1382 return 1;
1384 for (p = PyImport_Inittab; p->name != NULL; p++) {
1385 if (strcmp(name, p->name) == 0) {
1386 if (p->initfunc == NULL) {
1387 PyErr_Format(PyExc_ImportError,
1388 "Cannot re-init internal module %.200s",
1389 name);
1390 return -1;
1392 if (Py_VerboseFlag)
1393 PySys_WriteStderr("import %s # builtin\n", name);
1394 (*p->initfunc)();
1395 if (PyErr_Occurred())
1396 return -1;
1397 if (_PyImport_FixupExtension(name, name) == NULL)
1398 return -1;
1399 return 1;
1402 return 0;
1406 /* Frozen modules */
1408 static struct _frozen *
1409 find_frozen(char *name)
1411 struct _frozen *p;
1413 for (p = PyImport_FrozenModules; ; p++) {
1414 if (p->name == NULL)
1415 return NULL;
1416 if (strcmp(p->name, name) == 0)
1417 break;
1419 return p;
1422 static PyObject *
1423 get_frozen_object(char *name)
1425 struct _frozen *p = find_frozen(name);
1426 int size;
1428 if (p == NULL) {
1429 PyErr_Format(PyExc_ImportError,
1430 "No such frozen object named %.200s",
1431 name);
1432 return NULL;
1434 size = p->size;
1435 if (size < 0)
1436 size = -size;
1437 return PyMarshal_ReadObjectFromString((char *)p->code, size);
1440 /* Initialize a frozen module.
1441 Return 1 for succes, 0 if the module is not found, and -1 with
1442 an exception set if the initialization failed.
1443 This function is also used from frozenmain.c */
1446 PyImport_ImportFrozenModule(char *name)
1448 struct _frozen *p = find_frozen(name);
1449 PyObject *co;
1450 PyObject *m;
1451 int ispackage;
1452 int size;
1454 if (p == NULL)
1455 return 0;
1456 size = p->size;
1457 ispackage = (size < 0);
1458 if (ispackage)
1459 size = -size;
1460 if (Py_VerboseFlag)
1461 PySys_WriteStderr("import %s # frozen%s\n",
1462 name, ispackage ? " package" : "");
1463 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1464 if (co == NULL)
1465 return -1;
1466 if (!PyCode_Check(co)) {
1467 Py_DECREF(co);
1468 PyErr_Format(PyExc_TypeError,
1469 "frozen object %.200s is not a code object",
1470 name);
1471 return -1;
1473 if (ispackage) {
1474 /* Set __path__ to the package name */
1475 PyObject *d, *s;
1476 int err;
1477 m = PyImport_AddModule(name);
1478 if (m == NULL)
1479 return -1;
1480 d = PyModule_GetDict(m);
1481 s = PyString_InternFromString(name);
1482 if (s == NULL)
1483 return -1;
1484 err = PyDict_SetItemString(d, "__path__", s);
1485 Py_DECREF(s);
1486 if (err != 0)
1487 return err;
1489 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
1490 Py_DECREF(co);
1491 if (m == NULL)
1492 return -1;
1493 Py_DECREF(m);
1494 return 1;
1498 /* Import a module, either built-in, frozen, or external, and return
1499 its module object WITH INCREMENTED REFERENCE COUNT */
1501 PyObject *
1502 PyImport_ImportModule(char *name)
1504 PyObject *pname;
1505 PyObject *result;
1507 pname = PyString_FromString(name);
1508 result = PyImport_Import(pname);
1509 Py_DECREF(pname);
1510 return result;
1513 /* Forward declarations for helper routines */
1514 static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1515 static PyObject *load_next(PyObject *mod, PyObject *altmod,
1516 char **p_name, char *buf, int *p_buflen);
1517 static int mark_miss(char *name);
1518 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1519 char *buf, int buflen, int recursive);
1520 static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
1522 /* The Magnum Opus of dotted-name import :-) */
1524 static PyObject *
1525 import_module_ex(char *name, PyObject *globals, PyObject *locals,
1526 PyObject *fromlist)
1528 char buf[MAXPATHLEN+1];
1529 int buflen = 0;
1530 PyObject *parent, *head, *next, *tail;
1532 parent = get_parent(globals, buf, &buflen);
1533 if (parent == NULL)
1534 return NULL;
1536 head = load_next(parent, Py_None, &name, buf, &buflen);
1537 if (head == NULL)
1538 return NULL;
1540 tail = head;
1541 Py_INCREF(tail);
1542 while (name) {
1543 next = load_next(tail, tail, &name, buf, &buflen);
1544 Py_DECREF(tail);
1545 if (next == NULL) {
1546 Py_DECREF(head);
1547 return NULL;
1549 tail = next;
1552 if (fromlist != NULL) {
1553 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1554 fromlist = NULL;
1557 if (fromlist == NULL) {
1558 Py_DECREF(tail);
1559 return head;
1562 Py_DECREF(head);
1563 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
1564 Py_DECREF(tail);
1565 return NULL;
1568 return tail;
1571 PyObject *
1572 PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1573 PyObject *fromlist)
1575 PyObject *result;
1576 lock_import();
1577 result = import_module_ex(name, globals, locals, fromlist);
1578 unlock_import();
1579 return result;
1582 static PyObject *
1583 get_parent(PyObject *globals, char *buf, int *p_buflen)
1585 static PyObject *namestr = NULL;
1586 static PyObject *pathstr = NULL;
1587 PyObject *modname, *modpath, *modules, *parent;
1589 if (globals == NULL || !PyDict_Check(globals))
1590 return Py_None;
1592 if (namestr == NULL) {
1593 namestr = PyString_InternFromString("__name__");
1594 if (namestr == NULL)
1595 return NULL;
1597 if (pathstr == NULL) {
1598 pathstr = PyString_InternFromString("__path__");
1599 if (pathstr == NULL)
1600 return NULL;
1603 *buf = '\0';
1604 *p_buflen = 0;
1605 modname = PyDict_GetItem(globals, namestr);
1606 if (modname == NULL || !PyString_Check(modname))
1607 return Py_None;
1609 modpath = PyDict_GetItem(globals, pathstr);
1610 if (modpath != NULL) {
1611 int len = PyString_GET_SIZE(modname);
1612 if (len > MAXPATHLEN) {
1613 PyErr_SetString(PyExc_ValueError,
1614 "Module name too long");
1615 return NULL;
1617 strcpy(buf, PyString_AS_STRING(modname));
1618 *p_buflen = len;
1620 else {
1621 char *start = PyString_AS_STRING(modname);
1622 char *lastdot = strrchr(start, '.');
1623 size_t len;
1624 if (lastdot == NULL)
1625 return Py_None;
1626 len = lastdot - start;
1627 if (len >= MAXPATHLEN) {
1628 PyErr_SetString(PyExc_ValueError,
1629 "Module name too long");
1630 return NULL;
1632 strncpy(buf, start, len);
1633 buf[len] = '\0';
1634 *p_buflen = len;
1637 modules = PyImport_GetModuleDict();
1638 parent = PyDict_GetItemString(modules, buf);
1639 if (parent == NULL)
1640 parent = Py_None;
1641 return parent;
1642 /* We expect, but can't guarantee, if parent != None, that:
1643 - parent.__name__ == buf
1644 - parent.__dict__ is globals
1645 If this is violated... Who cares? */
1648 /* altmod is either None or same as mod */
1649 static PyObject *
1650 load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1651 int *p_buflen)
1653 char *name = *p_name;
1654 char *dot = strchr(name, '.');
1655 size_t len;
1656 char *p;
1657 PyObject *result;
1659 if (dot == NULL) {
1660 *p_name = NULL;
1661 len = strlen(name);
1663 else {
1664 *p_name = dot+1;
1665 len = dot-name;
1667 if (len == 0) {
1668 PyErr_SetString(PyExc_ValueError,
1669 "Empty module name");
1670 return NULL;
1673 p = buf + *p_buflen;
1674 if (p != buf)
1675 *p++ = '.';
1676 if (p+len-buf >= MAXPATHLEN) {
1677 PyErr_SetString(PyExc_ValueError,
1678 "Module name too long");
1679 return NULL;
1681 strncpy(p, name, len);
1682 p[len] = '\0';
1683 *p_buflen = p+len-buf;
1685 result = import_submodule(mod, p, buf);
1686 if (result == Py_None && altmod != mod) {
1687 Py_DECREF(result);
1688 /* Here, altmod must be None and mod must not be None */
1689 result = import_submodule(altmod, p, p);
1690 if (result != NULL && result != Py_None) {
1691 if (mark_miss(buf) != 0) {
1692 Py_DECREF(result);
1693 return NULL;
1695 strncpy(buf, name, len);
1696 buf[len] = '\0';
1697 *p_buflen = len;
1700 if (result == NULL)
1701 return NULL;
1703 if (result == Py_None) {
1704 Py_DECREF(result);
1705 PyErr_Format(PyExc_ImportError,
1706 "No module named %.200s", name);
1707 return NULL;
1710 return result;
1713 static int
1714 mark_miss(char *name)
1716 PyObject *modules = PyImport_GetModuleDict();
1717 return PyDict_SetItemString(modules, name, Py_None);
1720 static int
1721 ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1722 int recursive)
1724 int i;
1726 if (!PyObject_HasAttrString(mod, "__path__"))
1727 return 1;
1729 for (i = 0; ; i++) {
1730 PyObject *item = PySequence_GetItem(fromlist, i);
1731 int hasit;
1732 if (item == NULL) {
1733 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1734 PyErr_Clear();
1735 return 1;
1737 return 0;
1739 if (!PyString_Check(item)) {
1740 PyErr_SetString(PyExc_TypeError,
1741 "Item in ``from list'' not a string");
1742 Py_DECREF(item);
1743 return 0;
1745 if (PyString_AS_STRING(item)[0] == '*') {
1746 PyObject *all;
1747 Py_DECREF(item);
1748 /* See if the package defines __all__ */
1749 if (recursive)
1750 continue; /* Avoid endless recursion */
1751 all = PyObject_GetAttrString(mod, "__all__");
1752 if (all == NULL)
1753 PyErr_Clear();
1754 else {
1755 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1756 return 0;
1757 Py_DECREF(all);
1759 continue;
1761 hasit = PyObject_HasAttr(mod, item);
1762 if (!hasit) {
1763 char *subname = PyString_AS_STRING(item);
1764 PyObject *submod;
1765 char *p;
1766 if (buflen + strlen(subname) >= MAXPATHLEN) {
1767 PyErr_SetString(PyExc_ValueError,
1768 "Module name too long");
1769 Py_DECREF(item);
1770 return 0;
1772 p = buf + buflen;
1773 *p++ = '.';
1774 strcpy(p, subname);
1775 submod = import_submodule(mod, subname, buf);
1776 Py_XDECREF(submod);
1777 if (submod == NULL) {
1778 Py_DECREF(item);
1779 return 0;
1782 Py_DECREF(item);
1785 /* NOTREACHED */
1788 static PyObject *
1789 import_submodule(PyObject *mod, char *subname, char *fullname)
1791 PyObject *modules = PyImport_GetModuleDict();
1792 PyObject *m;
1794 /* Require:
1795 if mod == None: subname == fullname
1796 else: mod.__name__ + "." + subname == fullname
1799 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
1800 Py_INCREF(m);
1802 else {
1803 PyObject *path;
1804 char buf[MAXPATHLEN+1];
1805 struct filedescr *fdp;
1806 FILE *fp = NULL;
1808 if (mod == Py_None)
1809 path = NULL;
1810 else {
1811 path = PyObject_GetAttrString(mod, "__path__");
1812 if (path == NULL) {
1813 PyErr_Clear();
1814 Py_INCREF(Py_None);
1815 return Py_None;
1819 buf[0] = '\0';
1820 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1821 Py_XDECREF(path);
1822 if (fdp == NULL) {
1823 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1824 return NULL;
1825 PyErr_Clear();
1826 Py_INCREF(Py_None);
1827 return Py_None;
1829 m = load_module(fullname, fp, buf, fdp->type);
1830 if (fp)
1831 fclose(fp);
1832 if (m != NULL && mod != Py_None) {
1833 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1834 Py_DECREF(m);
1835 m = NULL;
1840 return m;
1844 /* Re-import a module of any kind and return its module object, WITH
1845 INCREMENTED REFERENCE COUNT */
1847 PyObject *
1848 PyImport_ReloadModule(PyObject *m)
1850 PyObject *modules = PyImport_GetModuleDict();
1851 PyObject *path = NULL;
1852 char *name, *subname;
1853 char buf[MAXPATHLEN+1];
1854 struct filedescr *fdp;
1855 FILE *fp = NULL;
1857 if (m == NULL || !PyModule_Check(m)) {
1858 PyErr_SetString(PyExc_TypeError,
1859 "reload() argument must be module");
1860 return NULL;
1862 name = PyModule_GetName(m);
1863 if (name == NULL)
1864 return NULL;
1865 if (m != PyDict_GetItemString(modules, name)) {
1866 PyErr_Format(PyExc_ImportError,
1867 "reload(): module %.200s not in sys.modules",
1868 name);
1869 return NULL;
1871 subname = strrchr(name, '.');
1872 if (subname == NULL)
1873 subname = name;
1874 else {
1875 PyObject *parentname, *parent;
1876 parentname = PyString_FromStringAndSize(name, (subname-name));
1877 if (parentname == NULL)
1878 return NULL;
1879 parent = PyDict_GetItem(modules, parentname);
1880 Py_DECREF(parentname);
1881 if (parent == NULL) {
1882 PyErr_Format(PyExc_ImportError,
1883 "reload(): parent %.200s not in sys.modules",
1884 name);
1885 return NULL;
1887 subname++;
1888 path = PyObject_GetAttrString(parent, "__path__");
1889 if (path == NULL)
1890 PyErr_Clear();
1892 buf[0] = '\0';
1893 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1894 Py_XDECREF(path);
1895 if (fdp == NULL)
1896 return NULL;
1897 m = load_module(name, fp, buf, fdp->type);
1898 if (fp)
1899 fclose(fp);
1900 return m;
1904 /* Higher-level import emulator which emulates the "import" statement
1905 more accurately -- it invokes the __import__() function from the
1906 builtins of the current globals. This means that the import is
1907 done using whatever import hooks are installed in the current
1908 environment, e.g. by "rexec".
1909 A dummy list ["__doc__"] is passed as the 4th argument so that
1910 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1911 will return <module "gencache"> instead of <module "win32com">. */
1913 PyObject *
1914 PyImport_Import(PyObject *module_name)
1916 static PyObject *silly_list = NULL;
1917 static PyObject *builtins_str = NULL;
1918 static PyObject *import_str = NULL;
1919 PyObject *globals = NULL;
1920 PyObject *import = NULL;
1921 PyObject *builtins = NULL;
1922 PyObject *r = NULL;
1924 /* Initialize constant string objects */
1925 if (silly_list == NULL) {
1926 import_str = PyString_InternFromString("__import__");
1927 if (import_str == NULL)
1928 return NULL;
1929 builtins_str = PyString_InternFromString("__builtins__");
1930 if (builtins_str == NULL)
1931 return NULL;
1932 silly_list = Py_BuildValue("[s]", "__doc__");
1933 if (silly_list == NULL)
1934 return NULL;
1937 /* Get the builtins from current globals */
1938 globals = PyEval_GetGlobals();
1939 if (globals != NULL) {
1940 Py_INCREF(globals);
1941 builtins = PyObject_GetItem(globals, builtins_str);
1942 if (builtins == NULL)
1943 goto err;
1945 else {
1946 /* No globals -- use standard builtins, and fake globals */
1947 PyErr_Clear();
1949 builtins = PyImport_ImportModuleEx("__builtin__",
1950 NULL, NULL, NULL);
1951 if (builtins == NULL)
1952 return NULL;
1953 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1954 if (globals == NULL)
1955 goto err;
1958 /* Get the __import__ function from the builtins */
1959 if (PyDict_Check(builtins))
1960 import = PyObject_GetItem(builtins, import_str);
1961 else
1962 import = PyObject_GetAttr(builtins, import_str);
1963 if (import == NULL)
1964 goto err;
1966 /* Call the _import__ function with the proper argument list */
1967 r = PyObject_CallFunction(import, "OOOO",
1968 module_name, globals, globals, silly_list);
1970 err:
1971 Py_XDECREF(globals);
1972 Py_XDECREF(builtins);
1973 Py_XDECREF(import);
1975 return r;
1979 /* Module 'imp' provides Python access to the primitives used for
1980 importing modules.
1983 static PyObject *
1984 imp_get_magic(PyObject *self, PyObject *args)
1986 char buf[4];
1988 if (!PyArg_ParseTuple(args, ":get_magic"))
1989 return NULL;
1990 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1991 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1992 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1993 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
1995 return PyString_FromStringAndSize(buf, 4);
1998 static PyObject *
1999 imp_get_suffixes(PyObject *self, PyObject *args)
2001 PyObject *list;
2002 struct filedescr *fdp;
2004 if (!PyArg_ParseTuple(args, ":get_suffixes"))
2005 return NULL;
2006 list = PyList_New(0);
2007 if (list == NULL)
2008 return NULL;
2009 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2010 PyObject *item = Py_BuildValue("ssi",
2011 fdp->suffix, fdp->mode, fdp->type);
2012 if (item == NULL) {
2013 Py_DECREF(list);
2014 return NULL;
2016 if (PyList_Append(list, item) < 0) {
2017 Py_DECREF(list);
2018 Py_DECREF(item);
2019 return NULL;
2021 Py_DECREF(item);
2023 return list;
2026 static PyObject *
2027 call_find_module(char *name, PyObject *path)
2029 extern int fclose(FILE *);
2030 PyObject *fob, *ret;
2031 struct filedescr *fdp;
2032 char pathname[MAXPATHLEN+1];
2033 FILE *fp = NULL;
2035 pathname[0] = '\0';
2036 if (path == Py_None)
2037 path = NULL;
2038 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
2039 if (fdp == NULL)
2040 return NULL;
2041 if (fp != NULL) {
2042 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2043 if (fob == NULL) {
2044 fclose(fp);
2045 return NULL;
2048 else {
2049 fob = Py_None;
2050 Py_INCREF(fob);
2052 ret = Py_BuildValue("Os(ssi)",
2053 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
2054 Py_DECREF(fob);
2055 return ret;
2058 static PyObject *
2059 imp_find_module(PyObject *self, PyObject *args)
2061 char *name;
2062 PyObject *path = NULL;
2063 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
2064 return NULL;
2065 return call_find_module(name, path);
2068 static PyObject *
2069 imp_init_builtin(PyObject *self, PyObject *args)
2071 char *name;
2072 int ret;
2073 PyObject *m;
2074 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2075 return NULL;
2076 ret = init_builtin(name);
2077 if (ret < 0)
2078 return NULL;
2079 if (ret == 0) {
2080 Py_INCREF(Py_None);
2081 return Py_None;
2083 m = PyImport_AddModule(name);
2084 Py_XINCREF(m);
2085 return m;
2088 static PyObject *
2089 imp_init_frozen(PyObject *self, PyObject *args)
2091 char *name;
2092 int ret;
2093 PyObject *m;
2094 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2095 return NULL;
2096 ret = PyImport_ImportFrozenModule(name);
2097 if (ret < 0)
2098 return NULL;
2099 if (ret == 0) {
2100 Py_INCREF(Py_None);
2101 return Py_None;
2103 m = PyImport_AddModule(name);
2104 Py_XINCREF(m);
2105 return m;
2108 static PyObject *
2109 imp_get_frozen_object(PyObject *self, PyObject *args)
2111 char *name;
2113 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2114 return NULL;
2115 return get_frozen_object(name);
2118 static PyObject *
2119 imp_is_builtin(PyObject *self, PyObject *args)
2121 char *name;
2122 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2123 return NULL;
2124 return PyInt_FromLong(is_builtin(name));
2127 static PyObject *
2128 imp_is_frozen(PyObject *self, PyObject *args)
2130 char *name;
2131 struct _frozen *p;
2132 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2133 return NULL;
2134 p = find_frozen(name);
2135 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
2138 static FILE *
2139 get_file(char *pathname, PyObject *fob, char *mode)
2141 FILE *fp;
2142 if (fob == NULL) {
2143 fp = fopen(pathname, mode);
2144 if (fp == NULL)
2145 PyErr_SetFromErrno(PyExc_IOError);
2147 else {
2148 fp = PyFile_AsFile(fob);
2149 if (fp == NULL)
2150 PyErr_SetString(PyExc_ValueError,
2151 "bad/closed file object");
2153 return fp;
2156 static PyObject *
2157 imp_load_compiled(PyObject *self, PyObject *args)
2159 char *name;
2160 char *pathname;
2161 PyObject *fob = NULL;
2162 PyObject *m;
2163 FILE *fp;
2164 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
2165 &PyFile_Type, &fob))
2166 return NULL;
2167 fp = get_file(pathname, fob, "rb");
2168 if (fp == NULL)
2169 return NULL;
2170 m = load_compiled_module(name, pathname, fp);
2171 if (fob == NULL)
2172 fclose(fp);
2173 return m;
2176 #ifdef HAVE_DYNAMIC_LOADING
2178 static PyObject *
2179 imp_load_dynamic(PyObject *self, PyObject *args)
2181 char *name;
2182 char *pathname;
2183 PyObject *fob = NULL;
2184 PyObject *m;
2185 FILE *fp = NULL;
2186 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
2187 &PyFile_Type, &fob))
2188 return NULL;
2189 if (fob) {
2190 fp = get_file(pathname, fob, "r");
2191 if (fp == NULL)
2192 return NULL;
2194 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2195 return m;
2198 #endif /* HAVE_DYNAMIC_LOADING */
2200 static PyObject *
2201 imp_load_source(PyObject *self, PyObject *args)
2203 char *name;
2204 char *pathname;
2205 PyObject *fob = NULL;
2206 PyObject *m;
2207 FILE *fp;
2208 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
2209 &PyFile_Type, &fob))
2210 return NULL;
2211 fp = get_file(pathname, fob, "r");
2212 if (fp == NULL)
2213 return NULL;
2214 m = load_source_module(name, pathname, fp);
2215 if (fob == NULL)
2216 fclose(fp);
2217 return m;
2220 #ifdef macintosh
2221 static PyObject *
2222 imp_load_resource(PyObject *self, PyObject *args)
2224 char *name;
2225 char *pathname;
2226 PyObject *m;
2228 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
2229 return NULL;
2230 m = PyMac_LoadResourceModule(name, pathname);
2231 return m;
2233 #endif /* macintosh */
2235 static PyObject *
2236 imp_load_module(PyObject *self, PyObject *args)
2238 char *name;
2239 PyObject *fob;
2240 char *pathname;
2241 char *suffix; /* Unused */
2242 char *mode;
2243 int type;
2244 FILE *fp;
2246 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
2247 &name, &fob, &pathname,
2248 &suffix, &mode, &type))
2249 return NULL;
2250 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2251 PyErr_Format(PyExc_ValueError,
2252 "invalid file open mode %.200s", mode);
2253 return NULL;
2255 if (fob == Py_None)
2256 fp = NULL;
2257 else {
2258 if (!PyFile_Check(fob)) {
2259 PyErr_SetString(PyExc_ValueError,
2260 "load_module arg#2 should be a file or None");
2261 return NULL;
2263 fp = get_file(pathname, fob, mode);
2264 if (fp == NULL)
2265 return NULL;
2267 return load_module(name, fp, pathname, type);
2270 static PyObject *
2271 imp_load_package(PyObject *self, PyObject *args)
2273 char *name;
2274 char *pathname;
2275 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
2276 return NULL;
2277 return load_package(name, pathname);
2280 static PyObject *
2281 imp_new_module(PyObject *self, PyObject *args)
2283 char *name;
2284 if (!PyArg_ParseTuple(args, "s:new_module", &name))
2285 return NULL;
2286 return PyModule_New(name);
2289 /* Doc strings */
2291 static char doc_imp[] = "\
2292 This module provides the components needed to build your own\n\
2293 __import__ function. Undocumented functions are obsolete.\n\
2296 static char doc_find_module[] = "\
2297 find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2298 Search for a module. If path is omitted or None, search for a\n\
2299 built-in, frozen or special module and continue search in sys.path.\n\
2300 The module name cannot contain '.'; to search for a submodule of a\n\
2301 package, pass the submodule name and the package's __path__.\
2304 static char doc_load_module[] = "\
2305 load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2306 Load a module, given information returned by find_module().\n\
2307 The module name must include the full package name, if any.\
2310 static char doc_get_magic[] = "\
2311 get_magic() -> string\n\
2312 Return the magic number for .pyc or .pyo files.\
2315 static char doc_get_suffixes[] = "\
2316 get_suffixes() -> [(suffix, mode, type), ...]\n\
2317 Return a list of (suffix, mode, type) tuples describing the files\n\
2318 that find_module() looks for.\
2321 static char doc_new_module[] = "\
2322 new_module(name) -> module\n\
2323 Create a new module. Do not enter it in sys.modules.\n\
2324 The module name must include the full package name, if any.\
2327 static PyMethodDef imp_methods[] = {
2328 {"find_module", imp_find_module, 1, doc_find_module},
2329 {"get_magic", imp_get_magic, 1, doc_get_magic},
2330 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2331 {"load_module", imp_load_module, 1, doc_load_module},
2332 {"new_module", imp_new_module, 1, doc_new_module},
2333 /* The rest are obsolete */
2334 {"get_frozen_object", imp_get_frozen_object, 1},
2335 {"init_builtin", imp_init_builtin, 1},
2336 {"init_frozen", imp_init_frozen, 1},
2337 {"is_builtin", imp_is_builtin, 1},
2338 {"is_frozen", imp_is_frozen, 1},
2339 {"load_compiled", imp_load_compiled, 1},
2340 #ifdef HAVE_DYNAMIC_LOADING
2341 {"load_dynamic", imp_load_dynamic, 1},
2342 #endif
2343 {"load_package", imp_load_package, 1},
2344 #ifdef macintosh
2345 {"load_resource", imp_load_resource, 1},
2346 #endif
2347 {"load_source", imp_load_source, 1},
2348 {NULL, NULL} /* sentinel */
2351 static int
2352 setint(PyObject *d, char *name, int value)
2354 PyObject *v;
2355 int err;
2357 v = PyInt_FromLong((long)value);
2358 err = PyDict_SetItemString(d, name, v);
2359 Py_XDECREF(v);
2360 return err;
2363 void
2364 initimp(void)
2366 PyObject *m, *d;
2368 m = Py_InitModule4("imp", imp_methods, doc_imp,
2369 NULL, PYTHON_API_VERSION);
2370 d = PyModule_GetDict(m);
2372 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2373 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2374 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2375 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2376 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2377 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2378 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2379 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
2380 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
2382 failure:
2387 /* API for embedding applications that want to add their own entries
2388 to the table of built-in modules. This should normally be called
2389 *before* Py_Initialize(). When the table resize fails, -1 is
2390 returned and the existing table is unchanged.
2392 After a similar function by Just van Rossum. */
2395 PyImport_ExtendInittab(struct _inittab *newtab)
2397 static struct _inittab *our_copy = NULL;
2398 struct _inittab *p;
2399 int i, n;
2401 /* Count the number of entries in both tables */
2402 for (n = 0; newtab[n].name != NULL; n++)
2404 if (n == 0)
2405 return 0; /* Nothing to do */
2406 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2409 /* Allocate new memory for the combined table */
2410 p = our_copy;
2411 PyMem_RESIZE(p, struct _inittab, i+n+1);
2412 if (p == NULL)
2413 return -1;
2415 /* Copy the tables into the new memory */
2416 if (our_copy != PyImport_Inittab)
2417 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2418 PyImport_Inittab = our_copy = p;
2419 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2421 return 0;
2424 /* Shorthand to add a single entry given a name and a function */
2427 PyImport_AppendInittab(char *name, void (*initfunc)(void))
2429 struct _inittab newtab[2];
2431 memset(newtab, '\0', sizeof newtab);
2433 newtab[0].name = name;
2434 newtab[0].initfunc = initfunc;
2436 return PyImport_ExtendInittab(newtab);