import.c revision 89825958705d1aaf2aa7cdf96f79d2f292f3274c
1
2/* Module definition and import implementation */
3
4#include "Python.h"
5
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
17
18#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif
21
22extern time_t PyOS_GetLastModificationTime(char *, FILE *);
23						/* In getmtime.c */
24
25/* Magic word to reject .pyc files generated by other Python versions */
26/* Change for each incompatible change */
27/* The value of CR and LF is incorporated so if you ever read or write
28   a .pyc file in text mode the magic number will be wrong; also, the
29   Apple MPW compiler swaps their values, botching string constants.
30   XXX That probably isn't important anymore.
31*/
32/* XXX Perhaps the magic number should be frozen and a version field
33   added to the .pyc file header? */
34/* New way to come up with the low 16 bits of the magic number:
35      (YEAR-1995) * 10000 +  MONTH * 100 + DAY
36   where MONTH and DAY are 1-based.
37   XXX Whatever the "old way" may have been isn't documented.
38   XXX This scheme breaks in 2002, as (2002-1995)*10000 = 70000 doesn't
39       fit in 16 bits.
40   XXX Later, sometimes 1 gets added to MAGIC in order to record that
41       the Unicode -U option is in use.  IMO (Tim's), that's a Bad Idea
42       (quite apart from that the -U option doesn't work so isn't used
43       anyway).
44
45   XXX MAL, 2002-02-07: I had to modify the MAGIC due to a fix of the
46       UTF-8 encoder (it previously produced invalid UTF-8 for unpaired
47       high surrogates), so I simply bumped the month value to 20 (invalid
48       month) and set the day to 1.  This should be recognizable by any
49       algorithm relying on the above scheme. Perhaps we should simply
50       start counting in increments of 10 from now on ?!
51
52       MWH, 2002-08-03: Removed SET_LINENO.  Couldn't be bothered figuring
53       out the MAGIC schemes, so just incremented it by 10.
54
55       GvR, 2002-08-31: Because MWH changed the bytecode again, moved the
56       magic number *back* to 62011.  This should get the snake-farm to
57       throw away its old .pyc files, amongst others.
58
59   Known values:
60       Python 1.5:   20121
61       Python 1.5.1: 20121
62       Python 1.5.2: 20121
63       Python 2.0:   50823
64       Python 2.0.1: 50823
65       Python 2.1:   60202
66       Python 2.1.1: 60202
67       Python 2.1.2: 60202
68       Python 2.2:   60717
69       Python 2.3a0: 62011
70       Python 2.3a0: 62021
71       Python 2.3a0: 62011 (!)
72*/
73#define MAGIC (62011 | ((long)'\r'<<16) | ((long)'\n'<<24))
74
75/* Magic word as global; note that _PyImport_Init() can change the
76   value of this global to accommodate for alterations of how the
77   compiler works which are enabled by command line switches. */
78static long pyc_magic = MAGIC;
79
80/* See _PyImport_FixupExtension() below */
81static PyObject *extensions = NULL;
82
83/* This table is defined in config.c: */
84extern struct _inittab _PyImport_Inittab[];
85
86struct _inittab *PyImport_Inittab = _PyImport_Inittab;
87
88/* these tables define the module suffixes that Python recognizes */
89struct filedescr * _PyImport_Filetab = NULL;
90
91#ifdef RISCOS
92static const struct filedescr _PyImport_StandardFiletab[] = {
93	{"/py", "U", PY_SOURCE},
94	{"/pyc", "rb", PY_COMPILED},
95	{0, 0}
96};
97#else
98static const struct filedescr _PyImport_StandardFiletab[] = {
99	{".py", "U", PY_SOURCE},
100#ifdef MS_WINDOWS
101	{".pyw", "U", PY_SOURCE},
102#endif
103	{".pyc", "rb", PY_COMPILED},
104	{0, 0}
105};
106#endif
107
108/* Initialize things */
109
110void
111_PyImport_Init(void)
112{
113	const struct filedescr *scan;
114	struct filedescr *filetab;
115	int countD = 0;
116	int countS = 0;
117
118	/* prepare _PyImport_Filetab: copy entries from
119	   _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
120	 */
121	for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
122		++countD;
123	for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
124		++countS;
125	filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
126	memcpy(filetab, _PyImport_DynLoadFiletab,
127	       countD * sizeof(struct filedescr));
128	memcpy(filetab + countD, _PyImport_StandardFiletab,
129	       countS * sizeof(struct filedescr));
130	filetab[countD + countS].suffix = NULL;
131
132	_PyImport_Filetab = filetab;
133
134	if (Py_OptimizeFlag) {
135		/* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
136		for (; filetab->suffix != NULL; filetab++) {
137#ifndef RISCOS
138			if (strcmp(filetab->suffix, ".pyc") == 0)
139				filetab->suffix = ".pyo";
140#else
141			if (strcmp(filetab->suffix, "/pyc") == 0)
142				filetab->suffix = "/pyo";
143#endif
144		}
145	}
146
147	if (Py_UnicodeFlag) {
148		/* Fix the pyc_magic so that byte compiled code created
149		   using the all-Unicode method doesn't interfere with
150		   code created in normal operation mode. */
151		pyc_magic = MAGIC + 1;
152	}
153}
154
155void
156_PyImport_Fini(void)
157{
158	Py_XDECREF(extensions);
159	extensions = NULL;
160	PyMem_DEL(_PyImport_Filetab);
161	_PyImport_Filetab = NULL;
162}
163
164
165/* Locking primitives to prevent parallel imports of the same module
166   in different threads to return with a partially loaded module.
167   These calls are serialized by the global interpreter lock. */
168
169#ifdef WITH_THREAD
170
171#include "pythread.h"
172
173static PyThread_type_lock import_lock = 0;
174static long import_lock_thread = -1;
175static int import_lock_level = 0;
176
177static void
178lock_import(void)
179{
180	long me = PyThread_get_thread_ident();
181	if (me == -1)
182		return; /* Too bad */
183	if (import_lock == NULL)
184		import_lock = PyThread_allocate_lock();
185	if (import_lock_thread == me) {
186		import_lock_level++;
187		return;
188	}
189	if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
190		PyThreadState *tstate = PyEval_SaveThread();
191		PyThread_acquire_lock(import_lock, 1);
192		PyEval_RestoreThread(tstate);
193	}
194	import_lock_thread = me;
195	import_lock_level = 1;
196}
197
198static void
199unlock_import(void)
200{
201	long me = PyThread_get_thread_ident();
202	if (me == -1)
203		return; /* Too bad */
204	if (import_lock_thread != me)
205		Py_FatalError("unlock_import: not holding the import lock");
206	import_lock_level--;
207	if (import_lock_level == 0) {
208		import_lock_thread = -1;
209		PyThread_release_lock(import_lock);
210	}
211}
212
213#else
214
215#define lock_import()
216#define unlock_import()
217
218#endif
219
220static PyObject *
221imp_lock_held(PyObject *self, PyObject *args)
222{
223	if (!PyArg_ParseTuple(args, ":lock_held"))
224		return NULL;
225#ifdef WITH_THREAD
226	return PyBool_FromLong(import_lock_thread != -1);
227#else
228	return PyBool_FromLong(0);
229#endif
230}
231
232/* Helper for sys */
233
234PyObject *
235PyImport_GetModuleDict(void)
236{
237	PyInterpreterState *interp = PyThreadState_Get()->interp;
238	if (interp->modules == NULL)
239		Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
240	return interp->modules;
241}
242
243
244/* List of names to clear in sys */
245static char* sys_deletes[] = {
246	"path", "argv", "ps1", "ps2", "exitfunc",
247	"exc_type", "exc_value", "exc_traceback",
248	"last_type", "last_value", "last_traceback",
249	NULL
250};
251
252static char* sys_files[] = {
253	"stdin", "__stdin__",
254	"stdout", "__stdout__",
255	"stderr", "__stderr__",
256	NULL
257};
258
259
260/* Un-initialize things, as good as we can */
261
262void
263PyImport_Cleanup(void)
264{
265	int pos, ndone;
266	char *name;
267	PyObject *key, *value, *dict;
268	PyInterpreterState *interp = PyThreadState_Get()->interp;
269	PyObject *modules = interp->modules;
270
271	if (modules == NULL)
272		return; /* Already done */
273
274	/* Delete some special variables first.  These are common
275	   places where user values hide and people complain when their
276	   destructors fail.  Since the modules containing them are
277	   deleted *last* of all, they would come too late in the normal
278	   destruction order.  Sigh. */
279
280	value = PyDict_GetItemString(modules, "__builtin__");
281	if (value != NULL && PyModule_Check(value)) {
282		dict = PyModule_GetDict(value);
283		if (Py_VerboseFlag)
284			PySys_WriteStderr("# clear __builtin__._\n");
285		PyDict_SetItemString(dict, "_", Py_None);
286	}
287	value = PyDict_GetItemString(modules, "sys");
288	if (value != NULL && PyModule_Check(value)) {
289		char **p;
290		PyObject *v;
291		dict = PyModule_GetDict(value);
292		for (p = sys_deletes; *p != NULL; p++) {
293			if (Py_VerboseFlag)
294				PySys_WriteStderr("# clear sys.%s\n", *p);
295			PyDict_SetItemString(dict, *p, Py_None);
296		}
297		for (p = sys_files; *p != NULL; p+=2) {
298			if (Py_VerboseFlag)
299				PySys_WriteStderr("# restore sys.%s\n", *p);
300			v = PyDict_GetItemString(dict, *(p+1));
301			if (v == NULL)
302				v = Py_None;
303			PyDict_SetItemString(dict, *p, v);
304		}
305	}
306
307	/* First, delete __main__ */
308	value = PyDict_GetItemString(modules, "__main__");
309	if (value != NULL && PyModule_Check(value)) {
310		if (Py_VerboseFlag)
311			PySys_WriteStderr("# cleanup __main__\n");
312		_PyModule_Clear(value);
313		PyDict_SetItemString(modules, "__main__", Py_None);
314	}
315
316	/* The special treatment of __builtin__ here is because even
317	   when it's not referenced as a module, its dictionary is
318	   referenced by almost every module's __builtins__.  Since
319	   deleting a module clears its dictionary (even if there are
320	   references left to it), we need to delete the __builtin__
321	   module last.  Likewise, we don't delete sys until the very
322	   end because it is implicitly referenced (e.g. by print).
323
324	   Also note that we 'delete' modules by replacing their entry
325	   in the modules dict with None, rather than really deleting
326	   them; this avoids a rehash of the modules dictionary and
327	   also marks them as "non existent" so they won't be
328	   re-imported. */
329
330	/* Next, repeatedly delete modules with a reference count of
331	   one (skipping __builtin__ and sys) and delete them */
332	do {
333		ndone = 0;
334		pos = 0;
335		while (PyDict_Next(modules, &pos, &key, &value)) {
336			if (value->ob_refcnt != 1)
337				continue;
338			if (PyString_Check(key) && PyModule_Check(value)) {
339				name = PyString_AS_STRING(key);
340				if (strcmp(name, "__builtin__") == 0)
341					continue;
342				if (strcmp(name, "sys") == 0)
343					continue;
344				if (Py_VerboseFlag)
345					PySys_WriteStderr(
346						"# cleanup[1] %s\n", name);
347				_PyModule_Clear(value);
348				PyDict_SetItem(modules, key, Py_None);
349				ndone++;
350			}
351		}
352	} while (ndone > 0);
353
354	/* Next, delete all modules (still skipping __builtin__ and sys) */
355	pos = 0;
356	while (PyDict_Next(modules, &pos, &key, &value)) {
357		if (PyString_Check(key) && PyModule_Check(value)) {
358			name = PyString_AS_STRING(key);
359			if (strcmp(name, "__builtin__") == 0)
360				continue;
361			if (strcmp(name, "sys") == 0)
362				continue;
363			if (Py_VerboseFlag)
364				PySys_WriteStderr("# cleanup[2] %s\n", name);
365			_PyModule_Clear(value);
366			PyDict_SetItem(modules, key, Py_None);
367		}
368	}
369
370	/* Next, delete sys and __builtin__ (in that order) */
371	value = PyDict_GetItemString(modules, "sys");
372	if (value != NULL && PyModule_Check(value)) {
373		if (Py_VerboseFlag)
374			PySys_WriteStderr("# cleanup sys\n");
375		_PyModule_Clear(value);
376		PyDict_SetItemString(modules, "sys", Py_None);
377	}
378	value = PyDict_GetItemString(modules, "__builtin__");
379	if (value != NULL && PyModule_Check(value)) {
380		if (Py_VerboseFlag)
381			PySys_WriteStderr("# cleanup __builtin__\n");
382		_PyModule_Clear(value);
383		PyDict_SetItemString(modules, "__builtin__", Py_None);
384	}
385
386	/* Finally, clear and delete the modules directory */
387	PyDict_Clear(modules);
388	interp->modules = NULL;
389	Py_DECREF(modules);
390}
391
392
393/* Helper for pythonrun.c -- return magic number */
394
395long
396PyImport_GetMagicNumber(void)
397{
398	return pyc_magic;
399}
400
401
402/* Magic for extension modules (built-in as well as dynamically
403   loaded).  To prevent initializing an extension module more than
404   once, we keep a static dictionary 'extensions' keyed by module name
405   (for built-in modules) or by filename (for dynamically loaded
406   modules), containing these modules.  A copy of the module's
407   dictionary is stored by calling _PyImport_FixupExtension()
408   immediately after the module initialization function succeeds.  A
409   copy can be retrieved from there by calling
410   _PyImport_FindExtension(). */
411
412PyObject *
413_PyImport_FixupExtension(char *name, char *filename)
414{
415	PyObject *modules, *mod, *dict, *copy;
416	if (extensions == NULL) {
417		extensions = PyDict_New();
418		if (extensions == NULL)
419			return NULL;
420	}
421	modules = PyImport_GetModuleDict();
422	mod = PyDict_GetItemString(modules, name);
423	if (mod == NULL || !PyModule_Check(mod)) {
424		PyErr_Format(PyExc_SystemError,
425		  "_PyImport_FixupExtension: module %.200s not loaded", name);
426		return NULL;
427	}
428	dict = PyModule_GetDict(mod);
429	if (dict == NULL)
430		return NULL;
431	copy = PyDict_Copy(dict);
432	if (copy == NULL)
433		return NULL;
434	PyDict_SetItemString(extensions, filename, copy);
435	Py_DECREF(copy);
436	return copy;
437}
438
439PyObject *
440_PyImport_FindExtension(char *name, char *filename)
441{
442	PyObject *dict, *mod, *mdict;
443	if (extensions == NULL)
444		return NULL;
445	dict = PyDict_GetItemString(extensions, filename);
446	if (dict == NULL)
447		return NULL;
448	mod = PyImport_AddModule(name);
449	if (mod == NULL)
450		return NULL;
451	mdict = PyModule_GetDict(mod);
452	if (mdict == NULL)
453		return NULL;
454	if (PyDict_Update(mdict, dict))
455		return NULL;
456	if (Py_VerboseFlag)
457		PySys_WriteStderr("import %s # previously loaded (%s)\n",
458			name, filename);
459	return mod;
460}
461
462
463/* Get the module object corresponding to a module name.
464   First check the modules dictionary if there's one there,
465   if not, create a new one and insert in in the modules dictionary.
466   Because the former action is most common, THIS DOES NOT RETURN A
467   'NEW' REFERENCE! */
468
469PyObject *
470PyImport_AddModule(char *name)
471{
472	PyObject *modules = PyImport_GetModuleDict();
473	PyObject *m;
474
475	if ((m = PyDict_GetItemString(modules, name)) != NULL &&
476	    PyModule_Check(m))
477		return m;
478	m = PyModule_New(name);
479	if (m == NULL)
480		return NULL;
481	if (PyDict_SetItemString(modules, name, m) != 0) {
482		Py_DECREF(m);
483		return NULL;
484	}
485	Py_DECREF(m); /* Yes, it still exists, in modules! */
486
487	return m;
488}
489
490
491/* Execute a code object in a module and return the module object
492   WITH INCREMENTED REFERENCE COUNT */
493
494PyObject *
495PyImport_ExecCodeModule(char *name, PyObject *co)
496{
497	return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
498}
499
500PyObject *
501PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
502{
503	PyObject *modules = PyImport_GetModuleDict();
504	PyObject *m, *d, *v;
505
506	m = PyImport_AddModule(name);
507	if (m == NULL)
508		return NULL;
509	d = PyModule_GetDict(m);
510	if (PyDict_GetItemString(d, "__builtins__") == NULL) {
511		if (PyDict_SetItemString(d, "__builtins__",
512					 PyEval_GetBuiltins()) != 0)
513			return NULL;
514	}
515	/* Remember the filename as the __file__ attribute */
516	v = NULL;
517	if (pathname != NULL) {
518		v = PyString_FromString(pathname);
519		if (v == NULL)
520			PyErr_Clear();
521	}
522	if (v == NULL) {
523		v = ((PyCodeObject *)co)->co_filename;
524		Py_INCREF(v);
525	}
526	if (PyDict_SetItemString(d, "__file__", v) != 0)
527		PyErr_Clear(); /* Not important enough to report */
528	Py_DECREF(v);
529
530	v = PyEval_EvalCode((PyCodeObject *)co, d, d);
531	if (v == NULL)
532		return NULL;
533	Py_DECREF(v);
534
535	if ((m = PyDict_GetItemString(modules, name)) == NULL) {
536		PyErr_Format(PyExc_ImportError,
537			     "Loaded module %.200s not found in sys.modules",
538			     name);
539		return NULL;
540	}
541
542	Py_INCREF(m);
543
544	return m;
545}
546
547
548/* Given a pathname for a Python source file, fill a buffer with the
549   pathname for the corresponding compiled file.  Return the pathname
550   for the compiled file, or NULL if there's no space in the buffer.
551   Doesn't set an exception. */
552
553static char *
554make_compiled_pathname(char *pathname, char *buf, size_t buflen)
555{
556	size_t len = strlen(pathname);
557	if (len+2 > buflen)
558		return NULL;
559
560#ifdef MS_WINDOWS
561	/* Treat .pyw as if it were .py.  The case of ".pyw" must match
562	   that used in _PyImport_StandardFiletab. */
563	if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
564		--len;	/* pretend 'w' isn't there */
565#endif
566	memcpy(buf, pathname, len);
567	buf[len] = Py_OptimizeFlag ? 'o' : 'c';
568	buf[len+1] = '\0';
569
570	return buf;
571}
572
573
574/* Given a pathname for a Python source file, its time of last
575   modification, and a pathname for a compiled file, check whether the
576   compiled file represents the same version of the source.  If so,
577   return a FILE pointer for the compiled file, positioned just after
578   the header; if not, return NULL.
579   Doesn't set an exception. */
580
581static FILE *
582check_compiled_module(char *pathname, long mtime, char *cpathname)
583{
584	FILE *fp;
585	long magic;
586	long pyc_mtime;
587
588	fp = fopen(cpathname, "rb");
589	if (fp == NULL)
590		return NULL;
591	magic = PyMarshal_ReadLongFromFile(fp);
592	if (magic != pyc_magic) {
593		if (Py_VerboseFlag)
594			PySys_WriteStderr("# %s has bad magic\n", cpathname);
595		fclose(fp);
596		return NULL;
597	}
598	pyc_mtime = PyMarshal_ReadLongFromFile(fp);
599	if (pyc_mtime != mtime) {
600		if (Py_VerboseFlag)
601			PySys_WriteStderr("# %s has bad mtime\n", cpathname);
602		fclose(fp);
603		return NULL;
604	}
605	if (Py_VerboseFlag)
606		PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
607	return fp;
608}
609
610
611/* Read a code object from a file and check it for validity */
612
613static PyCodeObject *
614read_compiled_module(char *cpathname, FILE *fp)
615{
616	PyObject *co;
617
618	co = PyMarshal_ReadLastObjectFromFile(fp);
619	/* Ugly: rd_object() may return NULL with or without error */
620	if (co == NULL || !PyCode_Check(co)) {
621		if (!PyErr_Occurred())
622			PyErr_Format(PyExc_ImportError,
623			    "Non-code object in %.200s", cpathname);
624		Py_XDECREF(co);
625		return NULL;
626	}
627	return (PyCodeObject *)co;
628}
629
630
631/* Load a module from a compiled file, execute it, and return its
632   module object WITH INCREMENTED REFERENCE COUNT */
633
634static PyObject *
635load_compiled_module(char *name, char *cpathname, FILE *fp)
636{
637	long magic;
638	PyCodeObject *co;
639	PyObject *m;
640
641	magic = PyMarshal_ReadLongFromFile(fp);
642	if (magic != pyc_magic) {
643		PyErr_Format(PyExc_ImportError,
644			     "Bad magic number in %.200s", cpathname);
645		return NULL;
646	}
647	(void) PyMarshal_ReadLongFromFile(fp);
648	co = read_compiled_module(cpathname, fp);
649	if (co == NULL)
650		return NULL;
651	if (Py_VerboseFlag)
652		PySys_WriteStderr("import %s # precompiled from %s\n",
653			name, cpathname);
654	m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
655	Py_DECREF(co);
656
657	return m;
658}
659
660/* Parse a source file and return the corresponding code object */
661
662static PyCodeObject *
663parse_source_module(char *pathname, FILE *fp)
664{
665	PyCodeObject *co;
666	node *n;
667
668	n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
669	if (n == NULL)
670		return NULL;
671	co = PyNode_Compile(n, pathname);
672	PyNode_Free(n);
673
674	return co;
675}
676
677
678/* Helper to open a bytecode file for writing in exclusive mode */
679
680static FILE *
681open_exclusive(char *filename)
682{
683#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
684	/* Use O_EXCL to avoid a race condition when another process tries to
685	   write the same file.  When that happens, our open() call fails,
686	   which is just fine (since it's only a cache).
687	   XXX If the file exists and is writable but the directory is not
688	   writable, the file will never be written.  Oh well.
689	*/
690	int fd;
691	(void) unlink(filename);
692	fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
693#ifdef O_BINARY
694				|O_BINARY   /* necessary for Windows */
695#endif
696#ifdef __VMS
697                        , 0666, "ctxt=bin", "shr=nil");
698#else
699                        , 0666);
700#endif
701	if (fd < 0)
702		return NULL;
703	return fdopen(fd, "wb");
704#else
705	/* Best we can do -- on Windows this can't happen anyway */
706	return fopen(filename, "wb");
707#endif
708}
709
710
711/* Write a compiled module to a file, placing the time of last
712   modification of its source into the header.
713   Errors are ignored, if a write error occurs an attempt is made to
714   remove the file. */
715
716static void
717write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
718{
719	FILE *fp;
720
721	fp = open_exclusive(cpathname);
722	if (fp == NULL) {
723		if (Py_VerboseFlag)
724			PySys_WriteStderr(
725				"# can't create %s\n", cpathname);
726		return;
727	}
728	PyMarshal_WriteLongToFile(pyc_magic, fp);
729	/* First write a 0 for mtime */
730	PyMarshal_WriteLongToFile(0L, fp);
731	PyMarshal_WriteObjectToFile((PyObject *)co, fp);
732	if (ferror(fp)) {
733		if (Py_VerboseFlag)
734			PySys_WriteStderr("# can't write %s\n", cpathname);
735		/* Don't keep partial file */
736		fclose(fp);
737		(void) unlink(cpathname);
738		return;
739	}
740	/* Now write the true mtime */
741	fseek(fp, 4L, 0);
742	PyMarshal_WriteLongToFile(mtime, fp);
743	fflush(fp);
744	fclose(fp);
745	if (Py_VerboseFlag)
746		PySys_WriteStderr("# wrote %s\n", cpathname);
747#ifdef macintosh
748	PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
749#endif
750}
751
752
753/* Load a source module from a given file and return its module
754   object WITH INCREMENTED REFERENCE COUNT.  If there's a matching
755   byte-compiled file, use that instead. */
756
757static PyObject *
758load_source_module(char *name, char *pathname, FILE *fp)
759{
760	time_t mtime;
761	FILE *fpc;
762	char buf[MAXPATHLEN+1];
763	char *cpathname;
764	PyCodeObject *co;
765	PyObject *m;
766
767	mtime = PyOS_GetLastModificationTime(pathname, fp);
768	if (mtime == (time_t)(-1))
769		return NULL;
770#if SIZEOF_TIME_T > 4
771	/* Python's .pyc timestamp handling presumes that the timestamp fits
772	   in 4 bytes. This will be fine until sometime in the year 2038,
773	   when a 4-byte signed time_t will overflow.
774	 */
775	if (mtime >> 32) {
776		PyErr_SetString(PyExc_OverflowError,
777			"modification time overflows a 4 byte field");
778		return NULL;
779	}
780#endif
781	cpathname = make_compiled_pathname(pathname, buf,
782					   (size_t)MAXPATHLEN + 1);
783	if (cpathname != NULL &&
784	    (fpc = check_compiled_module(pathname, mtime, cpathname))) {
785		co = read_compiled_module(cpathname, fpc);
786		fclose(fpc);
787		if (co == NULL)
788			return NULL;
789		if (Py_VerboseFlag)
790			PySys_WriteStderr("import %s # precompiled from %s\n",
791				name, cpathname);
792		pathname = cpathname;
793	}
794	else {
795		co = parse_source_module(pathname, fp);
796		if (co == NULL)
797			return NULL;
798		if (Py_VerboseFlag)
799			PySys_WriteStderr("import %s # from %s\n",
800				name, pathname);
801		write_compiled_module(co, cpathname, mtime);
802	}
803	m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
804	Py_DECREF(co);
805
806	return m;
807}
808
809
810/* Forward */
811static PyObject *load_module(char *, FILE *, char *, int);
812static struct filedescr *find_module(char *, PyObject *,
813				     char *, size_t, FILE **);
814static struct _frozen *find_frozen(char *name);
815
816/* Load a package and return its module object WITH INCREMENTED
817   REFERENCE COUNT */
818
819static PyObject *
820load_package(char *name, char *pathname)
821{
822	PyObject *m, *d, *file, *path;
823	int err;
824	char buf[MAXPATHLEN+1];
825	FILE *fp = NULL;
826	struct filedescr *fdp;
827
828	m = PyImport_AddModule(name);
829	if (m == NULL)
830		return NULL;
831	if (Py_VerboseFlag)
832		PySys_WriteStderr("import %s # directory %s\n",
833			name, pathname);
834	d = PyModule_GetDict(m);
835	file = PyString_FromString(pathname);
836	if (file == NULL)
837		return NULL;
838	path = Py_BuildValue("[O]", file);
839	if (path == NULL) {
840		Py_DECREF(file);
841		return NULL;
842	}
843	err = PyDict_SetItemString(d, "__file__", file);
844	if (err == 0)
845		err = PyDict_SetItemString(d, "__path__", path);
846	if (err != 0) {
847		m = NULL;
848		goto cleanup;
849	}
850	buf[0] = '\0';
851	fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
852	if (fdp == NULL) {
853		if (PyErr_ExceptionMatches(PyExc_ImportError)) {
854			PyErr_Clear();
855		}
856		else
857			m = NULL;
858		goto cleanup;
859	}
860	m = load_module(name, fp, buf, fdp->type);
861	if (fp != NULL)
862		fclose(fp);
863  cleanup:
864	Py_XDECREF(path);
865	Py_XDECREF(file);
866	return m;
867}
868
869
870/* Helper to test for built-in module */
871
872static int
873is_builtin(char *name)
874{
875	int i;
876	for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
877		if (strcmp(name, PyImport_Inittab[i].name) == 0) {
878			if (PyImport_Inittab[i].initfunc == NULL)
879				return -1;
880			else
881				return 1;
882		}
883	}
884	return 0;
885}
886
887
888/* Search the path (default sys.path) for a module.  Return the
889   corresponding filedescr struct, and (via return arguments) the
890   pathname and an open file.  Return NULL if the module is not found. */
891
892#ifdef MS_COREDLL
893extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
894					char *, int);
895#endif
896
897static int case_ok(char *, int, int, char *);
898static int find_init_module(char *); /* Forward */
899
900static struct filedescr *
901find_module(char *realname, PyObject *path, char *buf, size_t buflen,
902	    FILE **p_fp)
903{
904	int i, npath;
905	size_t len, namelen;
906	struct filedescr *fdp = NULL;
907	char *filemode;
908	FILE *fp = NULL;
909#ifndef RISCOS
910	struct stat statbuf;
911#endif
912	static struct filedescr fd_frozen = {"", "", PY_FROZEN};
913	static struct filedescr fd_builtin = {"", "", C_BUILTIN};
914	static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
915	char name[MAXPATHLEN+1];
916#if defined(PYOS_OS2)
917	size_t saved_len;
918	size_t saved_namelen;
919	char *saved_buf = NULL;
920#endif
921
922	if (strlen(realname) > MAXPATHLEN) {
923		PyErr_SetString(PyExc_OverflowError,
924				"module name is too long");
925		return NULL;
926	}
927	strcpy(name, realname);
928
929	if (path != NULL && PyString_Check(path)) {
930		/* The only type of submodule allowed inside a "frozen"
931		   package are other frozen modules or packages. */
932		if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
933			PyErr_SetString(PyExc_ImportError,
934					"full frozen module name too long");
935			return NULL;
936		}
937		strcpy(buf, PyString_AsString(path));
938		strcat(buf, ".");
939		strcat(buf, name);
940		strcpy(name, buf);
941#ifdef macintosh
942		/* Freezing on the mac works different, and the modules are
943		** actually on sys.path. So we don't take the quick exit but
944		** continue with the normal flow.
945		*/
946		path = NULL;
947#else
948		if (find_frozen(name) != NULL) {
949			strcpy(buf, name);
950			return &fd_frozen;
951		}
952		PyErr_Format(PyExc_ImportError,
953			     "No frozen submodule named %.200s", name);
954		return NULL;
955#endif
956	}
957	if (path == NULL) {
958		if (is_builtin(name)) {
959			strcpy(buf, name);
960			return &fd_builtin;
961		}
962		if ((find_frozen(name)) != NULL) {
963			strcpy(buf, name);
964			return &fd_frozen;
965		}
966
967#ifdef MS_COREDLL
968		fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
969		if (fp != NULL) {
970			*p_fp = fp;
971			return fdp;
972		}
973#endif
974		path = PySys_GetObject("path");
975	}
976	if (path == NULL || !PyList_Check(path)) {
977		PyErr_SetString(PyExc_ImportError,
978				"sys.path must be a list of directory names");
979		return NULL;
980	}
981	npath = PyList_Size(path);
982	namelen = strlen(name);
983	for (i = 0; i < npath; i++) {
984		PyObject *copy = NULL;
985		PyObject *v = PyList_GetItem(path, i);
986#ifdef Py_USING_UNICODE
987		if (PyUnicode_Check(v)) {
988			copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
989				PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
990			if (copy == NULL)
991				return NULL;
992			v = copy;
993		}
994		else
995#endif
996		if (!PyString_Check(v))
997			continue;
998		len = PyString_Size(v);
999		if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1000			Py_XDECREF(copy);
1001			continue; /* Too long */
1002		}
1003		strcpy(buf, PyString_AsString(v));
1004		if (strlen(buf) != len) {
1005			Py_XDECREF(copy);
1006			continue; /* v contains '\0' */
1007		}
1008#ifdef macintosh
1009		/*
1010		** Speedup: each sys.path item is interned, and
1011		** FindResourceModule remembers which items refer to
1012		** folders (so we don't have to bother trying to look
1013		** into them for resources).
1014		*/
1015		PyString_InternInPlace(&PyList_GET_ITEM(path, i));
1016		v = PyList_GET_ITEM(path, i);
1017		if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
1018			static struct filedescr resfiledescr =
1019				{"", "", PY_RESOURCE};
1020
1021			Py_XDECREF(copy);
1022			return &resfiledescr;
1023		}
1024		if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
1025			static struct filedescr resfiledescr =
1026				{"", "", PY_CODERESOURCE};
1027
1028			Py_XDECREF(copy);
1029			return &resfiledescr;
1030		}
1031#endif
1032		if (len > 0 && buf[len-1] != SEP
1033#ifdef ALTSEP
1034		    && buf[len-1] != ALTSEP
1035#endif
1036		    )
1037			buf[len++] = SEP;
1038		strcpy(buf+len, name);
1039		len += namelen;
1040
1041		/* Check for package import (buf holds a directory name,
1042		   and there's an __init__ module in that directory */
1043#ifdef HAVE_STAT
1044		if (stat(buf, &statbuf) == 0 &&         /* it exists */
1045		    S_ISDIR(statbuf.st_mode) &&         /* it's a directory */
1046		    find_init_module(buf) &&            /* it has __init__.py */
1047		    case_ok(buf, len, namelen, name)) { /* and case matches */
1048			Py_XDECREF(copy);
1049			return &fd_package;
1050		}
1051#else
1052		/* XXX How are you going to test for directories? */
1053#ifdef RISCOS
1054		if (isdir(buf) &&
1055		    find_init_module(buf) &&
1056		    case_ok(buf, len, namelen, name)) {
1057			Py_XDECREF(copy);
1058			return &fd_package;
1059		}
1060#endif
1061#endif
1062#ifdef macintosh
1063		fdp = PyMac_FindModuleExtension(buf, &len, name);
1064		if (fdp) {
1065#else
1066#if defined(PYOS_OS2)
1067		/* take a snapshot of the module spec for restoration
1068		 * after the 8 character DLL hackery
1069		 */
1070		saved_buf = strdup(buf);
1071		saved_len = len;
1072		saved_namelen = namelen;
1073#endif /* PYOS_OS2 */
1074		for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1075#if defined(PYOS_OS2)
1076			/* OS/2 limits DLLs to 8 character names (w/o
1077			   extension)
1078			 * so if the name is longer than that and its a
1079			 * dynamically loaded module we're going to try,
1080			 * truncate the name before trying
1081			 */
1082			if (strlen(realname) > 8) {
1083				/* is this an attempt to load a C extension? */
1084				const struct filedescr *scan;
1085				scan = _PyImport_DynLoadFiletab;
1086				while (scan->suffix != NULL) {
1087					if (!strcmp(scan->suffix, fdp->suffix))
1088						break;
1089					else
1090						scan++;
1091				}
1092				if (scan->suffix != NULL) {
1093					/* yes, so truncate the name */
1094					namelen = 8;
1095					len -= strlen(realname) - namelen;
1096					buf[len] = '\0';
1097				}
1098			}
1099#endif /* PYOS_OS2 */
1100			strcpy(buf+len, fdp->suffix);
1101			if (Py_VerboseFlag > 1)
1102				PySys_WriteStderr("# trying %s\n", buf);
1103#endif /* !macintosh */
1104			filemode = fdp->mode;
1105			if (filemode[0] == 'U')
1106				filemode = "r" PY_STDIOTEXTMODE;
1107			fp = fopen(buf, filemode);
1108			if (fp != NULL) {
1109				if (case_ok(buf, len, namelen, name))
1110					break;
1111				else {	 /* continue search */
1112					fclose(fp);
1113					fp = NULL;
1114				}
1115			}
1116#if defined(PYOS_OS2)
1117			/* restore the saved snapshot */
1118			strcpy(buf, saved_buf);
1119			len = saved_len;
1120			namelen = saved_namelen;
1121#endif
1122		}
1123#if defined(PYOS_OS2)
1124		/* don't need/want the module name snapshot anymore */
1125		if (saved_buf)
1126		{
1127			free(saved_buf);
1128			saved_buf = NULL;
1129		}
1130#endif
1131		Py_XDECREF(copy);
1132		if (fp != NULL)
1133			break;
1134	}
1135	if (fp == NULL) {
1136		PyErr_Format(PyExc_ImportError,
1137			     "No module named %.200s", name);
1138		return NULL;
1139	}
1140	*p_fp = fp;
1141	return fdp;
1142}
1143
1144/* case_ok(char* buf, int len, int namelen, char* name)
1145 * The arguments here are tricky, best shown by example:
1146 *    /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1147 *    ^                      ^                   ^    ^
1148 *    |--------------------- buf ---------------------|
1149 *    |------------------- len ------------------|
1150 *                           |------ name -------|
1151 *                           |----- namelen -----|
1152 * buf is the full path, but len only counts up to (& exclusive of) the
1153 * extension.  name is the module name, also exclusive of extension.
1154 *
1155 * We've already done a successful stat() or fopen() on buf, so know that
1156 * there's some match, possibly case-insensitive.
1157 *
1158 * case_ok() is to return 1 if there's a case-sensitive match for
1159 * name, else 0.  case_ok() is also to return 1 if envar PYTHONCASEOK
1160 * exists.
1161 *
1162 * case_ok() is used to implement case-sensitive import semantics even
1163 * on platforms with case-insensitive filesystems.  It's trivial to implement
1164 * for case-sensitive filesystems.  It's pretty much a cross-platform
1165 * nightmare for systems with case-insensitive filesystems.
1166 */
1167
1168/* First we may need a pile of platform-specific header files; the sequence
1169 * of #if's here should match the sequence in the body of case_ok().
1170 */
1171#if defined(MS_WINDOWS) || defined(__CYGWIN__)
1172#include <windows.h>
1173#ifdef __CYGWIN__
1174#include <sys/cygwin.h>
1175#endif
1176
1177#elif defined(DJGPP)
1178#include <dir.h>
1179
1180#elif defined(macintosh)
1181#include <TextUtils.h>
1182
1183#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
1184#include <sys/types.h>
1185#include <dirent.h>
1186
1187#elif defined(PYOS_OS2)
1188#define INCL_DOS
1189#define INCL_DOSERRORS
1190#define INCL_NOPMAPI
1191#include <os2.h>
1192
1193#elif defined(RISCOS)
1194#include "oslib/osfscontrol.h"
1195#endif
1196
1197static int
1198case_ok(char *buf, int len, int namelen, char *name)
1199{
1200/* Pick a platform-specific implementation; the sequence of #if's here should
1201 * match the sequence just above.
1202 */
1203
1204/* MS_WINDOWS || __CYGWIN__ */
1205#if defined(MS_WINDOWS) || defined(__CYGWIN__)
1206	WIN32_FIND_DATA data;
1207	HANDLE h;
1208#ifdef __CYGWIN__
1209	char tempbuf[MAX_PATH];
1210#endif
1211
1212	if (Py_GETENV("PYTHONCASEOK") != NULL)
1213		return 1;
1214
1215#ifdef __CYGWIN__
1216	cygwin32_conv_to_win32_path(buf, tempbuf);
1217	h = FindFirstFile(tempbuf, &data);
1218#else
1219	h = FindFirstFile(buf, &data);
1220#endif
1221	if (h == INVALID_HANDLE_VALUE) {
1222		PyErr_Format(PyExc_NameError,
1223		  "Can't find file for module %.100s\n(filename %.300s)",
1224		  name, buf);
1225		return 0;
1226	}
1227	FindClose(h);
1228	return strncmp(data.cFileName, name, namelen) == 0;
1229
1230/* DJGPP */
1231#elif defined(DJGPP)
1232	struct ffblk ffblk;
1233	int done;
1234
1235	if (Py_GETENV("PYTHONCASEOK") != NULL)
1236		return 1;
1237
1238	done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1239	if (done) {
1240		PyErr_Format(PyExc_NameError,
1241		  "Can't find file for module %.100s\n(filename %.300s)",
1242		  name, buf);
1243		return 0;
1244	}
1245	return strncmp(ffblk.ff_name, name, namelen) == 0;
1246
1247/* macintosh */
1248#elif defined(macintosh)
1249	FSSpec fss;
1250	OSErr err;
1251
1252	if (Py_GETENV("PYTHONCASEOK") != NULL)
1253		return 1;
1254
1255	err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1256	if (err) {
1257		PyErr_Format(PyExc_NameError,
1258		     "Can't find file for module %.100s\n(filename %.300s)",
1259		     name, buf);
1260		return 0;
1261	}
1262	return fss.name[0] >= namelen &&
1263	       strncmp(name, (char *)fss.name+1, namelen) == 0;
1264
1265/* new-fangled macintosh (macosx) */
1266#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
1267	DIR *dirp;
1268	struct dirent *dp;
1269	char dirname[MAXPATHLEN + 1];
1270	const int dirlen = len - namelen - 1; /* don't want trailing SEP */
1271
1272	if (Py_GETENV("PYTHONCASEOK") != NULL)
1273		return 1;
1274
1275	/* Copy the dir component into dirname; substitute "." if empty */
1276	if (dirlen <= 0) {
1277		dirname[0] = '.';
1278		dirname[1] = '\0';
1279	}
1280	else {
1281		assert(dirlen <= MAXPATHLEN);
1282		memcpy(dirname, buf, dirlen);
1283		dirname[dirlen] = '\0';
1284	}
1285	/* Open the directory and search the entries for an exact match. */
1286	dirp = opendir(dirname);
1287	if (dirp) {
1288		char *nameWithExt = buf + len - namelen;
1289		while ((dp = readdir(dirp)) != NULL) {
1290			const int thislen =
1291#ifdef _DIRENT_HAVE_D_NAMELEN
1292						dp->d_namlen;
1293#else
1294						strlen(dp->d_name);
1295#endif
1296			if (thislen >= namelen &&
1297			    strcmp(dp->d_name, nameWithExt) == 0) {
1298				(void)closedir(dirp);
1299				return 1; /* Found */
1300			}
1301		}
1302		(void)closedir(dirp);
1303	}
1304	return 0 ; /* Not found */
1305
1306/* RISC OS */
1307#elif defined(RISCOS)
1308	char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1309	char buf2[MAXPATHLEN+2];
1310	char *nameWithExt = buf+len-namelen;
1311	int canonlen;
1312	os_error *e;
1313
1314	if (Py_GETENV("PYTHONCASEOK") != NULL)
1315		return 1;
1316
1317	/* workaround:
1318	   append wildcard, otherwise case of filename wouldn't be touched */
1319	strcpy(buf2, buf);
1320	strcat(buf2, "*");
1321
1322	e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1323	canonlen = MAXPATHLEN+1-canonlen;
1324	if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1325		return 0;
1326	if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1327		return 1; /* match */
1328
1329	return 0;
1330
1331/* OS/2 */
1332#elif defined(PYOS_OS2)
1333	HDIR hdir = 1;
1334	ULONG srchcnt = 1;
1335	FILEFINDBUF3 ffbuf;
1336	APIRET rc;
1337
1338	if (getenv("PYTHONCASEOK") != NULL)
1339		return 1;
1340
1341	rc = DosFindFirst(buf,
1342			  &hdir,
1343			  FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1344			  &ffbuf, sizeof(ffbuf),
1345			  &srchcnt,
1346			  FIL_STANDARD);
1347	if (rc != NO_ERROR)
1348		return 0;
1349	return strncmp(ffbuf.achName, name, namelen) == 0;
1350
1351/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1352#else
1353	return 1;
1354
1355#endif
1356}
1357
1358
1359#ifdef HAVE_STAT
1360/* Helper to look for __init__.py or __init__.py[co] in potential package */
1361static int
1362find_init_module(char *buf)
1363{
1364	const size_t save_len = strlen(buf);
1365	size_t i = save_len;
1366	char *pname;  /* pointer to start of __init__ */
1367	struct stat statbuf;
1368
1369/*	For calling case_ok(buf, len, namelen, name):
1370 *	/a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1371 *	^                      ^                   ^    ^
1372 *	|--------------------- buf ---------------------|
1373 *	|------------------- len ------------------|
1374 *	                       |------ name -------|
1375 *	                       |----- namelen -----|
1376 */
1377	if (save_len + 13 >= MAXPATHLEN)
1378		return 0;
1379	buf[i++] = SEP;
1380	pname = buf + i;
1381	strcpy(pname, "__init__.py");
1382	if (stat(buf, &statbuf) == 0) {
1383		if (case_ok(buf,
1384			    save_len + 9,	/* len("/__init__") */
1385		            8,   		/* len("__init__") */
1386		            pname)) {
1387			buf[save_len] = '\0';
1388			return 1;
1389		}
1390	}
1391	i += strlen(pname);
1392	strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
1393	if (stat(buf, &statbuf) == 0) {
1394		if (case_ok(buf,
1395			    save_len + 9,	/* len("/__init__") */
1396		            8,   		/* len("__init__") */
1397		            pname)) {
1398			buf[save_len] = '\0';
1399			return 1;
1400		}
1401	}
1402	buf[save_len] = '\0';
1403	return 0;
1404}
1405
1406#else
1407
1408#ifdef RISCOS
1409static int
1410find_init_module(buf)
1411	char *buf;
1412{
1413	int save_len = strlen(buf);
1414	int i = save_len;
1415
1416	if (save_len + 13 >= MAXPATHLEN)
1417		return 0;
1418	buf[i++] = SEP;
1419	strcpy(buf+i, "__init__/py");
1420	if (isfile(buf)) {
1421		buf[save_len] = '\0';
1422		return 1;
1423	}
1424
1425	if (Py_OptimizeFlag)
1426		strcpy(buf+i, "o");
1427	else
1428		strcpy(buf+i, "c");
1429	if (isfile(buf)) {
1430		buf[save_len] = '\0';
1431		return 1;
1432	}
1433	buf[save_len] = '\0';
1434	return 0;
1435}
1436#endif /*RISCOS*/
1437
1438#endif /* HAVE_STAT */
1439
1440
1441static int init_builtin(char *); /* Forward */
1442
1443/* Load an external module using the default search path and return
1444   its module object WITH INCREMENTED REFERENCE COUNT */
1445
1446static PyObject *
1447load_module(char *name, FILE *fp, char *buf, int type)
1448{
1449	PyObject *modules;
1450	PyObject *m;
1451	int err;
1452
1453	/* First check that there's an open file (if we need one)  */
1454	switch (type) {
1455	case PY_SOURCE:
1456	case PY_COMPILED:
1457		if (fp == NULL) {
1458			PyErr_Format(PyExc_ValueError,
1459			   "file object required for import (type code %d)",
1460				     type);
1461			return NULL;
1462		}
1463	}
1464
1465	switch (type) {
1466
1467	case PY_SOURCE:
1468		m = load_source_module(name, buf, fp);
1469		break;
1470
1471	case PY_COMPILED:
1472		m = load_compiled_module(name, buf, fp);
1473		break;
1474
1475#ifdef HAVE_DYNAMIC_LOADING
1476	case C_EXTENSION:
1477		m = _PyImport_LoadDynamicModule(name, buf, fp);
1478		break;
1479#endif
1480
1481#ifdef macintosh
1482	case PY_RESOURCE:
1483		m = PyMac_LoadResourceModule(name, buf);
1484		break;
1485	case PY_CODERESOURCE:
1486		m = PyMac_LoadCodeResourceModule(name, buf);
1487		break;
1488#endif
1489
1490	case PKG_DIRECTORY:
1491		m = load_package(name, buf);
1492		break;
1493
1494	case C_BUILTIN:
1495	case PY_FROZEN:
1496		if (buf != NULL && buf[0] != '\0')
1497			name = buf;
1498		if (type == C_BUILTIN)
1499			err = init_builtin(name);
1500		else
1501			err = PyImport_ImportFrozenModule(name);
1502		if (err < 0)
1503			return NULL;
1504		if (err == 0) {
1505			PyErr_Format(PyExc_ImportError,
1506				     "Purported %s module %.200s not found",
1507				     type == C_BUILTIN ?
1508						"builtin" : "frozen",
1509				     name);
1510			return NULL;
1511		}
1512		modules = PyImport_GetModuleDict();
1513		m = PyDict_GetItemString(modules, name);
1514		if (m == NULL) {
1515			PyErr_Format(
1516				PyExc_ImportError,
1517				"%s module %.200s not properly initialized",
1518				type == C_BUILTIN ?
1519					"builtin" : "frozen",
1520				name);
1521			return NULL;
1522		}
1523		Py_INCREF(m);
1524		break;
1525
1526	default:
1527		PyErr_Format(PyExc_ImportError,
1528			     "Don't know how to import %.200s (type code %d)",
1529			      name, type);
1530		m = NULL;
1531
1532	}
1533
1534	return m;
1535}
1536
1537
1538/* Initialize a built-in module.
1539   Return 1 for succes, 0 if the module is not found, and -1 with
1540   an exception set if the initialization failed. */
1541
1542static int
1543init_builtin(char *name)
1544{
1545	struct _inittab *p;
1546
1547	if (_PyImport_FindExtension(name, name) != NULL)
1548		return 1;
1549
1550	for (p = PyImport_Inittab; p->name != NULL; p++) {
1551		if (strcmp(name, p->name) == 0) {
1552			if (p->initfunc == NULL) {
1553				PyErr_Format(PyExc_ImportError,
1554				    "Cannot re-init internal module %.200s",
1555				    name);
1556				return -1;
1557			}
1558			if (Py_VerboseFlag)
1559				PySys_WriteStderr("import %s # builtin\n", name);
1560			(*p->initfunc)();
1561			if (PyErr_Occurred())
1562				return -1;
1563			if (_PyImport_FixupExtension(name, name) == NULL)
1564				return -1;
1565			return 1;
1566		}
1567	}
1568	return 0;
1569}
1570
1571
1572/* Frozen modules */
1573
1574static struct _frozen *
1575find_frozen(char *name)
1576{
1577	struct _frozen *p;
1578
1579	for (p = PyImport_FrozenModules; ; p++) {
1580		if (p->name == NULL)
1581			return NULL;
1582		if (strcmp(p->name, name) == 0)
1583			break;
1584	}
1585	return p;
1586}
1587
1588static PyObject *
1589get_frozen_object(char *name)
1590{
1591	struct _frozen *p = find_frozen(name);
1592	int size;
1593
1594	if (p == NULL) {
1595		PyErr_Format(PyExc_ImportError,
1596			     "No such frozen object named %.200s",
1597			     name);
1598		return NULL;
1599	}
1600	if (p->code == NULL) {
1601		PyErr_Format(PyExc_ImportError,
1602			     "Excluded frozen object named %.200s",
1603			     name);
1604		return NULL;
1605	}
1606	size = p->size;
1607	if (size < 0)
1608		size = -size;
1609	return PyMarshal_ReadObjectFromString((char *)p->code, size);
1610}
1611
1612/* Initialize a frozen module.
1613   Return 1 for succes, 0 if the module is not found, and -1 with
1614   an exception set if the initialization failed.
1615   This function is also used from frozenmain.c */
1616
1617int
1618PyImport_ImportFrozenModule(char *name)
1619{
1620	struct _frozen *p = find_frozen(name);
1621	PyObject *co;
1622	PyObject *m;
1623	int ispackage;
1624	int size;
1625
1626	if (p == NULL)
1627		return 0;
1628	if (p->code == NULL) {
1629		PyErr_Format(PyExc_ImportError,
1630			     "Excluded frozen object named %.200s",
1631			     name);
1632		return -1;
1633	}
1634	size = p->size;
1635	ispackage = (size < 0);
1636	if (ispackage)
1637		size = -size;
1638	if (Py_VerboseFlag)
1639		PySys_WriteStderr("import %s # frozen%s\n",
1640			name, ispackage ? " package" : "");
1641	co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1642	if (co == NULL)
1643		return -1;
1644	if (!PyCode_Check(co)) {
1645		Py_DECREF(co);
1646		PyErr_Format(PyExc_TypeError,
1647			     "frozen object %.200s is not a code object",
1648			     name);
1649		return -1;
1650	}
1651	if (ispackage) {
1652		/* Set __path__ to the package name */
1653		PyObject *d, *s;
1654		int err;
1655		m = PyImport_AddModule(name);
1656		if (m == NULL)
1657			return -1;
1658		d = PyModule_GetDict(m);
1659		s = PyString_InternFromString(name);
1660		if (s == NULL)
1661			return -1;
1662		err = PyDict_SetItemString(d, "__path__", s);
1663		Py_DECREF(s);
1664		if (err != 0)
1665			return err;
1666	}
1667	m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
1668	Py_DECREF(co);
1669	if (m == NULL)
1670		return -1;
1671	Py_DECREF(m);
1672	return 1;
1673}
1674
1675
1676/* Import a module, either built-in, frozen, or external, and return
1677   its module object WITH INCREMENTED REFERENCE COUNT */
1678
1679PyObject *
1680PyImport_ImportModule(char *name)
1681{
1682	PyObject *pname;
1683	PyObject *result;
1684
1685	pname = PyString_FromString(name);
1686	result = PyImport_Import(pname);
1687	Py_DECREF(pname);
1688	return result;
1689}
1690
1691/* Forward declarations for helper routines */
1692static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1693static PyObject *load_next(PyObject *mod, PyObject *altmod,
1694			   char **p_name, char *buf, int *p_buflen);
1695static int mark_miss(char *name);
1696static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1697			   char *buf, int buflen, int recursive);
1698static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
1699
1700/* The Magnum Opus of dotted-name import :-) */
1701
1702static PyObject *
1703import_module_ex(char *name, PyObject *globals, PyObject *locals,
1704		 PyObject *fromlist)
1705{
1706	char buf[MAXPATHLEN+1];
1707	int buflen = 0;
1708	PyObject *parent, *head, *next, *tail;
1709
1710	parent = get_parent(globals, buf, &buflen);
1711	if (parent == NULL)
1712		return NULL;
1713
1714	head = load_next(parent, Py_None, &name, buf, &buflen);
1715	if (head == NULL)
1716		return NULL;
1717
1718	tail = head;
1719	Py_INCREF(tail);
1720	while (name) {
1721		next = load_next(tail, tail, &name, buf, &buflen);
1722		Py_DECREF(tail);
1723		if (next == NULL) {
1724			Py_DECREF(head);
1725			return NULL;
1726		}
1727		tail = next;
1728	}
1729
1730	if (fromlist != NULL) {
1731		if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1732			fromlist = NULL;
1733	}
1734
1735	if (fromlist == NULL) {
1736		Py_DECREF(tail);
1737		return head;
1738	}
1739
1740	Py_DECREF(head);
1741	if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
1742		Py_DECREF(tail);
1743		return NULL;
1744	}
1745
1746	return tail;
1747}
1748
1749PyObject *
1750PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1751			PyObject *fromlist)
1752{
1753	PyObject *result;
1754	lock_import();
1755	result = import_module_ex(name, globals, locals, fromlist);
1756	unlock_import();
1757	return result;
1758}
1759
1760static PyObject *
1761get_parent(PyObject *globals, char *buf, int *p_buflen)
1762{
1763	static PyObject *namestr = NULL;
1764	static PyObject *pathstr = NULL;
1765	PyObject *modname, *modpath, *modules, *parent;
1766
1767	if (globals == NULL || !PyDict_Check(globals))
1768		return Py_None;
1769
1770	if (namestr == NULL) {
1771		namestr = PyString_InternFromString("__name__");
1772		if (namestr == NULL)
1773			return NULL;
1774	}
1775	if (pathstr == NULL) {
1776		pathstr = PyString_InternFromString("__path__");
1777		if (pathstr == NULL)
1778			return NULL;
1779	}
1780
1781	*buf = '\0';
1782	*p_buflen = 0;
1783	modname = PyDict_GetItem(globals, namestr);
1784	if (modname == NULL || !PyString_Check(modname))
1785		return Py_None;
1786
1787	modpath = PyDict_GetItem(globals, pathstr);
1788	if (modpath != NULL) {
1789		int len = PyString_GET_SIZE(modname);
1790		if (len > MAXPATHLEN) {
1791			PyErr_SetString(PyExc_ValueError,
1792					"Module name too long");
1793			return NULL;
1794		}
1795		strcpy(buf, PyString_AS_STRING(modname));
1796		*p_buflen = len;
1797	}
1798	else {
1799		char *start = PyString_AS_STRING(modname);
1800		char *lastdot = strrchr(start, '.');
1801		size_t len;
1802		if (lastdot == NULL)
1803			return Py_None;
1804		len = lastdot - start;
1805		if (len >= MAXPATHLEN) {
1806			PyErr_SetString(PyExc_ValueError,
1807					"Module name too long");
1808			return NULL;
1809		}
1810		strncpy(buf, start, len);
1811		buf[len] = '\0';
1812		*p_buflen = len;
1813	}
1814
1815	modules = PyImport_GetModuleDict();
1816	parent = PyDict_GetItemString(modules, buf);
1817	if (parent == NULL)
1818		parent = Py_None;
1819	return parent;
1820	/* We expect, but can't guarantee, if parent != None, that:
1821	   - parent.__name__ == buf
1822	   - parent.__dict__ is globals
1823	   If this is violated...  Who cares? */
1824}
1825
1826/* altmod is either None or same as mod */
1827static PyObject *
1828load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1829	  int *p_buflen)
1830{
1831	char *name = *p_name;
1832	char *dot = strchr(name, '.');
1833	size_t len;
1834	char *p;
1835	PyObject *result;
1836
1837	if (dot == NULL) {
1838		*p_name = NULL;
1839		len = strlen(name);
1840	}
1841	else {
1842		*p_name = dot+1;
1843		len = dot-name;
1844	}
1845	if (len == 0) {
1846		PyErr_SetString(PyExc_ValueError,
1847				"Empty module name");
1848		return NULL;
1849	}
1850
1851	p = buf + *p_buflen;
1852	if (p != buf)
1853		*p++ = '.';
1854	if (p+len-buf >= MAXPATHLEN) {
1855		PyErr_SetString(PyExc_ValueError,
1856				"Module name too long");
1857		return NULL;
1858	}
1859	strncpy(p, name, len);
1860	p[len] = '\0';
1861	*p_buflen = p+len-buf;
1862
1863	result = import_submodule(mod, p, buf);
1864	if (result == Py_None && altmod != mod) {
1865		Py_DECREF(result);
1866		/* Here, altmod must be None and mod must not be None */
1867		result = import_submodule(altmod, p, p);
1868		if (result != NULL && result != Py_None) {
1869			if (mark_miss(buf) != 0) {
1870				Py_DECREF(result);
1871				return NULL;
1872			}
1873			strncpy(buf, name, len);
1874			buf[len] = '\0';
1875			*p_buflen = len;
1876		}
1877	}
1878	if (result == NULL)
1879		return NULL;
1880
1881	if (result == Py_None) {
1882		Py_DECREF(result);
1883		PyErr_Format(PyExc_ImportError,
1884			     "No module named %.200s", name);
1885		return NULL;
1886	}
1887
1888	return result;
1889}
1890
1891static int
1892mark_miss(char *name)
1893{
1894	PyObject *modules = PyImport_GetModuleDict();
1895	return PyDict_SetItemString(modules, name, Py_None);
1896}
1897
1898static int
1899ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1900		int recursive)
1901{
1902	int i;
1903
1904	if (!PyObject_HasAttrString(mod, "__path__"))
1905		return 1;
1906
1907	for (i = 0; ; i++) {
1908		PyObject *item = PySequence_GetItem(fromlist, i);
1909		int hasit;
1910		if (item == NULL) {
1911			if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1912				PyErr_Clear();
1913				return 1;
1914			}
1915			return 0;
1916		}
1917		if (!PyString_Check(item)) {
1918			PyErr_SetString(PyExc_TypeError,
1919					"Item in ``from list'' not a string");
1920			Py_DECREF(item);
1921			return 0;
1922		}
1923		if (PyString_AS_STRING(item)[0] == '*') {
1924			PyObject *all;
1925			Py_DECREF(item);
1926			/* See if the package defines __all__ */
1927			if (recursive)
1928				continue; /* Avoid endless recursion */
1929			all = PyObject_GetAttrString(mod, "__all__");
1930			if (all == NULL)
1931				PyErr_Clear();
1932			else {
1933				if (!ensure_fromlist(mod, all, buf, buflen, 1))
1934					return 0;
1935				Py_DECREF(all);
1936			}
1937			continue;
1938		}
1939		hasit = PyObject_HasAttr(mod, item);
1940		if (!hasit) {
1941			char *subname = PyString_AS_STRING(item);
1942			PyObject *submod;
1943			char *p;
1944			if (buflen + strlen(subname) >= MAXPATHLEN) {
1945				PyErr_SetString(PyExc_ValueError,
1946						"Module name too long");
1947				Py_DECREF(item);
1948				return 0;
1949			}
1950			p = buf + buflen;
1951			*p++ = '.';
1952			strcpy(p, subname);
1953			submod = import_submodule(mod, subname, buf);
1954			Py_XDECREF(submod);
1955			if (submod == NULL) {
1956				Py_DECREF(item);
1957				return 0;
1958			}
1959		}
1960		Py_DECREF(item);
1961	}
1962
1963	/* NOTREACHED */
1964}
1965
1966static PyObject *
1967import_submodule(PyObject *mod, char *subname, char *fullname)
1968{
1969	PyObject *modules = PyImport_GetModuleDict();
1970	PyObject *m, *res = NULL;
1971
1972	/* Require:
1973	   if mod == None: subname == fullname
1974	   else: mod.__name__ + "." + subname == fullname
1975	*/
1976
1977	if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
1978		Py_INCREF(m);
1979	}
1980	else {
1981		PyObject *path;
1982		char buf[MAXPATHLEN+1];
1983		struct filedescr *fdp;
1984		FILE *fp = NULL;
1985
1986		if (mod == Py_None)
1987			path = NULL;
1988		else {
1989			path = PyObject_GetAttrString(mod, "__path__");
1990			if (path == NULL) {
1991				PyErr_Clear();
1992				Py_INCREF(Py_None);
1993				return Py_None;
1994			}
1995		}
1996
1997		buf[0] = '\0';
1998		fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1999		Py_XDECREF(path);
2000		if (fdp == NULL) {
2001			if (!PyErr_ExceptionMatches(PyExc_ImportError))
2002				return NULL;
2003			PyErr_Clear();
2004			Py_INCREF(Py_None);
2005			return Py_None;
2006		}
2007		m = load_module(fullname, fp, buf, fdp->type);
2008		if (fp)
2009			fclose(fp);
2010		if (mod != Py_None) {
2011			/* Irrespective of the success of this load, make a
2012			   reference to it in the parent package module.
2013			   A copy gets saved in the modules dictionary
2014			   under the full name, so get a reference from
2015			   there, if need be.  (The exception is when
2016			   the load failed with a SyntaxError -- then
2017			   there's no trace in sys.modules.  In that case,
2018			   of course, do nothing extra.) */
2019			res = m;
2020			if (res == NULL)
2021				res = PyDict_GetItemString(modules, fullname);
2022			if (res != NULL &&
2023			    PyObject_SetAttrString(mod, subname, res) < 0) {
2024				Py_XDECREF(m);
2025				m = NULL;
2026			}
2027		}
2028	}
2029
2030	return m;
2031}
2032
2033
2034/* Re-import a module of any kind and return its module object, WITH
2035   INCREMENTED REFERENCE COUNT */
2036
2037PyObject *
2038PyImport_ReloadModule(PyObject *m)
2039{
2040	PyObject *modules = PyImport_GetModuleDict();
2041	PyObject *path = NULL;
2042	char *name, *subname;
2043	char buf[MAXPATHLEN+1];
2044	struct filedescr *fdp;
2045	FILE *fp = NULL;
2046
2047	if (m == NULL || !PyModule_Check(m)) {
2048		PyErr_SetString(PyExc_TypeError,
2049				"reload() argument must be module");
2050		return NULL;
2051	}
2052	name = PyModule_GetName(m);
2053	if (name == NULL)
2054		return NULL;
2055	if (m != PyDict_GetItemString(modules, name)) {
2056		PyErr_Format(PyExc_ImportError,
2057			     "reload(): module %.200s not in sys.modules",
2058			     name);
2059		return NULL;
2060	}
2061	subname = strrchr(name, '.');
2062	if (subname == NULL)
2063		subname = name;
2064	else {
2065		PyObject *parentname, *parent;
2066		parentname = PyString_FromStringAndSize(name, (subname-name));
2067		if (parentname == NULL)
2068			return NULL;
2069		parent = PyDict_GetItem(modules, parentname);
2070		Py_DECREF(parentname);
2071		if (parent == NULL) {
2072			PyErr_Format(PyExc_ImportError,
2073			    "reload(): parent %.200s not in sys.modules",
2074			    name);
2075			return NULL;
2076		}
2077		subname++;
2078		path = PyObject_GetAttrString(parent, "__path__");
2079		if (path == NULL)
2080			PyErr_Clear();
2081	}
2082	buf[0] = '\0';
2083	fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
2084	Py_XDECREF(path);
2085	if (fdp == NULL)
2086		return NULL;
2087	m = load_module(name, fp, buf, fdp->type);
2088	if (fp)
2089		fclose(fp);
2090	return m;
2091}
2092
2093
2094/* Higher-level import emulator which emulates the "import" statement
2095   more accurately -- it invokes the __import__() function from the
2096   builtins of the current globals.  This means that the import is
2097   done using whatever import hooks are installed in the current
2098   environment, e.g. by "rexec".
2099   A dummy list ["__doc__"] is passed as the 4th argument so that
2100   e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2101   will return <module "gencache"> instead of <module "win32com">. */
2102
2103PyObject *
2104PyImport_Import(PyObject *module_name)
2105{
2106	static PyObject *silly_list = NULL;
2107	static PyObject *builtins_str = NULL;
2108	static PyObject *import_str = NULL;
2109	PyObject *globals = NULL;
2110	PyObject *import = NULL;
2111	PyObject *builtins = NULL;
2112	PyObject *r = NULL;
2113
2114	/* Initialize constant string objects */
2115	if (silly_list == NULL) {
2116		import_str = PyString_InternFromString("__import__");
2117		if (import_str == NULL)
2118			return NULL;
2119		builtins_str = PyString_InternFromString("__builtins__");
2120		if (builtins_str == NULL)
2121			return NULL;
2122		silly_list = Py_BuildValue("[s]", "__doc__");
2123		if (silly_list == NULL)
2124			return NULL;
2125	}
2126
2127	/* Get the builtins from current globals */
2128	globals = PyEval_GetGlobals();
2129	if (globals != NULL) {
2130	        Py_INCREF(globals);
2131		builtins = PyObject_GetItem(globals, builtins_str);
2132		if (builtins == NULL)
2133			goto err;
2134	}
2135	else {
2136		/* No globals -- use standard builtins, and fake globals */
2137		PyErr_Clear();
2138
2139		builtins = PyImport_ImportModuleEx("__builtin__",
2140						   NULL, NULL, NULL);
2141		if (builtins == NULL)
2142			return NULL;
2143		globals = Py_BuildValue("{OO}", builtins_str, builtins);
2144		if (globals == NULL)
2145			goto err;
2146	}
2147
2148	/* Get the __import__ function from the builtins */
2149	if (PyDict_Check(builtins)) {
2150		import = PyObject_GetItem(builtins, import_str);
2151		if (import == NULL)
2152			PyErr_SetObject(PyExc_KeyError, import_str);
2153	}
2154	else
2155		import = PyObject_GetAttr(builtins, import_str);
2156	if (import == NULL)
2157		goto err;
2158
2159	/* Call the _import__ function with the proper argument list */
2160	r = PyObject_CallFunction(import, "OOOO",
2161				  module_name, globals, globals, silly_list);
2162
2163  err:
2164	Py_XDECREF(globals);
2165	Py_XDECREF(builtins);
2166	Py_XDECREF(import);
2167
2168	return r;
2169}
2170
2171
2172/* Module 'imp' provides Python access to the primitives used for
2173   importing modules.
2174*/
2175
2176static PyObject *
2177imp_get_magic(PyObject *self, PyObject *args)
2178{
2179	char buf[4];
2180
2181	if (!PyArg_ParseTuple(args, ":get_magic"))
2182		return NULL;
2183	buf[0] = (char) ((pyc_magic >>  0) & 0xff);
2184	buf[1] = (char) ((pyc_magic >>  8) & 0xff);
2185	buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2186	buf[3] = (char) ((pyc_magic >> 24) & 0xff);
2187
2188	return PyString_FromStringAndSize(buf, 4);
2189}
2190
2191static PyObject *
2192imp_get_suffixes(PyObject *self, PyObject *args)
2193{
2194	PyObject *list;
2195	struct filedescr *fdp;
2196
2197	if (!PyArg_ParseTuple(args, ":get_suffixes"))
2198		return NULL;
2199	list = PyList_New(0);
2200	if (list == NULL)
2201		return NULL;
2202	for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2203		PyObject *item = Py_BuildValue("ssi",
2204				       fdp->suffix, fdp->mode, fdp->type);
2205		if (item == NULL) {
2206			Py_DECREF(list);
2207			return NULL;
2208		}
2209		if (PyList_Append(list, item) < 0) {
2210			Py_DECREF(list);
2211			Py_DECREF(item);
2212			return NULL;
2213		}
2214		Py_DECREF(item);
2215	}
2216	return list;
2217}
2218
2219static PyObject *
2220call_find_module(char *name, PyObject *path)
2221{
2222	extern int fclose(FILE *);
2223	PyObject *fob, *ret;
2224	struct filedescr *fdp;
2225	char pathname[MAXPATHLEN+1];
2226	FILE *fp = NULL;
2227
2228	pathname[0] = '\0';
2229	if (path == Py_None)
2230		path = NULL;
2231	fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
2232	if (fdp == NULL)
2233		return NULL;
2234	if (fp != NULL) {
2235		fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2236		if (fob == NULL) {
2237			fclose(fp);
2238			return NULL;
2239		}
2240	}
2241	else {
2242		fob = Py_None;
2243		Py_INCREF(fob);
2244	}
2245	ret = Py_BuildValue("Os(ssi)",
2246		      fob, pathname, fdp->suffix, fdp->mode, fdp->type);
2247	Py_DECREF(fob);
2248	return ret;
2249}
2250
2251static PyObject *
2252imp_find_module(PyObject *self, PyObject *args)
2253{
2254	char *name;
2255	PyObject *path = NULL;
2256	if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
2257		return NULL;
2258	return call_find_module(name, path);
2259}
2260
2261static PyObject *
2262imp_init_builtin(PyObject *self, PyObject *args)
2263{
2264	char *name;
2265	int ret;
2266	PyObject *m;
2267	if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2268		return NULL;
2269	ret = init_builtin(name);
2270	if (ret < 0)
2271		return NULL;
2272	if (ret == 0) {
2273		Py_INCREF(Py_None);
2274		return Py_None;
2275	}
2276	m = PyImport_AddModule(name);
2277	Py_XINCREF(m);
2278	return m;
2279}
2280
2281static PyObject *
2282imp_init_frozen(PyObject *self, PyObject *args)
2283{
2284	char *name;
2285	int ret;
2286	PyObject *m;
2287	if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2288		return NULL;
2289	ret = PyImport_ImportFrozenModule(name);
2290	if (ret < 0)
2291		return NULL;
2292	if (ret == 0) {
2293		Py_INCREF(Py_None);
2294		return Py_None;
2295	}
2296	m = PyImport_AddModule(name);
2297	Py_XINCREF(m);
2298	return m;
2299}
2300
2301static PyObject *
2302imp_get_frozen_object(PyObject *self, PyObject *args)
2303{
2304	char *name;
2305
2306	if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2307		return NULL;
2308	return get_frozen_object(name);
2309}
2310
2311static PyObject *
2312imp_is_builtin(PyObject *self, PyObject *args)
2313{
2314	char *name;
2315	if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2316		return NULL;
2317	return PyInt_FromLong(is_builtin(name));
2318}
2319
2320static PyObject *
2321imp_is_frozen(PyObject *self, PyObject *args)
2322{
2323	char *name;
2324	struct _frozen *p;
2325	if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2326		return NULL;
2327	p = find_frozen(name);
2328	return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
2329}
2330
2331static FILE *
2332get_file(char *pathname, PyObject *fob, char *mode)
2333{
2334	FILE *fp;
2335	if (fob == NULL) {
2336		if (mode[0] == 'U')
2337			mode = "r" PY_STDIOTEXTMODE;
2338		fp = fopen(pathname, mode);
2339		if (fp == NULL)
2340			PyErr_SetFromErrno(PyExc_IOError);
2341	}
2342	else {
2343		fp = PyFile_AsFile(fob);
2344		if (fp == NULL)
2345			PyErr_SetString(PyExc_ValueError,
2346					"bad/closed file object");
2347	}
2348	return fp;
2349}
2350
2351static PyObject *
2352imp_load_compiled(PyObject *self, PyObject *args)
2353{
2354	char *name;
2355	char *pathname;
2356	PyObject *fob = NULL;
2357	PyObject *m;
2358	FILE *fp;
2359	if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
2360			      &PyFile_Type, &fob))
2361		return NULL;
2362	fp = get_file(pathname, fob, "rb");
2363	if (fp == NULL)
2364		return NULL;
2365	m = load_compiled_module(name, pathname, fp);
2366	if (fob == NULL)
2367		fclose(fp);
2368	return m;
2369}
2370
2371#ifdef HAVE_DYNAMIC_LOADING
2372
2373static PyObject *
2374imp_load_dynamic(PyObject *self, PyObject *args)
2375{
2376	char *name;
2377	char *pathname;
2378	PyObject *fob = NULL;
2379	PyObject *m;
2380	FILE *fp = NULL;
2381	if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
2382			      &PyFile_Type, &fob))
2383		return NULL;
2384	if (fob) {
2385		fp = get_file(pathname, fob, "r");
2386		if (fp == NULL)
2387			return NULL;
2388	}
2389	m = _PyImport_LoadDynamicModule(name, pathname, fp);
2390	return m;
2391}
2392
2393#endif /* HAVE_DYNAMIC_LOADING */
2394
2395static PyObject *
2396imp_load_source(PyObject *self, PyObject *args)
2397{
2398	char *name;
2399	char *pathname;
2400	PyObject *fob = NULL;
2401	PyObject *m;
2402	FILE *fp;
2403	if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
2404			      &PyFile_Type, &fob))
2405		return NULL;
2406	fp = get_file(pathname, fob, "r");
2407	if (fp == NULL)
2408		return NULL;
2409	m = load_source_module(name, pathname, fp);
2410	if (fob == NULL)
2411		fclose(fp);
2412	return m;
2413}
2414
2415#ifdef macintosh
2416static PyObject *
2417imp_load_resource(PyObject *self, PyObject *args)
2418{
2419	char *name;
2420	char *pathname;
2421	PyObject *m;
2422
2423	if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
2424		return NULL;
2425	m = PyMac_LoadResourceModule(name, pathname);
2426	return m;
2427}
2428#endif /* macintosh */
2429
2430static PyObject *
2431imp_load_module(PyObject *self, PyObject *args)
2432{
2433	char *name;
2434	PyObject *fob;
2435	char *pathname;
2436	char *suffix; /* Unused */
2437	char *mode;
2438	int type;
2439	FILE *fp;
2440
2441	if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
2442			      &name, &fob, &pathname,
2443			      &suffix, &mode, &type))
2444		return NULL;
2445	if (*mode) {
2446		/* Mode must start with 'r' or 'U' and must not contain '+'.
2447		   Implicit in this test is the assumption that the mode
2448		   may contain other modifiers like 'b' or 't'. */
2449
2450		if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
2451			PyErr_Format(PyExc_ValueError,
2452				     "invalid file open mode %.200s", mode);
2453			return NULL;
2454		}
2455	}
2456	if (fob == Py_None)
2457		fp = NULL;
2458	else {
2459		if (!PyFile_Check(fob)) {
2460			PyErr_SetString(PyExc_ValueError,
2461				"load_module arg#2 should be a file or None");
2462			return NULL;
2463		}
2464		fp = get_file(pathname, fob, mode);
2465		if (fp == NULL)
2466			return NULL;
2467	}
2468	return load_module(name, fp, pathname, type);
2469}
2470
2471static PyObject *
2472imp_load_package(PyObject *self, PyObject *args)
2473{
2474	char *name;
2475	char *pathname;
2476	if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
2477		return NULL;
2478	return load_package(name, pathname);
2479}
2480
2481static PyObject *
2482imp_new_module(PyObject *self, PyObject *args)
2483{
2484	char *name;
2485	if (!PyArg_ParseTuple(args, "s:new_module", &name))
2486		return NULL;
2487	return PyModule_New(name);
2488}
2489
2490/* Doc strings */
2491
2492PyDoc_STRVAR(doc_imp,
2493"This module provides the components needed to build your own\n\
2494__import__ function.  Undocumented functions are obsolete.");
2495
2496PyDoc_STRVAR(doc_find_module,
2497"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2498Search for a module.  If path is omitted or None, search for a\n\
2499built-in, frozen or special module and continue search in sys.path.\n\
2500The module name cannot contain '.'; to search for a submodule of a\n\
2501package, pass the submodule name and the package's __path__.");
2502
2503PyDoc_STRVAR(doc_load_module,
2504"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2505Load a module, given information returned by find_module().\n\
2506The module name must include the full package name, if any.");
2507
2508PyDoc_STRVAR(doc_get_magic,
2509"get_magic() -> string\n\
2510Return the magic number for .pyc or .pyo files.");
2511
2512PyDoc_STRVAR(doc_get_suffixes,
2513"get_suffixes() -> [(suffix, mode, type), ...]\n\
2514Return a list of (suffix, mode, type) tuples describing the files\n\
2515that find_module() looks for.");
2516
2517PyDoc_STRVAR(doc_new_module,
2518"new_module(name) -> module\n\
2519Create a new module.  Do not enter it in sys.modules.\n\
2520The module name must include the full package name, if any.");
2521
2522PyDoc_STRVAR(doc_lock_held,
2523"lock_held() -> 0 or 1\n\
2524Return 1 if the import lock is currently held.\n\
2525On platforms without threads, return 0.");
2526
2527static PyMethodDef imp_methods[] = {
2528	{"find_module",		imp_find_module, METH_VARARGS, doc_find_module},
2529	{"get_magic",		imp_get_magic,	 METH_VARARGS, doc_get_magic},
2530	{"get_suffixes",	imp_get_suffixes, METH_VARARGS, doc_get_suffixes},
2531	{"load_module",		imp_load_module, METH_VARARGS, doc_load_module},
2532	{"new_module",		imp_new_module,	 METH_VARARGS, doc_new_module},
2533	{"lock_held",		imp_lock_held,	 METH_VARARGS, doc_lock_held},
2534	/* The rest are obsolete */
2535	{"get_frozen_object",	imp_get_frozen_object,	METH_VARARGS},
2536	{"init_builtin",	imp_init_builtin,	METH_VARARGS},
2537	{"init_frozen",		imp_init_frozen,	METH_VARARGS},
2538	{"is_builtin",		imp_is_builtin,		METH_VARARGS},
2539	{"is_frozen",		imp_is_frozen,		METH_VARARGS},
2540	{"load_compiled",	imp_load_compiled,	METH_VARARGS},
2541#ifdef HAVE_DYNAMIC_LOADING
2542	{"load_dynamic",	imp_load_dynamic,	METH_VARARGS},
2543#endif
2544	{"load_package",	imp_load_package,	METH_VARARGS},
2545#ifdef macintosh
2546	{"load_resource",	imp_load_resource,	METH_VARARGS},
2547#endif
2548	{"load_source",		imp_load_source,	METH_VARARGS},
2549	{NULL,			NULL}		/* sentinel */
2550};
2551
2552static int
2553setint(PyObject *d, char *name, int value)
2554{
2555	PyObject *v;
2556	int err;
2557
2558	v = PyInt_FromLong((long)value);
2559	err = PyDict_SetItemString(d, name, v);
2560	Py_XDECREF(v);
2561	return err;
2562}
2563
2564void
2565initimp(void)
2566{
2567	PyObject *m, *d;
2568
2569	m = Py_InitModule4("imp", imp_methods, doc_imp,
2570			   NULL, PYTHON_API_VERSION);
2571	d = PyModule_GetDict(m);
2572
2573	if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2574	if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2575	if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2576	if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2577	if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2578	if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2579	if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2580	if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
2581	if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
2582
2583  failure:
2584	;
2585}
2586
2587
2588/* API for embedding applications that want to add their own entries
2589   to the table of built-in modules.  This should normally be called
2590   *before* Py_Initialize().  When the table resize fails, -1 is
2591   returned and the existing table is unchanged.
2592
2593   After a similar function by Just van Rossum. */
2594
2595int
2596PyImport_ExtendInittab(struct _inittab *newtab)
2597{
2598	static struct _inittab *our_copy = NULL;
2599	struct _inittab *p;
2600	int i, n;
2601
2602	/* Count the number of entries in both tables */
2603	for (n = 0; newtab[n].name != NULL; n++)
2604		;
2605	if (n == 0)
2606		return 0; /* Nothing to do */
2607	for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2608		;
2609
2610	/* Allocate new memory for the combined table */
2611	p = our_copy;
2612	PyMem_RESIZE(p, struct _inittab, i+n+1);
2613	if (p == NULL)
2614		return -1;
2615
2616	/* Copy the tables into the new memory */
2617	if (our_copy != PyImport_Inittab)
2618		memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2619	PyImport_Inittab = our_copy = p;
2620	memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2621
2622	return 0;
2623}
2624
2625/* Shorthand to add a single entry given a name and a function */
2626
2627int
2628PyImport_AppendInittab(char *name, void (*initfunc)(void))
2629{
2630	struct _inittab newtab[2];
2631
2632	memset(newtab, '\0', sizeof newtab);
2633
2634	newtab[0].name = name;
2635	newtab[0].initfunc = initfunc;
2636
2637	return PyImport_ExtendInittab(newtab);
2638}
2639