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