blob: b2cefc0a7ee41221be8bb8aa34e9da93ec73bbb7 [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
Jack Jansen9363dca2003-01-24 16:15:45 +00001217 ** into them for resources). We only do this for string
1218 ** items.
Guido van Rossum741689d1997-08-12 14:53:39 +00001219 */
Jack Jansen9363dca2003-01-24 16:15:45 +00001220 if (PyString_Check(PyList_GET_ITEM(path, i))) {
1221 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
1222 v = PyList_GET_ITEM(path, i);
1223 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
1224 static struct filedescr resfiledescr =
1225 {"", "", PY_RESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +00001226
Jack Jansen9363dca2003-01-24 16:15:45 +00001227 Py_XDECREF(copy);
1228 return &resfiledescr;
1229 }
1230 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
1231 static struct filedescr resfiledescr =
1232 {"", "", PY_CODERESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +00001233
Jack Jansen9363dca2003-01-24 16:15:45 +00001234 Py_XDECREF(copy);
1235 return &resfiledescr;
1236 }
Guido van Rossum0f84a341998-08-06 13:36:01 +00001237 }
Jack Jansen9c96a921995-02-15 22:57:06 +00001238#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001239 if (len > 0 && buf[len-1] != SEP
1240#ifdef ALTSEP
1241 && buf[len-1] != ALTSEP
1242#endif
1243 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001245 strcpy(buf+len, name);
1246 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001247
1248 /* Check for package import (buf holds a directory name,
1249 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001250#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001251 if (stat(buf, &statbuf) == 0 && /* it exists */
1252 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1253 find_init_module(buf) && /* it has __init__.py */
1254 case_ok(buf, len, namelen, name)) { /* and case matches */
1255 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001256 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001257 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001258#else
1259 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001260#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001261 if (isdir(buf) &&
1262 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001263 case_ok(buf, len, namelen, name)) {
1264 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001265 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001266 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001267#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001268#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001269#ifdef macintosh
1270 fdp = PyMac_FindModuleExtension(buf, &len, name);
Jack Jansen4df3c522001-03-20 23:09:54 +00001271 if (fdp) {
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001272#else
Andrew MacIntyred9400542002-02-26 11:41:34 +00001273#if defined(PYOS_OS2)
1274 /* take a snapshot of the module spec for restoration
1275 * after the 8 character DLL hackery
1276 */
1277 saved_buf = strdup(buf);
1278 saved_len = len;
1279 saved_namelen = namelen;
1280#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001281 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001282#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001283 /* OS/2 limits DLLs to 8 character names (w/o
1284 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001285 * so if the name is longer than that and its a
1286 * dynamically loaded module we're going to try,
1287 * truncate the name before trying
1288 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001289 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001290 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001291 const struct filedescr *scan;
1292 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001293 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001294 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001295 break;
1296 else
1297 scan++;
1298 }
1299 if (scan->suffix != NULL) {
1300 /* yes, so truncate the name */
1301 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001302 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001303 buf[len] = '\0';
1304 }
1305 }
1306#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001309 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansen4df3c522001-03-20 23:09:54 +00001310#endif /* !macintosh */
Jack Jansenc88da1f2002-05-28 10:58:19 +00001311 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001312 if (filemode[0] == 'U')
1313 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001314 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001315 if (fp != NULL) {
1316 if (case_ok(buf, len, namelen, name))
1317 break;
1318 else { /* continue search */
1319 fclose(fp);
1320 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001321 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001322 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001323#if defined(PYOS_OS2)
1324 /* restore the saved snapshot */
1325 strcpy(buf, saved_buf);
1326 len = saved_len;
1327 namelen = saved_namelen;
1328#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001329 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001330#if defined(PYOS_OS2)
1331 /* don't need/want the module name snapshot anymore */
1332 if (saved_buf)
1333 {
1334 free(saved_buf);
1335 saved_buf = NULL;
1336 }
1337#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001338 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001339 if (fp != NULL)
1340 break;
1341 }
1342 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001343 PyErr_Format(PyExc_ImportError,
1344 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001345 return NULL;
1346 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001347 *p_fp = fp;
1348 return fdp;
1349}
1350
Tim Petersd1e87a82001-03-01 18:12:00 +00001351/* case_ok(char* buf, int len, int namelen, char* name)
1352 * The arguments here are tricky, best shown by example:
1353 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1354 * ^ ^ ^ ^
1355 * |--------------------- buf ---------------------|
1356 * |------------------- len ------------------|
1357 * |------ name -------|
1358 * |----- namelen -----|
1359 * buf is the full path, but len only counts up to (& exclusive of) the
1360 * extension. name is the module name, also exclusive of extension.
1361 *
1362 * We've already done a successful stat() or fopen() on buf, so know that
1363 * there's some match, possibly case-insensitive.
1364 *
Tim Peters50d8d372001-02-28 05:34:27 +00001365 * case_ok() is to return 1 if there's a case-sensitive match for
1366 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1367 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001368 *
Tim Peters50d8d372001-02-28 05:34:27 +00001369 * case_ok() is used to implement case-sensitive import semantics even
1370 * on platforms with case-insensitive filesystems. It's trivial to implement
1371 * for case-sensitive filesystems. It's pretty much a cross-platform
1372 * nightmare for systems with case-insensitive filesystems.
1373 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001374
Tim Peters50d8d372001-02-28 05:34:27 +00001375/* First we may need a pile of platform-specific header files; the sequence
1376 * of #if's here should match the sequence in the body of case_ok().
1377 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001378#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001379#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001380#ifdef __CYGWIN__
1381#include <sys/cygwin.h>
1382#endif
1383
Tim Peters50d8d372001-02-28 05:34:27 +00001384#elif defined(DJGPP)
1385#include <dir.h>
1386
1387#elif defined(macintosh)
1388#include <TextUtils.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001389
Tim Petersd1e87a82001-03-01 18:12:00 +00001390#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001391#include <sys/types.h>
1392#include <dirent.h>
1393
Andrew MacIntyred9400542002-02-26 11:41:34 +00001394#elif defined(PYOS_OS2)
1395#define INCL_DOS
1396#define INCL_DOSERRORS
1397#define INCL_NOPMAPI
1398#include <os2.h>
1399
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001400#elif defined(RISCOS)
1401#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001402#endif
1403
Guido van Rossum0980bd91998-02-13 17:18:36 +00001404static int
Tim Peters50d8d372001-02-28 05:34:27 +00001405case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001406{
Tim Peters50d8d372001-02-28 05:34:27 +00001407/* Pick a platform-specific implementation; the sequence of #if's here should
1408 * match the sequence just above.
1409 */
1410
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001411/* MS_WINDOWS || __CYGWIN__ */
1412#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001413 WIN32_FIND_DATA data;
1414 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001415#ifdef __CYGWIN__
1416 char tempbuf[MAX_PATH];
1417#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001418
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001419 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001420 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001421
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001422#ifdef __CYGWIN__
1423 cygwin32_conv_to_win32_path(buf, tempbuf);
1424 h = FindFirstFile(tempbuf, &data);
1425#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001426 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001427#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001428 if (h == INVALID_HANDLE_VALUE) {
1429 PyErr_Format(PyExc_NameError,
1430 "Can't find file for module %.100s\n(filename %.300s)",
1431 name, buf);
1432 return 0;
1433 }
1434 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001435 return strncmp(data.cFileName, name, namelen) == 0;
1436
1437/* DJGPP */
1438#elif defined(DJGPP)
1439 struct ffblk ffblk;
1440 int done;
1441
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001442 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001443 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001444
1445 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1446 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001447 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001448 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001449 name, buf);
1450 return 0;
1451 }
Tim Peters50d8d372001-02-28 05:34:27 +00001452 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001453
Tim Peters50d8d372001-02-28 05:34:27 +00001454/* macintosh */
1455#elif defined(macintosh)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001456 FSSpec fss;
1457 OSErr err;
Tim Peters50d8d372001-02-28 05:34:27 +00001458
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001459 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Peters50d8d372001-02-28 05:34:27 +00001460 return 1;
1461
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001462 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001463 if (err) {
1464 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001465 "Can't find file for module %.100s\n(filename %.300s)",
1466 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001467 return 0;
1468 }
Tim Peters50d8d372001-02-28 05:34:27 +00001469 return fss.name[0] >= namelen &&
1470 strncmp(name, (char *)fss.name+1, namelen) == 0;
1471
Tim Peters677898a2001-03-02 03:28:03 +00001472/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001473#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001474 DIR *dirp;
1475 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001476 char dirname[MAXPATHLEN + 1];
1477 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001478
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001479 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001480 return 1;
1481
Tim Petersd1e87a82001-03-01 18:12:00 +00001482 /* Copy the dir component into dirname; substitute "." if empty */
1483 if (dirlen <= 0) {
1484 dirname[0] = '.';
1485 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001486 }
1487 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001488 assert(dirlen <= MAXPATHLEN);
1489 memcpy(dirname, buf, dirlen);
1490 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001491 }
1492 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001493 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001494 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001495 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001496 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001497 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001498#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001499 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001500#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001501 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001502#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001503 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001504 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001505 (void)closedir(dirp);
1506 return 1; /* Found */
1507 }
1508 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001509 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001510 }
Tim Peters430f5d42001-03-01 01:30:56 +00001511 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001512
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001513/* RISC OS */
1514#elif defined(RISCOS)
1515 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1516 char buf2[MAXPATHLEN+2];
1517 char *nameWithExt = buf+len-namelen;
1518 int canonlen;
1519 os_error *e;
1520
1521 if (Py_GETENV("PYTHONCASEOK") != NULL)
1522 return 1;
1523
1524 /* workaround:
1525 append wildcard, otherwise case of filename wouldn't be touched */
1526 strcpy(buf2, buf);
1527 strcat(buf2, "*");
1528
1529 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1530 canonlen = MAXPATHLEN+1-canonlen;
1531 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1532 return 0;
1533 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1534 return 1; /* match */
1535
1536 return 0;
1537
Andrew MacIntyred9400542002-02-26 11:41:34 +00001538/* OS/2 */
1539#elif defined(PYOS_OS2)
1540 HDIR hdir = 1;
1541 ULONG srchcnt = 1;
1542 FILEFINDBUF3 ffbuf;
1543 APIRET rc;
1544
1545 if (getenv("PYTHONCASEOK") != NULL)
1546 return 1;
1547
1548 rc = DosFindFirst(buf,
1549 &hdir,
1550 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1551 &ffbuf, sizeof(ffbuf),
1552 &srchcnt,
1553 FIL_STANDARD);
1554 if (rc != NO_ERROR)
1555 return 0;
1556 return strncmp(ffbuf.achName, name, namelen) == 0;
1557
Tim Peters50d8d372001-02-28 05:34:27 +00001558/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1559#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001560 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001561
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001562#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001563}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001564
Guido van Rossum0980bd91998-02-13 17:18:36 +00001565
Guido van Rossum197346f1997-10-31 18:38:52 +00001566#ifdef HAVE_STAT
1567/* Helper to look for __init__.py or __init__.py[co] in potential package */
1568static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001569find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001570{
Tim Peters0f9431f2001-07-05 03:47:53 +00001571 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001572 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001573 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001574 struct stat statbuf;
1575
Tim Peters0f9431f2001-07-05 03:47:53 +00001576/* For calling case_ok(buf, len, namelen, name):
1577 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1578 * ^ ^ ^ ^
1579 * |--------------------- buf ---------------------|
1580 * |------------------- len ------------------|
1581 * |------ name -------|
1582 * |----- namelen -----|
1583 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001584 if (save_len + 13 >= MAXPATHLEN)
1585 return 0;
1586 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001587 pname = buf + i;
1588 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001589 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001590 if (case_ok(buf,
1591 save_len + 9, /* len("/__init__") */
1592 8, /* len("__init__") */
1593 pname)) {
1594 buf[save_len] = '\0';
1595 return 1;
1596 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001597 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001598 i += strlen(pname);
1599 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001600 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001601 if (case_ok(buf,
1602 save_len + 9, /* len("/__init__") */
1603 8, /* len("__init__") */
1604 pname)) {
1605 buf[save_len] = '\0';
1606 return 1;
1607 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001608 }
1609 buf[save_len] = '\0';
1610 return 0;
1611}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001612
1613#else
1614
1615#ifdef RISCOS
1616static int
1617find_init_module(buf)
1618 char *buf;
1619{
1620 int save_len = strlen(buf);
1621 int i = save_len;
1622
1623 if (save_len + 13 >= MAXPATHLEN)
1624 return 0;
1625 buf[i++] = SEP;
1626 strcpy(buf+i, "__init__/py");
1627 if (isfile(buf)) {
1628 buf[save_len] = '\0';
1629 return 1;
1630 }
1631
1632 if (Py_OptimizeFlag)
1633 strcpy(buf+i, "o");
1634 else
1635 strcpy(buf+i, "c");
1636 if (isfile(buf)) {
1637 buf[save_len] = '\0';
1638 return 1;
1639 }
1640 buf[save_len] = '\0';
1641 return 0;
1642}
1643#endif /*RISCOS*/
1644
Guido van Rossum197346f1997-10-31 18:38:52 +00001645#endif /* HAVE_STAT */
1646
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001647
Tim Petersdbd9ba62000-07-09 03:09:57 +00001648static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001649
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001650/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001651 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001652
Guido van Rossum79f25d91997-04-29 20:08:16 +00001653static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001654load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001655{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001656 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001658 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001659
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001660 /* First check that there's an open file (if we need one) */
1661 switch (type) {
1662 case PY_SOURCE:
1663 case PY_COMPILED:
1664 if (fp == NULL) {
1665 PyErr_Format(PyExc_ValueError,
1666 "file object required for import (type code %d)",
1667 type);
1668 return NULL;
1669 }
1670 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001671
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001672 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001673
1674 case PY_SOURCE:
1675 m = load_source_module(name, buf, fp);
1676 break;
1677
1678 case PY_COMPILED:
1679 m = load_compiled_module(name, buf, fp);
1680 break;
1681
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001682#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001683 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001685 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001686#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001687
Jack Jansen9c96a921995-02-15 22:57:06 +00001688#ifdef macintosh
1689 case PY_RESOURCE:
1690 m = PyMac_LoadResourceModule(name, buf);
1691 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001692 case PY_CODERESOURCE:
1693 m = PyMac_LoadCodeResourceModule(name, buf);
1694 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001695#endif
1696
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001697 case PKG_DIRECTORY:
1698 m = load_package(name, buf);
1699 break;
1700
1701 case C_BUILTIN:
1702 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001703 if (buf != NULL && buf[0] != '\0')
1704 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001705 if (type == C_BUILTIN)
1706 err = init_builtin(name);
1707 else
1708 err = PyImport_ImportFrozenModule(name);
1709 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001710 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001711 if (err == 0) {
1712 PyErr_Format(PyExc_ImportError,
1713 "Purported %s module %.200s not found",
1714 type == C_BUILTIN ?
1715 "builtin" : "frozen",
1716 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001717 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001718 }
1719 modules = PyImport_GetModuleDict();
1720 m = PyDict_GetItemString(modules, name);
1721 if (m == NULL) {
1722 PyErr_Format(
1723 PyExc_ImportError,
1724 "%s module %.200s not properly initialized",
1725 type == C_BUILTIN ?
1726 "builtin" : "frozen",
1727 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001728 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001729 }
1730 Py_INCREF(m);
1731 break;
1732
Just van Rossum52e14d62002-12-30 22:08:05 +00001733 case IMP_HOOK: {
1734 if (loader == NULL) {
1735 PyErr_SetString(PyExc_ImportError,
1736 "import hook without loader");
1737 return NULL;
1738 }
1739 m = PyObject_CallMethod(loader, "load_module", "s", name);
1740 break;
1741 }
1742
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001743 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001744 PyErr_Format(PyExc_ImportError,
1745 "Don't know how to import %.200s (type code %d)",
1746 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001747 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001748
1749 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750
1751 return m;
1752}
1753
1754
1755/* Initialize a built-in module.
1756 Return 1 for succes, 0 if the module is not found, and -1 with
1757 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001758
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001759static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001760init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001761{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001762 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001763
Greg Ward201baee2001-10-04 14:52:06 +00001764 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001765 return 1;
1766
Guido van Rossum771c6c81997-10-31 18:37:24 +00001767 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001768 if (strcmp(name, p->name) == 0) {
1769 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001770 PyErr_Format(PyExc_ImportError,
1771 "Cannot re-init internal module %.200s",
1772 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001773 return -1;
1774 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001776 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001777 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001780 if (_PyImport_FixupExtension(name, name) == NULL)
1781 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001782 return 1;
1783 }
1784 }
1785 return 0;
1786}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001787
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001789/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001790
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001791static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001792find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001793{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001794 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001795
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001797 if (p->name == NULL)
1798 return NULL;
1799 if (strcmp(p->name, name) == 0)
1800 break;
1801 }
1802 return p;
1803}
1804
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001806get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001807{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001808 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001809 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001810
1811 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001812 PyErr_Format(PyExc_ImportError,
1813 "No such frozen object named %.200s",
1814 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001815 return NULL;
1816 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001817 if (p->code == NULL) {
1818 PyErr_Format(PyExc_ImportError,
1819 "Excluded frozen object named %.200s",
1820 name);
1821 return NULL;
1822 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001823 size = p->size;
1824 if (size < 0)
1825 size = -size;
1826 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001827}
1828
1829/* Initialize a frozen module.
1830 Return 1 for succes, 0 if the module is not found, and -1 with
1831 an exception set if the initialization failed.
1832 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001833
1834int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001835PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001836{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001837 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 PyObject *co;
1839 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001840 int ispackage;
1841 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001842
1843 if (p == NULL)
1844 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001845 if (p->code == NULL) {
1846 PyErr_Format(PyExc_ImportError,
1847 "Excluded frozen object named %.200s",
1848 name);
1849 return -1;
1850 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001851 size = p->size;
1852 ispackage = (size < 0);
1853 if (ispackage)
1854 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001856 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001857 name, ispackage ? " package" : "");
1858 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001859 if (co == NULL)
1860 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861 if (!PyCode_Check(co)) {
1862 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001863 PyErr_Format(PyExc_TypeError,
1864 "frozen object %.200s is not a code object",
1865 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001866 return -1;
1867 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001868 if (ispackage) {
1869 /* Set __path__ to the package name */
1870 PyObject *d, *s;
1871 int err;
1872 m = PyImport_AddModule(name);
1873 if (m == NULL)
1874 return -1;
1875 d = PyModule_GetDict(m);
1876 s = PyString_InternFromString(name);
1877 if (s == NULL)
1878 return -1;
1879 err = PyDict_SetItemString(d, "__path__", s);
1880 Py_DECREF(s);
1881 if (err != 0)
1882 return err;
1883 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001884 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001886 if (m == NULL)
1887 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001888 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001889 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001890}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001891
1892
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001893/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001894 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001895
Guido van Rossum79f25d91997-04-29 20:08:16 +00001896PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001897PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001898{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001899 PyObject *pname;
1900 PyObject *result;
1901
1902 pname = PyString_FromString(name);
1903 result = PyImport_Import(pname);
1904 Py_DECREF(pname);
1905 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001906}
1907
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001908/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001909static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1910static PyObject *load_next(PyObject *mod, PyObject *altmod,
1911 char **p_name, char *buf, int *p_buflen);
1912static int mark_miss(char *name);
1913static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1914 char *buf, int buflen, int recursive);
1915static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001916
1917/* The Magnum Opus of dotted-name import :-) */
1918
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001919static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001920import_module_ex(char *name, PyObject *globals, PyObject *locals,
1921 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001922{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001923 char buf[MAXPATHLEN+1];
1924 int buflen = 0;
1925 PyObject *parent, *head, *next, *tail;
1926
1927 parent = get_parent(globals, buf, &buflen);
1928 if (parent == NULL)
1929 return NULL;
1930
1931 head = load_next(parent, Py_None, &name, buf, &buflen);
1932 if (head == NULL)
1933 return NULL;
1934
1935 tail = head;
1936 Py_INCREF(tail);
1937 while (name) {
1938 next = load_next(tail, tail, &name, buf, &buflen);
1939 Py_DECREF(tail);
1940 if (next == NULL) {
1941 Py_DECREF(head);
1942 return NULL;
1943 }
1944 tail = next;
1945 }
1946
1947 if (fromlist != NULL) {
1948 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1949 fromlist = NULL;
1950 }
1951
1952 if (fromlist == NULL) {
1953 Py_DECREF(tail);
1954 return head;
1955 }
1956
1957 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001958 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001959 Py_DECREF(tail);
1960 return NULL;
1961 }
1962
1963 return tail;
1964}
1965
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001966PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001967PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1968 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001969{
1970 PyObject *result;
1971 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001972 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001973 unlock_import();
1974 return result;
1975}
1976
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001977static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001978get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001979{
1980 static PyObject *namestr = NULL;
1981 static PyObject *pathstr = NULL;
1982 PyObject *modname, *modpath, *modules, *parent;
1983
1984 if (globals == NULL || !PyDict_Check(globals))
1985 return Py_None;
1986
1987 if (namestr == NULL) {
1988 namestr = PyString_InternFromString("__name__");
1989 if (namestr == NULL)
1990 return NULL;
1991 }
1992 if (pathstr == NULL) {
1993 pathstr = PyString_InternFromString("__path__");
1994 if (pathstr == NULL)
1995 return NULL;
1996 }
1997
1998 *buf = '\0';
1999 *p_buflen = 0;
2000 modname = PyDict_GetItem(globals, namestr);
2001 if (modname == NULL || !PyString_Check(modname))
2002 return Py_None;
2003
2004 modpath = PyDict_GetItem(globals, pathstr);
2005 if (modpath != NULL) {
2006 int len = PyString_GET_SIZE(modname);
2007 if (len > MAXPATHLEN) {
2008 PyErr_SetString(PyExc_ValueError,
2009 "Module name too long");
2010 return NULL;
2011 }
2012 strcpy(buf, PyString_AS_STRING(modname));
2013 *p_buflen = len;
2014 }
2015 else {
2016 char *start = PyString_AS_STRING(modname);
2017 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002018 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002019 if (lastdot == NULL)
2020 return Py_None;
2021 len = lastdot - start;
2022 if (len >= MAXPATHLEN) {
2023 PyErr_SetString(PyExc_ValueError,
2024 "Module name too long");
2025 return NULL;
2026 }
2027 strncpy(buf, start, len);
2028 buf[len] = '\0';
2029 *p_buflen = len;
2030 }
2031
2032 modules = PyImport_GetModuleDict();
2033 parent = PyDict_GetItemString(modules, buf);
2034 if (parent == NULL)
2035 parent = Py_None;
2036 return parent;
2037 /* We expect, but can't guarantee, if parent != None, that:
2038 - parent.__name__ == buf
2039 - parent.__dict__ is globals
2040 If this is violated... Who cares? */
2041}
2042
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002043/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002044static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002045load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2046 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002047{
2048 char *name = *p_name;
2049 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002050 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002051 char *p;
2052 PyObject *result;
2053
2054 if (dot == NULL) {
2055 *p_name = NULL;
2056 len = strlen(name);
2057 }
2058 else {
2059 *p_name = dot+1;
2060 len = dot-name;
2061 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002062 if (len == 0) {
2063 PyErr_SetString(PyExc_ValueError,
2064 "Empty module name");
2065 return NULL;
2066 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002067
2068 p = buf + *p_buflen;
2069 if (p != buf)
2070 *p++ = '.';
2071 if (p+len-buf >= MAXPATHLEN) {
2072 PyErr_SetString(PyExc_ValueError,
2073 "Module name too long");
2074 return NULL;
2075 }
2076 strncpy(p, name, len);
2077 p[len] = '\0';
2078 *p_buflen = p+len-buf;
2079
2080 result = import_submodule(mod, p, buf);
2081 if (result == Py_None && altmod != mod) {
2082 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002083 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002084 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002085 if (result != NULL && result != Py_None) {
2086 if (mark_miss(buf) != 0) {
2087 Py_DECREF(result);
2088 return NULL;
2089 }
2090 strncpy(buf, name, len);
2091 buf[len] = '\0';
2092 *p_buflen = len;
2093 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002094 }
2095 if (result == NULL)
2096 return NULL;
2097
2098 if (result == Py_None) {
2099 Py_DECREF(result);
2100 PyErr_Format(PyExc_ImportError,
2101 "No module named %.200s", name);
2102 return NULL;
2103 }
2104
2105 return result;
2106}
2107
2108static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002109mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002110{
2111 PyObject *modules = PyImport_GetModuleDict();
2112 return PyDict_SetItemString(modules, name, Py_None);
2113}
2114
2115static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002116ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2117 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002118{
2119 int i;
2120
2121 if (!PyObject_HasAttrString(mod, "__path__"))
2122 return 1;
2123
2124 for (i = 0; ; i++) {
2125 PyObject *item = PySequence_GetItem(fromlist, i);
2126 int hasit;
2127 if (item == NULL) {
2128 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2129 PyErr_Clear();
2130 return 1;
2131 }
2132 return 0;
2133 }
2134 if (!PyString_Check(item)) {
2135 PyErr_SetString(PyExc_TypeError,
2136 "Item in ``from list'' not a string");
2137 Py_DECREF(item);
2138 return 0;
2139 }
2140 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002141 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002142 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002143 /* See if the package defines __all__ */
2144 if (recursive)
2145 continue; /* Avoid endless recursion */
2146 all = PyObject_GetAttrString(mod, "__all__");
2147 if (all == NULL)
2148 PyErr_Clear();
2149 else {
2150 if (!ensure_fromlist(mod, all, buf, buflen, 1))
2151 return 0;
2152 Py_DECREF(all);
2153 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002154 continue;
2155 }
2156 hasit = PyObject_HasAttr(mod, item);
2157 if (!hasit) {
2158 char *subname = PyString_AS_STRING(item);
2159 PyObject *submod;
2160 char *p;
2161 if (buflen + strlen(subname) >= MAXPATHLEN) {
2162 PyErr_SetString(PyExc_ValueError,
2163 "Module name too long");
2164 Py_DECREF(item);
2165 return 0;
2166 }
2167 p = buf + buflen;
2168 *p++ = '.';
2169 strcpy(p, subname);
2170 submod = import_submodule(mod, subname, buf);
2171 Py_XDECREF(submod);
2172 if (submod == NULL) {
2173 Py_DECREF(item);
2174 return 0;
2175 }
2176 }
2177 Py_DECREF(item);
2178 }
2179
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002180 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002181}
2182
2183static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002184import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002185{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002186 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumf48f11c2001-07-23 13:27:49 +00002187 PyObject *m, *res = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002188
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002189 /* Require:
2190 if mod == None: subname == fullname
2191 else: mod.__name__ + "." + subname == fullname
2192 */
2193
Tim Peters50d8d372001-02-28 05:34:27 +00002194 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002195 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002196 }
2197 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002198 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002199 char buf[MAXPATHLEN+1];
2200 struct filedescr *fdp;
2201 FILE *fp = NULL;
2202
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002203 if (mod == Py_None)
2204 path = NULL;
2205 else {
2206 path = PyObject_GetAttrString(mod, "__path__");
2207 if (path == NULL) {
2208 PyErr_Clear();
2209 Py_INCREF(Py_None);
2210 return Py_None;
2211 }
2212 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002213
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002214 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002215 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2216 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002217 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002218 if (fdp == NULL) {
2219 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2220 return NULL;
2221 PyErr_Clear();
2222 Py_INCREF(Py_None);
2223 return Py_None;
2224 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002225 m = load_module(fullname, fp, buf, fdp->type, loader);
2226 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002227 if (fp)
2228 fclose(fp);
Guido van Rossumf48f11c2001-07-23 13:27:49 +00002229 if (mod != Py_None) {
2230 /* Irrespective of the success of this load, make a
2231 reference to it in the parent package module.
2232 A copy gets saved in the modules dictionary
2233 under the full name, so get a reference from
2234 there, if need be. (The exception is when
2235 the load failed with a SyntaxError -- then
2236 there's no trace in sys.modules. In that case,
2237 of course, do nothing extra.) */
2238 res = m;
2239 if (res == NULL)
2240 res = PyDict_GetItemString(modules, fullname);
2241 if (res != NULL &&
2242 PyObject_SetAttrString(mod, subname, res) < 0) {
2243 Py_XDECREF(m);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002244 m = NULL;
2245 }
2246 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002247 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002248
2249 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002250}
2251
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002252
2253/* Re-import a module of any kind and return its module object, WITH
2254 INCREMENTED REFERENCE COUNT */
2255
Guido van Rossum79f25d91997-04-29 20:08:16 +00002256PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002257PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002258{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002259 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002260 PyObject *path = NULL;
2261 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002262 char buf[MAXPATHLEN+1];
2263 struct filedescr *fdp;
2264 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002265
Guido van Rossum79f25d91997-04-29 20:08:16 +00002266 if (m == NULL || !PyModule_Check(m)) {
2267 PyErr_SetString(PyExc_TypeError,
2268 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002269 return NULL;
2270 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002271 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002272 if (name == NULL)
2273 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002274 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002275 PyErr_Format(PyExc_ImportError,
2276 "reload(): module %.200s not in sys.modules",
2277 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002278 return NULL;
2279 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002280 subname = strrchr(name, '.');
2281 if (subname == NULL)
2282 subname = name;
2283 else {
2284 PyObject *parentname, *parent;
2285 parentname = PyString_FromStringAndSize(name, (subname-name));
2286 if (parentname == NULL)
2287 return NULL;
2288 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002289 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002290 if (parent == NULL) {
2291 PyErr_Format(PyExc_ImportError,
2292 "reload(): parent %.200s not in sys.modules",
2293 name);
2294 return NULL;
2295 }
2296 subname++;
2297 path = PyObject_GetAttrString(parent, "__path__");
2298 if (path == NULL)
2299 PyErr_Clear();
2300 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002301 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002302 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002303 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002304 if (fdp == NULL)
2305 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002306 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002307 if (fp)
2308 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002309 return m;
2310}
2311
2312
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002313/* Higher-level import emulator which emulates the "import" statement
2314 more accurately -- it invokes the __import__() function from the
2315 builtins of the current globals. This means that the import is
2316 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002317 environment, e.g. by "rexec".
2318 A dummy list ["__doc__"] is passed as the 4th argument so that
2319 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2320 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002321
2322PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002323PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002324{
2325 static PyObject *silly_list = NULL;
2326 static PyObject *builtins_str = NULL;
2327 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002328 PyObject *globals = NULL;
2329 PyObject *import = NULL;
2330 PyObject *builtins = NULL;
2331 PyObject *r = NULL;
2332
2333 /* Initialize constant string objects */
2334 if (silly_list == NULL) {
2335 import_str = PyString_InternFromString("__import__");
2336 if (import_str == NULL)
2337 return NULL;
2338 builtins_str = PyString_InternFromString("__builtins__");
2339 if (builtins_str == NULL)
2340 return NULL;
2341 silly_list = Py_BuildValue("[s]", "__doc__");
2342 if (silly_list == NULL)
2343 return NULL;
2344 }
2345
2346 /* Get the builtins from current globals */
2347 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002348 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002349 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002350 builtins = PyObject_GetItem(globals, builtins_str);
2351 if (builtins == NULL)
2352 goto err;
2353 }
2354 else {
2355 /* No globals -- use standard builtins, and fake globals */
2356 PyErr_Clear();
2357
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002358 builtins = PyImport_ImportModuleEx("__builtin__",
2359 NULL, NULL, NULL);
2360 if (builtins == NULL)
2361 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002362 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2363 if (globals == NULL)
2364 goto err;
2365 }
2366
2367 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002368 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002369 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002370 if (import == NULL)
2371 PyErr_SetObject(PyExc_KeyError, import_str);
2372 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002373 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002374 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002375 if (import == NULL)
2376 goto err;
2377
2378 /* Call the _import__ function with the proper argument list */
2379 r = PyObject_CallFunction(import, "OOOO",
2380 module_name, globals, globals, silly_list);
2381
2382 err:
2383 Py_XDECREF(globals);
2384 Py_XDECREF(builtins);
2385 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002386
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002387 return r;
2388}
2389
2390
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002391/* Module 'imp' provides Python access to the primitives used for
2392 importing modules.
2393*/
2394
Guido van Rossum79f25d91997-04-29 20:08:16 +00002395static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002396imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002397{
2398 char buf[4];
2399
Guido van Rossum43713e52000-02-29 13:59:29 +00002400 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002401 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00002402 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2403 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2404 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2405 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002406
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002408}
2409
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002411imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002412{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002413 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002414 struct filedescr *fdp;
2415
Guido van Rossum43713e52000-02-29 13:59:29 +00002416 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002419 if (list == NULL)
2420 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002421 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2422 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002423 fdp->suffix, fdp->mode, fdp->type);
2424 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002425 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002426 return NULL;
2427 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002428 if (PyList_Append(list, item) < 0) {
2429 Py_DECREF(list);
2430 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002431 return NULL;
2432 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002433 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002434 }
2435 return list;
2436}
2437
Guido van Rossum79f25d91997-04-29 20:08:16 +00002438static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002439call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002440{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002441 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002442 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002443 struct filedescr *fdp;
2444 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002445 FILE *fp = NULL;
2446
2447 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002448 if (path == Py_None)
2449 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002450 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002451 if (fdp == NULL)
2452 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002453 if (fp != NULL) {
2454 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2455 if (fob == NULL) {
2456 fclose(fp);
2457 return NULL;
2458 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002459 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002460 else {
2461 fob = Py_None;
2462 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002463 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002466 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002467 return ret;
2468}
2469
Guido van Rossum79f25d91997-04-29 20:08:16 +00002470static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002471imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002472{
2473 char *name;
2474 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002475 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002476 return NULL;
2477 return call_find_module(name, path);
2478}
2479
2480static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002481imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002482{
2483 char *name;
2484 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002485 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002486 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002487 return NULL;
2488 ret = init_builtin(name);
2489 if (ret < 0)
2490 return NULL;
2491 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002492 Py_INCREF(Py_None);
2493 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002494 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002495 m = PyImport_AddModule(name);
2496 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002497 return m;
2498}
2499
Guido van Rossum79f25d91997-04-29 20:08:16 +00002500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002501imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002502{
2503 char *name;
2504 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002505 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002506 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002507 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002508 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002509 if (ret < 0)
2510 return NULL;
2511 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002512 Py_INCREF(Py_None);
2513 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002514 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002515 m = PyImport_AddModule(name);
2516 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002517 return m;
2518}
2519
Guido van Rossum79f25d91997-04-29 20:08:16 +00002520static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002521imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002522{
2523 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002524
Guido van Rossum43713e52000-02-29 13:59:29 +00002525 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002526 return NULL;
2527 return get_frozen_object(name);
2528}
2529
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002531imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002532{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002533 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002534 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002535 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002536 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002537}
2538
Guido van Rossum79f25d91997-04-29 20:08:16 +00002539static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002540imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002541{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002542 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002543 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002544 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002545 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002546 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002547 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002548}
2549
2550static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002551get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002552{
2553 FILE *fp;
2554 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002555 if (mode[0] == 'U')
2556 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002557 fp = fopen(pathname, mode);
2558 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002559 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002560 }
2561 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002562 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002564 PyErr_SetString(PyExc_ValueError,
2565 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002566 }
2567 return fp;
2568}
2569
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002571imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572{
2573 char *name;
2574 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002575 PyObject *fob = NULL;
2576 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002578 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002579 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002580 return NULL;
2581 fp = get_file(pathname, fob, "rb");
2582 if (fp == NULL)
2583 return NULL;
2584 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002585 if (fob == NULL)
2586 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002587 return m;
2588}
2589
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002590#ifdef HAVE_DYNAMIC_LOADING
2591
Guido van Rossum79f25d91997-04-29 20:08:16 +00002592static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002593imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002594{
2595 char *name;
2596 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002597 PyObject *fob = NULL;
2598 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002599 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002600 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002601 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002603 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002604 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002605 if (fp == NULL)
2606 return NULL;
2607 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002609 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002610}
2611
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002612#endif /* HAVE_DYNAMIC_LOADING */
2613
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002615imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002616{
2617 char *name;
2618 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002619 PyObject *fob = NULL;
2620 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002621 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002622 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002623 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002624 return NULL;
2625 fp = get_file(pathname, fob, "r");
2626 if (fp == NULL)
2627 return NULL;
2628 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002629 if (fob == NULL)
2630 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002631 return m;
2632}
2633
Jack Jansen9c96a921995-02-15 22:57:06 +00002634#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002635static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002636imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002637{
2638 char *name;
2639 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002640 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002641
Guido van Rossum43713e52000-02-29 13:59:29 +00002642 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002643 return NULL;
2644 m = PyMac_LoadResourceModule(name, pathname);
2645 return m;
2646}
2647#endif /* macintosh */
2648
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002650imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002651{
2652 char *name;
2653 PyObject *fob;
2654 char *pathname;
2655 char *suffix; /* Unused */
2656 char *mode;
2657 int type;
2658 FILE *fp;
2659
Guido van Rossum43713e52000-02-29 13:59:29 +00002660 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002661 &name, &fob, &pathname,
2662 &suffix, &mode, &type))
2663 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002664 if (*mode) {
2665 /* Mode must start with 'r' or 'U' and must not contain '+'.
2666 Implicit in this test is the assumption that the mode
2667 may contain other modifiers like 'b' or 't'. */
2668
2669 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002670 PyErr_Format(PyExc_ValueError,
2671 "invalid file open mode %.200s", mode);
2672 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002673 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002674 }
2675 if (fob == Py_None)
2676 fp = NULL;
2677 else {
2678 if (!PyFile_Check(fob)) {
2679 PyErr_SetString(PyExc_ValueError,
2680 "load_module arg#2 should be a file or None");
2681 return NULL;
2682 }
2683 fp = get_file(pathname, fob, mode);
2684 if (fp == NULL)
2685 return NULL;
2686 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002687 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002688}
2689
2690static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002691imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002692{
2693 char *name;
2694 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002695 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002696 return NULL;
2697 return load_package(name, pathname);
2698}
2699
2700static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002701imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002702{
2703 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002704 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002705 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002706 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002707}
2708
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002709/* Doc strings */
2710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002711PyDoc_STRVAR(doc_imp,
2712"This module provides the components needed to build your own\n\
2713__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002714
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002715PyDoc_STRVAR(doc_find_module,
2716"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002717Search for a module. If path is omitted or None, search for a\n\
2718built-in, frozen or special module and continue search in sys.path.\n\
2719The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002720package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722PyDoc_STRVAR(doc_load_module,
2723"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002724Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727PyDoc_STRVAR(doc_get_magic,
2728"get_magic() -> string\n\
2729Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002731PyDoc_STRVAR(doc_get_suffixes,
2732"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002733Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002735
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002736PyDoc_STRVAR(doc_new_module,
2737"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002738Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002739The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741PyDoc_STRVAR(doc_lock_held,
2742"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002743Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002744On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002745
Guido van Rossum79f25d91997-04-29 20:08:16 +00002746static PyMethodDef imp_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00002747 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2748 {"get_magic", imp_get_magic, METH_VARARGS, doc_get_magic},
2749 {"get_suffixes", imp_get_suffixes, METH_VARARGS, doc_get_suffixes},
2750 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2751 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2752 {"lock_held", imp_lock_held, METH_VARARGS, doc_lock_held},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002753 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002754 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2755 {"init_builtin", imp_init_builtin, METH_VARARGS},
2756 {"init_frozen", imp_init_frozen, METH_VARARGS},
2757 {"is_builtin", imp_is_builtin, METH_VARARGS},
2758 {"is_frozen", imp_is_frozen, METH_VARARGS},
2759 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002760#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002761 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002762#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002763 {"load_package", imp_load_package, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002764#ifdef macintosh
Neal Norwitz031829d2002-03-31 14:37:44 +00002765 {"load_resource", imp_load_resource, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002766#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002767 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002768 {NULL, NULL} /* sentinel */
2769};
2770
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002771static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002772setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002773{
2774 PyObject *v;
2775 int err;
2776
2777 v = PyInt_FromLong((long)value);
2778 err = PyDict_SetItemString(d, name, v);
2779 Py_XDECREF(v);
2780 return err;
2781}
2782
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002783void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002784initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002785{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002786 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002787
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002788 m = Py_InitModule4("imp", imp_methods, doc_imp,
2789 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002790 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002791
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002792 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2793 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2794 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2795 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2796 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2797 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2798 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2799 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002800 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002801 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002802
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002803 failure:
2804 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002805}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002806
2807
Guido van Rossumb18618d2000-05-03 23:44:39 +00002808/* API for embedding applications that want to add their own entries
2809 to the table of built-in modules. This should normally be called
2810 *before* Py_Initialize(). When the table resize fails, -1 is
2811 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002812
2813 After a similar function by Just van Rossum. */
2814
2815int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002816PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002817{
2818 static struct _inittab *our_copy = NULL;
2819 struct _inittab *p;
2820 int i, n;
2821
2822 /* Count the number of entries in both tables */
2823 for (n = 0; newtab[n].name != NULL; n++)
2824 ;
2825 if (n == 0)
2826 return 0; /* Nothing to do */
2827 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2828 ;
2829
2830 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002831 p = our_copy;
2832 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002833 if (p == NULL)
2834 return -1;
2835
2836 /* Copy the tables into the new memory */
2837 if (our_copy != PyImport_Inittab)
2838 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2839 PyImport_Inittab = our_copy = p;
2840 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2841
2842 return 0;
2843}
2844
2845/* Shorthand to add a single entry given a name and a function */
2846
2847int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002848PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002849{
2850 struct _inittab newtab[2];
2851
2852 memset(newtab, '\0', sizeof newtab);
2853
2854 newtab[0].name = name;
2855 newtab[0].initfunc = initfunc;
2856
2857 return PyImport_ExtendInittab(newtab);
2858}