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