blob: fcdc46b6a4967986510eff9bffd0c7e309acf0f3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
7#include "pythonrun.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010#include "code.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000011#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000012#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000013#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000014#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000015
Guido van Rossum55a83382000-09-20 20:31:38 +000016#ifdef HAVE_FCNTL_H
17#include <fcntl.h>
18#endif
19
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000020extern time_t PyOS_GetLastModificationTime(char *, FILE *);
21 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000022
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000023/* Magic word to reject .pyc files generated by other Python versions.
24 It should change for each incompatible change to the bytecode.
25
26 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000027 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000028 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000029
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000030 The magic numbers must be spaced apart atleast 2 values, as the
31 -U interpeter flag will cause MAGIC+1 being used. They have been
32 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000033
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000034 There were a variety of old schemes for setting the magic number.
35 The current working scheme is to increment the previous value by
36 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000037
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000038 Known values:
39 Python 1.5: 20121
40 Python 1.5.1: 20121
41 Python 1.5.2: 20121
42 Python 2.0: 50823
43 Python 2.0.1: 50823
44 Python 2.1: 60202
45 Python 2.1.1: 60202
46 Python 2.1.2: 60202
47 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000048 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000049 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000050 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000051 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000052 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000053 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000054 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000055 Python 2.5a0: 62081 (ast-branch)
56.
Tim Peters36515e22001-11-18 04:06:29 +000057*/
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000058#define MAGIC (62081 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000059
Guido van Rossum96774c12000-05-01 20:19:08 +000060/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000061 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000062 compiler works which are enabled by command line switches. */
63static long pyc_magic = MAGIC;
64
Guido van Rossum25ce5661997-08-02 03:10:38 +000065/* See _PyImport_FixupExtension() below */
66static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000067
Guido van Rossum771c6c81997-10-31 18:37:24 +000068/* This table is defined in config.c: */
69extern struct _inittab _PyImport_Inittab[];
70
71struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000072
Guido van Rossumed1170e1999-12-20 21:23:41 +000073/* these tables define the module suffixes that Python recognizes */
74struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000075
76#ifdef RISCOS
77static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000078 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000079 {"/pyc", "rb", PY_COMPILED},
80 {0, 0}
81};
82#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000083static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000084 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000085#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000086 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000087#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000088 {".pyc", "rb", PY_COMPILED},
89 {0, 0}
90};
Guido van Rossum48a680c2001-03-02 06:34:14 +000091#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000092
Guido van Rossum1ae940a1995-01-02 19:04:15 +000093/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094
95void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000096_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumed1170e1999-12-20 21:23:41 +000098 const struct filedescr *scan;
99 struct filedescr *filetab;
100 int countD = 0;
101 int countS = 0;
102
103 /* prepare _PyImport_Filetab: copy entries from
104 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
105 */
106 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
107 ++countD;
108 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
109 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000111 memcpy(filetab, _PyImport_DynLoadFiletab,
112 countD * sizeof(struct filedescr));
113 memcpy(filetab + countD, _PyImport_StandardFiletab,
114 countS * sizeof(struct filedescr));
115 filetab[countD + countS].suffix = NULL;
116
117 _PyImport_Filetab = filetab;
118
Guido van Rossum0824f631997-03-11 18:37:35 +0000119 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000120 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
121 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000122#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000123 if (strcmp(filetab->suffix, ".pyc") == 0)
124 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000125#else
126 if (strcmp(filetab->suffix, "/pyc") == 0)
127 filetab->suffix = "/pyo";
128#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000129 }
130 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000131
132 if (Py_UnicodeFlag) {
133 /* Fix the pyc_magic so that byte compiled code created
134 using the all-Unicode method doesn't interfere with
135 code created in normal operation mode. */
136 pyc_magic = MAGIC + 1;
137 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138}
139
Guido van Rossum25ce5661997-08-02 03:10:38 +0000140void
Just van Rossum52e14d62002-12-30 22:08:05 +0000141_PyImportHooks_Init(void)
142{
143 PyObject *v, *path_hooks = NULL, *zimpimport;
144 int err = 0;
145
146 /* adding sys.path_hooks and sys.path_importer_cache, setting up
147 zipimport */
148
149 if (Py_VerboseFlag)
150 PySys_WriteStderr("# installing zipimport hook\n");
151
152 v = PyList_New(0);
153 if (v == NULL)
154 goto error;
155 err = PySys_SetObject("meta_path", v);
156 Py_DECREF(v);
157 if (err)
158 goto error;
159 v = PyDict_New();
160 if (v == NULL)
161 goto error;
162 err = PySys_SetObject("path_importer_cache", v);
163 Py_DECREF(v);
164 if (err)
165 goto error;
166 path_hooks = PyList_New(0);
167 if (path_hooks == NULL)
168 goto error;
169 err = PySys_SetObject("path_hooks", path_hooks);
170 if (err) {
171 error:
172 PyErr_Print();
173 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
174 "path_importer_cache failed");
175 }
176 zimpimport = PyImport_ImportModule("zipimport");
177 if (zimpimport == NULL) {
178 PyErr_Clear(); /* No zip import module -- okay */
179 if (Py_VerboseFlag)
180 PySys_WriteStderr("# can't import zipimport\n");
181 }
182 else {
183 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
184 "zipimporter");
185 Py_DECREF(zimpimport);
186 if (zipimporter == NULL) {
187 PyErr_Clear(); /* No zipimporter object -- okay */
188 if (Py_VerboseFlag)
189 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000190 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000191 }
192 else {
193 /* sys.path_hooks.append(zipimporter) */
194 err = PyList_Append(path_hooks, zipimporter);
195 Py_DECREF(zipimporter);
196 if (err)
197 goto error;
198 if (Py_VerboseFlag)
199 PySys_WriteStderr(
200 "# installed zipimport hook\n");
201 }
202 }
203 Py_DECREF(path_hooks);
204}
205
206void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208{
209 Py_XDECREF(extensions);
210 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000211 PyMem_DEL(_PyImport_Filetab);
212 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213}
214
215
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000216/* Locking primitives to prevent parallel imports of the same module
217 in different threads to return with a partially loaded module.
218 These calls are serialized by the global interpreter lock. */
219
220#ifdef WITH_THREAD
221
Guido van Rossum49b56061998-10-01 20:42:43 +0000222#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000223
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000225static long import_lock_thread = -1;
226static int import_lock_level = 0;
227
228static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000232 if (me == -1)
233 return; /* Too bad */
234 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000236 if (import_lock_thread == me) {
237 import_lock_level++;
238 return;
239 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000240 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
241 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000242 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000243 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000244 PyEval_RestoreThread(tstate);
245 }
246 import_lock_thread = me;
247 import_lock_level = 1;
248}
249
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000250static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000253 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000255 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000257 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258 import_lock_level--;
259 if (import_lock_level == 0) {
260 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000262 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000263 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000264}
265
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000266/* This function is called from PyOS_AfterFork to ensure that newly
267 created child processes do not share locks with the parent. */
268
269void
270_PyImport_ReInitLock(void)
271{
272#ifdef _AIX
273 if (import_lock != NULL)
274 import_lock = PyThread_allocate_lock();
275#endif
276}
277
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000278#else
279
280#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000281#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000282
283#endif
284
Tim Peters69232342001-08-30 05:16:13 +0000285static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000286imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000287{
Tim Peters69232342001-08-30 05:16:13 +0000288#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000289 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000290#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000291 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000292#endif
293}
294
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000296imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000297{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000298#ifdef WITH_THREAD
299 lock_import();
300#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000301 Py_INCREF(Py_None);
302 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000303}
304
305static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000306imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000307{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000308#ifdef WITH_THREAD
309 if (unlock_import() < 0) {
310 PyErr_SetString(PyExc_RuntimeError,
311 "not holding the import lock");
312 return NULL;
313 }
314#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000315 Py_INCREF(Py_None);
316 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000317}
318
Guido van Rossum25ce5661997-08-02 03:10:38 +0000319/* Helper for sys */
320
321PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000323{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000324 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000325 if (interp->modules == NULL)
326 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
327 return interp->modules;
328}
329
Guido van Rossum3f5da241990-12-20 15:06:42 +0000330
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331/* List of names to clear in sys */
332static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000333 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000334 "exc_type", "exc_value", "exc_traceback",
335 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000336 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000337 NULL
338};
339
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000340static char* sys_files[] = {
341 "stdin", "__stdin__",
342 "stdout", "__stdout__",
343 "stderr", "__stderr__",
344 NULL
345};
346
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000347
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000348/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000349
Guido van Rossum3f5da241990-12-20 15:06:42 +0000350void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000352{
Guido van Rossum758eec01998-01-19 21:58:26 +0000353 int pos, ndone;
354 char *name;
355 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000356 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000357 PyObject *modules = interp->modules;
358
359 if (modules == NULL)
360 return; /* Already done */
361
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000362 /* Delete some special variables first. These are common
363 places where user values hide and people complain when their
364 destructors fail. Since the modules containing them are
365 deleted *last* of all, they would come too late in the normal
366 destruction order. Sigh. */
367
368 value = PyDict_GetItemString(modules, "__builtin__");
369 if (value != NULL && PyModule_Check(value)) {
370 dict = PyModule_GetDict(value);
371 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000372 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000373 PyDict_SetItemString(dict, "_", Py_None);
374 }
375 value = PyDict_GetItemString(modules, "sys");
376 if (value != NULL && PyModule_Check(value)) {
377 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000378 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000379 dict = PyModule_GetDict(value);
380 for (p = sys_deletes; *p != NULL; p++) {
381 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000382 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000383 PyDict_SetItemString(dict, *p, Py_None);
384 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000385 for (p = sys_files; *p != NULL; p+=2) {
386 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000387 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000388 v = PyDict_GetItemString(dict, *(p+1));
389 if (v == NULL)
390 v = Py_None;
391 PyDict_SetItemString(dict, *p, v);
392 }
393 }
394
395 /* First, delete __main__ */
396 value = PyDict_GetItemString(modules, "__main__");
397 if (value != NULL && PyModule_Check(value)) {
398 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000399 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000400 _PyModule_Clear(value);
401 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000402 }
403
Guido van Rossum758eec01998-01-19 21:58:26 +0000404 /* The special treatment of __builtin__ here is because even
405 when it's not referenced as a module, its dictionary is
406 referenced by almost every module's __builtins__. Since
407 deleting a module clears its dictionary (even if there are
408 references left to it), we need to delete the __builtin__
409 module last. Likewise, we don't delete sys until the very
410 end because it is implicitly referenced (e.g. by print).
411
412 Also note that we 'delete' modules by replacing their entry
413 in the modules dict with None, rather than really deleting
414 them; this avoids a rehash of the modules dictionary and
415 also marks them as "non existent" so they won't be
416 re-imported. */
417
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000418 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000419 one (skipping __builtin__ and sys) and delete them */
420 do {
421 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000423 while (PyDict_Next(modules, &pos, &key, &value)) {
424 if (value->ob_refcnt != 1)
425 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000426 if (PyString_Check(key) && PyModule_Check(value)) {
427 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000428 if (strcmp(name, "__builtin__") == 0)
429 continue;
430 if (strcmp(name, "sys") == 0)
431 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000432 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000433 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000434 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000435 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000436 PyDict_SetItem(modules, key, Py_None);
437 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000438 }
439 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000440 } while (ndone > 0);
441
Guido van Rossum758eec01998-01-19 21:58:26 +0000442 /* Next, delete all modules (still skipping __builtin__ and sys) */
443 pos = 0;
444 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000445 if (PyString_Check(key) && PyModule_Check(value)) {
446 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000447 if (strcmp(name, "__builtin__") == 0)
448 continue;
449 if (strcmp(name, "sys") == 0)
450 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000451 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000452 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000453 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000454 PyDict_SetItem(modules, key, Py_None);
455 }
456 }
457
458 /* Next, delete sys and __builtin__ (in that order) */
459 value = PyDict_GetItemString(modules, "sys");
460 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000461 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000462 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000463 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000464 PyDict_SetItemString(modules, "sys", Py_None);
465 }
466 value = PyDict_GetItemString(modules, "__builtin__");
467 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000468 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000469 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000470 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000471 PyDict_SetItemString(modules, "__builtin__", Py_None);
472 }
473
474 /* Finally, clear and delete the modules directory */
475 PyDict_Clear(modules);
476 interp->modules = NULL;
477 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000478}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000479
480
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481/* Helper for pythonrun.c -- return magic number */
482
483long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000484PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000485{
Guido van Rossum96774c12000-05-01 20:19:08 +0000486 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000487}
488
489
Guido van Rossum25ce5661997-08-02 03:10:38 +0000490/* Magic for extension modules (built-in as well as dynamically
491 loaded). To prevent initializing an extension module more than
492 once, we keep a static dictionary 'extensions' keyed by module name
493 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000494 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000495 dictionary is stored by calling _PyImport_FixupExtension()
496 immediately after the module initialization function succeeds. A
497 copy can be retrieved from there by calling
498 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499
Guido van Rossum79f25d91997-04-29 20:08:16 +0000500PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000502{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000503 PyObject *modules, *mod, *dict, *copy;
504 if (extensions == NULL) {
505 extensions = PyDict_New();
506 if (extensions == NULL)
507 return NULL;
508 }
509 modules = PyImport_GetModuleDict();
510 mod = PyDict_GetItemString(modules, name);
511 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000512 PyErr_Format(PyExc_SystemError,
513 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000514 return NULL;
515 }
516 dict = PyModule_GetDict(mod);
517 if (dict == NULL)
518 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000519 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000520 if (copy == NULL)
521 return NULL;
522 PyDict_SetItemString(extensions, filename, copy);
523 Py_DECREF(copy);
524 return copy;
525}
526
527PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000528_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000530 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000531 if (extensions == NULL)
532 return NULL;
533 dict = PyDict_GetItemString(extensions, filename);
534 if (dict == NULL)
535 return NULL;
536 mod = PyImport_AddModule(name);
537 if (mod == NULL)
538 return NULL;
539 mdict = PyModule_GetDict(mod);
540 if (mdict == NULL)
541 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000542 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000545 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546 name, filename);
547 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548}
549
550
551/* Get the module object corresponding to a module name.
552 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000553 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000554 Because the former action is most common, THIS DOES NOT RETURN A
555 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000558PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 if (m == NULL)
568 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000569 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 return NULL;
572 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574
575 return m;
576}
577
Tim Peters1cd70172004-08-02 03:52:12 +0000578/* Remove name from sys.modules, if it's there. */
579static void
580_RemoveModule(const char *name)
581{
582 PyObject *modules = PyImport_GetModuleDict();
583 if (PyDict_GetItemString(modules, name) == NULL)
584 return;
585 if (PyDict_DelItemString(modules, name) < 0)
586 Py_FatalError("import: deleting existing key in"
587 "sys.modules failed");
588}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000590/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000591 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
592 * removed from sys.modules, to avoid leaving damaged module objects
593 * in sys.modules. The caller may wish to restore the original
594 * module object (if any) in this case; PyImport_ReloadModule is an
595 * example.
596 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000598PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000599{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000600 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
601}
602
603PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000604PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000605{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 if (m == NULL)
611 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000612 /* If the module is being reloaded, we get the old module back
613 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 d = PyModule_GetDict(m);
615 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
616 if (PyDict_SetItemString(d, "__builtins__",
617 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000618 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000619 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000620 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000621 v = NULL;
622 if (pathname != NULL) {
623 v = PyString_FromString(pathname);
624 if (v == NULL)
625 PyErr_Clear();
626 }
627 if (v == NULL) {
628 v = ((PyCodeObject *)co)->co_filename;
629 Py_INCREF(v);
630 }
631 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000633 Py_DECREF(v);
634
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000635 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000637 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000639
Guido van Rossum25ce5661997-08-02 03:10:38 +0000640 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000641 PyErr_Format(PyExc_ImportError,
642 "Loaded module %.200s not found in sys.modules",
643 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000644 return NULL;
645 }
646
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648
649 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000650
651 error:
652 _RemoveModule(name);
653 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654}
655
656
657/* Given a pathname for a Python source file, fill a buffer with the
658 pathname for the corresponding compiled file. Return the pathname
659 for the compiled file, or NULL if there's no space in the buffer.
660 Doesn't set an exception. */
661
662static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000663make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664{
Tim Petersc1731372001-08-04 08:12:36 +0000665 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 if (len+2 > buflen)
667 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000668
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000669#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000670 /* Treat .pyw as if it were .py. The case of ".pyw" must match
671 that used in _PyImport_StandardFiletab. */
672 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
673 --len; /* pretend 'w' isn't there */
674#endif
675 memcpy(buf, pathname, len);
676 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
677 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000678
679 return buf;
680}
681
682
683/* Given a pathname for a Python source file, its time of last
684 modification, and a pathname for a compiled file, check whether the
685 compiled file represents the same version of the source. If so,
686 return a FILE pointer for the compiled file, positioned just after
687 the header; if not, return NULL.
688 Doesn't set an exception. */
689
690static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000691check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692{
693 FILE *fp;
694 long magic;
695 long pyc_mtime;
696
697 fp = fopen(cpathname, "rb");
698 if (fp == NULL)
699 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000701 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000703 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000704 fclose(fp);
705 return NULL;
706 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000710 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711 fclose(fp);
712 return NULL;
713 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000715 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716 return fp;
717}
718
719
720/* Read a code object from a file and check it for validity */
721
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000723read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000724{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726
Tim Petersd9b9ac82001-01-28 00:27:39 +0000727 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000728 if (co == NULL)
729 return NULL;
730 if (!PyCode_Check(co)) {
731 PyErr_Format(PyExc_ImportError,
732 "Non-code object in %.200s", cpathname);
733 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734 return NULL;
735 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737}
738
739
740/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000741 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000744load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745{
746 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 PyCodeObject *co;
748 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000751 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000752 PyErr_Format(PyExc_ImportError,
753 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754 return NULL;
755 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000757 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758 if (co == NULL)
759 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000761 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000763 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000765
766 return m;
767}
768
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000769/* Parse a source file and return the corresponding code object */
770
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000773{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 PyCodeObject *co = NULL;
775 mod_ty mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000776
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
778 NULL);
779 if (mod) {
780 co = PyAST_Compile(mod, pathname, NULL);
781 free_mod(mod);
782 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783 return co;
784}
785
786
Guido van Rossum55a83382000-09-20 20:31:38 +0000787/* Helper to open a bytecode file for writing in exclusive mode */
788
789static FILE *
790open_exclusive(char *filename)
791{
792#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
793 /* Use O_EXCL to avoid a race condition when another process tries to
794 write the same file. When that happens, our open() call fails,
795 which is just fine (since it's only a cache).
796 XXX If the file exists and is writable but the directory is not
797 writable, the file will never be written. Oh well.
798 */
799 int fd;
800 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000801 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
802#ifdef O_BINARY
803 |O_BINARY /* necessary for Windows */
804#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000805#ifdef __VMS
806 , 0666, "ctxt=bin", "shr=nil");
807#else
808 , 0666);
809#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000810 if (fd < 0)
811 return NULL;
812 return fdopen(fd, "wb");
813#else
814 /* Best we can do -- on Windows this can't happen anyway */
815 return fopen(filename, "wb");
816#endif
817}
818
819
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820/* Write a compiled module to a file, placing the time of last
821 modification of its source into the header.
822 Errors are ignored, if a write error occurs an attempt is made to
823 remove the file. */
824
825static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000826write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827{
828 FILE *fp;
829
Guido van Rossum55a83382000-09-20 20:31:38 +0000830 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000833 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000834 "# can't create %s\n", cpathname);
835 return;
836 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000837 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000839 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
840 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000841 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000843 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844 /* Don't keep partial file */
845 fclose(fp);
846 (void) unlink(cpathname);
847 return;
848 }
849 /* Now write the true mtime */
850 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000851 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852 fflush(fp);
853 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000855 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856}
857
858
859/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000860 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
861 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000864load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000865{
Fred Drake4c82b232000-06-30 16:18:57 +0000866 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000867 FILE *fpc;
868 char buf[MAXPATHLEN+1];
869 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 PyCodeObject *co;
871 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000873 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000874 if (mtime == (time_t)(-1)) {
875 PyErr_Format(PyExc_RuntimeError,
876 "unable to get modification time from '%s'",
877 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000878 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000879 }
Fred Drake4c82b232000-06-30 16:18:57 +0000880#if SIZEOF_TIME_T > 4
881 /* Python's .pyc timestamp handling presumes that the timestamp fits
882 in 4 bytes. This will be fine until sometime in the year 2038,
883 when a 4-byte signed time_t will overflow.
884 */
885 if (mtime >> 32) {
886 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000887 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000888 return NULL;
889 }
890#endif
Tim Peters36515e22001-11-18 04:06:29 +0000891 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000892 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893 if (cpathname != NULL &&
894 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000895 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000896 fclose(fpc);
897 if (co == NULL)
898 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000899 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000900 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000902 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000903 }
904 else {
905 co = parse_source_module(pathname, fp);
906 if (co == NULL)
907 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000909 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910 name, pathname);
911 write_compiled_module(co, cpathname, mtime);
912 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000913 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000915
916 return m;
917}
918
919
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000920/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000921static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
922static struct filedescr *find_module(char *, char *, PyObject *,
923 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000924static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000925
926/* Load a package and return its module object WITH INCREMENTED
927 REFERENCE COUNT */
928
929static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000930load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931{
Tim Peters1cd70172004-08-02 03:52:12 +0000932 PyObject *m, *d;
933 PyObject *file = NULL;
934 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000935 int err;
936 char buf[MAXPATHLEN+1];
937 FILE *fp = NULL;
938 struct filedescr *fdp;
939
940 m = PyImport_AddModule(name);
941 if (m == NULL)
942 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000943 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000944 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000945 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000946 d = PyModule_GetDict(m);
947 file = PyString_FromString(pathname);
948 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000949 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000950 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000951 if (path == NULL)
952 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000953 err = PyDict_SetItemString(d, "__file__", file);
954 if (err == 0)
955 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000956 if (err != 0)
957 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000958 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000959 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000960 if (fdp == NULL) {
961 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
962 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000963 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000964 }
965 else
966 m = NULL;
967 goto cleanup;
968 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000969 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000970 if (fp != NULL)
971 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000972 goto cleanup;
973
974 error:
975 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000976 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000977 Py_XDECREF(path);
978 Py_XDECREF(file);
979 return m;
980}
981
982
983/* Helper to test for built-in module */
984
985static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000986is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000987{
988 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000989 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
990 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
991 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000992 return -1;
993 else
994 return 1;
995 }
996 }
997 return 0;
998}
999
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001000
Just van Rossum52e14d62002-12-30 22:08:05 +00001001/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1002 possibly by fetching it from the path_importer_cache dict. If it
1003 wasn't yet cached, traverse path_hooks until it a hook is found
1004 that can handle the path item. Return None if no hook could;
1005 this tells our caller it should fall back to the builtin
1006 import mechanism. Cache the result in path_importer_cache.
1007 Returns a borrowed reference. */
1008
1009static PyObject *
1010get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1011 PyObject *p)
1012{
1013 PyObject *importer;
1014 int j, nhooks;
1015
1016 /* These conditions are the caller's responsibility: */
1017 assert(PyList_Check(path_hooks));
1018 assert(PyDict_Check(path_importer_cache));
1019
1020 nhooks = PyList_Size(path_hooks);
1021 if (nhooks < 0)
1022 return NULL; /* Shouldn't happen */
1023
1024 importer = PyDict_GetItem(path_importer_cache, p);
1025 if (importer != NULL)
1026 return importer;
1027
1028 /* set path_importer_cache[p] to None to avoid recursion */
1029 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1030 return NULL;
1031
1032 for (j = 0; j < nhooks; j++) {
1033 PyObject *hook = PyList_GetItem(path_hooks, j);
1034 if (hook == NULL)
1035 return NULL;
1036 importer = PyObject_CallFunction(hook, "O", p);
1037 if (importer != NULL)
1038 break;
1039
1040 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1041 return NULL;
1042 }
1043 PyErr_Clear();
1044 }
1045 if (importer == NULL)
1046 importer = Py_None;
1047 else if (importer != Py_None) {
1048 int err = PyDict_SetItem(path_importer_cache, p, importer);
1049 Py_DECREF(importer);
1050 if (err != 0)
1051 return NULL;
1052 }
1053 return importer;
1054}
1055
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001056/* Search the path (default sys.path) for a module. Return the
1057 corresponding filedescr struct, and (via return arguments) the
1058 pathname and an open file. Return NULL if the module is not found. */
1059
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001060#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001061extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1062 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001063#endif
1064
Tim Peters50d8d372001-02-28 05:34:27 +00001065static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001066static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001067static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001068
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001069static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001070find_module(char *fullname, char *subname, PyObject *path, char *buf,
1071 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001072{
Fred Drake4c82b232000-06-30 16:18:57 +00001073 int i, npath;
1074 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001075 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001076 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001077 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001078 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001079#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001080 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001081#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001082 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1083 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1084 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001085 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001086#if defined(PYOS_OS2)
1087 size_t saved_len;
1088 size_t saved_namelen;
1089 char *saved_buf = NULL;
1090#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001091 if (p_loader != NULL)
1092 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001093
Just van Rossum52e14d62002-12-30 22:08:05 +00001094 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001095 PyErr_SetString(PyExc_OverflowError,
1096 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001097 return NULL;
1098 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001099 strcpy(name, subname);
1100
1101 /* sys.meta_path import hook */
1102 if (p_loader != NULL) {
1103 PyObject *meta_path;
1104
1105 meta_path = PySys_GetObject("meta_path");
1106 if (meta_path == NULL || !PyList_Check(meta_path)) {
1107 PyErr_SetString(PyExc_ImportError,
1108 "sys.meta_path must be a list of "
1109 "import hooks");
1110 return NULL;
1111 }
1112 Py_INCREF(meta_path); /* zap guard */
1113 npath = PyList_Size(meta_path);
1114 for (i = 0; i < npath; i++) {
1115 PyObject *loader;
1116 PyObject *hook = PyList_GetItem(meta_path, i);
1117 loader = PyObject_CallMethod(hook, "find_module",
1118 "sO", fullname,
1119 path != NULL ?
1120 path : Py_None);
1121 if (loader == NULL) {
1122 Py_DECREF(meta_path);
1123 return NULL; /* true error */
1124 }
1125 if (loader != Py_None) {
1126 /* a loader was found */
1127 *p_loader = loader;
1128 Py_DECREF(meta_path);
1129 return &importhookdescr;
1130 }
1131 Py_DECREF(loader);
1132 }
1133 Py_DECREF(meta_path);
1134 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001135
1136 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001137 /* The only type of submodule allowed inside a "frozen"
1138 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001139 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1140 PyErr_SetString(PyExc_ImportError,
1141 "full frozen module name too long");
1142 return NULL;
1143 }
1144 strcpy(buf, PyString_AsString(path));
1145 strcat(buf, ".");
1146 strcat(buf, name);
1147 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001148 if (find_frozen(name) != NULL) {
1149 strcpy(buf, name);
1150 return &fd_frozen;
1151 }
1152 PyErr_Format(PyExc_ImportError,
1153 "No frozen submodule named %.200s", name);
1154 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001155 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001156 if (path == NULL) {
1157 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001158 strcpy(buf, name);
1159 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001160 }
Greg Ward201baee2001-10-04 14:52:06 +00001161 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001162 strcpy(buf, name);
1163 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001164 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001165
Guido van Rossumac279101996-08-22 23:10:58 +00001166#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001167 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1168 if (fp != NULL) {
1169 *p_fp = fp;
1170 return fdp;
1171 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001172#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001173 path = PySys_GetObject("path");
1174 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175 if (path == NULL || !PyList_Check(path)) {
1176 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001177 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 return NULL;
1179 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001180
1181 path_hooks = PySys_GetObject("path_hooks");
1182 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1183 PyErr_SetString(PyExc_ImportError,
1184 "sys.path_hooks must be a list of "
1185 "import hooks");
1186 return NULL;
1187 }
1188 path_importer_cache = PySys_GetObject("path_importer_cache");
1189 if (path_importer_cache == NULL ||
1190 !PyDict_Check(path_importer_cache)) {
1191 PyErr_SetString(PyExc_ImportError,
1192 "sys.path_importer_cache must be a dict");
1193 return NULL;
1194 }
1195
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197 namelen = strlen(name);
1198 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001199 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001200 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001201#ifdef Py_USING_UNICODE
1202 if (PyUnicode_Check(v)) {
1203 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1204 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1205 if (copy == NULL)
1206 return NULL;
1207 v = copy;
1208 }
1209 else
1210#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001211 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001212 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001214 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1215 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001216 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001217 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001219 if (strlen(buf) != len) {
1220 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001221 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001222 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001223
1224 /* sys.path_hooks import hook */
1225 if (p_loader != NULL) {
1226 PyObject *importer;
1227
1228 importer = get_path_importer(path_importer_cache,
1229 path_hooks, v);
1230 if (importer == NULL)
1231 return NULL;
1232 /* Note: importer is a borrowed reference */
1233 if (importer != Py_None) {
1234 PyObject *loader;
1235 loader = PyObject_CallMethod(importer,
1236 "find_module",
1237 "s", fullname);
1238 if (loader == NULL)
1239 return NULL; /* error */
1240 if (loader != Py_None) {
1241 /* a loader was found */
1242 *p_loader = loader;
1243 return &importhookdescr;
1244 }
1245 Py_DECREF(loader);
1246 }
1247 /* no hook was successful, use builtin import */
1248 }
1249
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001250 if (len > 0 && buf[len-1] != SEP
1251#ifdef ALTSEP
1252 && buf[len-1] != ALTSEP
1253#endif
1254 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001255 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001256 strcpy(buf+len, name);
1257 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001258
1259 /* Check for package import (buf holds a directory name,
1260 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001261#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001262 if (stat(buf, &statbuf) == 0 && /* it exists */
1263 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1264 find_init_module(buf) && /* it has __init__.py */
1265 case_ok(buf, len, namelen, name)) { /* and case matches */
1266 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001267 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001268 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001269#else
1270 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001271#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001272 if (isdir(buf) &&
1273 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001274 case_ok(buf, len, namelen, name)) {
1275 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001276 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001277 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001278#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001279#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001280#if defined(PYOS_OS2)
1281 /* take a snapshot of the module spec for restoration
1282 * after the 8 character DLL hackery
1283 */
1284 saved_buf = strdup(buf);
1285 saved_len = len;
1286 saved_namelen = namelen;
1287#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001289#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001290 /* OS/2 limits DLLs to 8 character names (w/o
1291 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001292 * so if the name is longer than that and its a
1293 * dynamically loaded module we're going to try,
1294 * truncate the name before trying
1295 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001296 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001297 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001298 const struct filedescr *scan;
1299 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001300 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001301 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001302 break;
1303 else
1304 scan++;
1305 }
1306 if (scan->suffix != NULL) {
1307 /* yes, so truncate the name */
1308 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001309 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001310 buf[len] = '\0';
1311 }
1312 }
1313#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001314 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001315 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001316 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001317 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001318 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001319 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001320 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001321 if (fp != NULL) {
1322 if (case_ok(buf, len, namelen, name))
1323 break;
1324 else { /* continue search */
1325 fclose(fp);
1326 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001327 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001328 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001329#if defined(PYOS_OS2)
1330 /* restore the saved snapshot */
1331 strcpy(buf, saved_buf);
1332 len = saved_len;
1333 namelen = saved_namelen;
1334#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001336#if defined(PYOS_OS2)
1337 /* don't need/want the module name snapshot anymore */
1338 if (saved_buf)
1339 {
1340 free(saved_buf);
1341 saved_buf = NULL;
1342 }
1343#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001344 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001345 if (fp != NULL)
1346 break;
1347 }
1348 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001349 PyErr_Format(PyExc_ImportError,
1350 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001351 return NULL;
1352 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001353 *p_fp = fp;
1354 return fdp;
1355}
1356
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001357/* Helpers for main.c
1358 * Find the source file corresponding to a named module
1359 */
1360struct filedescr *
1361_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1362 size_t buflen, FILE **p_fp, PyObject **p_loader)
1363{
1364 return find_module((char *) name, (char *) name, path,
1365 buf, buflen, p_fp, p_loader);
1366}
1367
1368PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1369{
1370 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1371}
1372
Tim Petersd1e87a82001-03-01 18:12:00 +00001373/* case_ok(char* buf, int len, int namelen, char* name)
1374 * The arguments here are tricky, best shown by example:
1375 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1376 * ^ ^ ^ ^
1377 * |--------------------- buf ---------------------|
1378 * |------------------- len ------------------|
1379 * |------ name -------|
1380 * |----- namelen -----|
1381 * buf is the full path, but len only counts up to (& exclusive of) the
1382 * extension. name is the module name, also exclusive of extension.
1383 *
1384 * We've already done a successful stat() or fopen() on buf, so know that
1385 * there's some match, possibly case-insensitive.
1386 *
Tim Peters50d8d372001-02-28 05:34:27 +00001387 * case_ok() is to return 1 if there's a case-sensitive match for
1388 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1389 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001390 *
Tim Peters50d8d372001-02-28 05:34:27 +00001391 * case_ok() is used to implement case-sensitive import semantics even
1392 * on platforms with case-insensitive filesystems. It's trivial to implement
1393 * for case-sensitive filesystems. It's pretty much a cross-platform
1394 * nightmare for systems with case-insensitive filesystems.
1395 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001396
Tim Peters50d8d372001-02-28 05:34:27 +00001397/* First we may need a pile of platform-specific header files; the sequence
1398 * of #if's here should match the sequence in the body of case_ok().
1399 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001400#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001401#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001402
Tim Peters50d8d372001-02-28 05:34:27 +00001403#elif defined(DJGPP)
1404#include <dir.h>
1405
Jason Tishler7961aa62005-05-20 00:56:54 +00001406#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001407#include <sys/types.h>
1408#include <dirent.h>
1409
Andrew MacIntyred9400542002-02-26 11:41:34 +00001410#elif defined(PYOS_OS2)
1411#define INCL_DOS
1412#define INCL_DOSERRORS
1413#define INCL_NOPMAPI
1414#include <os2.h>
1415
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001416#elif defined(RISCOS)
1417#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001418#endif
1419
Guido van Rossum0980bd91998-02-13 17:18:36 +00001420static int
Tim Peters50d8d372001-02-28 05:34:27 +00001421case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001422{
Tim Peters50d8d372001-02-28 05:34:27 +00001423/* Pick a platform-specific implementation; the sequence of #if's here should
1424 * match the sequence just above.
1425 */
1426
Jason Tishler7961aa62005-05-20 00:56:54 +00001427/* MS_WINDOWS */
1428#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001429 WIN32_FIND_DATA data;
1430 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001431
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001432 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001433 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001434
Guido van Rossum0980bd91998-02-13 17:18:36 +00001435 h = FindFirstFile(buf, &data);
1436 if (h == INVALID_HANDLE_VALUE) {
1437 PyErr_Format(PyExc_NameError,
1438 "Can't find file for module %.100s\n(filename %.300s)",
1439 name, buf);
1440 return 0;
1441 }
1442 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001443 return strncmp(data.cFileName, name, namelen) == 0;
1444
1445/* DJGPP */
1446#elif defined(DJGPP)
1447 struct ffblk ffblk;
1448 int done;
1449
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001450 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001451 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001452
1453 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1454 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001455 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001456 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001457 name, buf);
1458 return 0;
1459 }
Tim Peters50d8d372001-02-28 05:34:27 +00001460 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001461
Jason Tishler7961aa62005-05-20 00:56:54 +00001462/* new-fangled macintosh (macosx) or Cygwin */
1463#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001464 DIR *dirp;
1465 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001466 char dirname[MAXPATHLEN + 1];
1467 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001468
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001469 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001470 return 1;
1471
Tim Petersd1e87a82001-03-01 18:12:00 +00001472 /* Copy the dir component into dirname; substitute "." if empty */
1473 if (dirlen <= 0) {
1474 dirname[0] = '.';
1475 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001476 }
1477 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001478 assert(dirlen <= MAXPATHLEN);
1479 memcpy(dirname, buf, dirlen);
1480 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001481 }
1482 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001483 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001484 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001485 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001486 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001487 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001488#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001489 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001490#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001491 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001492#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001493 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001494 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001495 (void)closedir(dirp);
1496 return 1; /* Found */
1497 }
1498 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001499 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001500 }
Tim Peters430f5d42001-03-01 01:30:56 +00001501 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001502
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001503/* RISC OS */
1504#elif defined(RISCOS)
1505 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1506 char buf2[MAXPATHLEN+2];
1507 char *nameWithExt = buf+len-namelen;
1508 int canonlen;
1509 os_error *e;
1510
1511 if (Py_GETENV("PYTHONCASEOK") != NULL)
1512 return 1;
1513
1514 /* workaround:
1515 append wildcard, otherwise case of filename wouldn't be touched */
1516 strcpy(buf2, buf);
1517 strcat(buf2, "*");
1518
1519 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1520 canonlen = MAXPATHLEN+1-canonlen;
1521 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1522 return 0;
1523 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1524 return 1; /* match */
1525
1526 return 0;
1527
Andrew MacIntyred9400542002-02-26 11:41:34 +00001528/* OS/2 */
1529#elif defined(PYOS_OS2)
1530 HDIR hdir = 1;
1531 ULONG srchcnt = 1;
1532 FILEFINDBUF3 ffbuf;
1533 APIRET rc;
1534
1535 if (getenv("PYTHONCASEOK") != NULL)
1536 return 1;
1537
1538 rc = DosFindFirst(buf,
1539 &hdir,
1540 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1541 &ffbuf, sizeof(ffbuf),
1542 &srchcnt,
1543 FIL_STANDARD);
1544 if (rc != NO_ERROR)
1545 return 0;
1546 return strncmp(ffbuf.achName, name, namelen) == 0;
1547
Tim Peters50d8d372001-02-28 05:34:27 +00001548/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1549#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001550 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001551
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001552#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001553}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001554
Guido van Rossum0980bd91998-02-13 17:18:36 +00001555
Guido van Rossum197346f1997-10-31 18:38:52 +00001556#ifdef HAVE_STAT
1557/* Helper to look for __init__.py or __init__.py[co] in potential package */
1558static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001559find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001560{
Tim Peters0f9431f2001-07-05 03:47:53 +00001561 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001562 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001563 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001564 struct stat statbuf;
1565
Tim Peters0f9431f2001-07-05 03:47:53 +00001566/* For calling case_ok(buf, len, namelen, name):
1567 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1568 * ^ ^ ^ ^
1569 * |--------------------- buf ---------------------|
1570 * |------------------- len ------------------|
1571 * |------ name -------|
1572 * |----- namelen -----|
1573 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001574 if (save_len + 13 >= MAXPATHLEN)
1575 return 0;
1576 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001577 pname = buf + i;
1578 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001579 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001580 if (case_ok(buf,
1581 save_len + 9, /* len("/__init__") */
1582 8, /* len("__init__") */
1583 pname)) {
1584 buf[save_len] = '\0';
1585 return 1;
1586 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001587 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001588 i += strlen(pname);
1589 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001590 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001591 if (case_ok(buf,
1592 save_len + 9, /* len("/__init__") */
1593 8, /* len("__init__") */
1594 pname)) {
1595 buf[save_len] = '\0';
1596 return 1;
1597 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001598 }
1599 buf[save_len] = '\0';
1600 return 0;
1601}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001602
1603#else
1604
1605#ifdef RISCOS
1606static int
1607find_init_module(buf)
1608 char *buf;
1609{
1610 int save_len = strlen(buf);
1611 int i = save_len;
1612
1613 if (save_len + 13 >= MAXPATHLEN)
1614 return 0;
1615 buf[i++] = SEP;
1616 strcpy(buf+i, "__init__/py");
1617 if (isfile(buf)) {
1618 buf[save_len] = '\0';
1619 return 1;
1620 }
1621
1622 if (Py_OptimizeFlag)
1623 strcpy(buf+i, "o");
1624 else
1625 strcpy(buf+i, "c");
1626 if (isfile(buf)) {
1627 buf[save_len] = '\0';
1628 return 1;
1629 }
1630 buf[save_len] = '\0';
1631 return 0;
1632}
1633#endif /*RISCOS*/
1634
Guido van Rossum197346f1997-10-31 18:38:52 +00001635#endif /* HAVE_STAT */
1636
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001637
Tim Petersdbd9ba62000-07-09 03:09:57 +00001638static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001639
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001640/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001641 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001642
Guido van Rossum79f25d91997-04-29 20:08:16 +00001643static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001644load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001645{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001646 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001648 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001649
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001650 /* First check that there's an open file (if we need one) */
1651 switch (type) {
1652 case PY_SOURCE:
1653 case PY_COMPILED:
1654 if (fp == NULL) {
1655 PyErr_Format(PyExc_ValueError,
1656 "file object required for import (type code %d)",
1657 type);
1658 return NULL;
1659 }
1660 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001661
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001662 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001663
1664 case PY_SOURCE:
1665 m = load_source_module(name, buf, fp);
1666 break;
1667
1668 case PY_COMPILED:
1669 m = load_compiled_module(name, buf, fp);
1670 break;
1671
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001672#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001673 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001674 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001675 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001676#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001677
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001678 case PKG_DIRECTORY:
1679 m = load_package(name, buf);
1680 break;
1681
1682 case C_BUILTIN:
1683 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001684 if (buf != NULL && buf[0] != '\0')
1685 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001686 if (type == C_BUILTIN)
1687 err = init_builtin(name);
1688 else
1689 err = PyImport_ImportFrozenModule(name);
1690 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001691 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001692 if (err == 0) {
1693 PyErr_Format(PyExc_ImportError,
1694 "Purported %s module %.200s not found",
1695 type == C_BUILTIN ?
1696 "builtin" : "frozen",
1697 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001698 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001699 }
1700 modules = PyImport_GetModuleDict();
1701 m = PyDict_GetItemString(modules, name);
1702 if (m == NULL) {
1703 PyErr_Format(
1704 PyExc_ImportError,
1705 "%s module %.200s not properly initialized",
1706 type == C_BUILTIN ?
1707 "builtin" : "frozen",
1708 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001709 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001710 }
1711 Py_INCREF(m);
1712 break;
1713
Just van Rossum52e14d62002-12-30 22:08:05 +00001714 case IMP_HOOK: {
1715 if (loader == NULL) {
1716 PyErr_SetString(PyExc_ImportError,
1717 "import hook without loader");
1718 return NULL;
1719 }
1720 m = PyObject_CallMethod(loader, "load_module", "s", name);
1721 break;
1722 }
1723
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001724 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001725 PyErr_Format(PyExc_ImportError,
1726 "Don't know how to import %.200s (type code %d)",
1727 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001728 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729
1730 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731
1732 return m;
1733}
1734
1735
1736/* Initialize a built-in module.
1737 Return 1 for succes, 0 if the module is not found, and -1 with
1738 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001739
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001740static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001741init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001742{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001743 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001744
Greg Ward201baee2001-10-04 14:52:06 +00001745 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001746 return 1;
1747
Guido van Rossum771c6c81997-10-31 18:37:24 +00001748 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001749 if (strcmp(name, p->name) == 0) {
1750 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001751 PyErr_Format(PyExc_ImportError,
1752 "Cannot re-init internal module %.200s",
1753 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001754 return -1;
1755 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001757 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001758 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001760 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001761 if (_PyImport_FixupExtension(name, name) == NULL)
1762 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001763 return 1;
1764 }
1765 }
1766 return 0;
1767}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001768
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001770/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001771
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001772static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001773find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001774{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001775 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001776
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001778 if (p->name == NULL)
1779 return NULL;
1780 if (strcmp(p->name, name) == 0)
1781 break;
1782 }
1783 return p;
1784}
1785
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001787get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001788{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001789 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001790 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001791
1792 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001793 PyErr_Format(PyExc_ImportError,
1794 "No such frozen object named %.200s",
1795 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001796 return NULL;
1797 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001798 if (p->code == NULL) {
1799 PyErr_Format(PyExc_ImportError,
1800 "Excluded frozen object named %.200s",
1801 name);
1802 return NULL;
1803 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001804 size = p->size;
1805 if (size < 0)
1806 size = -size;
1807 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001808}
1809
1810/* Initialize a frozen module.
1811 Return 1 for succes, 0 if the module is not found, and -1 with
1812 an exception set if the initialization failed.
1813 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001814
1815int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001816PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001817{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001818 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 PyObject *co;
1820 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001821 int ispackage;
1822 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001823
1824 if (p == NULL)
1825 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001826 if (p->code == NULL) {
1827 PyErr_Format(PyExc_ImportError,
1828 "Excluded frozen object named %.200s",
1829 name);
1830 return -1;
1831 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001832 size = p->size;
1833 ispackage = (size < 0);
1834 if (ispackage)
1835 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001837 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001838 name, ispackage ? " package" : "");
1839 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001840 if (co == NULL)
1841 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 if (!PyCode_Check(co)) {
1843 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001844 PyErr_Format(PyExc_TypeError,
1845 "frozen object %.200s is not a code object",
1846 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001847 return -1;
1848 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001849 if (ispackage) {
1850 /* Set __path__ to the package name */
1851 PyObject *d, *s;
1852 int err;
1853 m = PyImport_AddModule(name);
1854 if (m == NULL)
1855 return -1;
1856 d = PyModule_GetDict(m);
1857 s = PyString_InternFromString(name);
1858 if (s == NULL)
1859 return -1;
1860 err = PyDict_SetItemString(d, "__path__", s);
1861 Py_DECREF(s);
1862 if (err != 0)
1863 return err;
1864 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001865 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001866 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001867 if (m == NULL)
1868 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001870 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001871}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001872
1873
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001874/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001875 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001876
Guido van Rossum79f25d91997-04-29 20:08:16 +00001877PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001878PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001879{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001880 PyObject *pname;
1881 PyObject *result;
1882
1883 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001884 if (pname == NULL)
1885 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001886 result = PyImport_Import(pname);
1887 Py_DECREF(pname);
1888 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001889}
1890
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001891/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001892static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1893static PyObject *load_next(PyObject *mod, PyObject *altmod,
1894 char **p_name, char *buf, int *p_buflen);
1895static int mark_miss(char *name);
1896static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1897 char *buf, int buflen, int recursive);
1898static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001899
1900/* The Magnum Opus of dotted-name import :-) */
1901
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001902static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001903import_module_ex(char *name, PyObject *globals, PyObject *locals,
1904 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001905{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001906 char buf[MAXPATHLEN+1];
1907 int buflen = 0;
1908 PyObject *parent, *head, *next, *tail;
1909
1910 parent = get_parent(globals, buf, &buflen);
1911 if (parent == NULL)
1912 return NULL;
1913
1914 head = load_next(parent, Py_None, &name, buf, &buflen);
1915 if (head == NULL)
1916 return NULL;
1917
1918 tail = head;
1919 Py_INCREF(tail);
1920 while (name) {
1921 next = load_next(tail, tail, &name, buf, &buflen);
1922 Py_DECREF(tail);
1923 if (next == NULL) {
1924 Py_DECREF(head);
1925 return NULL;
1926 }
1927 tail = next;
1928 }
1929
1930 if (fromlist != NULL) {
1931 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1932 fromlist = NULL;
1933 }
1934
1935 if (fromlist == NULL) {
1936 Py_DECREF(tail);
1937 return head;
1938 }
1939
1940 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001941 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001942 Py_DECREF(tail);
1943 return NULL;
1944 }
1945
1946 return tail;
1947}
1948
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001949PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001950PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1951 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001952{
1953 PyObject *result;
1954 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001955 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001956 if (unlock_import() < 0) {
1957 Py_XDECREF(result);
1958 PyErr_SetString(PyExc_RuntimeError,
1959 "not holding the import lock");
1960 return NULL;
1961 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001962 return result;
1963}
1964
Fred Drake87590902004-05-28 20:21:36 +00001965/* Return the package that an import is being performed in. If globals comes
1966 from the module foo.bar.bat (not itself a package), this returns the
1967 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1968 the package's entry in sys.modules is returned.
1969
1970 The *name* of the returned package is returned in buf, with the length of
1971 the name in *p_buflen.
1972
1973 If globals doesn't come from a package or a module in a package, or a
1974 corresponding entry is not found in sys.modules, Py_None is returned.
1975*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001976static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001977get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001978{
1979 static PyObject *namestr = NULL;
1980 static PyObject *pathstr = NULL;
1981 PyObject *modname, *modpath, *modules, *parent;
1982
1983 if (globals == NULL || !PyDict_Check(globals))
1984 return Py_None;
1985
1986 if (namestr == NULL) {
1987 namestr = PyString_InternFromString("__name__");
1988 if (namestr == NULL)
1989 return NULL;
1990 }
1991 if (pathstr == NULL) {
1992 pathstr = PyString_InternFromString("__path__");
1993 if (pathstr == NULL)
1994 return NULL;
1995 }
1996
1997 *buf = '\0';
1998 *p_buflen = 0;
1999 modname = PyDict_GetItem(globals, namestr);
2000 if (modname == NULL || !PyString_Check(modname))
2001 return Py_None;
2002
2003 modpath = PyDict_GetItem(globals, pathstr);
2004 if (modpath != NULL) {
2005 int len = PyString_GET_SIZE(modname);
2006 if (len > MAXPATHLEN) {
2007 PyErr_SetString(PyExc_ValueError,
2008 "Module name too long");
2009 return NULL;
2010 }
2011 strcpy(buf, PyString_AS_STRING(modname));
2012 *p_buflen = len;
2013 }
2014 else {
2015 char *start = PyString_AS_STRING(modname);
2016 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002017 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002018 if (lastdot == NULL)
2019 return Py_None;
2020 len = lastdot - start;
2021 if (len >= MAXPATHLEN) {
2022 PyErr_SetString(PyExc_ValueError,
2023 "Module name too long");
2024 return NULL;
2025 }
2026 strncpy(buf, start, len);
2027 buf[len] = '\0';
2028 *p_buflen = len;
2029 }
2030
2031 modules = PyImport_GetModuleDict();
2032 parent = PyDict_GetItemString(modules, buf);
2033 if (parent == NULL)
2034 parent = Py_None;
2035 return parent;
2036 /* We expect, but can't guarantee, if parent != None, that:
2037 - parent.__name__ == buf
2038 - parent.__dict__ is globals
2039 If this is violated... Who cares? */
2040}
2041
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002042/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002044load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2045 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002046{
2047 char *name = *p_name;
2048 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002049 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002050 char *p;
2051 PyObject *result;
2052
2053 if (dot == NULL) {
2054 *p_name = NULL;
2055 len = strlen(name);
2056 }
2057 else {
2058 *p_name = dot+1;
2059 len = dot-name;
2060 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002061 if (len == 0) {
2062 PyErr_SetString(PyExc_ValueError,
2063 "Empty module name");
2064 return NULL;
2065 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002066
2067 p = buf + *p_buflen;
2068 if (p != buf)
2069 *p++ = '.';
2070 if (p+len-buf >= MAXPATHLEN) {
2071 PyErr_SetString(PyExc_ValueError,
2072 "Module name too long");
2073 return NULL;
2074 }
2075 strncpy(p, name, len);
2076 p[len] = '\0';
2077 *p_buflen = p+len-buf;
2078
2079 result = import_submodule(mod, p, buf);
2080 if (result == Py_None && altmod != mod) {
2081 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002082 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002083 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002084 if (result != NULL && result != Py_None) {
2085 if (mark_miss(buf) != 0) {
2086 Py_DECREF(result);
2087 return NULL;
2088 }
2089 strncpy(buf, name, len);
2090 buf[len] = '\0';
2091 *p_buflen = len;
2092 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002093 }
2094 if (result == NULL)
2095 return NULL;
2096
2097 if (result == Py_None) {
2098 Py_DECREF(result);
2099 PyErr_Format(PyExc_ImportError,
2100 "No module named %.200s", name);
2101 return NULL;
2102 }
2103
2104 return result;
2105}
2106
2107static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002108mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002109{
2110 PyObject *modules = PyImport_GetModuleDict();
2111 return PyDict_SetItemString(modules, name, Py_None);
2112}
2113
2114static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002115ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2116 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002117{
2118 int i;
2119
2120 if (!PyObject_HasAttrString(mod, "__path__"))
2121 return 1;
2122
2123 for (i = 0; ; i++) {
2124 PyObject *item = PySequence_GetItem(fromlist, i);
2125 int hasit;
2126 if (item == NULL) {
2127 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2128 PyErr_Clear();
2129 return 1;
2130 }
2131 return 0;
2132 }
2133 if (!PyString_Check(item)) {
2134 PyErr_SetString(PyExc_TypeError,
2135 "Item in ``from list'' not a string");
2136 Py_DECREF(item);
2137 return 0;
2138 }
2139 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002140 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002141 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002142 /* See if the package defines __all__ */
2143 if (recursive)
2144 continue; /* Avoid endless recursion */
2145 all = PyObject_GetAttrString(mod, "__all__");
2146 if (all == NULL)
2147 PyErr_Clear();
2148 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002149 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002150 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002151 if (!ret)
2152 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002153 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002154 continue;
2155 }
2156 hasit = PyObject_HasAttr(mod, item);
2157 if (!hasit) {
2158 char *subname = PyString_AS_STRING(item);
2159 PyObject *submod;
2160 char *p;
2161 if (buflen + strlen(subname) >= MAXPATHLEN) {
2162 PyErr_SetString(PyExc_ValueError,
2163 "Module name too long");
2164 Py_DECREF(item);
2165 return 0;
2166 }
2167 p = buf + buflen;
2168 *p++ = '.';
2169 strcpy(p, subname);
2170 submod = import_submodule(mod, subname, buf);
2171 Py_XDECREF(submod);
2172 if (submod == NULL) {
2173 Py_DECREF(item);
2174 return 0;
2175 }
2176 }
2177 Py_DECREF(item);
2178 }
2179
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002180 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002181}
2182
Neil Schemenauer00b09662003-06-16 21:03:07 +00002183static int
2184add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2185 PyObject *modules)
2186{
2187 if (mod == Py_None)
2188 return 1;
2189 /* Irrespective of the success of this load, make a
2190 reference to it in the parent package module. A copy gets
2191 saved in the modules dictionary under the full name, so get a
2192 reference from there, if need be. (The exception is when the
2193 load failed with a SyntaxError -- then there's no trace in
2194 sys.modules. In that case, of course, do nothing extra.) */
2195 if (submod == NULL) {
2196 submod = PyDict_GetItemString(modules, fullname);
2197 if (submod == NULL)
2198 return 1;
2199 }
2200 if (PyModule_Check(mod)) {
2201 /* We can't use setattr here since it can give a
2202 * spurious warning if the submodule name shadows a
2203 * builtin name */
2204 PyObject *dict = PyModule_GetDict(mod);
2205 if (!dict)
2206 return 0;
2207 if (PyDict_SetItemString(dict, subname, submod) < 0)
2208 return 0;
2209 }
2210 else {
2211 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2212 return 0;
2213 }
2214 return 1;
2215}
2216
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002217static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002218import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002219{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002220 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002221 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002222
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002223 /* Require:
2224 if mod == None: subname == fullname
2225 else: mod.__name__ + "." + subname == fullname
2226 */
2227
Tim Peters50d8d372001-02-28 05:34:27 +00002228 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002229 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002230 }
2231 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002232 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002233 char buf[MAXPATHLEN+1];
2234 struct filedescr *fdp;
2235 FILE *fp = NULL;
2236
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002237 if (mod == Py_None)
2238 path = NULL;
2239 else {
2240 path = PyObject_GetAttrString(mod, "__path__");
2241 if (path == NULL) {
2242 PyErr_Clear();
2243 Py_INCREF(Py_None);
2244 return Py_None;
2245 }
2246 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002247
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002248 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002249 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2250 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002251 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002252 if (fdp == NULL) {
2253 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2254 return NULL;
2255 PyErr_Clear();
2256 Py_INCREF(Py_None);
2257 return Py_None;
2258 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002259 m = load_module(fullname, fp, buf, fdp->type, loader);
2260 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002261 if (fp)
2262 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002263 if (!add_submodule(mod, m, fullname, subname, modules)) {
2264 Py_XDECREF(m);
2265 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002266 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002267 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002268
2269 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002270}
2271
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002272
2273/* Re-import a module of any kind and return its module object, WITH
2274 INCREMENTED REFERENCE COUNT */
2275
Guido van Rossum79f25d91997-04-29 20:08:16 +00002276PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002277PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002278{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002279 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002280 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002281 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002282 char buf[MAXPATHLEN+1];
2283 struct filedescr *fdp;
2284 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002285 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002286
Guido van Rossum79f25d91997-04-29 20:08:16 +00002287 if (m == NULL || !PyModule_Check(m)) {
2288 PyErr_SetString(PyExc_TypeError,
2289 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002290 return NULL;
2291 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002292 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002293 if (name == NULL)
2294 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002295 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002296 PyErr_Format(PyExc_ImportError,
2297 "reload(): module %.200s not in sys.modules",
2298 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002299 return NULL;
2300 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002301 subname = strrchr(name, '.');
2302 if (subname == NULL)
2303 subname = name;
2304 else {
2305 PyObject *parentname, *parent;
2306 parentname = PyString_FromStringAndSize(name, (subname-name));
2307 if (parentname == NULL)
2308 return NULL;
2309 parent = PyDict_GetItem(modules, parentname);
2310 if (parent == NULL) {
2311 PyErr_Format(PyExc_ImportError,
2312 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002313 PyString_AS_STRING(parentname));
2314 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002315 return NULL;
2316 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002317 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002318 subname++;
2319 path = PyObject_GetAttrString(parent, "__path__");
2320 if (path == NULL)
2321 PyErr_Clear();
2322 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002323 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002324 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002325 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002326
2327 if (fdp == NULL) {
2328 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002329 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002330 }
2331
2332 newm = load_module(name, fp, buf, fdp->type, loader);
2333 Py_XDECREF(loader);
2334
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002335 if (fp)
2336 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002337 if (newm == NULL) {
2338 /* load_module probably removed name from modules because of
2339 * the error. Put back the original module object. We're
2340 * going to return NULL in this case regardless of whether
2341 * replacing name succeeds, so the return value is ignored.
2342 */
2343 PyDict_SetItemString(modules, name, m);
2344 }
2345 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002346}
2347
2348
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002349/* Higher-level import emulator which emulates the "import" statement
2350 more accurately -- it invokes the __import__() function from the
2351 builtins of the current globals. This means that the import is
2352 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002353 environment, e.g. by "rexec".
2354 A dummy list ["__doc__"] is passed as the 4th argument so that
2355 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2356 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002357
2358PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002359PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002360{
2361 static PyObject *silly_list = NULL;
2362 static PyObject *builtins_str = NULL;
2363 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002364 PyObject *globals = NULL;
2365 PyObject *import = NULL;
2366 PyObject *builtins = NULL;
2367 PyObject *r = NULL;
2368
2369 /* Initialize constant string objects */
2370 if (silly_list == NULL) {
2371 import_str = PyString_InternFromString("__import__");
2372 if (import_str == NULL)
2373 return NULL;
2374 builtins_str = PyString_InternFromString("__builtins__");
2375 if (builtins_str == NULL)
2376 return NULL;
2377 silly_list = Py_BuildValue("[s]", "__doc__");
2378 if (silly_list == NULL)
2379 return NULL;
2380 }
2381
2382 /* Get the builtins from current globals */
2383 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002384 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002385 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002386 builtins = PyObject_GetItem(globals, builtins_str);
2387 if (builtins == NULL)
2388 goto err;
2389 }
2390 else {
2391 /* No globals -- use standard builtins, and fake globals */
2392 PyErr_Clear();
2393
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002394 builtins = PyImport_ImportModuleEx("__builtin__",
2395 NULL, NULL, NULL);
2396 if (builtins == NULL)
2397 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002398 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2399 if (globals == NULL)
2400 goto err;
2401 }
2402
2403 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002404 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002405 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002406 if (import == NULL)
2407 PyErr_SetObject(PyExc_KeyError, import_str);
2408 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002409 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002410 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002411 if (import == NULL)
2412 goto err;
2413
2414 /* Call the _import__ function with the proper argument list */
2415 r = PyObject_CallFunction(import, "OOOO",
2416 module_name, globals, globals, silly_list);
2417
2418 err:
2419 Py_XDECREF(globals);
2420 Py_XDECREF(builtins);
2421 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002422
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002423 return r;
2424}
2425
2426
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002427/* Module 'imp' provides Python access to the primitives used for
2428 importing modules.
2429*/
2430
Guido van Rossum79f25d91997-04-29 20:08:16 +00002431static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002432imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433{
2434 char buf[4];
2435
Guido van Rossum96774c12000-05-01 20:19:08 +00002436 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2437 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2438 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2439 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002440
Guido van Rossum79f25d91997-04-29 20:08:16 +00002441 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002442}
2443
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002445imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002446{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002447 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002448 struct filedescr *fdp;
2449
Guido van Rossum79f25d91997-04-29 20:08:16 +00002450 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002451 if (list == NULL)
2452 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2454 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002455 fdp->suffix, fdp->mode, fdp->type);
2456 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002457 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002458 return NULL;
2459 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460 if (PyList_Append(list, item) < 0) {
2461 Py_DECREF(list);
2462 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002463 return NULL;
2464 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002465 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002466 }
2467 return list;
2468}
2469
Guido van Rossum79f25d91997-04-29 20:08:16 +00002470static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002471call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002472{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002473 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002474 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002475 struct filedescr *fdp;
2476 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002477 FILE *fp = NULL;
2478
2479 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002480 if (path == Py_None)
2481 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002482 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002483 if (fdp == NULL)
2484 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002485 if (fp != NULL) {
2486 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2487 if (fob == NULL) {
2488 fclose(fp);
2489 return NULL;
2490 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002491 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002492 else {
2493 fob = Py_None;
2494 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002495 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002496 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002497 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002498 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002499 return ret;
2500}
2501
Guido van Rossum79f25d91997-04-29 20:08:16 +00002502static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002503imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002504{
2505 char *name;
2506 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002507 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002508 return NULL;
2509 return call_find_module(name, path);
2510}
2511
2512static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002513imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002514{
2515 char *name;
2516 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002517 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002518 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519 return NULL;
2520 ret = init_builtin(name);
2521 if (ret < 0)
2522 return NULL;
2523 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002524 Py_INCREF(Py_None);
2525 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002526 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527 m = PyImport_AddModule(name);
2528 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002529 return m;
2530}
2531
Guido van Rossum79f25d91997-04-29 20:08:16 +00002532static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002533imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534{
2535 char *name;
2536 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002537 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002538 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002540 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002541 if (ret < 0)
2542 return NULL;
2543 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002544 Py_INCREF(Py_None);
2545 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002546 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002547 m = PyImport_AddModule(name);
2548 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002549 return m;
2550}
2551
Guido van Rossum79f25d91997-04-29 20:08:16 +00002552static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002553imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002554{
2555 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002556
Guido van Rossum43713e52000-02-29 13:59:29 +00002557 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002558 return NULL;
2559 return get_frozen_object(name);
2560}
2561
Guido van Rossum79f25d91997-04-29 20:08:16 +00002562static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002563imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002564{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002565 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002566 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002567 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002568 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002569}
2570
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002572imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002573{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002574 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002575 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002576 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002578 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002579 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002580}
2581
2582static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002583get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002584{
2585 FILE *fp;
2586 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002587 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002588 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002589 fp = fopen(pathname, mode);
2590 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592 }
2593 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002596 PyErr_SetString(PyExc_ValueError,
2597 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002598 }
2599 return fp;
2600}
2601
Guido van Rossum79f25d91997-04-29 20:08:16 +00002602static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002603imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002604{
2605 char *name;
2606 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002607 PyObject *fob = NULL;
2608 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002609 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002610 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002611 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002612 return NULL;
2613 fp = get_file(pathname, fob, "rb");
2614 if (fp == NULL)
2615 return NULL;
2616 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002617 if (fob == NULL)
2618 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002619 return m;
2620}
2621
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002622#ifdef HAVE_DYNAMIC_LOADING
2623
Guido van Rossum79f25d91997-04-29 20:08:16 +00002624static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002625imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002626{
2627 char *name;
2628 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002629 PyObject *fob = NULL;
2630 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002631 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002632 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002634 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002635 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002636 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002637 if (fp == NULL)
2638 return NULL;
2639 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002640 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002641 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002642}
2643
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002644#endif /* HAVE_DYNAMIC_LOADING */
2645
Guido van Rossum79f25d91997-04-29 20:08:16 +00002646static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002647imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002648{
2649 char *name;
2650 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002651 PyObject *fob = NULL;
2652 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002653 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002654 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002655 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656 return NULL;
2657 fp = get_file(pathname, fob, "r");
2658 if (fp == NULL)
2659 return NULL;
2660 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002661 if (fob == NULL)
2662 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002663 return m;
2664}
2665
Guido van Rossum79f25d91997-04-29 20:08:16 +00002666static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002667imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002668{
2669 char *name;
2670 PyObject *fob;
2671 char *pathname;
2672 char *suffix; /* Unused */
2673 char *mode;
2674 int type;
2675 FILE *fp;
2676
Guido van Rossum43713e52000-02-29 13:59:29 +00002677 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002678 &name, &fob, &pathname,
2679 &suffix, &mode, &type))
2680 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002681 if (*mode) {
2682 /* Mode must start with 'r' or 'U' and must not contain '+'.
2683 Implicit in this test is the assumption that the mode
2684 may contain other modifiers like 'b' or 't'. */
2685
2686 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002687 PyErr_Format(PyExc_ValueError,
2688 "invalid file open mode %.200s", mode);
2689 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002690 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002691 }
2692 if (fob == Py_None)
2693 fp = NULL;
2694 else {
2695 if (!PyFile_Check(fob)) {
2696 PyErr_SetString(PyExc_ValueError,
2697 "load_module arg#2 should be a file or None");
2698 return NULL;
2699 }
2700 fp = get_file(pathname, fob, mode);
2701 if (fp == NULL)
2702 return NULL;
2703 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002704 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002705}
2706
2707static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002708imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002709{
2710 char *name;
2711 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002712 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002713 return NULL;
2714 return load_package(name, pathname);
2715}
2716
2717static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002718imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002719{
2720 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002721 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002722 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002723 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002724}
2725
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002726/* Doc strings */
2727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728PyDoc_STRVAR(doc_imp,
2729"This module provides the components needed to build your own\n\
2730__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732PyDoc_STRVAR(doc_find_module,
2733"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002734Search for a module. If path is omitted or None, search for a\n\
2735built-in, frozen or special module and continue search in sys.path.\n\
2736The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002737package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002739PyDoc_STRVAR(doc_load_module,
2740"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002741Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002742The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002744PyDoc_STRVAR(doc_get_magic,
2745"get_magic() -> string\n\
2746Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002748PyDoc_STRVAR(doc_get_suffixes,
2749"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002750Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002751that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002753PyDoc_STRVAR(doc_new_module,
2754"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002755Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002756The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002758PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002759"lock_held() -> boolean\n\
2760Return True if the import lock is currently held, else False.\n\
2761On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002762
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002763PyDoc_STRVAR(doc_acquire_lock,
2764"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002765Acquires the interpreter's import lock for the current thread.\n\
2766This lock should be used by import hooks to ensure thread-safety\n\
2767when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002768On platforms without threads, this function does nothing.");
2769
2770PyDoc_STRVAR(doc_release_lock,
2771"release_lock() -> None\n\
2772Release the interpreter's import lock.\n\
2773On platforms without threads, this function does nothing.");
2774
Guido van Rossum79f25d91997-04-29 20:08:16 +00002775static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002776 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2777 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2778 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2779 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2780 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2781 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2782 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2783 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002784 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002785 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2786 {"init_builtin", imp_init_builtin, METH_VARARGS},
2787 {"init_frozen", imp_init_frozen, METH_VARARGS},
2788 {"is_builtin", imp_is_builtin, METH_VARARGS},
2789 {"is_frozen", imp_is_frozen, METH_VARARGS},
2790 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002791#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002792 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002793#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002794 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002795 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002796 {NULL, NULL} /* sentinel */
2797};
2798
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002799static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002800setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002801{
2802 PyObject *v;
2803 int err;
2804
2805 v = PyInt_FromLong((long)value);
2806 err = PyDict_SetItemString(d, name, v);
2807 Py_XDECREF(v);
2808 return err;
2809}
2810
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002811PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002812initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002813{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002814 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002815
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002816 m = Py_InitModule4("imp", imp_methods, doc_imp,
2817 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002818 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002819
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002820 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2821 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2822 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2823 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2824 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2825 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2826 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2827 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002828 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002829 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002830
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002831 failure:
2832 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002833}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002834
2835
Guido van Rossumb18618d2000-05-03 23:44:39 +00002836/* API for embedding applications that want to add their own entries
2837 to the table of built-in modules. This should normally be called
2838 *before* Py_Initialize(). When the table resize fails, -1 is
2839 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002840
2841 After a similar function by Just van Rossum. */
2842
2843int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002844PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002845{
2846 static struct _inittab *our_copy = NULL;
2847 struct _inittab *p;
2848 int i, n;
2849
2850 /* Count the number of entries in both tables */
2851 for (n = 0; newtab[n].name != NULL; n++)
2852 ;
2853 if (n == 0)
2854 return 0; /* Nothing to do */
2855 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2856 ;
2857
2858 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002859 p = our_copy;
2860 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002861 if (p == NULL)
2862 return -1;
2863
2864 /* Copy the tables into the new memory */
2865 if (our_copy != PyImport_Inittab)
2866 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2867 PyImport_Inittab = our_copy = p;
2868 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2869
2870 return 0;
2871}
2872
2873/* Shorthand to add a single entry given a name and a function */
2874
2875int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002876PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002877{
2878 struct _inittab newtab[2];
2879
2880 memset(newtab, '\0', sizeof newtab);
2881
2882 newtab[0].name = name;
2883 newtab[0].initfunc = initfunc;
2884
2885 return PyImport_ExtendInittab(newtab);
2886}