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