Updated for 2.1b2 distribution.
[python/dscho.git] / Python / import.c
blob7f49e57699355f953c78258efb598841c909a7ba
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, (size_t)MAXPATHLEN+1);
733 if (cpathname != NULL &&
734 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
735 co = read_compiled_module(cpathname, fpc);
736 fclose(fpc);
737 if (co == NULL)
738 return NULL;
739 if (Py_VerboseFlag)
740 PySys_WriteStderr("import %s # precompiled from %s\n",
741 name, cpathname);
742 pathname = cpathname;
744 else {
745 co = parse_source_module(pathname, fp);
746 if (co == NULL)
747 return NULL;
748 if (Py_VerboseFlag)
749 PySys_WriteStderr("import %s # from %s\n",
750 name, pathname);
751 write_compiled_module(co, cpathname, mtime);
753 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
754 Py_DECREF(co);
756 return m;
760 /* Forward */
761 static PyObject *load_module(char *, FILE *, char *, int);
762 static struct filedescr *find_module(char *, PyObject *,
763 char *, size_t, FILE **);
764 static struct _frozen *find_frozen(char *name);
766 /* Load a package and return its module object WITH INCREMENTED
767 REFERENCE COUNT */
769 static PyObject *
770 load_package(char *name, char *pathname)
772 PyObject *m, *d, *file, *path;
773 int err;
774 char buf[MAXPATHLEN+1];
775 FILE *fp = NULL;
776 struct filedescr *fdp;
778 m = PyImport_AddModule(name);
779 if (m == NULL)
780 return NULL;
781 if (Py_VerboseFlag)
782 PySys_WriteStderr("import %s # directory %s\n",
783 name, pathname);
784 d = PyModule_GetDict(m);
785 file = PyString_FromString(pathname);
786 if (file == NULL)
787 return NULL;
788 path = Py_BuildValue("[O]", file);
789 if (path == NULL) {
790 Py_DECREF(file);
791 return NULL;
793 err = PyDict_SetItemString(d, "__file__", file);
794 if (err == 0)
795 err = PyDict_SetItemString(d, "__path__", path);
796 if (err != 0) {
797 m = NULL;
798 goto cleanup;
800 buf[0] = '\0';
801 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
802 if (fdp == NULL) {
803 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
804 PyErr_Clear();
806 else
807 m = NULL;
808 goto cleanup;
810 m = load_module(name, fp, buf, fdp->type);
811 if (fp != NULL)
812 fclose(fp);
813 cleanup:
814 Py_XDECREF(path);
815 Py_XDECREF(file);
816 return m;
820 /* Helper to test for built-in module */
822 static int
823 is_builtin(char *name)
825 int i;
826 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
827 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
828 if (PyImport_Inittab[i].initfunc == NULL)
829 return -1;
830 else
831 return 1;
834 return 0;
838 /* Search the path (default sys.path) for a module. Return the
839 corresponding filedescr struct, and (via return arguments) the
840 pathname and an open file. Return NULL if the module is not found. */
842 #ifdef MS_COREDLL
843 extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
844 char *, int);
845 #endif
847 static int case_ok(char *, int, int, char *);
848 static int find_init_module(char *); /* Forward */
850 static struct filedescr *
851 find_module(char *realname, PyObject *path, char *buf, size_t buflen,
852 FILE **p_fp)
854 int i, npath;
855 size_t len, namelen;
856 struct _frozen *f;
857 struct filedescr *fdp = NULL;
858 FILE *fp = NULL;
859 #ifndef RISCOS
860 struct stat statbuf;
861 #endif
862 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
863 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
864 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
865 char name[MAXPATHLEN+1];
867 if (strlen(realname) > MAXPATHLEN) {
868 PyErr_SetString(PyExc_OverflowError, "module name is too long");
869 return NULL;
871 strcpy(name, realname);
873 if (path != NULL && PyString_Check(path)) {
874 /* Submodule of "frozen" package:
875 Set name to the fullname, path to NULL
876 and continue as "usual" */
877 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
878 PyErr_SetString(PyExc_ImportError,
879 "full frozen module name too long");
880 return NULL;
882 strcpy(buf, PyString_AsString(path));
883 strcat(buf, ".");
884 strcat(buf, name);
885 strcpy(name, buf);
886 path = NULL;
888 if (path == NULL) {
889 if (is_builtin(name)) {
890 strcpy(buf, name);
891 return &fd_builtin;
893 if ((f = find_frozen(name)) != NULL) {
894 strcpy(buf, name);
895 return &fd_frozen;
898 #ifdef MS_COREDLL
899 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
900 if (fp != NULL) {
901 *p_fp = fp;
902 return fdp;
904 #endif
905 path = PySys_GetObject("path");
907 if (path == NULL || !PyList_Check(path)) {
908 PyErr_SetString(PyExc_ImportError,
909 "sys.path must be a list of directory names");
910 return NULL;
912 npath = PyList_Size(path);
913 namelen = strlen(name);
914 for (i = 0; i < npath; i++) {
915 PyObject *v = PyList_GetItem(path, i);
916 if (!PyString_Check(v))
917 continue;
918 len = PyString_Size(v);
919 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
920 continue; /* Too long */
921 strcpy(buf, PyString_AsString(v));
922 if (strlen(buf) != len)
923 continue; /* v contains '\0' */
924 #ifdef macintosh
925 #ifdef INTERN_STRINGS
927 ** Speedup: each sys.path item is interned, and
928 ** FindResourceModule remembers which items refer to
929 ** folders (so we don't have to bother trying to look
930 ** into them for resources).
932 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
933 v = PyList_GET_ITEM(path, i);
934 #endif
935 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
936 static struct filedescr resfiledescr =
937 {"", "", PY_RESOURCE};
939 return &resfiledescr;
941 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
942 static struct filedescr resfiledescr =
943 {"", "", PY_CODERESOURCE};
945 return &resfiledescr;
947 #endif
948 if (len > 0 && buf[len-1] != SEP
949 #ifdef ALTSEP
950 && buf[len-1] != ALTSEP
951 #endif
953 buf[len++] = SEP;
954 strcpy(buf+len, name);
955 len += namelen;
957 /* Check for package import (buf holds a directory name,
958 and there's an __init__ module in that directory */
959 #ifdef HAVE_STAT
960 if (stat(buf, &statbuf) == 0 &&
961 S_ISDIR(statbuf.st_mode) &&
962 find_init_module(buf)) {
963 if (case_ok(buf, len, namelen, name))
964 return &fd_package;
965 else
966 return NULL;
968 #else
969 /* XXX How are you going to test for directories? */
970 #ifdef RISCOS
972 static struct filedescr fd = {"", "", PKG_DIRECTORY};
973 if (isdir(buf)) {
974 if (find_init_module(buf))
975 return &fd;
978 #endif
979 #endif
980 #ifdef macintosh
981 fdp = PyMac_FindModuleExtension(buf, &len, name);
982 if (fdp) {
983 #else
984 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
985 strcpy(buf+len, fdp->suffix);
986 if (Py_VerboseFlag > 1)
987 PySys_WriteStderr("# trying %s\n", buf);
988 #endif /* !macintosh */
989 fp = fopen(buf, fdp->mode);
990 if (fp != NULL) {
991 if (case_ok(buf, len, namelen, name))
992 break;
993 else { /* continue search */
994 fclose(fp);
995 fp = NULL;
999 if (fp != NULL)
1000 break;
1002 if (fp == NULL) {
1003 PyErr_Format(PyExc_ImportError,
1004 "No module named %.200s", name);
1005 return NULL;
1007 *p_fp = fp;
1008 return fdp;
1011 /* case_ok(char* buf, int len, int namelen, char* name)
1012 * The arguments here are tricky, best shown by example:
1013 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1014 * ^ ^ ^ ^
1015 * |--------------------- buf ---------------------|
1016 * |------------------- len ------------------|
1017 * |------ name -------|
1018 * |----- namelen -----|
1019 * buf is the full path, but len only counts up to (& exclusive of) the
1020 * extension. name is the module name, also exclusive of extension.
1022 * We've already done a successful stat() or fopen() on buf, so know that
1023 * there's some match, possibly case-insensitive.
1025 * case_ok() is to return 1 if there's a case-sensitive match for
1026 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1027 * exists.
1029 * case_ok() is used to implement case-sensitive import semantics even
1030 * on platforms with case-insensitive filesystems. It's trivial to implement
1031 * for case-sensitive filesystems. It's pretty much a cross-platform
1032 * nightmare for systems with case-insensitive filesystems.
1035 /* First we may need a pile of platform-specific header files; the sequence
1036 * of #if's here should match the sequence in the body of case_ok().
1038 #if defined(MS_WIN32) || defined(__CYGWIN__)
1039 #include <windows.h>
1040 #ifdef __CYGWIN__
1041 #include <sys/cygwin.h>
1042 #endif
1044 #elif defined(DJGPP)
1045 #include <dir.h>
1047 #elif defined(macintosh)
1048 #include <TextUtils.h>
1049 #ifdef USE_GUSI1
1050 #include "TFileSpec.h" /* for Path2FSSpec() */
1051 #endif
1053 #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
1054 #include <sys/types.h>
1055 #include <dirent.h>
1057 #endif
1059 static int
1060 case_ok(char *buf, int len, int namelen, char *name)
1062 /* Pick a platform-specific implementation; the sequence of #if's here should
1063 * match the sequence just above.
1066 /* MS_WIN32 || __CYGWIN__ */
1067 #if defined(MS_WIN32) || defined(__CYGWIN__)
1068 WIN32_FIND_DATA data;
1069 HANDLE h;
1070 #ifdef __CYGWIN__
1071 char tempbuf[MAX_PATH];
1072 #endif
1074 if (getenv("PYTHONCASEOK") != NULL)
1075 return 1;
1077 #ifdef __CYGWIN__
1078 cygwin32_conv_to_win32_path(buf, tempbuf);
1079 h = FindFirstFile(tempbuf, &data);
1080 #else
1081 h = FindFirstFile(buf, &data);
1082 #endif
1083 if (h == INVALID_HANDLE_VALUE) {
1084 PyErr_Format(PyExc_NameError,
1085 "Can't find file for module %.100s\n(filename %.300s)",
1086 name, buf);
1087 return 0;
1089 FindClose(h);
1090 return strncmp(data.cFileName, name, namelen) == 0;
1092 /* DJGPP */
1093 #elif defined(DJGPP)
1094 struct ffblk ffblk;
1095 int done;
1097 if (getenv("PYTHONCASEOK") != NULL)
1098 return 1;
1100 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1101 if (done) {
1102 PyErr_Format(PyExc_NameError,
1103 "Can't find file for module %.100s\n(filename %.300s)",
1104 name, buf);
1105 return 0;
1107 return strncmp(ffblk.ff_name, name, namelen) == 0;
1109 /* macintosh */
1110 #elif defined(macintosh)
1111 FSSpec fss;
1112 OSErr err;
1114 if (getenv("PYTHONCASEOK") != NULL)
1115 return 1;
1117 #ifndef USE_GUSI1
1118 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1119 #else
1120 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1121 the way, which is fine for all directories, but here we need
1122 the original name of the alias file (say, Dlg.ppc.slb, not
1123 toolboxmodules.ppc.slb). */
1124 char *colon;
1125 err = Path2FSSpec(buf, &fss);
1126 if (err == noErr) {
1127 colon = strrchr(buf, ':'); /* find filename */
1128 if (colon != NULL)
1129 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1130 Pstring(colon+1), &fss);
1131 else
1132 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1133 fss.name, &fss);
1135 #endif
1136 if (err) {
1137 PyErr_Format(PyExc_NameError,
1138 "Can't find file for module %.100s\n(filename %.300s)",
1139 name, buf);
1140 return 0;
1142 return fss.name[0] >= namelen &&
1143 strncmp(name, (char *)fss.name+1, namelen) == 0;
1145 /* new-fangled macintosh (macosx) */
1146 #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
1147 DIR *dirp;
1148 struct dirent *dp;
1149 char dirname[MAXPATHLEN + 1];
1150 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
1152 if (getenv("PYTHONCASEOK") != NULL)
1153 return 1;
1155 /* Copy the dir component into dirname; substitute "." if empty */
1156 if (dirlen <= 0) {
1157 dirname[0] = '.';
1158 dirname[1] = '\0';
1160 else {
1161 assert(dirlen <= MAXPATHLEN);
1162 memcpy(dirname, buf, dirlen);
1163 dirname[dirlen] = '\0';
1165 /* Open the directory and search the entries for an exact match. */
1166 dirp = opendir(dirname);
1167 if (dirp) {
1168 char *nameWithExt = buf + len - namelen;
1169 while ((dp = readdir(dirp)) != NULL) {
1170 const int thislen =
1171 #ifdef _DIRENT_HAVE_D_NAMELEN
1172 dp->d_namlen;
1173 #else
1174 strlen(dp->d_name);
1175 #endif
1176 if (thislen >= namelen &&
1177 strcmp(dp->d_name, nameWithExt) == 0) {
1178 (void)closedir(dirp);
1179 return 1; /* Found */
1182 (void)closedir(dirp);
1184 return 0 ; /* Not found */
1186 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1187 #else
1188 return 1;
1190 #endif
1194 #ifdef HAVE_STAT
1195 /* Helper to look for __init__.py or __init__.py[co] in potential package */
1196 static int
1197 find_init_module(char *buf)
1199 size_t save_len = strlen(buf);
1200 size_t i = save_len;
1201 struct stat statbuf;
1203 if (save_len + 13 >= MAXPATHLEN)
1204 return 0;
1205 buf[i++] = SEP;
1206 strcpy(buf+i, "__init__.py");
1207 if (stat(buf, &statbuf) == 0) {
1208 buf[save_len] = '\0';
1209 return 1;
1211 i += strlen(buf+i);
1212 if (Py_OptimizeFlag)
1213 strcpy(buf+i, "o");
1214 else
1215 strcpy(buf+i, "c");
1216 if (stat(buf, &statbuf) == 0) {
1217 buf[save_len] = '\0';
1218 return 1;
1220 buf[save_len] = '\0';
1221 return 0;
1224 #else
1226 #ifdef RISCOS
1227 static int
1228 find_init_module(buf)
1229 char *buf;
1231 int save_len = strlen(buf);
1232 int i = save_len;
1234 if (save_len + 13 >= MAXPATHLEN)
1235 return 0;
1236 buf[i++] = SEP;
1237 strcpy(buf+i, "__init__/py");
1238 if (isfile(buf)) {
1239 buf[save_len] = '\0';
1240 return 1;
1243 if (Py_OptimizeFlag)
1244 strcpy(buf+i, "o");
1245 else
1246 strcpy(buf+i, "c");
1247 if (isfile(buf)) {
1248 buf[save_len] = '\0';
1249 return 1;
1251 buf[save_len] = '\0';
1252 return 0;
1254 #endif /*RISCOS*/
1256 #endif /* HAVE_STAT */
1259 static int init_builtin(char *); /* Forward */
1261 /* Load an external module using the default search path and return
1262 its module object WITH INCREMENTED REFERENCE COUNT */
1264 static PyObject *
1265 load_module(char *name, FILE *fp, char *buf, int type)
1267 PyObject *modules;
1268 PyObject *m;
1269 int err;
1271 /* First check that there's an open file (if we need one) */
1272 switch (type) {
1273 case PY_SOURCE:
1274 case PY_COMPILED:
1275 if (fp == NULL) {
1276 PyErr_Format(PyExc_ValueError,
1277 "file object required for import (type code %d)",
1278 type);
1279 return NULL;
1283 switch (type) {
1285 case PY_SOURCE:
1286 m = load_source_module(name, buf, fp);
1287 break;
1289 case PY_COMPILED:
1290 m = load_compiled_module(name, buf, fp);
1291 break;
1293 #ifdef HAVE_DYNAMIC_LOADING
1294 case C_EXTENSION:
1295 m = _PyImport_LoadDynamicModule(name, buf, fp);
1296 break;
1297 #endif
1299 #ifdef macintosh
1300 case PY_RESOURCE:
1301 m = PyMac_LoadResourceModule(name, buf);
1302 break;
1303 case PY_CODERESOURCE:
1304 m = PyMac_LoadCodeResourceModule(name, buf);
1305 break;
1306 #endif
1308 case PKG_DIRECTORY:
1309 m = load_package(name, buf);
1310 break;
1312 case C_BUILTIN:
1313 case PY_FROZEN:
1314 if (buf != NULL && buf[0] != '\0')
1315 name = buf;
1316 if (type == C_BUILTIN)
1317 err = init_builtin(name);
1318 else
1319 err = PyImport_ImportFrozenModule(name);
1320 if (err < 0)
1321 return NULL;
1322 if (err == 0) {
1323 PyErr_Format(PyExc_ImportError,
1324 "Purported %s module %.200s not found",
1325 type == C_BUILTIN ?
1326 "builtin" : "frozen",
1327 name);
1328 return NULL;
1330 modules = PyImport_GetModuleDict();
1331 m = PyDict_GetItemString(modules, name);
1332 if (m == NULL) {
1333 PyErr_Format(
1334 PyExc_ImportError,
1335 "%s module %.200s not properly initialized",
1336 type == C_BUILTIN ?
1337 "builtin" : "frozen",
1338 name);
1339 return NULL;
1341 Py_INCREF(m);
1342 break;
1344 default:
1345 PyErr_Format(PyExc_ImportError,
1346 "Don't know how to import %.200s (type code %d)",
1347 name, type);
1348 m = NULL;
1352 return m;
1356 /* Initialize a built-in module.
1357 Return 1 for succes, 0 if the module is not found, and -1 with
1358 an exception set if the initialization failed. */
1360 static int
1361 init_builtin(char *name)
1363 struct _inittab *p;
1364 PyObject *mod;
1366 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1367 return 1;
1369 for (p = PyImport_Inittab; p->name != NULL; p++) {
1370 if (strcmp(name, p->name) == 0) {
1371 if (p->initfunc == NULL) {
1372 PyErr_Format(PyExc_ImportError,
1373 "Cannot re-init internal module %.200s",
1374 name);
1375 return -1;
1377 if (Py_VerboseFlag)
1378 PySys_WriteStderr("import %s # builtin\n", name);
1379 (*p->initfunc)();
1380 if (PyErr_Occurred())
1381 return -1;
1382 if (_PyImport_FixupExtension(name, name) == NULL)
1383 return -1;
1384 return 1;
1387 return 0;
1391 /* Frozen modules */
1393 static struct _frozen *
1394 find_frozen(char *name)
1396 struct _frozen *p;
1398 for (p = PyImport_FrozenModules; ; p++) {
1399 if (p->name == NULL)
1400 return NULL;
1401 if (strcmp(p->name, name) == 0)
1402 break;
1404 return p;
1407 static PyObject *
1408 get_frozen_object(char *name)
1410 struct _frozen *p = find_frozen(name);
1411 int size;
1413 if (p == NULL) {
1414 PyErr_Format(PyExc_ImportError,
1415 "No such frozen object named %.200s",
1416 name);
1417 return NULL;
1419 size = p->size;
1420 if (size < 0)
1421 size = -size;
1422 return PyMarshal_ReadObjectFromString((char *)p->code, size);
1425 /* Initialize a frozen module.
1426 Return 1 for succes, 0 if the module is not found, and -1 with
1427 an exception set if the initialization failed.
1428 This function is also used from frozenmain.c */
1431 PyImport_ImportFrozenModule(char *name)
1433 struct _frozen *p = find_frozen(name);
1434 PyObject *co;
1435 PyObject *m;
1436 int ispackage;
1437 int size;
1439 if (p == NULL)
1440 return 0;
1441 size = p->size;
1442 ispackage = (size < 0);
1443 if (ispackage)
1444 size = -size;
1445 if (Py_VerboseFlag)
1446 PySys_WriteStderr("import %s # frozen%s\n",
1447 name, ispackage ? " package" : "");
1448 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1449 if (co == NULL)
1450 return -1;
1451 if (!PyCode_Check(co)) {
1452 Py_DECREF(co);
1453 PyErr_Format(PyExc_TypeError,
1454 "frozen object %.200s is not a code object",
1455 name);
1456 return -1;
1458 if (ispackage) {
1459 /* Set __path__ to the package name */
1460 PyObject *d, *s;
1461 int err;
1462 m = PyImport_AddModule(name);
1463 if (m == NULL)
1464 return -1;
1465 d = PyModule_GetDict(m);
1466 s = PyString_InternFromString(name);
1467 if (s == NULL)
1468 return -1;
1469 err = PyDict_SetItemString(d, "__path__", s);
1470 Py_DECREF(s);
1471 if (err != 0)
1472 return err;
1474 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
1475 Py_DECREF(co);
1476 if (m == NULL)
1477 return -1;
1478 Py_DECREF(m);
1479 return 1;
1483 /* Import a module, either built-in, frozen, or external, and return
1484 its module object WITH INCREMENTED REFERENCE COUNT */
1486 PyObject *
1487 PyImport_ImportModule(char *name)
1489 PyObject *pname;
1490 PyObject *result;
1492 pname = PyString_FromString(name);
1493 result = PyImport_Import(pname);
1494 Py_DECREF(pname);
1495 return result;
1498 /* Forward declarations for helper routines */
1499 static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1500 static PyObject *load_next(PyObject *mod, PyObject *altmod,
1501 char **p_name, char *buf, int *p_buflen);
1502 static int mark_miss(char *name);
1503 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1504 char *buf, int buflen, int recursive);
1505 static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
1507 /* The Magnum Opus of dotted-name import :-) */
1509 static PyObject *
1510 import_module_ex(char *name, PyObject *globals, PyObject *locals,
1511 PyObject *fromlist)
1513 char buf[MAXPATHLEN+1];
1514 int buflen = 0;
1515 PyObject *parent, *head, *next, *tail;
1517 parent = get_parent(globals, buf, &buflen);
1518 if (parent == NULL)
1519 return NULL;
1521 head = load_next(parent, Py_None, &name, buf, &buflen);
1522 if (head == NULL)
1523 return NULL;
1525 tail = head;
1526 Py_INCREF(tail);
1527 while (name) {
1528 next = load_next(tail, tail, &name, buf, &buflen);
1529 Py_DECREF(tail);
1530 if (next == NULL) {
1531 Py_DECREF(head);
1532 return NULL;
1534 tail = next;
1537 if (fromlist != NULL) {
1538 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1539 fromlist = NULL;
1542 if (fromlist == NULL) {
1543 Py_DECREF(tail);
1544 return head;
1547 Py_DECREF(head);
1548 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
1549 Py_DECREF(tail);
1550 return NULL;
1553 return tail;
1556 PyObject *
1557 PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1558 PyObject *fromlist)
1560 PyObject *result;
1561 lock_import();
1562 result = import_module_ex(name, globals, locals, fromlist);
1563 unlock_import();
1564 return result;
1567 static PyObject *
1568 get_parent(PyObject *globals, char *buf, int *p_buflen)
1570 static PyObject *namestr = NULL;
1571 static PyObject *pathstr = NULL;
1572 PyObject *modname, *modpath, *modules, *parent;
1574 if (globals == NULL || !PyDict_Check(globals))
1575 return Py_None;
1577 if (namestr == NULL) {
1578 namestr = PyString_InternFromString("__name__");
1579 if (namestr == NULL)
1580 return NULL;
1582 if (pathstr == NULL) {
1583 pathstr = PyString_InternFromString("__path__");
1584 if (pathstr == NULL)
1585 return NULL;
1588 *buf = '\0';
1589 *p_buflen = 0;
1590 modname = PyDict_GetItem(globals, namestr);
1591 if (modname == NULL || !PyString_Check(modname))
1592 return Py_None;
1594 modpath = PyDict_GetItem(globals, pathstr);
1595 if (modpath != NULL) {
1596 int len = PyString_GET_SIZE(modname);
1597 if (len > MAXPATHLEN) {
1598 PyErr_SetString(PyExc_ValueError,
1599 "Module name too long");
1600 return NULL;
1602 strcpy(buf, PyString_AS_STRING(modname));
1603 *p_buflen = len;
1605 else {
1606 char *start = PyString_AS_STRING(modname);
1607 char *lastdot = strrchr(start, '.');
1608 size_t len;
1609 if (lastdot == NULL)
1610 return Py_None;
1611 len = lastdot - start;
1612 if (len >= MAXPATHLEN) {
1613 PyErr_SetString(PyExc_ValueError,
1614 "Module name too long");
1615 return NULL;
1617 strncpy(buf, start, len);
1618 buf[len] = '\0';
1619 *p_buflen = len;
1622 modules = PyImport_GetModuleDict();
1623 parent = PyDict_GetItemString(modules, buf);
1624 if (parent == NULL)
1625 parent = Py_None;
1626 return parent;
1627 /* We expect, but can't guarantee, if parent != None, that:
1628 - parent.__name__ == buf
1629 - parent.__dict__ is globals
1630 If this is violated... Who cares? */
1633 /* altmod is either None or same as mod */
1634 static PyObject *
1635 load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1636 int *p_buflen)
1638 char *name = *p_name;
1639 char *dot = strchr(name, '.');
1640 size_t len;
1641 char *p;
1642 PyObject *result;
1644 if (dot == NULL) {
1645 *p_name = NULL;
1646 len = strlen(name);
1648 else {
1649 *p_name = dot+1;
1650 len = dot-name;
1652 if (len == 0) {
1653 PyErr_SetString(PyExc_ValueError,
1654 "Empty module name");
1655 return NULL;
1658 p = buf + *p_buflen;
1659 if (p != buf)
1660 *p++ = '.';
1661 if (p+len-buf >= MAXPATHLEN) {
1662 PyErr_SetString(PyExc_ValueError,
1663 "Module name too long");
1664 return NULL;
1666 strncpy(p, name, len);
1667 p[len] = '\0';
1668 *p_buflen = p+len-buf;
1670 result = import_submodule(mod, p, buf);
1671 if (result == Py_None && altmod != mod) {
1672 Py_DECREF(result);
1673 /* Here, altmod must be None and mod must not be None */
1674 result = import_submodule(altmod, p, p);
1675 if (result != NULL && result != Py_None) {
1676 if (mark_miss(buf) != 0) {
1677 Py_DECREF(result);
1678 return NULL;
1680 strncpy(buf, name, len);
1681 buf[len] = '\0';
1682 *p_buflen = len;
1685 if (result == NULL)
1686 return NULL;
1688 if (result == Py_None) {
1689 Py_DECREF(result);
1690 PyErr_Format(PyExc_ImportError,
1691 "No module named %.200s", name);
1692 return NULL;
1695 return result;
1698 static int
1699 mark_miss(char *name)
1701 PyObject *modules = PyImport_GetModuleDict();
1702 return PyDict_SetItemString(modules, name, Py_None);
1705 static int
1706 ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1707 int recursive)
1709 int i;
1711 if (!PyObject_HasAttrString(mod, "__path__"))
1712 return 1;
1714 for (i = 0; ; i++) {
1715 PyObject *item = PySequence_GetItem(fromlist, i);
1716 int hasit;
1717 if (item == NULL) {
1718 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1719 PyErr_Clear();
1720 return 1;
1722 return 0;
1724 if (!PyString_Check(item)) {
1725 PyErr_SetString(PyExc_TypeError,
1726 "Item in ``from list'' not a string");
1727 Py_DECREF(item);
1728 return 0;
1730 if (PyString_AS_STRING(item)[0] == '*') {
1731 PyObject *all;
1732 Py_DECREF(item);
1733 /* See if the package defines __all__ */
1734 if (recursive)
1735 continue; /* Avoid endless recursion */
1736 all = PyObject_GetAttrString(mod, "__all__");
1737 if (all == NULL)
1738 PyErr_Clear();
1739 else {
1740 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1741 return 0;
1742 Py_DECREF(all);
1744 continue;
1746 hasit = PyObject_HasAttr(mod, item);
1747 if (!hasit) {
1748 char *subname = PyString_AS_STRING(item);
1749 PyObject *submod;
1750 char *p;
1751 if (buflen + strlen(subname) >= MAXPATHLEN) {
1752 PyErr_SetString(PyExc_ValueError,
1753 "Module name too long");
1754 Py_DECREF(item);
1755 return 0;
1757 p = buf + buflen;
1758 *p++ = '.';
1759 strcpy(p, subname);
1760 submod = import_submodule(mod, subname, buf);
1761 Py_XDECREF(submod);
1762 if (submod == NULL) {
1763 Py_DECREF(item);
1764 return 0;
1767 Py_DECREF(item);
1770 /* NOTREACHED */
1773 static PyObject *
1774 import_submodule(PyObject *mod, char *subname, char *fullname)
1776 PyObject *modules = PyImport_GetModuleDict();
1777 PyObject *m;
1779 /* Require:
1780 if mod == None: subname == fullname
1781 else: mod.__name__ + "." + subname == fullname
1784 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
1785 Py_INCREF(m);
1787 else {
1788 PyObject *path;
1789 char buf[MAXPATHLEN+1];
1790 struct filedescr *fdp;
1791 FILE *fp = NULL;
1793 if (mod == Py_None)
1794 path = NULL;
1795 else {
1796 path = PyObject_GetAttrString(mod, "__path__");
1797 if (path == NULL) {
1798 PyErr_Clear();
1799 Py_INCREF(Py_None);
1800 return Py_None;
1804 buf[0] = '\0';
1805 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1806 Py_XDECREF(path);
1807 if (fdp == NULL) {
1808 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1809 return NULL;
1810 PyErr_Clear();
1811 Py_INCREF(Py_None);
1812 return Py_None;
1814 m = load_module(fullname, fp, buf, fdp->type);
1815 if (fp)
1816 fclose(fp);
1817 if (m != NULL && mod != Py_None) {
1818 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1819 Py_DECREF(m);
1820 m = NULL;
1825 return m;
1829 /* Re-import a module of any kind and return its module object, WITH
1830 INCREMENTED REFERENCE COUNT */
1832 PyObject *
1833 PyImport_ReloadModule(PyObject *m)
1835 PyObject *modules = PyImport_GetModuleDict();
1836 PyObject *path = NULL;
1837 char *name, *subname;
1838 char buf[MAXPATHLEN+1];
1839 struct filedescr *fdp;
1840 FILE *fp = NULL;
1842 if (m == NULL || !PyModule_Check(m)) {
1843 PyErr_SetString(PyExc_TypeError,
1844 "reload() argument must be module");
1845 return NULL;
1847 name = PyModule_GetName(m);
1848 if (name == NULL)
1849 return NULL;
1850 if (m != PyDict_GetItemString(modules, name)) {
1851 PyErr_Format(PyExc_ImportError,
1852 "reload(): module %.200s not in sys.modules",
1853 name);
1854 return NULL;
1856 subname = strrchr(name, '.');
1857 if (subname == NULL)
1858 subname = name;
1859 else {
1860 PyObject *parentname, *parent;
1861 parentname = PyString_FromStringAndSize(name, (subname-name));
1862 if (parentname == NULL)
1863 return NULL;
1864 parent = PyDict_GetItem(modules, parentname);
1865 Py_DECREF(parentname);
1866 if (parent == NULL) {
1867 PyErr_Format(PyExc_ImportError,
1868 "reload(): parent %.200s not in sys.modules",
1869 name);
1870 return NULL;
1872 subname++;
1873 path = PyObject_GetAttrString(parent, "__path__");
1874 if (path == NULL)
1875 PyErr_Clear();
1877 buf[0] = '\0';
1878 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1879 Py_XDECREF(path);
1880 if (fdp == NULL)
1881 return NULL;
1882 m = load_module(name, fp, buf, fdp->type);
1883 if (fp)
1884 fclose(fp);
1885 return m;
1889 /* Higher-level import emulator which emulates the "import" statement
1890 more accurately -- it invokes the __import__() function from the
1891 builtins of the current globals. This means that the import is
1892 done using whatever import hooks are installed in the current
1893 environment, e.g. by "rexec".
1894 A dummy list ["__doc__"] is passed as the 4th argument so that
1895 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1896 will return <module "gencache"> instead of <module "win32com">. */
1898 PyObject *
1899 PyImport_Import(PyObject *module_name)
1901 static PyObject *silly_list = NULL;
1902 static PyObject *builtins_str = NULL;
1903 static PyObject *import_str = NULL;
1904 PyObject *globals = NULL;
1905 PyObject *import = NULL;
1906 PyObject *builtins = NULL;
1907 PyObject *r = NULL;
1909 /* Initialize constant string objects */
1910 if (silly_list == NULL) {
1911 import_str = PyString_InternFromString("__import__");
1912 if (import_str == NULL)
1913 return NULL;
1914 builtins_str = PyString_InternFromString("__builtins__");
1915 if (builtins_str == NULL)
1916 return NULL;
1917 silly_list = Py_BuildValue("[s]", "__doc__");
1918 if (silly_list == NULL)
1919 return NULL;
1922 /* Get the builtins from current globals */
1923 globals = PyEval_GetGlobals();
1924 if (globals != NULL) {
1925 Py_INCREF(globals);
1926 builtins = PyObject_GetItem(globals, builtins_str);
1927 if (builtins == NULL)
1928 goto err;
1930 else {
1931 /* No globals -- use standard builtins, and fake globals */
1932 PyErr_Clear();
1934 builtins = PyImport_ImportModuleEx("__builtin__",
1935 NULL, NULL, NULL);
1936 if (builtins == NULL)
1937 return NULL;
1938 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1939 if (globals == NULL)
1940 goto err;
1943 /* Get the __import__ function from the builtins */
1944 if (PyDict_Check(builtins))
1945 import = PyObject_GetItem(builtins, import_str);
1946 else
1947 import = PyObject_GetAttr(builtins, import_str);
1948 if (import == NULL)
1949 goto err;
1951 /* Call the _import__ function with the proper argument list */
1952 r = PyObject_CallFunction(import, "OOOO",
1953 module_name, globals, globals, silly_list);
1955 err:
1956 Py_XDECREF(globals);
1957 Py_XDECREF(builtins);
1958 Py_XDECREF(import);
1960 return r;
1964 /* Module 'imp' provides Python access to the primitives used for
1965 importing modules.
1968 static PyObject *
1969 imp_get_magic(PyObject *self, PyObject *args)
1971 char buf[4];
1973 if (!PyArg_ParseTuple(args, ":get_magic"))
1974 return NULL;
1975 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1976 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1977 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1978 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
1980 return PyString_FromStringAndSize(buf, 4);
1983 static PyObject *
1984 imp_get_suffixes(PyObject *self, PyObject *args)
1986 PyObject *list;
1987 struct filedescr *fdp;
1989 if (!PyArg_ParseTuple(args, ":get_suffixes"))
1990 return NULL;
1991 list = PyList_New(0);
1992 if (list == NULL)
1993 return NULL;
1994 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1995 PyObject *item = Py_BuildValue("ssi",
1996 fdp->suffix, fdp->mode, fdp->type);
1997 if (item == NULL) {
1998 Py_DECREF(list);
1999 return NULL;
2001 if (PyList_Append(list, item) < 0) {
2002 Py_DECREF(list);
2003 Py_DECREF(item);
2004 return NULL;
2006 Py_DECREF(item);
2008 return list;
2011 static PyObject *
2012 call_find_module(char *name, PyObject *path)
2014 extern int fclose(FILE *);
2015 PyObject *fob, *ret;
2016 struct filedescr *fdp;
2017 char pathname[MAXPATHLEN+1];
2018 FILE *fp = NULL;
2020 pathname[0] = '\0';
2021 if (path == Py_None)
2022 path = NULL;
2023 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
2024 if (fdp == NULL)
2025 return NULL;
2026 if (fp != NULL) {
2027 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2028 if (fob == NULL) {
2029 fclose(fp);
2030 return NULL;
2033 else {
2034 fob = Py_None;
2035 Py_INCREF(fob);
2037 ret = Py_BuildValue("Os(ssi)",
2038 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
2039 Py_DECREF(fob);
2040 return ret;
2043 static PyObject *
2044 imp_find_module(PyObject *self, PyObject *args)
2046 char *name;
2047 PyObject *path = NULL;
2048 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
2049 return NULL;
2050 return call_find_module(name, path);
2053 static PyObject *
2054 imp_init_builtin(PyObject *self, PyObject *args)
2056 char *name;
2057 int ret;
2058 PyObject *m;
2059 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2060 return NULL;
2061 ret = init_builtin(name);
2062 if (ret < 0)
2063 return NULL;
2064 if (ret == 0) {
2065 Py_INCREF(Py_None);
2066 return Py_None;
2068 m = PyImport_AddModule(name);
2069 Py_XINCREF(m);
2070 return m;
2073 static PyObject *
2074 imp_init_frozen(PyObject *self, PyObject *args)
2076 char *name;
2077 int ret;
2078 PyObject *m;
2079 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2080 return NULL;
2081 ret = PyImport_ImportFrozenModule(name);
2082 if (ret < 0)
2083 return NULL;
2084 if (ret == 0) {
2085 Py_INCREF(Py_None);
2086 return Py_None;
2088 m = PyImport_AddModule(name);
2089 Py_XINCREF(m);
2090 return m;
2093 static PyObject *
2094 imp_get_frozen_object(PyObject *self, PyObject *args)
2096 char *name;
2098 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2099 return NULL;
2100 return get_frozen_object(name);
2103 static PyObject *
2104 imp_is_builtin(PyObject *self, PyObject *args)
2106 char *name;
2107 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2108 return NULL;
2109 return PyInt_FromLong(is_builtin(name));
2112 static PyObject *
2113 imp_is_frozen(PyObject *self, PyObject *args)
2115 char *name;
2116 struct _frozen *p;
2117 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2118 return NULL;
2119 p = find_frozen(name);
2120 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
2123 static FILE *
2124 get_file(char *pathname, PyObject *fob, char *mode)
2126 FILE *fp;
2127 if (fob == NULL) {
2128 fp = fopen(pathname, mode);
2129 if (fp == NULL)
2130 PyErr_SetFromErrno(PyExc_IOError);
2132 else {
2133 fp = PyFile_AsFile(fob);
2134 if (fp == NULL)
2135 PyErr_SetString(PyExc_ValueError,
2136 "bad/closed file object");
2138 return fp;
2141 static PyObject *
2142 imp_load_compiled(PyObject *self, PyObject *args)
2144 char *name;
2145 char *pathname;
2146 PyObject *fob = NULL;
2147 PyObject *m;
2148 FILE *fp;
2149 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
2150 &PyFile_Type, &fob))
2151 return NULL;
2152 fp = get_file(pathname, fob, "rb");
2153 if (fp == NULL)
2154 return NULL;
2155 m = load_compiled_module(name, pathname, fp);
2156 if (fob == NULL)
2157 fclose(fp);
2158 return m;
2161 #ifdef HAVE_DYNAMIC_LOADING
2163 static PyObject *
2164 imp_load_dynamic(PyObject *self, PyObject *args)
2166 char *name;
2167 char *pathname;
2168 PyObject *fob = NULL;
2169 PyObject *m;
2170 FILE *fp = NULL;
2171 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
2172 &PyFile_Type, &fob))
2173 return NULL;
2174 if (fob) {
2175 fp = get_file(pathname, fob, "r");
2176 if (fp == NULL)
2177 return NULL;
2179 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2180 return m;
2183 #endif /* HAVE_DYNAMIC_LOADING */
2185 static PyObject *
2186 imp_load_source(PyObject *self, PyObject *args)
2188 char *name;
2189 char *pathname;
2190 PyObject *fob = NULL;
2191 PyObject *m;
2192 FILE *fp;
2193 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
2194 &PyFile_Type, &fob))
2195 return NULL;
2196 fp = get_file(pathname, fob, "r");
2197 if (fp == NULL)
2198 return NULL;
2199 m = load_source_module(name, pathname, fp);
2200 if (fob == NULL)
2201 fclose(fp);
2202 return m;
2205 #ifdef macintosh
2206 static PyObject *
2207 imp_load_resource(PyObject *self, PyObject *args)
2209 char *name;
2210 char *pathname;
2211 PyObject *m;
2213 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
2214 return NULL;
2215 m = PyMac_LoadResourceModule(name, pathname);
2216 return m;
2218 #endif /* macintosh */
2220 static PyObject *
2221 imp_load_module(PyObject *self, PyObject *args)
2223 char *name;
2224 PyObject *fob;
2225 char *pathname;
2226 char *suffix; /* Unused */
2227 char *mode;
2228 int type;
2229 FILE *fp;
2231 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
2232 &name, &fob, &pathname,
2233 &suffix, &mode, &type))
2234 return NULL;
2235 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2236 PyErr_Format(PyExc_ValueError,
2237 "invalid file open mode %.200s", mode);
2238 return NULL;
2240 if (fob == Py_None)
2241 fp = NULL;
2242 else {
2243 if (!PyFile_Check(fob)) {
2244 PyErr_SetString(PyExc_ValueError,
2245 "load_module arg#2 should be a file or None");
2246 return NULL;
2248 fp = get_file(pathname, fob, mode);
2249 if (fp == NULL)
2250 return NULL;
2252 return load_module(name, fp, pathname, type);
2255 static PyObject *
2256 imp_load_package(PyObject *self, PyObject *args)
2258 char *name;
2259 char *pathname;
2260 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
2261 return NULL;
2262 return load_package(name, pathname);
2265 static PyObject *
2266 imp_new_module(PyObject *self, PyObject *args)
2268 char *name;
2269 if (!PyArg_ParseTuple(args, "s:new_module", &name))
2270 return NULL;
2271 return PyModule_New(name);
2274 /* Doc strings */
2276 static char doc_imp[] = "\
2277 This module provides the components needed to build your own\n\
2278 __import__ function. Undocumented functions are obsolete.\n\
2281 static char doc_find_module[] = "\
2282 find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2283 Search for a module. If path is omitted or None, search for a\n\
2284 built-in, frozen or special module and continue search in sys.path.\n\
2285 The module name cannot contain '.'; to search for a submodule of a\n\
2286 package, pass the submodule name and the package's __path__.\
2289 static char doc_load_module[] = "\
2290 load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2291 Load a module, given information returned by find_module().\n\
2292 The module name must include the full package name, if any.\
2295 static char doc_get_magic[] = "\
2296 get_magic() -> string\n\
2297 Return the magic number for .pyc or .pyo files.\
2300 static char doc_get_suffixes[] = "\
2301 get_suffixes() -> [(suffix, mode, type), ...]\n\
2302 Return a list of (suffix, mode, type) tuples describing the files\n\
2303 that find_module() looks for.\
2306 static char doc_new_module[] = "\
2307 new_module(name) -> module\n\
2308 Create a new module. Do not enter it in sys.modules.\n\
2309 The module name must include the full package name, if any.\
2312 static PyMethodDef imp_methods[] = {
2313 {"find_module", imp_find_module, 1, doc_find_module},
2314 {"get_magic", imp_get_magic, 1, doc_get_magic},
2315 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2316 {"load_module", imp_load_module, 1, doc_load_module},
2317 {"new_module", imp_new_module, 1, doc_new_module},
2318 /* The rest are obsolete */
2319 {"get_frozen_object", imp_get_frozen_object, 1},
2320 {"init_builtin", imp_init_builtin, 1},
2321 {"init_frozen", imp_init_frozen, 1},
2322 {"is_builtin", imp_is_builtin, 1},
2323 {"is_frozen", imp_is_frozen, 1},
2324 {"load_compiled", imp_load_compiled, 1},
2325 #ifdef HAVE_DYNAMIC_LOADING
2326 {"load_dynamic", imp_load_dynamic, 1},
2327 #endif
2328 {"load_package", imp_load_package, 1},
2329 #ifdef macintosh
2330 {"load_resource", imp_load_resource, 1},
2331 #endif
2332 {"load_source", imp_load_source, 1},
2333 {NULL, NULL} /* sentinel */
2336 static int
2337 setint(PyObject *d, char *name, int value)
2339 PyObject *v;
2340 int err;
2342 v = PyInt_FromLong((long)value);
2343 err = PyDict_SetItemString(d, name, v);
2344 Py_XDECREF(v);
2345 return err;
2348 void
2349 initimp(void)
2351 PyObject *m, *d;
2353 m = Py_InitModule4("imp", imp_methods, doc_imp,
2354 NULL, PYTHON_API_VERSION);
2355 d = PyModule_GetDict(m);
2357 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2358 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2359 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2360 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2361 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2362 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2363 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2364 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
2365 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
2367 failure:
2372 /* API for embedding applications that want to add their own entries
2373 to the table of built-in modules. This should normally be called
2374 *before* Py_Initialize(). When the table resize fails, -1 is
2375 returned and the existing table is unchanged.
2377 After a similar function by Just van Rossum. */
2380 PyImport_ExtendInittab(struct _inittab *newtab)
2382 static struct _inittab *our_copy = NULL;
2383 struct _inittab *p;
2384 int i, n;
2386 /* Count the number of entries in both tables */
2387 for (n = 0; newtab[n].name != NULL; n++)
2389 if (n == 0)
2390 return 0; /* Nothing to do */
2391 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2394 /* Allocate new memory for the combined table */
2395 p = our_copy;
2396 PyMem_RESIZE(p, struct _inittab, i+n+1);
2397 if (p == NULL)
2398 return -1;
2400 /* Copy the tables into the new memory */
2401 if (our_copy != PyImport_Inittab)
2402 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2403 PyImport_Inittab = our_copy = p;
2404 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2406 return 0;
2409 /* Shorthand to add a single entry given a name and a function */
2412 PyImport_AppendInittab(char *name, void (*initfunc)(void))
2414 struct _inittab newtab[2];
2416 memset(newtab, '\0', sizeof newtab);
2418 newtab[0].name = name;
2419 newtab[0].initfunc = initfunc;
2421 return PyImport_ExtendInittab(newtab);