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