blob: 5df1f0a1d486d7d07e7bf7bd46a8fdfc33d8c3f4 [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.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"
10#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000011#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000014#ifdef macintosh
15#include "macglue.h"
16#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000017
Guido van Rossum55a83382000-09-20 20:31:38 +000018#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif
21
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000022extern time_t PyOS_GetLastModificationTime(char *, FILE *);
23 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000024
Guido van Rossum6c849691994-09-26 15:47:17 +000025/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000026/* Change for each incompatible change */
27/* The value of CR and LF is incorporated so if you ever read or write
28 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000029 Apple MPW compiler swaps their values, botching string constants.
30 XXX That probably isn't important anymore.
31*/
Guido van Rossum7faeab31995-07-07 22:50:36 +000032/* XXX Perhaps the magic number should be frozen and a version field
33 added to the .pyc file header? */
Tim Peters36515e22001-11-18 04:06:29 +000034/* New way to come up with the low 16 bits of the magic number:
35 (YEAR-1995) * 10000 + MONTH * 100 + DAY
36 where MONTH and DAY are 1-based.
37 XXX Whatever the "old way" may have been isn't documented.
38 XXX This scheme breaks in 2002, as (2002-1995)*10000 = 70000 doesn't
39 fit in 16 bits.
40 XXX Later, sometimes 1 gets added to MAGIC in order to record that
41 the Unicode -U option is in use. IMO (Tim's), that's a Bad Idea
42 (quite apart from that the -U option doesn't work so isn't used
43 anyway).
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000044
45 XXX MAL, 2002-02-07: I had to modify the MAGIC due to a fix of the
46 UTF-8 encoder (it previously produced invalid UTF-8 for unpaired
47 high surrogates), so I simply bumped the month value to 20 (invalid
48 month) and set the day to 1. This should be recognizable by any
49 algorithm relying on the above scheme. Perhaps we should simply
50 start counting in increments of 10 from now on ?!
51
Michael W. Hudsondd32a912002-08-15 14:59:02 +000052 MWH, 2002-08-03: Removed SET_LINENO. Couldn't be bothered figuring
53 out the MAGIC schemes, so just incremented it by 10.
54
Guido van Rossumf6894922002-08-31 15:16:14 +000055 GvR, 2002-08-31: Because MWH changed the bytecode again, moved the
56 magic number *back* to 62011. This should get the snake-farm to
57 throw away its old .pyc files, amongst others.
58
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000059 Known values:
60 Python 1.5: 20121
61 Python 1.5.1: 20121
62 Python 1.5.2: 20121
63 Python 2.0: 50823
64 Python 2.0.1: 50823
65 Python 2.1: 60202
66 Python 2.1.1: 60202
67 Python 2.1.2: 60202
68 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000069 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000070 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000071 Python 2.3a0: 62011 (!)
Tim Peters36515e22001-11-18 04:06:29 +000072*/
Guido van Rossumf6894922002-08-31 15:16:14 +000073#define MAGIC (62011 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000074
Guido van Rossum96774c12000-05-01 20:19:08 +000075/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000076 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000077 compiler works which are enabled by command line switches. */
78static long pyc_magic = MAGIC;
79
Guido van Rossum25ce5661997-08-02 03:10:38 +000080/* See _PyImport_FixupExtension() below */
81static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000082
Guido van Rossum771c6c81997-10-31 18:37:24 +000083/* This table is defined in config.c: */
84extern struct _inittab _PyImport_Inittab[];
85
86struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000087
Guido van Rossumed1170e1999-12-20 21:23:41 +000088/* these tables define the module suffixes that Python recognizes */
89struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000090
91#ifdef RISCOS
92static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000093 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000094 {"/pyc", "rb", PY_COMPILED},
95 {0, 0}
96};
97#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000098static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000099 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000100#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000101 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000102#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000103 {".pyc", "rb", PY_COMPILED},
104 {0, 0}
105};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000106#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000107
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000108/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
110void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000113 const struct filedescr *scan;
114 struct filedescr *filetab;
115 int countD = 0;
116 int countS = 0;
117
118 /* prepare _PyImport_Filetab: copy entries from
119 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
120 */
121 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
122 ++countD;
123 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
124 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000125 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000126 memcpy(filetab, _PyImport_DynLoadFiletab,
127 countD * sizeof(struct filedescr));
128 memcpy(filetab + countD, _PyImport_StandardFiletab,
129 countS * sizeof(struct filedescr));
130 filetab[countD + countS].suffix = NULL;
131
132 _PyImport_Filetab = filetab;
133
Guido van Rossum0824f631997-03-11 18:37:35 +0000134 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000135 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
136 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000137#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000138 if (strcmp(filetab->suffix, ".pyc") == 0)
139 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000140#else
141 if (strcmp(filetab->suffix, "/pyc") == 0)
142 filetab->suffix = "/pyo";
143#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000144 }
145 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000146
147 if (Py_UnicodeFlag) {
148 /* Fix the pyc_magic so that byte compiled code created
149 using the all-Unicode method doesn't interfere with
150 code created in normal operation mode. */
151 pyc_magic = MAGIC + 1;
152 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
Guido van Rossum25ce5661997-08-02 03:10:38 +0000155void
Just van Rossum52e14d62002-12-30 22:08:05 +0000156_PyImportHooks_Init(void)
157{
158 PyObject *v, *path_hooks = NULL, *zimpimport;
159 int err = 0;
160
161 /* adding sys.path_hooks and sys.path_importer_cache, setting up
162 zipimport */
163
164 if (Py_VerboseFlag)
165 PySys_WriteStderr("# installing zipimport hook\n");
166
167 v = PyList_New(0);
168 if (v == NULL)
169 goto error;
170 err = PySys_SetObject("meta_path", v);
171 Py_DECREF(v);
172 if (err)
173 goto error;
174 v = PyDict_New();
175 if (v == NULL)
176 goto error;
177 err = PySys_SetObject("path_importer_cache", v);
178 Py_DECREF(v);
179 if (err)
180 goto error;
181 path_hooks = PyList_New(0);
182 if (path_hooks == NULL)
183 goto error;
184 err = PySys_SetObject("path_hooks", path_hooks);
185 if (err) {
186 error:
187 PyErr_Print();
188 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
189 "path_importer_cache failed");
190 }
191 zimpimport = PyImport_ImportModule("zipimport");
192 if (zimpimport == NULL) {
193 PyErr_Clear(); /* No zip import module -- okay */
194 if (Py_VerboseFlag)
195 PySys_WriteStderr("# can't import zipimport\n");
196 }
197 else {
198 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
199 "zipimporter");
200 Py_DECREF(zimpimport);
201 if (zipimporter == NULL) {
202 PyErr_Clear(); /* No zipimporter object -- okay */
203 if (Py_VerboseFlag)
204 PySys_WriteStderr(
205 "# can't import zipimport.zimimporter\n");
206 }
207 else {
208 /* sys.path_hooks.append(zipimporter) */
209 err = PyList_Append(path_hooks, zipimporter);
210 Py_DECREF(zipimporter);
211 if (err)
212 goto error;
213 if (Py_VerboseFlag)
214 PySys_WriteStderr(
215 "# installed zipimport hook\n");
216 }
217 }
218 Py_DECREF(path_hooks);
219}
220
221void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000222_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223{
224 Py_XDECREF(extensions);
225 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000226 PyMem_DEL(_PyImport_Filetab);
227 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228}
229
230
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000231/* Locking primitives to prevent parallel imports of the same module
232 in different threads to return with a partially loaded module.
233 These calls are serialized by the global interpreter lock. */
234
235#ifdef WITH_THREAD
236
Guido van Rossum49b56061998-10-01 20:42:43 +0000237#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000238
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000240static long import_lock_thread = -1;
241static int import_lock_level = 0;
242
243static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000245{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000247 if (me == -1)
248 return; /* Too bad */
249 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251 if (import_lock_thread == me) {
252 import_lock_level++;
253 return;
254 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258 PyEval_RestoreThread(tstate);
259 }
260 import_lock_thread = me;
261 import_lock_level = 1;
262}
263
264static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000268 if (me == -1)
269 return; /* Too bad */
270 if (import_lock_thread != me)
271 Py_FatalError("unlock_import: not holding the import lock");
272 import_lock_level--;
273 if (import_lock_level == 0) {
274 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276 }
277}
278
279#else
280
281#define lock_import()
282#define unlock_import()
283
284#endif
285
Tim Peters69232342001-08-30 05:16:13 +0000286static PyObject *
287imp_lock_held(PyObject *self, PyObject *args)
288{
289 if (!PyArg_ParseTuple(args, ":lock_held"))
290 return NULL;
291#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000292 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000293#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000294 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000295#endif
296}
297
Guido van Rossum25ce5661997-08-02 03:10:38 +0000298/* Helper for sys */
299
300PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000302{
303 PyInterpreterState *interp = PyThreadState_Get()->interp;
304 if (interp->modules == NULL)
305 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
306 return interp->modules;
307}
308
Guido van Rossum3f5da241990-12-20 15:06:42 +0000309
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000310/* List of names to clear in sys */
311static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000312 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000313 "exc_type", "exc_value", "exc_traceback",
314 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000315 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000316 NULL
317};
318
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000319static char* sys_files[] = {
320 "stdin", "__stdin__",
321 "stdout", "__stdout__",
322 "stderr", "__stderr__",
323 NULL
324};
325
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000326
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000327/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331{
Guido van Rossum758eec01998-01-19 21:58:26 +0000332 int pos, ndone;
333 char *name;
334 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000336 PyObject *modules = interp->modules;
337
338 if (modules == NULL)
339 return; /* Already done */
340
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000341 /* Delete some special variables first. These are common
342 places where user values hide and people complain when their
343 destructors fail. Since the modules containing them are
344 deleted *last* of all, they would come too late in the normal
345 destruction order. Sigh. */
346
347 value = PyDict_GetItemString(modules, "__builtin__");
348 if (value != NULL && PyModule_Check(value)) {
349 dict = PyModule_GetDict(value);
350 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000351 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000352 PyDict_SetItemString(dict, "_", Py_None);
353 }
354 value = PyDict_GetItemString(modules, "sys");
355 if (value != NULL && PyModule_Check(value)) {
356 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000357 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000358 dict = PyModule_GetDict(value);
359 for (p = sys_deletes; *p != NULL; p++) {
360 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000361 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000362 PyDict_SetItemString(dict, *p, Py_None);
363 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000364 for (p = sys_files; *p != NULL; p+=2) {
365 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000366 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000367 v = PyDict_GetItemString(dict, *(p+1));
368 if (v == NULL)
369 v = Py_None;
370 PyDict_SetItemString(dict, *p, v);
371 }
372 }
373
374 /* First, delete __main__ */
375 value = PyDict_GetItemString(modules, "__main__");
376 if (value != NULL && PyModule_Check(value)) {
377 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000378 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000379 _PyModule_Clear(value);
380 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000381 }
382
Guido van Rossum758eec01998-01-19 21:58:26 +0000383 /* The special treatment of __builtin__ here is because even
384 when it's not referenced as a module, its dictionary is
385 referenced by almost every module's __builtins__. Since
386 deleting a module clears its dictionary (even if there are
387 references left to it), we need to delete the __builtin__
388 module last. Likewise, we don't delete sys until the very
389 end because it is implicitly referenced (e.g. by print).
390
391 Also note that we 'delete' modules by replacing their entry
392 in the modules dict with None, rather than really deleting
393 them; this avoids a rehash of the modules dictionary and
394 also marks them as "non existent" so they won't be
395 re-imported. */
396
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000397 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000398 one (skipping __builtin__ and sys) and delete them */
399 do {
400 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000401 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000402 while (PyDict_Next(modules, &pos, &key, &value)) {
403 if (value->ob_refcnt != 1)
404 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000405 if (PyString_Check(key) && PyModule_Check(value)) {
406 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000407 if (strcmp(name, "__builtin__") == 0)
408 continue;
409 if (strcmp(name, "sys") == 0)
410 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000411 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000412 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000413 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000414 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000415 PyDict_SetItem(modules, key, Py_None);
416 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000417 }
418 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000419 } while (ndone > 0);
420
Guido van Rossum758eec01998-01-19 21:58:26 +0000421 /* Next, delete all modules (still skipping __builtin__ and sys) */
422 pos = 0;
423 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000424 if (PyString_Check(key) && PyModule_Check(value)) {
425 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000426 if (strcmp(name, "__builtin__") == 0)
427 continue;
428 if (strcmp(name, "sys") == 0)
429 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000430 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000431 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000432 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000433 PyDict_SetItem(modules, key, Py_None);
434 }
435 }
436
437 /* Next, delete sys and __builtin__ (in that order) */
438 value = PyDict_GetItemString(modules, "sys");
439 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000440 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000441 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000442 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000443 PyDict_SetItemString(modules, "sys", Py_None);
444 }
445 value = PyDict_GetItemString(modules, "__builtin__");
446 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000447 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000448 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000449 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000450 PyDict_SetItemString(modules, "__builtin__", Py_None);
451 }
452
453 /* Finally, clear and delete the modules directory */
454 PyDict_Clear(modules);
455 interp->modules = NULL;
456 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000457}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000458
459
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000460/* Helper for pythonrun.c -- return magic number */
461
462long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000463PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464{
Guido van Rossum96774c12000-05-01 20:19:08 +0000465 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466}
467
468
Guido van Rossum25ce5661997-08-02 03:10:38 +0000469/* Magic for extension modules (built-in as well as dynamically
470 loaded). To prevent initializing an extension module more than
471 once, we keep a static dictionary 'extensions' keyed by module name
472 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000473 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000474 dictionary is stored by calling _PyImport_FixupExtension()
475 immediately after the module initialization function succeeds. A
476 copy can be retrieved from there by calling
477 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000478
Guido van Rossum79f25d91997-04-29 20:08:16 +0000479PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000480_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000482 PyObject *modules, *mod, *dict, *copy;
483 if (extensions == NULL) {
484 extensions = PyDict_New();
485 if (extensions == NULL)
486 return NULL;
487 }
488 modules = PyImport_GetModuleDict();
489 mod = PyDict_GetItemString(modules, name);
490 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000491 PyErr_Format(PyExc_SystemError,
492 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000493 return NULL;
494 }
495 dict = PyModule_GetDict(mod);
496 if (dict == NULL)
497 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000498 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000499 if (copy == NULL)
500 return NULL;
501 PyDict_SetItemString(extensions, filename, copy);
502 Py_DECREF(copy);
503 return copy;
504}
505
506PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000507_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000508{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000509 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000510 if (extensions == NULL)
511 return NULL;
512 dict = PyDict_GetItemString(extensions, filename);
513 if (dict == NULL)
514 return NULL;
515 mod = PyImport_AddModule(name);
516 if (mod == NULL)
517 return NULL;
518 mdict = PyModule_GetDict(mod);
519 if (mdict == NULL)
520 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000521 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000522 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000523 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000524 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000525 name, filename);
526 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000527}
528
529
530/* Get the module object corresponding to a module name.
531 First check the modules dictionary if there's one there,
532 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000533 Because the former action is most common, THIS DOES NOT RETURN A
534 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000535
Guido van Rossum79f25d91997-04-29 20:08:16 +0000536PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000537PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000539 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546 if (m == NULL)
547 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000548 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 return NULL;
551 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553
554 return m;
555}
556
557
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000558/* Execute a code object in a module and return the module object
559 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000562PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000564 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
565}
566
567PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000568PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000569{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000570 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574 if (m == NULL)
575 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 d = PyModule_GetDict(m);
577 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
578 if (PyDict_SetItemString(d, "__builtins__",
579 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000580 return NULL;
581 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000582 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000583 v = NULL;
584 if (pathname != NULL) {
585 v = PyString_FromString(pathname);
586 if (v == NULL)
587 PyErr_Clear();
588 }
589 if (v == NULL) {
590 v = ((PyCodeObject *)co)->co_filename;
591 Py_INCREF(v);
592 }
593 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000595 Py_DECREF(v);
596
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000597 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000598 if (v == NULL)
599 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000601
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000603 PyErr_Format(PyExc_ImportError,
604 "Loaded module %.200s not found in sys.modules",
605 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000606 return NULL;
607 }
608
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610
611 return m;
612}
613
614
615/* Given a pathname for a Python source file, fill a buffer with the
616 pathname for the corresponding compiled file. Return the pathname
617 for the compiled file, or NULL if there's no space in the buffer.
618 Doesn't set an exception. */
619
620static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000621make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000622{
Tim Petersc1731372001-08-04 08:12:36 +0000623 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624 if (len+2 > buflen)
625 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000626
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000627#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000628 /* Treat .pyw as if it were .py. The case of ".pyw" must match
629 that used in _PyImport_StandardFiletab. */
630 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
631 --len; /* pretend 'w' isn't there */
632#endif
633 memcpy(buf, pathname, len);
634 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
635 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636
637 return buf;
638}
639
640
641/* Given a pathname for a Python source file, its time of last
642 modification, and a pathname for a compiled file, check whether the
643 compiled file represents the same version of the source. If so,
644 return a FILE pointer for the compiled file, positioned just after
645 the header; if not, return NULL.
646 Doesn't set an exception. */
647
648static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000649check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650{
651 FILE *fp;
652 long magic;
653 long pyc_mtime;
654
655 fp = fopen(cpathname, "rb");
656 if (fp == NULL)
657 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000659 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000661 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662 fclose(fp);
663 return NULL;
664 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000668 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669 fclose(fp);
670 return NULL;
671 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000673 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 return fp;
675}
676
677
678/* Read a code object from a file and check it for validity */
679
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000681read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000682{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684
Tim Petersd9b9ac82001-01-28 00:27:39 +0000685 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687 if (co == NULL || !PyCode_Check(co)) {
688 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000689 PyErr_Format(PyExc_ImportError,
690 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 return NULL;
693 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695}
696
697
698/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000699 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000702load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703{
704 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705 PyCodeObject *co;
706 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000709 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000710 PyErr_Format(PyExc_ImportError,
711 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712 return NULL;
713 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000715 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716 if (co == NULL)
717 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000719 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000720 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000721 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723
724 return m;
725}
726
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000727/* Parse a source file and return the corresponding code object */
728
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000730parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000733 node *n;
734
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000735 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736 if (n == NULL)
737 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 co = PyNode_Compile(n, pathname);
739 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740
741 return co;
742}
743
744
Guido van Rossum55a83382000-09-20 20:31:38 +0000745/* Helper to open a bytecode file for writing in exclusive mode */
746
747static FILE *
748open_exclusive(char *filename)
749{
750#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
751 /* Use O_EXCL to avoid a race condition when another process tries to
752 write the same file. When that happens, our open() call fails,
753 which is just fine (since it's only a cache).
754 XXX If the file exists and is writable but the directory is not
755 writable, the file will never be written. Oh well.
756 */
757 int fd;
758 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000759 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
760#ifdef O_BINARY
761 |O_BINARY /* necessary for Windows */
762#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000763#ifdef __VMS
764 , 0666, "ctxt=bin", "shr=nil");
765#else
766 , 0666);
767#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000768 if (fd < 0)
769 return NULL;
770 return fdopen(fd, "wb");
771#else
772 /* Best we can do -- on Windows this can't happen anyway */
773 return fopen(filename, "wb");
774#endif
775}
776
777
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778/* Write a compiled module to a file, placing the time of last
779 modification of its source into the header.
780 Errors are ignored, if a write error occurs an attempt is made to
781 remove the file. */
782
783static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000784write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785{
786 FILE *fp;
787
Guido van Rossum55a83382000-09-20 20:31:38 +0000788 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000789 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000791 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792 "# can't create %s\n", cpathname);
793 return;
794 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000795 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000796 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 PyMarshal_WriteLongToFile(0L, fp);
798 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000799 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000801 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802 /* Don't keep partial file */
803 fclose(fp);
804 (void) unlink(cpathname);
805 return;
806 }
807 /* Now write the true mtime */
808 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810 fflush(fp);
811 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000813 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000814#ifdef macintosh
Jack Jansencbf630f2000-07-11 21:59:16 +0000815 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816#endif
817}
818
819
820/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000821 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
822 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000823
Guido van Rossum79f25d91997-04-29 20:08:16 +0000824static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000825load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826{
Fred Drake4c82b232000-06-30 16:18:57 +0000827 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828 FILE *fpc;
829 char buf[MAXPATHLEN+1];
830 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000831 PyCodeObject *co;
832 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000833
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000834 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000835 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000836 return NULL;
837#if SIZEOF_TIME_T > 4
838 /* Python's .pyc timestamp handling presumes that the timestamp fits
839 in 4 bytes. This will be fine until sometime in the year 2038,
840 when a 4-byte signed time_t will overflow.
841 */
842 if (mtime >> 32) {
843 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000844 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000845 return NULL;
846 }
847#endif
Tim Peters36515e22001-11-18 04:06:29 +0000848 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000849 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000850 if (cpathname != NULL &&
851 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000852 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853 fclose(fpc);
854 if (co == NULL)
855 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000856 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000857 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000859 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 }
861 else {
862 co = parse_source_module(pathname, fp);
863 if (co == NULL)
864 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000866 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000867 name, pathname);
868 write_compiled_module(co, cpathname, mtime);
869 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000870 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872
873 return m;
874}
875
876
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000877/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000878static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
879static struct filedescr *find_module(char *, char *, PyObject *,
880 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000881static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000882
883/* Load a package and return its module object WITH INCREMENTED
884 REFERENCE COUNT */
885
886static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000887load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000888{
889 PyObject *m, *d, *file, *path;
890 int err;
891 char buf[MAXPATHLEN+1];
892 FILE *fp = NULL;
893 struct filedescr *fdp;
894
895 m = PyImport_AddModule(name);
896 if (m == NULL)
897 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000898 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000899 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000900 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000901 d = PyModule_GetDict(m);
902 file = PyString_FromString(pathname);
903 if (file == NULL)
904 return NULL;
905 path = Py_BuildValue("[O]", file);
906 if (path == NULL) {
907 Py_DECREF(file);
908 return NULL;
909 }
910 err = PyDict_SetItemString(d, "__file__", file);
911 if (err == 0)
912 err = PyDict_SetItemString(d, "__path__", path);
913 if (err != 0) {
914 m = NULL;
915 goto cleanup;
916 }
917 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000918 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000919 if (fdp == NULL) {
920 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
921 PyErr_Clear();
922 }
923 else
924 m = NULL;
925 goto cleanup;
926 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000927 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000928 if (fp != NULL)
929 fclose(fp);
930 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931 Py_XDECREF(path);
932 Py_XDECREF(file);
933 return m;
934}
935
936
937/* Helper to test for built-in module */
938
939static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000940is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000941{
942 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000943 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
944 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
945 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000946 return -1;
947 else
948 return 1;
949 }
950 }
951 return 0;
952}
953
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000954
Just van Rossum52e14d62002-12-30 22:08:05 +0000955/* Return an importer object for a sys.path/pkg.__path__ item 'p',
956 possibly by fetching it from the path_importer_cache dict. If it
957 wasn't yet cached, traverse path_hooks until it a hook is found
958 that can handle the path item. Return None if no hook could;
959 this tells our caller it should fall back to the builtin
960 import mechanism. Cache the result in path_importer_cache.
961 Returns a borrowed reference. */
962
963static PyObject *
964get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
965 PyObject *p)
966{
967 PyObject *importer;
968 int j, nhooks;
969
970 /* These conditions are the caller's responsibility: */
971 assert(PyList_Check(path_hooks));
972 assert(PyDict_Check(path_importer_cache));
973
974 nhooks = PyList_Size(path_hooks);
975 if (nhooks < 0)
976 return NULL; /* Shouldn't happen */
977
978 importer = PyDict_GetItem(path_importer_cache, p);
979 if (importer != NULL)
980 return importer;
981
982 /* set path_importer_cache[p] to None to avoid recursion */
983 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
984 return NULL;
985
986 for (j = 0; j < nhooks; j++) {
987 PyObject *hook = PyList_GetItem(path_hooks, j);
988 if (hook == NULL)
989 return NULL;
990 importer = PyObject_CallFunction(hook, "O", p);
991 if (importer != NULL)
992 break;
993
994 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
995 return NULL;
996 }
997 PyErr_Clear();
998 }
999 if (importer == NULL)
1000 importer = Py_None;
1001 else if (importer != Py_None) {
1002 int err = PyDict_SetItem(path_importer_cache, p, importer);
1003 Py_DECREF(importer);
1004 if (err != 0)
1005 return NULL;
1006 }
1007 return importer;
1008}
1009
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001010/* Search the path (default sys.path) for a module. Return the
1011 corresponding filedescr struct, and (via return arguments) the
1012 pathname and an open file. Return NULL if the module is not found. */
1013
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001014#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001015extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1016 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001017#endif
1018
Tim Peters50d8d372001-02-28 05:34:27 +00001019static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001020static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001021static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001022
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001023static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001024find_module(char *fullname, char *subname, PyObject *path, char *buf,
1025 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001026{
Fred Drake4c82b232000-06-30 16:18:57 +00001027 int i, npath;
1028 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001029 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001030 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001031 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001032 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001033#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001034 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001035#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001036 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1037 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1038 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001039 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001040#if defined(PYOS_OS2)
1041 size_t saved_len;
1042 size_t saved_namelen;
1043 char *saved_buf = NULL;
1044#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001045 if (p_loader != NULL)
1046 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001047
Just van Rossum52e14d62002-12-30 22:08:05 +00001048 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001049 PyErr_SetString(PyExc_OverflowError,
1050 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001051 return NULL;
1052 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001053 strcpy(name, subname);
1054
1055 /* sys.meta_path import hook */
1056 if (p_loader != NULL) {
1057 PyObject *meta_path;
1058
1059 meta_path = PySys_GetObject("meta_path");
1060 if (meta_path == NULL || !PyList_Check(meta_path)) {
1061 PyErr_SetString(PyExc_ImportError,
1062 "sys.meta_path must be a list of "
1063 "import hooks");
1064 return NULL;
1065 }
1066 Py_INCREF(meta_path); /* zap guard */
1067 npath = PyList_Size(meta_path);
1068 for (i = 0; i < npath; i++) {
1069 PyObject *loader;
1070 PyObject *hook = PyList_GetItem(meta_path, i);
1071 loader = PyObject_CallMethod(hook, "find_module",
1072 "sO", fullname,
1073 path != NULL ?
1074 path : Py_None);
1075 if (loader == NULL) {
1076 Py_DECREF(meta_path);
1077 return NULL; /* true error */
1078 }
1079 if (loader != Py_None) {
1080 /* a loader was found */
1081 *p_loader = loader;
1082 Py_DECREF(meta_path);
1083 return &importhookdescr;
1084 }
1085 Py_DECREF(loader);
1086 }
1087 Py_DECREF(meta_path);
1088 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001089
1090 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001091 /* The only type of submodule allowed inside a "frozen"
1092 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001093 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1094 PyErr_SetString(PyExc_ImportError,
1095 "full frozen module name too long");
1096 return NULL;
1097 }
1098 strcpy(buf, PyString_AsString(path));
1099 strcat(buf, ".");
1100 strcat(buf, name);
1101 strcpy(name, buf);
Jack Jansen550fdae2001-10-30 13:08:39 +00001102#ifdef macintosh
1103 /* Freezing on the mac works different, and the modules are
1104 ** actually on sys.path. So we don't take the quick exit but
1105 ** continue with the normal flow.
1106 */
1107 path = NULL;
1108#else
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001109 if (find_frozen(name) != NULL) {
1110 strcpy(buf, name);
1111 return &fd_frozen;
1112 }
1113 PyErr_Format(PyExc_ImportError,
1114 "No frozen submodule named %.200s", name);
1115 return NULL;
Jack Jansen550fdae2001-10-30 13:08:39 +00001116#endif
Guido van Rossum0506a431998-08-11 15:07:39 +00001117 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001118 if (path == NULL) {
1119 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001120 strcpy(buf, name);
1121 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001122 }
Greg Ward201baee2001-10-04 14:52:06 +00001123 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001124 strcpy(buf, name);
1125 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001126 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001127
Guido van Rossumac279101996-08-22 23:10:58 +00001128#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001129 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1130 if (fp != NULL) {
1131 *p_fp = fp;
1132 return fdp;
1133 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001134#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001135 path = PySys_GetObject("path");
1136 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 if (path == NULL || !PyList_Check(path)) {
1138 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001139 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001140 return NULL;
1141 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001142
1143 path_hooks = PySys_GetObject("path_hooks");
1144 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1145 PyErr_SetString(PyExc_ImportError,
1146 "sys.path_hooks must be a list of "
1147 "import hooks");
1148 return NULL;
1149 }
1150 path_importer_cache = PySys_GetObject("path_importer_cache");
1151 if (path_importer_cache == NULL ||
1152 !PyDict_Check(path_importer_cache)) {
1153 PyErr_SetString(PyExc_ImportError,
1154 "sys.path_importer_cache must be a dict");
1155 return NULL;
1156 }
1157
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001159 namelen = strlen(name);
1160 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001161 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001163#ifdef Py_USING_UNICODE
1164 if (PyUnicode_Check(v)) {
1165 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1166 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1167 if (copy == NULL)
1168 return NULL;
1169 v = copy;
1170 }
1171 else
1172#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001174 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001176 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1177 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001179 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001181 if (strlen(buf) != len) {
1182 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001184 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001185
1186 /* sys.path_hooks import hook */
1187 if (p_loader != NULL) {
1188 PyObject *importer;
1189
1190 importer = get_path_importer(path_importer_cache,
1191 path_hooks, v);
1192 if (importer == NULL)
1193 return NULL;
1194 /* Note: importer is a borrowed reference */
1195 if (importer != Py_None) {
1196 PyObject *loader;
1197 loader = PyObject_CallMethod(importer,
1198 "find_module",
1199 "s", fullname);
1200 if (loader == NULL)
1201 return NULL; /* error */
1202 if (loader != Py_None) {
1203 /* a loader was found */
1204 *p_loader = loader;
1205 return &importhookdescr;
1206 }
1207 Py_DECREF(loader);
1208 }
1209 /* no hook was successful, use builtin import */
1210 }
1211
Jack Jansen9c96a921995-02-15 22:57:06 +00001212#ifdef macintosh
Tim Peters50d8d372001-02-28 05:34:27 +00001213 /*
Guido van Rossum741689d1997-08-12 14:53:39 +00001214 ** Speedup: each sys.path item is interned, and
1215 ** FindResourceModule remembers which items refer to
1216 ** folders (so we don't have to bother trying to look
Tim Peters50d8d372001-02-28 05:34:27 +00001217 ** into them for resources).
Guido van Rossum741689d1997-08-12 14:53:39 +00001218 */
1219 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
1220 v = PyList_GET_ITEM(path, i);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001221 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 static struct filedescr resfiledescr =
1223 {"", "", PY_RESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +00001224
Walter Dörwald3430d702002-06-17 10:43:59 +00001225 Py_XDECREF(copy);
Jack Jansen9c96a921995-02-15 22:57:06 +00001226 return &resfiledescr;
1227 }
Guido van Rossum0f84a341998-08-06 13:36:01 +00001228 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
1229 static struct filedescr resfiledescr =
1230 {"", "", PY_CODERESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +00001231
Walter Dörwald3430d702002-06-17 10:43:59 +00001232 Py_XDECREF(copy);
Guido van Rossum0f84a341998-08-06 13:36:01 +00001233 return &resfiledescr;
1234 }
Jack Jansen9c96a921995-02-15 22:57:06 +00001235#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001236 if (len > 0 && buf[len-1] != SEP
1237#ifdef ALTSEP
1238 && buf[len-1] != ALTSEP
1239#endif
1240 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001241 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001242 strcpy(buf+len, name);
1243 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001244
1245 /* Check for package import (buf holds a directory name,
1246 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001247#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001248 if (stat(buf, &statbuf) == 0 && /* it exists */
1249 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1250 find_init_module(buf) && /* it has __init__.py */
1251 case_ok(buf, len, namelen, name)) { /* and case matches */
1252 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001253 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001254 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001255#else
1256 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001257#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001258 if (isdir(buf) &&
1259 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001260 case_ok(buf, len, namelen, name)) {
1261 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001262 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001263 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001264#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001265#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001266#ifdef macintosh
1267 fdp = PyMac_FindModuleExtension(buf, &len, name);
Jack Jansen4df3c522001-03-20 23:09:54 +00001268 if (fdp) {
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001269#else
Andrew MacIntyred9400542002-02-26 11:41:34 +00001270#if defined(PYOS_OS2)
1271 /* take a snapshot of the module spec for restoration
1272 * after the 8 character DLL hackery
1273 */
1274 saved_buf = strdup(buf);
1275 saved_len = len;
1276 saved_namelen = namelen;
1277#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001278 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001279#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001280 /* OS/2 limits DLLs to 8 character names (w/o
1281 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001282 * so if the name is longer than that and its a
1283 * dynamically loaded module we're going to try,
1284 * truncate the name before trying
1285 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001286 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001287 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001288 const struct filedescr *scan;
1289 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001290 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001291 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001292 break;
1293 else
1294 scan++;
1295 }
1296 if (scan->suffix != NULL) {
1297 /* yes, so truncate the name */
1298 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001299 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001300 buf[len] = '\0';
1301 }
1302 }
1303#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001304 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001305 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001306 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansen4df3c522001-03-20 23:09:54 +00001307#endif /* !macintosh */
Jack Jansenc88da1f2002-05-28 10:58:19 +00001308 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001309 if (filemode[0] == 'U')
1310 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001311 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001312 if (fp != NULL) {
1313 if (case_ok(buf, len, namelen, name))
1314 break;
1315 else { /* continue search */
1316 fclose(fp);
1317 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001318 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001319 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001320#if defined(PYOS_OS2)
1321 /* restore the saved snapshot */
1322 strcpy(buf, saved_buf);
1323 len = saved_len;
1324 namelen = saved_namelen;
1325#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001326 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001327#if defined(PYOS_OS2)
1328 /* don't need/want the module name snapshot anymore */
1329 if (saved_buf)
1330 {
1331 free(saved_buf);
1332 saved_buf = NULL;
1333 }
1334#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001335 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001336 if (fp != NULL)
1337 break;
1338 }
1339 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001340 PyErr_Format(PyExc_ImportError,
1341 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001342 return NULL;
1343 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001344 *p_fp = fp;
1345 return fdp;
1346}
1347
Tim Petersd1e87a82001-03-01 18:12:00 +00001348/* case_ok(char* buf, int len, int namelen, char* name)
1349 * The arguments here are tricky, best shown by example:
1350 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1351 * ^ ^ ^ ^
1352 * |--------------------- buf ---------------------|
1353 * |------------------- len ------------------|
1354 * |------ name -------|
1355 * |----- namelen -----|
1356 * buf is the full path, but len only counts up to (& exclusive of) the
1357 * extension. name is the module name, also exclusive of extension.
1358 *
1359 * We've already done a successful stat() or fopen() on buf, so know that
1360 * there's some match, possibly case-insensitive.
1361 *
Tim Peters50d8d372001-02-28 05:34:27 +00001362 * case_ok() is to return 1 if there's a case-sensitive match for
1363 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1364 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001365 *
Tim Peters50d8d372001-02-28 05:34:27 +00001366 * case_ok() is used to implement case-sensitive import semantics even
1367 * on platforms with case-insensitive filesystems. It's trivial to implement
1368 * for case-sensitive filesystems. It's pretty much a cross-platform
1369 * nightmare for systems with case-insensitive filesystems.
1370 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001371
Tim Peters50d8d372001-02-28 05:34:27 +00001372/* First we may need a pile of platform-specific header files; the sequence
1373 * of #if's here should match the sequence in the body of case_ok().
1374 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001375#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001376#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001377#ifdef __CYGWIN__
1378#include <sys/cygwin.h>
1379#endif
1380
Tim Peters50d8d372001-02-28 05:34:27 +00001381#elif defined(DJGPP)
1382#include <dir.h>
1383
1384#elif defined(macintosh)
1385#include <TextUtils.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001386
Tim Petersd1e87a82001-03-01 18:12:00 +00001387#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001388#include <sys/types.h>
1389#include <dirent.h>
1390
Andrew MacIntyred9400542002-02-26 11:41:34 +00001391#elif defined(PYOS_OS2)
1392#define INCL_DOS
1393#define INCL_DOSERRORS
1394#define INCL_NOPMAPI
1395#include <os2.h>
1396
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001397#elif defined(RISCOS)
1398#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001399#endif
1400
Guido van Rossum0980bd91998-02-13 17:18:36 +00001401static int
Tim Peters50d8d372001-02-28 05:34:27 +00001402case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001403{
Tim Peters50d8d372001-02-28 05:34:27 +00001404/* Pick a platform-specific implementation; the sequence of #if's here should
1405 * match the sequence just above.
1406 */
1407
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001408/* MS_WINDOWS || __CYGWIN__ */
1409#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001410 WIN32_FIND_DATA data;
1411 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001412#ifdef __CYGWIN__
1413 char tempbuf[MAX_PATH];
1414#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001415
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001416 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001417 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001418
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001419#ifdef __CYGWIN__
1420 cygwin32_conv_to_win32_path(buf, tempbuf);
1421 h = FindFirstFile(tempbuf, &data);
1422#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001423 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001424#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001425 if (h == INVALID_HANDLE_VALUE) {
1426 PyErr_Format(PyExc_NameError,
1427 "Can't find file for module %.100s\n(filename %.300s)",
1428 name, buf);
1429 return 0;
1430 }
1431 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001432 return strncmp(data.cFileName, name, namelen) == 0;
1433
1434/* DJGPP */
1435#elif defined(DJGPP)
1436 struct ffblk ffblk;
1437 int done;
1438
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001439 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001440 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001441
1442 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1443 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001444 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001445 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001446 name, buf);
1447 return 0;
1448 }
Tim Peters50d8d372001-02-28 05:34:27 +00001449 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001450
Tim Peters50d8d372001-02-28 05:34:27 +00001451/* macintosh */
1452#elif defined(macintosh)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001453 FSSpec fss;
1454 OSErr err;
Tim Peters50d8d372001-02-28 05:34:27 +00001455
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001456 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Peters50d8d372001-02-28 05:34:27 +00001457 return 1;
1458
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001459 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001460 if (err) {
1461 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001462 "Can't find file for module %.100s\n(filename %.300s)",
1463 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001464 return 0;
1465 }
Tim Peters50d8d372001-02-28 05:34:27 +00001466 return fss.name[0] >= namelen &&
1467 strncmp(name, (char *)fss.name+1, namelen) == 0;
1468
Tim Peters677898a2001-03-02 03:28:03 +00001469/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001470#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001471 DIR *dirp;
1472 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001473 char dirname[MAXPATHLEN + 1];
1474 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001475
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001476 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001477 return 1;
1478
Tim Petersd1e87a82001-03-01 18:12:00 +00001479 /* Copy the dir component into dirname; substitute "." if empty */
1480 if (dirlen <= 0) {
1481 dirname[0] = '.';
1482 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001483 }
1484 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001485 assert(dirlen <= MAXPATHLEN);
1486 memcpy(dirname, buf, dirlen);
1487 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001488 }
1489 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001490 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001491 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001492 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001493 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001494 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001495#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001496 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001497#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001498 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001499#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001500 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001501 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001502 (void)closedir(dirp);
1503 return 1; /* Found */
1504 }
1505 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001506 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001507 }
Tim Peters430f5d42001-03-01 01:30:56 +00001508 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001509
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001510/* RISC OS */
1511#elif defined(RISCOS)
1512 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1513 char buf2[MAXPATHLEN+2];
1514 char *nameWithExt = buf+len-namelen;
1515 int canonlen;
1516 os_error *e;
1517
1518 if (Py_GETENV("PYTHONCASEOK") != NULL)
1519 return 1;
1520
1521 /* workaround:
1522 append wildcard, otherwise case of filename wouldn't be touched */
1523 strcpy(buf2, buf);
1524 strcat(buf2, "*");
1525
1526 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1527 canonlen = MAXPATHLEN+1-canonlen;
1528 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1529 return 0;
1530 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1531 return 1; /* match */
1532
1533 return 0;
1534
Andrew MacIntyred9400542002-02-26 11:41:34 +00001535/* OS/2 */
1536#elif defined(PYOS_OS2)
1537 HDIR hdir = 1;
1538 ULONG srchcnt = 1;
1539 FILEFINDBUF3 ffbuf;
1540 APIRET rc;
1541
1542 if (getenv("PYTHONCASEOK") != NULL)
1543 return 1;
1544
1545 rc = DosFindFirst(buf,
1546 &hdir,
1547 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1548 &ffbuf, sizeof(ffbuf),
1549 &srchcnt,
1550 FIL_STANDARD);
1551 if (rc != NO_ERROR)
1552 return 0;
1553 return strncmp(ffbuf.achName, name, namelen) == 0;
1554
Tim Peters50d8d372001-02-28 05:34:27 +00001555/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1556#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001557 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001558
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001559#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001560}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001561
Guido van Rossum0980bd91998-02-13 17:18:36 +00001562
Guido van Rossum197346f1997-10-31 18:38:52 +00001563#ifdef HAVE_STAT
1564/* Helper to look for __init__.py or __init__.py[co] in potential package */
1565static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001566find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001567{
Tim Peters0f9431f2001-07-05 03:47:53 +00001568 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001569 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001570 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001571 struct stat statbuf;
1572
Tim Peters0f9431f2001-07-05 03:47:53 +00001573/* For calling case_ok(buf, len, namelen, name):
1574 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1575 * ^ ^ ^ ^
1576 * |--------------------- buf ---------------------|
1577 * |------------------- len ------------------|
1578 * |------ name -------|
1579 * |----- namelen -----|
1580 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001581 if (save_len + 13 >= MAXPATHLEN)
1582 return 0;
1583 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001584 pname = buf + i;
1585 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001586 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001587 if (case_ok(buf,
1588 save_len + 9, /* len("/__init__") */
1589 8, /* len("__init__") */
1590 pname)) {
1591 buf[save_len] = '\0';
1592 return 1;
1593 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001594 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001595 i += strlen(pname);
1596 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001597 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001598 if (case_ok(buf,
1599 save_len + 9, /* len("/__init__") */
1600 8, /* len("__init__") */
1601 pname)) {
1602 buf[save_len] = '\0';
1603 return 1;
1604 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001605 }
1606 buf[save_len] = '\0';
1607 return 0;
1608}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001609
1610#else
1611
1612#ifdef RISCOS
1613static int
1614find_init_module(buf)
1615 char *buf;
1616{
1617 int save_len = strlen(buf);
1618 int i = save_len;
1619
1620 if (save_len + 13 >= MAXPATHLEN)
1621 return 0;
1622 buf[i++] = SEP;
1623 strcpy(buf+i, "__init__/py");
1624 if (isfile(buf)) {
1625 buf[save_len] = '\0';
1626 return 1;
1627 }
1628
1629 if (Py_OptimizeFlag)
1630 strcpy(buf+i, "o");
1631 else
1632 strcpy(buf+i, "c");
1633 if (isfile(buf)) {
1634 buf[save_len] = '\0';
1635 return 1;
1636 }
1637 buf[save_len] = '\0';
1638 return 0;
1639}
1640#endif /*RISCOS*/
1641
Guido van Rossum197346f1997-10-31 18:38:52 +00001642#endif /* HAVE_STAT */
1643
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001644
Tim Petersdbd9ba62000-07-09 03:09:57 +00001645static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001646
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001647/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001648 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001649
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001651load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001652{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001653 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001654 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001655 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001656
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001657 /* First check that there's an open file (if we need one) */
1658 switch (type) {
1659 case PY_SOURCE:
1660 case PY_COMPILED:
1661 if (fp == NULL) {
1662 PyErr_Format(PyExc_ValueError,
1663 "file object required for import (type code %d)",
1664 type);
1665 return NULL;
1666 }
1667 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001668
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001669 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001670
1671 case PY_SOURCE:
1672 m = load_source_module(name, buf, fp);
1673 break;
1674
1675 case PY_COMPILED:
1676 m = load_compiled_module(name, buf, fp);
1677 break;
1678
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001679#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001682 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001683#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001684
Jack Jansen9c96a921995-02-15 22:57:06 +00001685#ifdef macintosh
1686 case PY_RESOURCE:
1687 m = PyMac_LoadResourceModule(name, buf);
1688 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001689 case PY_CODERESOURCE:
1690 m = PyMac_LoadCodeResourceModule(name, buf);
1691 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001692#endif
1693
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001694 case PKG_DIRECTORY:
1695 m = load_package(name, buf);
1696 break;
1697
1698 case C_BUILTIN:
1699 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001700 if (buf != NULL && buf[0] != '\0')
1701 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001702 if (type == C_BUILTIN)
1703 err = init_builtin(name);
1704 else
1705 err = PyImport_ImportFrozenModule(name);
1706 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001707 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001708 if (err == 0) {
1709 PyErr_Format(PyExc_ImportError,
1710 "Purported %s module %.200s not found",
1711 type == C_BUILTIN ?
1712 "builtin" : "frozen",
1713 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001714 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001715 }
1716 modules = PyImport_GetModuleDict();
1717 m = PyDict_GetItemString(modules, name);
1718 if (m == NULL) {
1719 PyErr_Format(
1720 PyExc_ImportError,
1721 "%s module %.200s not properly initialized",
1722 type == C_BUILTIN ?
1723 "builtin" : "frozen",
1724 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001725 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001726 }
1727 Py_INCREF(m);
1728 break;
1729
Just van Rossum52e14d62002-12-30 22:08:05 +00001730 case IMP_HOOK: {
1731 if (loader == NULL) {
1732 PyErr_SetString(PyExc_ImportError,
1733 "import hook without loader");
1734 return NULL;
1735 }
1736 m = PyObject_CallMethod(loader, "load_module", "s", name);
1737 break;
1738 }
1739
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001741 PyErr_Format(PyExc_ImportError,
1742 "Don't know how to import %.200s (type code %d)",
1743 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001744 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001745
1746 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001747
1748 return m;
1749}
1750
1751
1752/* Initialize a built-in module.
1753 Return 1 for succes, 0 if the module is not found, and -1 with
1754 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001755
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001756static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001757init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001758{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001759 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001760
Greg Ward201baee2001-10-04 14:52:06 +00001761 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001762 return 1;
1763
Guido van Rossum771c6c81997-10-31 18:37:24 +00001764 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001765 if (strcmp(name, p->name) == 0) {
1766 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001767 PyErr_Format(PyExc_ImportError,
1768 "Cannot re-init internal module %.200s",
1769 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001770 return -1;
1771 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001773 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001774 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001777 if (_PyImport_FixupExtension(name, name) == NULL)
1778 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001779 return 1;
1780 }
1781 }
1782 return 0;
1783}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001784
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001785
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001786/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001787
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001788static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001789find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001790{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001791 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001792
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001794 if (p->name == NULL)
1795 return NULL;
1796 if (strcmp(p->name, name) == 0)
1797 break;
1798 }
1799 return p;
1800}
1801
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001803get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001804{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001805 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001806 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001807
1808 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001809 PyErr_Format(PyExc_ImportError,
1810 "No such frozen object named %.200s",
1811 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001812 return NULL;
1813 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001814 if (p->code == NULL) {
1815 PyErr_Format(PyExc_ImportError,
1816 "Excluded frozen object named %.200s",
1817 name);
1818 return NULL;
1819 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001820 size = p->size;
1821 if (size < 0)
1822 size = -size;
1823 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001824}
1825
1826/* Initialize a frozen module.
1827 Return 1 for succes, 0 if the module is not found, and -1 with
1828 an exception set if the initialization failed.
1829 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001830
1831int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001832PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001833{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001834 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 PyObject *co;
1836 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001837 int ispackage;
1838 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001839
1840 if (p == NULL)
1841 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001842 if (p->code == NULL) {
1843 PyErr_Format(PyExc_ImportError,
1844 "Excluded frozen object named %.200s",
1845 name);
1846 return -1;
1847 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001848 size = p->size;
1849 ispackage = (size < 0);
1850 if (ispackage)
1851 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001853 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001854 name, ispackage ? " package" : "");
1855 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001856 if (co == NULL)
1857 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 if (!PyCode_Check(co)) {
1859 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001860 PyErr_Format(PyExc_TypeError,
1861 "frozen object %.200s is not a code object",
1862 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001863 return -1;
1864 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001865 if (ispackage) {
1866 /* Set __path__ to the package name */
1867 PyObject *d, *s;
1868 int err;
1869 m = PyImport_AddModule(name);
1870 if (m == NULL)
1871 return -1;
1872 d = PyModule_GetDict(m);
1873 s = PyString_InternFromString(name);
1874 if (s == NULL)
1875 return -1;
1876 err = PyDict_SetItemString(d, "__path__", s);
1877 Py_DECREF(s);
1878 if (err != 0)
1879 return err;
1880 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001881 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001883 if (m == NULL)
1884 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001886 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001887}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001888
1889
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001890/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001891 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001892
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001894PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001895{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001896 PyObject *pname;
1897 PyObject *result;
1898
1899 pname = PyString_FromString(name);
1900 result = PyImport_Import(pname);
1901 Py_DECREF(pname);
1902 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001903}
1904
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001905/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001906static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1907static PyObject *load_next(PyObject *mod, PyObject *altmod,
1908 char **p_name, char *buf, int *p_buflen);
1909static int mark_miss(char *name);
1910static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1911 char *buf, int buflen, int recursive);
1912static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001913
1914/* The Magnum Opus of dotted-name import :-) */
1915
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001917import_module_ex(char *name, PyObject *globals, PyObject *locals,
1918 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001919{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001920 char buf[MAXPATHLEN+1];
1921 int buflen = 0;
1922 PyObject *parent, *head, *next, *tail;
1923
1924 parent = get_parent(globals, buf, &buflen);
1925 if (parent == NULL)
1926 return NULL;
1927
1928 head = load_next(parent, Py_None, &name, buf, &buflen);
1929 if (head == NULL)
1930 return NULL;
1931
1932 tail = head;
1933 Py_INCREF(tail);
1934 while (name) {
1935 next = load_next(tail, tail, &name, buf, &buflen);
1936 Py_DECREF(tail);
1937 if (next == NULL) {
1938 Py_DECREF(head);
1939 return NULL;
1940 }
1941 tail = next;
1942 }
1943
1944 if (fromlist != NULL) {
1945 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1946 fromlist = NULL;
1947 }
1948
1949 if (fromlist == NULL) {
1950 Py_DECREF(tail);
1951 return head;
1952 }
1953
1954 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001955 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001956 Py_DECREF(tail);
1957 return NULL;
1958 }
1959
1960 return tail;
1961}
1962
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001963PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001964PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1965 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001966{
1967 PyObject *result;
1968 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001969 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001970 unlock_import();
1971 return result;
1972}
1973
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001974static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001975get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001976{
1977 static PyObject *namestr = NULL;
1978 static PyObject *pathstr = NULL;
1979 PyObject *modname, *modpath, *modules, *parent;
1980
1981 if (globals == NULL || !PyDict_Check(globals))
1982 return Py_None;
1983
1984 if (namestr == NULL) {
1985 namestr = PyString_InternFromString("__name__");
1986 if (namestr == NULL)
1987 return NULL;
1988 }
1989 if (pathstr == NULL) {
1990 pathstr = PyString_InternFromString("__path__");
1991 if (pathstr == NULL)
1992 return NULL;
1993 }
1994
1995 *buf = '\0';
1996 *p_buflen = 0;
1997 modname = PyDict_GetItem(globals, namestr);
1998 if (modname == NULL || !PyString_Check(modname))
1999 return Py_None;
2000
2001 modpath = PyDict_GetItem(globals, pathstr);
2002 if (modpath != NULL) {
2003 int len = PyString_GET_SIZE(modname);
2004 if (len > MAXPATHLEN) {
2005 PyErr_SetString(PyExc_ValueError,
2006 "Module name too long");
2007 return NULL;
2008 }
2009 strcpy(buf, PyString_AS_STRING(modname));
2010 *p_buflen = len;
2011 }
2012 else {
2013 char *start = PyString_AS_STRING(modname);
2014 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002015 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002016 if (lastdot == NULL)
2017 return Py_None;
2018 len = lastdot - start;
2019 if (len >= MAXPATHLEN) {
2020 PyErr_SetString(PyExc_ValueError,
2021 "Module name too long");
2022 return NULL;
2023 }
2024 strncpy(buf, start, len);
2025 buf[len] = '\0';
2026 *p_buflen = len;
2027 }
2028
2029 modules = PyImport_GetModuleDict();
2030 parent = PyDict_GetItemString(modules, buf);
2031 if (parent == NULL)
2032 parent = Py_None;
2033 return parent;
2034 /* We expect, but can't guarantee, if parent != None, that:
2035 - parent.__name__ == buf
2036 - parent.__dict__ is globals
2037 If this is violated... Who cares? */
2038}
2039
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002040/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002041static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002042load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2043 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002044{
2045 char *name = *p_name;
2046 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002047 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002048 char *p;
2049 PyObject *result;
2050
2051 if (dot == NULL) {
2052 *p_name = NULL;
2053 len = strlen(name);
2054 }
2055 else {
2056 *p_name = dot+1;
2057 len = dot-name;
2058 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002059 if (len == 0) {
2060 PyErr_SetString(PyExc_ValueError,
2061 "Empty module name");
2062 return NULL;
2063 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002064
2065 p = buf + *p_buflen;
2066 if (p != buf)
2067 *p++ = '.';
2068 if (p+len-buf >= MAXPATHLEN) {
2069 PyErr_SetString(PyExc_ValueError,
2070 "Module name too long");
2071 return NULL;
2072 }
2073 strncpy(p, name, len);
2074 p[len] = '\0';
2075 *p_buflen = p+len-buf;
2076
2077 result = import_submodule(mod, p, buf);
2078 if (result == Py_None && altmod != mod) {
2079 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002080 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002081 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002082 if (result != NULL && result != Py_None) {
2083 if (mark_miss(buf) != 0) {
2084 Py_DECREF(result);
2085 return NULL;
2086 }
2087 strncpy(buf, name, len);
2088 buf[len] = '\0';
2089 *p_buflen = len;
2090 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002091 }
2092 if (result == NULL)
2093 return NULL;
2094
2095 if (result == Py_None) {
2096 Py_DECREF(result);
2097 PyErr_Format(PyExc_ImportError,
2098 "No module named %.200s", name);
2099 return NULL;
2100 }
2101
2102 return result;
2103}
2104
2105static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002106mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002107{
2108 PyObject *modules = PyImport_GetModuleDict();
2109 return PyDict_SetItemString(modules, name, Py_None);
2110}
2111
2112static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002113ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2114 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002115{
2116 int i;
2117
2118 if (!PyObject_HasAttrString(mod, "__path__"))
2119 return 1;
2120
2121 for (i = 0; ; i++) {
2122 PyObject *item = PySequence_GetItem(fromlist, i);
2123 int hasit;
2124 if (item == NULL) {
2125 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2126 PyErr_Clear();
2127 return 1;
2128 }
2129 return 0;
2130 }
2131 if (!PyString_Check(item)) {
2132 PyErr_SetString(PyExc_TypeError,
2133 "Item in ``from list'' not a string");
2134 Py_DECREF(item);
2135 return 0;
2136 }
2137 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002138 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002139 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002140 /* See if the package defines __all__ */
2141 if (recursive)
2142 continue; /* Avoid endless recursion */
2143 all = PyObject_GetAttrString(mod, "__all__");
2144 if (all == NULL)
2145 PyErr_Clear();
2146 else {
2147 if (!ensure_fromlist(mod, all, buf, buflen, 1))
2148 return 0;
2149 Py_DECREF(all);
2150 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002151 continue;
2152 }
2153 hasit = PyObject_HasAttr(mod, item);
2154 if (!hasit) {
2155 char *subname = PyString_AS_STRING(item);
2156 PyObject *submod;
2157 char *p;
2158 if (buflen + strlen(subname) >= MAXPATHLEN) {
2159 PyErr_SetString(PyExc_ValueError,
2160 "Module name too long");
2161 Py_DECREF(item);
2162 return 0;
2163 }
2164 p = buf + buflen;
2165 *p++ = '.';
2166 strcpy(p, subname);
2167 submod = import_submodule(mod, subname, buf);
2168 Py_XDECREF(submod);
2169 if (submod == NULL) {
2170 Py_DECREF(item);
2171 return 0;
2172 }
2173 }
2174 Py_DECREF(item);
2175 }
2176
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002177 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002178}
2179
2180static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002181import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002182{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002183 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumf48f11c2001-07-23 13:27:49 +00002184 PyObject *m, *res = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002185
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002186 /* Require:
2187 if mod == None: subname == fullname
2188 else: mod.__name__ + "." + subname == fullname
2189 */
2190
Tim Peters50d8d372001-02-28 05:34:27 +00002191 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002192 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002193 }
2194 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002195 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002196 char buf[MAXPATHLEN+1];
2197 struct filedescr *fdp;
2198 FILE *fp = NULL;
2199
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002200 if (mod == Py_None)
2201 path = NULL;
2202 else {
2203 path = PyObject_GetAttrString(mod, "__path__");
2204 if (path == NULL) {
2205 PyErr_Clear();
2206 Py_INCREF(Py_None);
2207 return Py_None;
2208 }
2209 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002210
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002211 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002212 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2213 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002214 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002215 if (fdp == NULL) {
2216 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2217 return NULL;
2218 PyErr_Clear();
2219 Py_INCREF(Py_None);
2220 return Py_None;
2221 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002222 m = load_module(fullname, fp, buf, fdp->type, loader);
2223 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002224 if (fp)
2225 fclose(fp);
Guido van Rossumf48f11c2001-07-23 13:27:49 +00002226 if (mod != Py_None) {
2227 /* Irrespective of the success of this load, make a
2228 reference to it in the parent package module.
2229 A copy gets saved in the modules dictionary
2230 under the full name, so get a reference from
2231 there, if need be. (The exception is when
2232 the load failed with a SyntaxError -- then
2233 there's no trace in sys.modules. In that case,
2234 of course, do nothing extra.) */
2235 res = m;
2236 if (res == NULL)
2237 res = PyDict_GetItemString(modules, fullname);
2238 if (res != NULL &&
2239 PyObject_SetAttrString(mod, subname, res) < 0) {
2240 Py_XDECREF(m);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002241 m = NULL;
2242 }
2243 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002244 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002245
2246 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002247}
2248
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002249
2250/* Re-import a module of any kind and return its module object, WITH
2251 INCREMENTED REFERENCE COUNT */
2252
Guido van Rossum79f25d91997-04-29 20:08:16 +00002253PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002254PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002255{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002256 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002257 PyObject *path = NULL;
2258 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002259 char buf[MAXPATHLEN+1];
2260 struct filedescr *fdp;
2261 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002262
Guido van Rossum79f25d91997-04-29 20:08:16 +00002263 if (m == NULL || !PyModule_Check(m)) {
2264 PyErr_SetString(PyExc_TypeError,
2265 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002266 return NULL;
2267 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002268 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002269 if (name == NULL)
2270 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002271 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002272 PyErr_Format(PyExc_ImportError,
2273 "reload(): module %.200s not in sys.modules",
2274 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002275 return NULL;
2276 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002277 subname = strrchr(name, '.');
2278 if (subname == NULL)
2279 subname = name;
2280 else {
2281 PyObject *parentname, *parent;
2282 parentname = PyString_FromStringAndSize(name, (subname-name));
2283 if (parentname == NULL)
2284 return NULL;
2285 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002286 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002287 if (parent == NULL) {
2288 PyErr_Format(PyExc_ImportError,
2289 "reload(): parent %.200s not in sys.modules",
2290 name);
2291 return NULL;
2292 }
2293 subname++;
2294 path = PyObject_GetAttrString(parent, "__path__");
2295 if (path == NULL)
2296 PyErr_Clear();
2297 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002298 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002299 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002300 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002301 if (fdp == NULL)
2302 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002303 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002304 if (fp)
2305 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002306 return m;
2307}
2308
2309
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002310/* Higher-level import emulator which emulates the "import" statement
2311 more accurately -- it invokes the __import__() function from the
2312 builtins of the current globals. This means that the import is
2313 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002314 environment, e.g. by "rexec".
2315 A dummy list ["__doc__"] is passed as the 4th argument so that
2316 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2317 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002318
2319PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002320PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002321{
2322 static PyObject *silly_list = NULL;
2323 static PyObject *builtins_str = NULL;
2324 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002325 PyObject *globals = NULL;
2326 PyObject *import = NULL;
2327 PyObject *builtins = NULL;
2328 PyObject *r = NULL;
2329
2330 /* Initialize constant string objects */
2331 if (silly_list == NULL) {
2332 import_str = PyString_InternFromString("__import__");
2333 if (import_str == NULL)
2334 return NULL;
2335 builtins_str = PyString_InternFromString("__builtins__");
2336 if (builtins_str == NULL)
2337 return NULL;
2338 silly_list = Py_BuildValue("[s]", "__doc__");
2339 if (silly_list == NULL)
2340 return NULL;
2341 }
2342
2343 /* Get the builtins from current globals */
2344 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002345 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002346 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002347 builtins = PyObject_GetItem(globals, builtins_str);
2348 if (builtins == NULL)
2349 goto err;
2350 }
2351 else {
2352 /* No globals -- use standard builtins, and fake globals */
2353 PyErr_Clear();
2354
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002355 builtins = PyImport_ImportModuleEx("__builtin__",
2356 NULL, NULL, NULL);
2357 if (builtins == NULL)
2358 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002359 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2360 if (globals == NULL)
2361 goto err;
2362 }
2363
2364 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002365 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002366 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002367 if (import == NULL)
2368 PyErr_SetObject(PyExc_KeyError, import_str);
2369 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002370 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002371 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002372 if (import == NULL)
2373 goto err;
2374
2375 /* Call the _import__ function with the proper argument list */
2376 r = PyObject_CallFunction(import, "OOOO",
2377 module_name, globals, globals, silly_list);
2378
2379 err:
2380 Py_XDECREF(globals);
2381 Py_XDECREF(builtins);
2382 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002383
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002384 return r;
2385}
2386
2387
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002388/* Module 'imp' provides Python access to the primitives used for
2389 importing modules.
2390*/
2391
Guido van Rossum79f25d91997-04-29 20:08:16 +00002392static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002393imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002394{
2395 char buf[4];
2396
Guido van Rossum43713e52000-02-29 13:59:29 +00002397 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002398 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00002399 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2400 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2401 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2402 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002403
Guido van Rossum79f25d91997-04-29 20:08:16 +00002404 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002405}
2406
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002408imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002409{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002411 struct filedescr *fdp;
2412
Guido van Rossum43713e52000-02-29 13:59:29 +00002413 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002414 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002415 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002416 if (list == NULL)
2417 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2419 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002420 fdp->suffix, fdp->mode, fdp->type);
2421 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002422 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002423 return NULL;
2424 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002425 if (PyList_Append(list, item) < 0) {
2426 Py_DECREF(list);
2427 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002428 return NULL;
2429 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002430 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002431 }
2432 return list;
2433}
2434
Guido van Rossum79f25d91997-04-29 20:08:16 +00002435static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002436call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002437{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002438 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002440 struct filedescr *fdp;
2441 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002442 FILE *fp = NULL;
2443
2444 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002445 if (path == Py_None)
2446 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002447 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002448 if (fdp == NULL)
2449 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002450 if (fp != NULL) {
2451 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2452 if (fob == NULL) {
2453 fclose(fp);
2454 return NULL;
2455 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002456 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002457 else {
2458 fob = Py_None;
2459 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002460 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002461 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002462 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002464 return ret;
2465}
2466
Guido van Rossum79f25d91997-04-29 20:08:16 +00002467static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002468imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002469{
2470 char *name;
2471 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002472 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002473 return NULL;
2474 return call_find_module(name, path);
2475}
2476
2477static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002478imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002479{
2480 char *name;
2481 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002482 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002483 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002484 return NULL;
2485 ret = init_builtin(name);
2486 if (ret < 0)
2487 return NULL;
2488 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002489 Py_INCREF(Py_None);
2490 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002491 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002492 m = PyImport_AddModule(name);
2493 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002494 return m;
2495}
2496
Guido van Rossum79f25d91997-04-29 20:08:16 +00002497static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002498imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002499{
2500 char *name;
2501 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002502 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002503 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002504 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002505 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002506 if (ret < 0)
2507 return NULL;
2508 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002509 Py_INCREF(Py_None);
2510 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002511 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002512 m = PyImport_AddModule(name);
2513 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002514 return m;
2515}
2516
Guido van Rossum79f25d91997-04-29 20:08:16 +00002517static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002518imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002519{
2520 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002521
Guido van Rossum43713e52000-02-29 13:59:29 +00002522 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002523 return NULL;
2524 return get_frozen_object(name);
2525}
2526
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002528imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002529{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002530 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002531 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002532 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002533 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534}
2535
Guido van Rossum79f25d91997-04-29 20:08:16 +00002536static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002537imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002538{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002540 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002541 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002542 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002543 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002544 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002545}
2546
2547static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002548get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002549{
2550 FILE *fp;
2551 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002552 if (mode[0] == 'U')
2553 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002554 fp = fopen(pathname, mode);
2555 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002556 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002557 }
2558 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002559 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002560 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002561 PyErr_SetString(PyExc_ValueError,
2562 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563 }
2564 return fp;
2565}
2566
Guido van Rossum79f25d91997-04-29 20:08:16 +00002567static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002568imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002569{
2570 char *name;
2571 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002572 PyObject *fob = NULL;
2573 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002574 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002575 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002576 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577 return NULL;
2578 fp = get_file(pathname, fob, "rb");
2579 if (fp == NULL)
2580 return NULL;
2581 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002582 if (fob == NULL)
2583 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002584 return m;
2585}
2586
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002587#ifdef HAVE_DYNAMIC_LOADING
2588
Guido van Rossum79f25d91997-04-29 20:08:16 +00002589static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002590imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002591{
2592 char *name;
2593 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 PyObject *fob = NULL;
2595 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002596 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002597 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002598 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002599 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002600 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002601 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002602 if (fp == NULL)
2603 return NULL;
2604 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002606 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002607}
2608
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002609#endif /* HAVE_DYNAMIC_LOADING */
2610
Guido van Rossum79f25d91997-04-29 20:08:16 +00002611static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002612imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002613{
2614 char *name;
2615 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002616 PyObject *fob = NULL;
2617 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002618 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002619 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002620 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002621 return NULL;
2622 fp = get_file(pathname, fob, "r");
2623 if (fp == NULL)
2624 return NULL;
2625 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002626 if (fob == NULL)
2627 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002628 return m;
2629}
2630
Jack Jansen9c96a921995-02-15 22:57:06 +00002631#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002632static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002633imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002634{
2635 char *name;
2636 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002637 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002638
Guido van Rossum43713e52000-02-29 13:59:29 +00002639 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002640 return NULL;
2641 m = PyMac_LoadResourceModule(name, pathname);
2642 return m;
2643}
2644#endif /* macintosh */
2645
Guido van Rossum79f25d91997-04-29 20:08:16 +00002646static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002647imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002648{
2649 char *name;
2650 PyObject *fob;
2651 char *pathname;
2652 char *suffix; /* Unused */
2653 char *mode;
2654 int type;
2655 FILE *fp;
2656
Guido van Rossum43713e52000-02-29 13:59:29 +00002657 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002658 &name, &fob, &pathname,
2659 &suffix, &mode, &type))
2660 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002661 if (*mode) {
2662 /* Mode must start with 'r' or 'U' and must not contain '+'.
2663 Implicit in this test is the assumption that the mode
2664 may contain other modifiers like 'b' or 't'. */
2665
2666 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002667 PyErr_Format(PyExc_ValueError,
2668 "invalid file open mode %.200s", mode);
2669 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002670 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002671 }
2672 if (fob == Py_None)
2673 fp = NULL;
2674 else {
2675 if (!PyFile_Check(fob)) {
2676 PyErr_SetString(PyExc_ValueError,
2677 "load_module arg#2 should be a file or None");
2678 return NULL;
2679 }
2680 fp = get_file(pathname, fob, mode);
2681 if (fp == NULL)
2682 return NULL;
2683 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002684 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002685}
2686
2687static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002688imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002689{
2690 char *name;
2691 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002692 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002693 return NULL;
2694 return load_package(name, pathname);
2695}
2696
2697static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002698imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002699{
2700 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002701 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002702 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002703 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002704}
2705
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002706/* Doc strings */
2707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002708PyDoc_STRVAR(doc_imp,
2709"This module provides the components needed to build your own\n\
2710__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002712PyDoc_STRVAR(doc_find_module,
2713"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002714Search for a module. If path is omitted or None, search for a\n\
2715built-in, frozen or special module and continue search in sys.path.\n\
2716The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719PyDoc_STRVAR(doc_load_module,
2720"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002721Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002724PyDoc_STRVAR(doc_get_magic,
2725"get_magic() -> string\n\
2726Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728PyDoc_STRVAR(doc_get_suffixes,
2729"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002730Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002731that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002732
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002733PyDoc_STRVAR(doc_new_module,
2734"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002735Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002736The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002738PyDoc_STRVAR(doc_lock_held,
2739"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002740Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002742
Guido van Rossum79f25d91997-04-29 20:08:16 +00002743static PyMethodDef imp_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00002744 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2745 {"get_magic", imp_get_magic, METH_VARARGS, doc_get_magic},
2746 {"get_suffixes", imp_get_suffixes, METH_VARARGS, doc_get_suffixes},
2747 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2748 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2749 {"lock_held", imp_lock_held, METH_VARARGS, doc_lock_held},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002750 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002751 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2752 {"init_builtin", imp_init_builtin, METH_VARARGS},
2753 {"init_frozen", imp_init_frozen, METH_VARARGS},
2754 {"is_builtin", imp_is_builtin, METH_VARARGS},
2755 {"is_frozen", imp_is_frozen, METH_VARARGS},
2756 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002757#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002758 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002759#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002760 {"load_package", imp_load_package, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002761#ifdef macintosh
Neal Norwitz031829d2002-03-31 14:37:44 +00002762 {"load_resource", imp_load_resource, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002763#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002764 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002765 {NULL, NULL} /* sentinel */
2766};
2767
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002768static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002769setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002770{
2771 PyObject *v;
2772 int err;
2773
2774 v = PyInt_FromLong((long)value);
2775 err = PyDict_SetItemString(d, name, v);
2776 Py_XDECREF(v);
2777 return err;
2778}
2779
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002780void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002781initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002782{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002783 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002784
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002785 m = Py_InitModule4("imp", imp_methods, doc_imp,
2786 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002787 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002788
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002789 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2790 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2791 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2792 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2793 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2794 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2795 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2796 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002797 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002798 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002799
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002800 failure:
2801 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002802}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002803
2804
Guido van Rossumb18618d2000-05-03 23:44:39 +00002805/* API for embedding applications that want to add their own entries
2806 to the table of built-in modules. This should normally be called
2807 *before* Py_Initialize(). When the table resize fails, -1 is
2808 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002809
2810 After a similar function by Just van Rossum. */
2811
2812int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002813PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002814{
2815 static struct _inittab *our_copy = NULL;
2816 struct _inittab *p;
2817 int i, n;
2818
2819 /* Count the number of entries in both tables */
2820 for (n = 0; newtab[n].name != NULL; n++)
2821 ;
2822 if (n == 0)
2823 return 0; /* Nothing to do */
2824 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2825 ;
2826
2827 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002828 p = our_copy;
2829 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002830 if (p == NULL)
2831 return -1;
2832
2833 /* Copy the tables into the new memory */
2834 if (our_copy != PyImport_Inittab)
2835 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2836 PyImport_Inittab = our_copy = p;
2837 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2838
2839 return 0;
2840}
2841
2842/* Shorthand to add a single entry given a name and a function */
2843
2844int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002845PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002846{
2847 struct _inittab newtab[2];
2848
2849 memset(newtab, '\0', sizeof newtab);
2850
2851 newtab[0].name = name;
2852 newtab[0].initfunc = initfunc;
2853
2854 return PyImport_ExtendInittab(newtab);
2855}