blob: ef3e0e6d7a88e2aa3b941e8f7cf9ac7e15dc329e [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 Rossum80bb9651996-12-05 23:27:02 +000018#ifdef HAVE_UNISTD_H
19#include <unistd.h>
20#endif
21
Guido van Rossum55a83382000-09-20 20:31:38 +000022#ifdef HAVE_FCNTL_H
23#include <fcntl.h>
24#endif
25
Guido van Rossum595d7ba1997-12-05 21:45:29 +000026#if defined(PYCC_VACPP)
27/* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
28#define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
29#endif
30
Guido van Rossumaee0bad1997-09-05 07:33:22 +000031#ifndef S_ISDIR
32#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
33#endif
34
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000035extern time_t PyOS_GetLastModificationTime(char *, FILE *);
36 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000037
Guido van Rossum6c849691994-09-26 15:47:17 +000038/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000039/* Change for each incompatible change */
40/* The value of CR and LF is incorporated so if you ever read or write
41 a .pyc file in text mode the magic number will be wrong; also, the
42 Apple MPW compiler swaps their values, botching string constants */
43/* XXX Perhaps the magic number should be frozen and a version field
44 added to the .pyc file header? */
Guido van Rossumdd5db431997-01-17 21:06:11 +000045/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +000046#define MAGIC (60420 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000047
Guido van Rossum96774c12000-05-01 20:19:08 +000048/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000049 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000050 compiler works which are enabled by command line switches. */
51static long pyc_magic = MAGIC;
52
Guido van Rossum25ce5661997-08-02 03:10:38 +000053/* See _PyImport_FixupExtension() below */
54static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055
Guido van Rossum771c6c81997-10-31 18:37:24 +000056/* This table is defined in config.c: */
57extern struct _inittab _PyImport_Inittab[];
58
59struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000060
Guido van Rossumed1170e1999-12-20 21:23:41 +000061/* these tables define the module suffixes that Python recognizes */
62struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000063
64#ifdef RISCOS
65static const struct filedescr _PyImport_StandardFiletab[] = {
66 {"/py", "r", PY_SOURCE},
67 {"/pyc", "rb", PY_COMPILED},
68 {0, 0}
69};
70#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000071static const struct filedescr _PyImport_StandardFiletab[] = {
72 {".py", "r", PY_SOURCE},
73 {".pyc", "rb", PY_COMPILED},
74 {0, 0}
75};
Guido van Rossum48a680c2001-03-02 06:34:14 +000076#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000077
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079
80void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000081_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082{
Guido van Rossumed1170e1999-12-20 21:23:41 +000083 const struct filedescr *scan;
84 struct filedescr *filetab;
85 int countD = 0;
86 int countS = 0;
87
88 /* prepare _PyImport_Filetab: copy entries from
89 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
90 */
91 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
92 ++countD;
93 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
94 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +000095 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +000096 memcpy(filetab, _PyImport_DynLoadFiletab,
97 countD * sizeof(struct filedescr));
98 memcpy(filetab + countD, _PyImport_StandardFiletab,
99 countS * sizeof(struct filedescr));
100 filetab[countD + countS].suffix = NULL;
101
102 _PyImport_Filetab = filetab;
103
Guido van Rossum0824f631997-03-11 18:37:35 +0000104 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000105 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
106 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000107#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000108 if (strcmp(filetab->suffix, ".pyc") == 0)
109 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000110#else
111 if (strcmp(filetab->suffix, "/pyc") == 0)
112 filetab->suffix = "/pyo";
113#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000114 }
115 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000116
117 if (Py_UnicodeFlag) {
118 /* Fix the pyc_magic so that byte compiled code created
119 using the all-Unicode method doesn't interfere with
120 code created in normal operation mode. */
121 pyc_magic = MAGIC + 1;
122 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}
124
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000126_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127{
128 Py_XDECREF(extensions);
129 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000130 PyMem_DEL(_PyImport_Filetab);
131 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132}
133
134
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000135/* Locking primitives to prevent parallel imports of the same module
136 in different threads to return with a partially loaded module.
137 These calls are serialized by the global interpreter lock. */
138
139#ifdef WITH_THREAD
140
Guido van Rossum49b56061998-10-01 20:42:43 +0000141#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000142
Guido van Rossum65d5b571998-12-21 19:32:43 +0000143static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000144static long import_lock_thread = -1;
145static int import_lock_level = 0;
146
147static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000148lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000149{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000150 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000151 if (me == -1)
152 return; /* Too bad */
153 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000154 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000155 if (import_lock_thread == me) {
156 import_lock_level++;
157 return;
158 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000159 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000160 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000161 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000162 PyEval_RestoreThread(tstate);
163 }
164 import_lock_thread = me;
165 import_lock_level = 1;
166}
167
168static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000169unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000170{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000171 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000172 if (me == -1)
173 return; /* Too bad */
174 if (import_lock_thread != me)
175 Py_FatalError("unlock_import: not holding the import lock");
176 import_lock_level--;
177 if (import_lock_level == 0) {
178 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000179 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000180 }
181}
182
183#else
184
185#define lock_import()
186#define unlock_import()
187
188#endif
189
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190/* Helper for sys */
191
192PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194{
195 PyInterpreterState *interp = PyThreadState_Get()->interp;
196 if (interp->modules == NULL)
197 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
198 return interp->modules;
199}
200
Guido van Rossum3f5da241990-12-20 15:06:42 +0000201
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000202/* List of names to clear in sys */
203static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000204 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000205 "exc_type", "exc_value", "exc_traceback",
206 "last_type", "last_value", "last_traceback",
207 NULL
208};
209
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000210static char* sys_files[] = {
211 "stdin", "__stdin__",
212 "stdout", "__stdout__",
213 "stderr", "__stderr__",
214 NULL
215};
216
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000217
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000218/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000219
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000222{
Guido van Rossum758eec01998-01-19 21:58:26 +0000223 int pos, ndone;
224 char *name;
225 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000226 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000227 PyObject *modules = interp->modules;
228
229 if (modules == NULL)
230 return; /* Already done */
231
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000232 /* Delete some special variables first. These are common
233 places where user values hide and people complain when their
234 destructors fail. Since the modules containing them are
235 deleted *last* of all, they would come too late in the normal
236 destruction order. Sigh. */
237
238 value = PyDict_GetItemString(modules, "__builtin__");
239 if (value != NULL && PyModule_Check(value)) {
240 dict = PyModule_GetDict(value);
241 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000242 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000243 PyDict_SetItemString(dict, "_", Py_None);
244 }
245 value = PyDict_GetItemString(modules, "sys");
246 if (value != NULL && PyModule_Check(value)) {
247 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000248 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000249 dict = PyModule_GetDict(value);
250 for (p = sys_deletes; *p != NULL; p++) {
251 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000252 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000253 PyDict_SetItemString(dict, *p, Py_None);
254 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000255 for (p = sys_files; *p != NULL; p+=2) {
256 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000257 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000258 v = PyDict_GetItemString(dict, *(p+1));
259 if (v == NULL)
260 v = Py_None;
261 PyDict_SetItemString(dict, *p, v);
262 }
263 }
264
265 /* First, delete __main__ */
266 value = PyDict_GetItemString(modules, "__main__");
267 if (value != NULL && PyModule_Check(value)) {
268 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000269 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000270 _PyModule_Clear(value);
271 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000272 }
273
Guido van Rossum758eec01998-01-19 21:58:26 +0000274 /* The special treatment of __builtin__ here is because even
275 when it's not referenced as a module, its dictionary is
276 referenced by almost every module's __builtins__. Since
277 deleting a module clears its dictionary (even if there are
278 references left to it), we need to delete the __builtin__
279 module last. Likewise, we don't delete sys until the very
280 end because it is implicitly referenced (e.g. by print).
281
282 Also note that we 'delete' modules by replacing their entry
283 in the modules dict with None, rather than really deleting
284 them; this avoids a rehash of the modules dictionary and
285 also marks them as "non existent" so they won't be
286 re-imported. */
287
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000288 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000289 one (skipping __builtin__ and sys) and delete them */
290 do {
291 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000292 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000293 while (PyDict_Next(modules, &pos, &key, &value)) {
294 if (value->ob_refcnt != 1)
295 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000296 if (PyString_Check(key) && PyModule_Check(value)) {
297 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000298 if (strcmp(name, "__builtin__") == 0)
299 continue;
300 if (strcmp(name, "sys") == 0)
301 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000302 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000303 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000304 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000305 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000306 PyDict_SetItem(modules, key, Py_None);
307 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000308 }
309 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000310 } while (ndone > 0);
311
Guido van Rossum758eec01998-01-19 21:58:26 +0000312 /* Next, delete all modules (still skipping __builtin__ and sys) */
313 pos = 0;
314 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000315 if (PyString_Check(key) && PyModule_Check(value)) {
316 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000317 if (strcmp(name, "__builtin__") == 0)
318 continue;
319 if (strcmp(name, "sys") == 0)
320 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000321 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000322 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000323 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000324 PyDict_SetItem(modules, key, Py_None);
325 }
326 }
327
328 /* Next, delete sys and __builtin__ (in that order) */
329 value = PyDict_GetItemString(modules, "sys");
330 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000332 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000333 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000334 PyDict_SetItemString(modules, "sys", Py_None);
335 }
336 value = PyDict_GetItemString(modules, "__builtin__");
337 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000338 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000339 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000340 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000341 PyDict_SetItemString(modules, "__builtin__", Py_None);
342 }
343
344 /* Finally, clear and delete the modules directory */
345 PyDict_Clear(modules);
346 interp->modules = NULL;
347 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000348}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000349
350
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000351/* Helper for pythonrun.c -- return magic number */
352
353long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000354PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000355{
Guido van Rossum96774c12000-05-01 20:19:08 +0000356 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000357}
358
359
Guido van Rossum25ce5661997-08-02 03:10:38 +0000360/* Magic for extension modules (built-in as well as dynamically
361 loaded). To prevent initializing an extension module more than
362 once, we keep a static dictionary 'extensions' keyed by module name
363 (for built-in modules) or by filename (for dynamically loaded
364 modules), containing these modules. A copy od the module's
365 dictionary is stored by calling _PyImport_FixupExtension()
366 immediately after the module initialization function succeeds. A
367 copy can be retrieved from there by calling
368 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000369
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000371_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000372{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000373 PyObject *modules, *mod, *dict, *copy;
374 if (extensions == NULL) {
375 extensions = PyDict_New();
376 if (extensions == NULL)
377 return NULL;
378 }
379 modules = PyImport_GetModuleDict();
380 mod = PyDict_GetItemString(modules, name);
381 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000382 PyErr_Format(PyExc_SystemError,
383 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000384 return NULL;
385 }
386 dict = PyModule_GetDict(mod);
387 if (dict == NULL)
388 return NULL;
389 copy = PyObject_CallMethod(dict, "copy", "");
390 if (copy == NULL)
391 return NULL;
392 PyDict_SetItemString(extensions, filename, copy);
393 Py_DECREF(copy);
394 return copy;
395}
396
397PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000399{
400 PyObject *dict, *mod, *mdict, *result;
401 if (extensions == NULL)
402 return NULL;
403 dict = PyDict_GetItemString(extensions, filename);
404 if (dict == NULL)
405 return NULL;
406 mod = PyImport_AddModule(name);
407 if (mod == NULL)
408 return NULL;
409 mdict = PyModule_GetDict(mod);
410 if (mdict == NULL)
411 return NULL;
412 result = PyObject_CallMethod(mdict, "update", "O", dict);
413 if (result == NULL)
414 return NULL;
415 Py_DECREF(result);
416 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000417 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418 name, filename);
419 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420}
421
422
423/* Get the module object corresponding to a module name.
424 First check the modules dictionary if there's one there,
425 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000426 Because the former action is most common, THIS DOES NOT RETURN A
427 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000428
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000431{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000432 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000434
Guido van Rossum25ce5661997-08-02 03:10:38 +0000435 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000436 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000437 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000439 if (m == NULL)
440 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000441 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000443 return NULL;
444 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446
447 return m;
448}
449
450
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000451/* Execute a code object in a module and return the module object
452 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453
Guido van Rossum79f25d91997-04-29 20:08:16 +0000454PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000456{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000457 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
458}
459
460PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000461PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000462{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000463 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465
Guido van Rossum79f25d91997-04-29 20:08:16 +0000466 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000467 if (m == NULL)
468 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469 d = PyModule_GetDict(m);
470 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
471 if (PyDict_SetItemString(d, "__builtins__",
472 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000473 return NULL;
474 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000475 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000476 v = NULL;
477 if (pathname != NULL) {
478 v = PyString_FromString(pathname);
479 if (v == NULL)
480 PyErr_Clear();
481 }
482 if (v == NULL) {
483 v = ((PyCodeObject *)co)->co_filename;
484 Py_INCREF(v);
485 }
486 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000487 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000488 Py_DECREF(v);
489
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000490 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000491 if (v == NULL)
492 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000493 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000494
Guido van Rossum25ce5661997-08-02 03:10:38 +0000495 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000496 PyErr_Format(PyExc_ImportError,
497 "Loaded module %.200s not found in sys.modules",
498 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000499 return NULL;
500 }
501
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503
504 return m;
505}
506
507
508/* Given a pathname for a Python source file, fill a buffer with the
509 pathname for the corresponding compiled file. Return the pathname
510 for the compiled file, or NULL if there's no space in the buffer.
511 Doesn't set an exception. */
512
513static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000514make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000515{
Fred Drake4c82b232000-06-30 16:18:57 +0000516 size_t len;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517
518 len = strlen(pathname);
519 if (len+2 > buflen)
520 return NULL;
521 strcpy(buf, pathname);
Guido van Rossum0824f631997-03-11 18:37:35 +0000522 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523
524 return buf;
525}
526
527
528/* Given a pathname for a Python source file, its time of last
529 modification, and a pathname for a compiled file, check whether the
530 compiled file represents the same version of the source. If so,
531 return a FILE pointer for the compiled file, positioned just after
532 the header; if not, return NULL.
533 Doesn't set an exception. */
534
535static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000536check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000537{
538 FILE *fp;
539 long magic;
540 long pyc_mtime;
541
542 fp = fopen(cpathname, "rb");
543 if (fp == NULL)
544 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000546 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000547 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000548 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 fclose(fp);
550 return NULL;
551 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000555 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 fclose(fp);
557 return NULL;
558 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000560 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000561 return fp;
562}
563
564
565/* Read a code object from a file and check it for validity */
566
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000568read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
Tim Petersd9b9ac82001-01-28 00:27:39 +0000572 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000574 if (co == NULL || !PyCode_Check(co)) {
575 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000576 PyErr_Format(PyExc_ImportError,
577 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000579 return NULL;
580 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582}
583
584
585/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000586 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000587
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000589load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590{
591 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 PyCodeObject *co;
593 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000596 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000597 PyErr_Format(PyExc_ImportError,
598 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000599 return NULL;
600 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000602 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603 if (co == NULL)
604 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000606 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000608 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610
611 return m;
612}
613
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614/* Parse a source file and return the corresponding code object */
615
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000617parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620 node *n;
621
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000622 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623 if (n == NULL)
624 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 co = PyNode_Compile(n, pathname);
626 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627
628 return co;
629}
630
631
Guido van Rossum55a83382000-09-20 20:31:38 +0000632/* Helper to open a bytecode file for writing in exclusive mode */
633
634static FILE *
635open_exclusive(char *filename)
636{
637#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
638 /* Use O_EXCL to avoid a race condition when another process tries to
639 write the same file. When that happens, our open() call fails,
640 which is just fine (since it's only a cache).
641 XXX If the file exists and is writable but the directory is not
642 writable, the file will never be written. Oh well.
643 */
644 int fd;
645 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000646 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
647#ifdef O_BINARY
648 |O_BINARY /* necessary for Windows */
649#endif
Tim Peters50d8d372001-02-28 05:34:27 +0000650
Tim Peters42c83af2000-09-29 04:03:10 +0000651 , 0666);
Guido van Rossum55a83382000-09-20 20:31:38 +0000652 if (fd < 0)
653 return NULL;
654 return fdopen(fd, "wb");
655#else
656 /* Best we can do -- on Windows this can't happen anyway */
657 return fopen(filename, "wb");
658#endif
659}
660
661
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662/* Write a compiled module to a file, placing the time of last
663 modification of its source into the header.
664 Errors are ignored, if a write error occurs an attempt is made to
665 remove the file. */
666
667static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000668write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669{
670 FILE *fp;
671
Guido van Rossum55a83382000-09-20 20:31:38 +0000672 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000674 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000675 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676 "# can't create %s\n", cpathname);
677 return;
678 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000679 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 PyMarshal_WriteLongToFile(0L, fp);
682 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000685 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686 /* Don't keep partial file */
687 fclose(fp);
688 (void) unlink(cpathname);
689 return;
690 }
691 /* Now write the true mtime */
692 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694 fflush(fp);
695 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000697 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698#ifdef macintosh
Jack Jansencbf630f2000-07-11 21:59:16 +0000699 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700#endif
701}
702
703
704/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000705 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
706 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000709load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000710{
Fred Drake4c82b232000-06-30 16:18:57 +0000711 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712 FILE *fpc;
713 char buf[MAXPATHLEN+1];
714 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 PyCodeObject *co;
716 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000718 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000719 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000720 return NULL;
721#if SIZEOF_TIME_T > 4
722 /* Python's .pyc timestamp handling presumes that the timestamp fits
723 in 4 bytes. This will be fine until sometime in the year 2038,
724 when a 4-byte signed time_t will overflow.
725 */
726 if (mtime >> 32) {
727 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000728 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000729 return NULL;
730 }
731#endif
Jeremy Hylton37832f02001-04-13 17:50:20 +0000732 cpathname = make_compiled_pathname(pathname, buf,
733 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734 if (cpathname != NULL &&
735 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000736 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737 fclose(fpc);
738 if (co == NULL)
739 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000741 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000743 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744 }
745 else {
746 co = parse_source_module(pathname, fp);
747 if (co == NULL)
748 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000750 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 name, pathname);
752 write_compiled_module(co, cpathname, mtime);
753 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000754 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756
757 return m;
758}
759
760
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000761/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000762static PyObject *load_module(char *, FILE *, char *, int);
763static struct filedescr *find_module(char *, PyObject *,
764 char *, size_t, FILE **);
765static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000766
767/* Load a package and return its module object WITH INCREMENTED
768 REFERENCE COUNT */
769
770static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000771load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000772{
773 PyObject *m, *d, *file, *path;
774 int err;
775 char buf[MAXPATHLEN+1];
776 FILE *fp = NULL;
777 struct filedescr *fdp;
778
779 m = PyImport_AddModule(name);
780 if (m == NULL)
781 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000782 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000783 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000784 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000785 d = PyModule_GetDict(m);
786 file = PyString_FromString(pathname);
787 if (file == NULL)
788 return NULL;
789 path = Py_BuildValue("[O]", file);
790 if (path == NULL) {
791 Py_DECREF(file);
792 return NULL;
793 }
794 err = PyDict_SetItemString(d, "__file__", file);
795 if (err == 0)
796 err = PyDict_SetItemString(d, "__path__", path);
797 if (err != 0) {
798 m = NULL;
799 goto cleanup;
800 }
801 buf[0] = '\0';
802 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
803 if (fdp == NULL) {
804 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
805 PyErr_Clear();
806 }
807 else
808 m = NULL;
809 goto cleanup;
810 }
811 m = load_module(name, fp, buf, fdp->type);
812 if (fp != NULL)
813 fclose(fp);
814 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000815 Py_XDECREF(path);
816 Py_XDECREF(file);
817 return m;
818}
819
820
821/* Helper to test for built-in module */
822
823static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000824is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000825{
826 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000827 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
828 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
829 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000830 return -1;
831 else
832 return 1;
833 }
834 }
835 return 0;
836}
837
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000838
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839/* Search the path (default sys.path) for a module. Return the
840 corresponding filedescr struct, and (via return arguments) the
841 pathname and an open file. Return NULL if the module is not found. */
842
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000843#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +0000844extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
845 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000846#endif
847
Tim Peters50d8d372001-02-28 05:34:27 +0000848static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000849static int find_init_module(char *); /* Forward */
Guido van Rossum197346f1997-10-31 18:38:52 +0000850
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851static struct filedescr *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000852find_module(char *realname, PyObject *path, char *buf, size_t buflen,
853 FILE **p_fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854{
Fred Drake4c82b232000-06-30 16:18:57 +0000855 int i, npath;
856 size_t len, namelen;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000857 struct _frozen *f;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000858 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000859 FILE *fp = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000860#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000861 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000862#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000863 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
864 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
865 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +0000866 char name[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000867
Fred Drake4c82b232000-06-30 16:18:57 +0000868 if (strlen(realname) > MAXPATHLEN) {
869 PyErr_SetString(PyExc_OverflowError, "module name is too long");
870 return NULL;
871 }
Guido van Rossum0506a431998-08-11 15:07:39 +0000872 strcpy(name, realname);
873
874 if (path != NULL && PyString_Check(path)) {
875 /* Submodule of "frozen" package:
876 Set name to the fullname, path to NULL
877 and continue as "usual" */
878 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
879 PyErr_SetString(PyExc_ImportError,
880 "full frozen module name too long");
881 return NULL;
882 }
883 strcpy(buf, PyString_AsString(path));
884 strcat(buf, ".");
885 strcat(buf, name);
886 strcpy(name, buf);
887 path = NULL;
888 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000889 if (path == NULL) {
890 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000891 strcpy(buf, name);
892 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000893 }
Guido van Rossuma5568d31998-03-05 03:45:08 +0000894 if ((f = find_frozen(name)) != NULL) {
895 strcpy(buf, name);
896 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000897 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000898
Guido van Rossumac279101996-08-22 23:10:58 +0000899#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000900 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
901 if (fp != NULL) {
902 *p_fp = fp;
903 return fdp;
904 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000905#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000906 path = PySys_GetObject("path");
907 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 if (path == NULL || !PyList_Check(path)) {
909 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +0000910 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000911 return NULL;
912 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000914 namelen = strlen(name);
915 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 PyObject *v = PyList_GetItem(path, i);
917 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000918 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 len = PyString_Size(v);
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000920 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921 continue; /* Too long */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 strcpy(buf, PyString_AsString(v));
Fred Drake4c82b232000-06-30 16:18:57 +0000923 if (strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000924 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000925#ifdef macintosh
Guido van Rossum741689d1997-08-12 14:53:39 +0000926#ifdef INTERN_STRINGS
Tim Peters50d8d372001-02-28 05:34:27 +0000927 /*
Guido van Rossum741689d1997-08-12 14:53:39 +0000928 ** Speedup: each sys.path item is interned, and
929 ** FindResourceModule remembers which items refer to
930 ** folders (so we don't have to bother trying to look
Tim Peters50d8d372001-02-28 05:34:27 +0000931 ** into them for resources).
Guido van Rossum741689d1997-08-12 14:53:39 +0000932 */
933 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
934 v = PyList_GET_ITEM(path, i);
935#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000936 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 static struct filedescr resfiledescr =
938 {"", "", PY_RESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +0000939
Jack Jansen9c96a921995-02-15 22:57:06 +0000940 return &resfiledescr;
941 }
Guido van Rossum0f84a341998-08-06 13:36:01 +0000942 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
943 static struct filedescr resfiledescr =
944 {"", "", PY_CODERESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +0000945
Guido van Rossum0f84a341998-08-06 13:36:01 +0000946 return &resfiledescr;
947 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000948#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949 if (len > 0 && buf[len-1] != SEP
950#ifdef ALTSEP
951 && buf[len-1] != ALTSEP
952#endif
953 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000954 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +0000955 strcpy(buf+len, name);
956 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +0000957
958 /* Check for package import (buf holds a directory name,
959 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000960#ifdef HAVE_STAT
Tim Peterscab3f682001-04-29 22:21:25 +0000961 if (stat(buf, &statbuf) == 0 && /* it exists */
962 S_ISDIR(statbuf.st_mode) && /* it's a directory */
963 find_init_module(buf) && /* it has __init__.py */
964 case_ok(buf, len, namelen, name)) /* and case matches */
965 return &fd_package;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000966#else
967 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +0000968#ifdef RISCOS
969 {
970 static struct filedescr fd = {"", "", PKG_DIRECTORY};
971 if (isdir(buf)) {
972 if (find_init_module(buf))
973 return &fd;
974 }
975 }
976#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000977#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +0000978#ifdef macintosh
979 fdp = PyMac_FindModuleExtension(buf, &len, name);
Jack Jansen4df3c522001-03-20 23:09:54 +0000980 if (fdp) {
Guido van Rossum9a61dc91997-10-08 15:25:08 +0000981#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000982 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000983 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000985 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansen4df3c522001-03-20 23:09:54 +0000986#endif /* !macintosh */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000987 fp = fopen(buf, fdp->mode);
Tim Peters50d8d372001-02-28 05:34:27 +0000988 if (fp != NULL) {
989 if (case_ok(buf, len, namelen, name))
990 break;
991 else { /* continue search */
992 fclose(fp);
993 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +0000994 }
Barry Warsaw914a0b12001-02-02 19:12:16 +0000995 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000996 }
997 if (fp != NULL)
998 break;
999 }
1000 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001001 PyErr_Format(PyExc_ImportError,
1002 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001003 return NULL;
1004 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001005 *p_fp = fp;
1006 return fdp;
1007}
1008
Tim Petersd1e87a82001-03-01 18:12:00 +00001009/* case_ok(char* buf, int len, int namelen, char* name)
1010 * The arguments here are tricky, best shown by example:
1011 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1012 * ^ ^ ^ ^
1013 * |--------------------- buf ---------------------|
1014 * |------------------- len ------------------|
1015 * |------ name -------|
1016 * |----- namelen -----|
1017 * buf is the full path, but len only counts up to (& exclusive of) the
1018 * extension. name is the module name, also exclusive of extension.
1019 *
1020 * We've already done a successful stat() or fopen() on buf, so know that
1021 * there's some match, possibly case-insensitive.
1022 *
Tim Peters50d8d372001-02-28 05:34:27 +00001023 * case_ok() is to return 1 if there's a case-sensitive match for
1024 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1025 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001026 *
Tim Peters50d8d372001-02-28 05:34:27 +00001027 * case_ok() is used to implement case-sensitive import semantics even
1028 * on platforms with case-insensitive filesystems. It's trivial to implement
1029 * for case-sensitive filesystems. It's pretty much a cross-platform
1030 * nightmare for systems with case-insensitive filesystems.
1031 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001032
Tim Peters50d8d372001-02-28 05:34:27 +00001033/* First we may need a pile of platform-specific header files; the sequence
1034 * of #if's here should match the sequence in the body of case_ok().
1035 */
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001036#if defined(MS_WIN32) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001037#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001038#ifdef __CYGWIN__
1039#include <sys/cygwin.h>
1040#endif
1041
Tim Peters50d8d372001-02-28 05:34:27 +00001042#elif defined(DJGPP)
1043#include <dir.h>
1044
1045#elif defined(macintosh)
1046#include <TextUtils.h>
1047#ifdef USE_GUSI1
1048#include "TFileSpec.h" /* for Path2FSSpec() */
1049#endif
1050
Tim Petersd1e87a82001-03-01 18:12:00 +00001051#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001052#include <sys/types.h>
1053#include <dirent.h>
1054
Tim Peters50d8d372001-02-28 05:34:27 +00001055#endif
1056
Guido van Rossum0980bd91998-02-13 17:18:36 +00001057static int
Tim Peters50d8d372001-02-28 05:34:27 +00001058case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001059{
Tim Peters50d8d372001-02-28 05:34:27 +00001060/* Pick a platform-specific implementation; the sequence of #if's here should
1061 * match the sequence just above.
1062 */
1063
1064/* MS_WIN32 || __CYGWIN__ */
1065#if defined(MS_WIN32) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001066 WIN32_FIND_DATA data;
1067 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001068#ifdef __CYGWIN__
1069 char tempbuf[MAX_PATH];
1070#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001071
Guido van Rossum0980bd91998-02-13 17:18:36 +00001072 if (getenv("PYTHONCASEOK") != NULL)
1073 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001074
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001075#ifdef __CYGWIN__
1076 cygwin32_conv_to_win32_path(buf, tempbuf);
1077 h = FindFirstFile(tempbuf, &data);
1078#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001079 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001080#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001081 if (h == INVALID_HANDLE_VALUE) {
1082 PyErr_Format(PyExc_NameError,
1083 "Can't find file for module %.100s\n(filename %.300s)",
1084 name, buf);
1085 return 0;
1086 }
1087 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001088 return strncmp(data.cFileName, name, namelen) == 0;
1089
1090/* DJGPP */
1091#elif defined(DJGPP)
1092 struct ffblk ffblk;
1093 int done;
1094
1095 if (getenv("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001096 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001097
1098 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1099 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001100 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001101 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001102 name, buf);
1103 return 0;
1104 }
Tim Peters50d8d372001-02-28 05:34:27 +00001105 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001106
Tim Peters50d8d372001-02-28 05:34:27 +00001107/* macintosh */
1108#elif defined(macintosh)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001109 FSSpec fss;
1110 OSErr err;
Tim Peters50d8d372001-02-28 05:34:27 +00001111
1112 if (getenv("PYTHONCASEOK") != NULL)
1113 return 1;
1114
Guido van Rossumb33aa1a2000-04-24 15:08:18 +00001115#ifndef USE_GUSI1
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001116 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1117#else
1118 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1119 the way, which is fine for all directories, but here we need
1120 the original name of the alias file (say, Dlg.ppc.slb, not
1121 toolboxmodules.ppc.slb). */
1122 char *colon;
1123 err = Path2FSSpec(buf, &fss);
1124 if (err == noErr) {
1125 colon = strrchr(buf, ':'); /* find filename */
1126 if (colon != NULL)
1127 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1128 Pstring(colon+1), &fss);
1129 else
1130 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1131 fss.name, &fss);
1132 }
1133#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001134 if (err) {
1135 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001136 "Can't find file for module %.100s\n(filename %.300s)",
1137 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001138 return 0;
1139 }
Tim Peters50d8d372001-02-28 05:34:27 +00001140 return fss.name[0] >= namelen &&
1141 strncmp(name, (char *)fss.name+1, namelen) == 0;
1142
Tim Peters677898a2001-03-02 03:28:03 +00001143/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001144#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001145 DIR *dirp;
1146 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001147 char dirname[MAXPATHLEN + 1];
1148 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001149
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001150 if (getenv("PYTHONCASEOK") != NULL)
1151 return 1;
1152
Tim Petersd1e87a82001-03-01 18:12:00 +00001153 /* Copy the dir component into dirname; substitute "." if empty */
1154 if (dirlen <= 0) {
1155 dirname[0] = '.';
1156 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001157 }
1158 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001159 assert(dirlen <= MAXPATHLEN);
1160 memcpy(dirname, buf, dirlen);
1161 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001162 }
1163 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001164 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001165 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001166 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001167 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001168 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001169#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001170 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001171#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001172 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001173#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001174 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001175 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001176 (void)closedir(dirp);
1177 return 1; /* Found */
1178 }
1179 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001180 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001181 }
Tim Peters430f5d42001-03-01 01:30:56 +00001182 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001183
Tim Peters50d8d372001-02-28 05:34:27 +00001184/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1185#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001186 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001187
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001188#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001189}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001190
Guido van Rossum0980bd91998-02-13 17:18:36 +00001191
Guido van Rossum197346f1997-10-31 18:38:52 +00001192#ifdef HAVE_STAT
1193/* Helper to look for __init__.py or __init__.py[co] in potential package */
1194static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001195find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001196{
Fred Drake4c82b232000-06-30 16:18:57 +00001197 size_t save_len = strlen(buf);
1198 size_t i = save_len;
Guido van Rossum197346f1997-10-31 18:38:52 +00001199 struct stat statbuf;
1200
1201 if (save_len + 13 >= MAXPATHLEN)
1202 return 0;
1203 buf[i++] = SEP;
1204 strcpy(buf+i, "__init__.py");
1205 if (stat(buf, &statbuf) == 0) {
1206 buf[save_len] = '\0';
1207 return 1;
1208 }
1209 i += strlen(buf+i);
1210 if (Py_OptimizeFlag)
1211 strcpy(buf+i, "o");
1212 else
1213 strcpy(buf+i, "c");
1214 if (stat(buf, &statbuf) == 0) {
1215 buf[save_len] = '\0';
1216 return 1;
1217 }
1218 buf[save_len] = '\0';
1219 return 0;
1220}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001221
1222#else
1223
1224#ifdef RISCOS
1225static int
1226find_init_module(buf)
1227 char *buf;
1228{
1229 int save_len = strlen(buf);
1230 int i = save_len;
1231
1232 if (save_len + 13 >= MAXPATHLEN)
1233 return 0;
1234 buf[i++] = SEP;
1235 strcpy(buf+i, "__init__/py");
1236 if (isfile(buf)) {
1237 buf[save_len] = '\0';
1238 return 1;
1239 }
1240
1241 if (Py_OptimizeFlag)
1242 strcpy(buf+i, "o");
1243 else
1244 strcpy(buf+i, "c");
1245 if (isfile(buf)) {
1246 buf[save_len] = '\0';
1247 return 1;
1248 }
1249 buf[save_len] = '\0';
1250 return 0;
1251}
1252#endif /*RISCOS*/
1253
Guido van Rossum197346f1997-10-31 18:38:52 +00001254#endif /* HAVE_STAT */
1255
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001256
Tim Petersdbd9ba62000-07-09 03:09:57 +00001257static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001258
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001259/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001260 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001261
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001263load_module(char *name, FILE *fp, char *buf, int type)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001264{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001265 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001267 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001268
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001269 /* First check that there's an open file (if we need one) */
1270 switch (type) {
1271 case PY_SOURCE:
1272 case PY_COMPILED:
1273 if (fp == NULL) {
1274 PyErr_Format(PyExc_ValueError,
1275 "file object required for import (type code %d)",
1276 type);
1277 return NULL;
1278 }
1279 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001280
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001281 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001282
1283 case PY_SOURCE:
1284 m = load_source_module(name, buf, fp);
1285 break;
1286
1287 case PY_COMPILED:
1288 m = load_compiled_module(name, buf, fp);
1289 break;
1290
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001291#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001292 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001293 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001294 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001295#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001296
Jack Jansen9c96a921995-02-15 22:57:06 +00001297#ifdef macintosh
1298 case PY_RESOURCE:
1299 m = PyMac_LoadResourceModule(name, buf);
1300 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001301 case PY_CODERESOURCE:
1302 m = PyMac_LoadCodeResourceModule(name, buf);
1303 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001304#endif
1305
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001306 case PKG_DIRECTORY:
1307 m = load_package(name, buf);
1308 break;
1309
1310 case C_BUILTIN:
1311 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001312 if (buf != NULL && buf[0] != '\0')
1313 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001314 if (type == C_BUILTIN)
1315 err = init_builtin(name);
1316 else
1317 err = PyImport_ImportFrozenModule(name);
1318 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001319 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001320 if (err == 0) {
1321 PyErr_Format(PyExc_ImportError,
1322 "Purported %s module %.200s not found",
1323 type == C_BUILTIN ?
1324 "builtin" : "frozen",
1325 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001326 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001327 }
1328 modules = PyImport_GetModuleDict();
1329 m = PyDict_GetItemString(modules, name);
1330 if (m == NULL) {
1331 PyErr_Format(
1332 PyExc_ImportError,
1333 "%s module %.200s not properly initialized",
1334 type == C_BUILTIN ?
1335 "builtin" : "frozen",
1336 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001337 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001338 }
1339 Py_INCREF(m);
1340 break;
1341
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001342 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001343 PyErr_Format(PyExc_ImportError,
1344 "Don't know how to import %.200s (type code %d)",
1345 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001346 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001347
1348 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001349
1350 return m;
1351}
1352
1353
1354/* Initialize a built-in module.
1355 Return 1 for succes, 0 if the module is not found, and -1 with
1356 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001357
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001358static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001359init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001360{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001361 struct _inittab *p;
1362 PyObject *mod;
1363
1364 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1365 return 1;
1366
Guido van Rossum771c6c81997-10-31 18:37:24 +00001367 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001368 if (strcmp(name, p->name) == 0) {
1369 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001370 PyErr_Format(PyExc_ImportError,
1371 "Cannot re-init internal module %.200s",
1372 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001373 return -1;
1374 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001376 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001377 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001378 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001379 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001380 if (_PyImport_FixupExtension(name, name) == NULL)
1381 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001382 return 1;
1383 }
1384 }
1385 return 0;
1386}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001387
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001388
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001389/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001390
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001391static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001392find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001393{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001394 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001395
Guido van Rossum79f25d91997-04-29 20:08:16 +00001396 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001397 if (p->name == NULL)
1398 return NULL;
1399 if (strcmp(p->name, name) == 0)
1400 break;
1401 }
1402 return p;
1403}
1404
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001406get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001407{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001408 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001409 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001410
1411 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001412 PyErr_Format(PyExc_ImportError,
1413 "No such frozen object named %.200s",
1414 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001415 return NULL;
1416 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001417 size = p->size;
1418 if (size < 0)
1419 size = -size;
1420 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001421}
1422
1423/* Initialize a frozen module.
1424 Return 1 for succes, 0 if the module is not found, and -1 with
1425 an exception set if the initialization failed.
1426 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001427
1428int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001429PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001430{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001431 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001432 PyObject *co;
1433 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001434 int ispackage;
1435 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001436
1437 if (p == NULL)
1438 return 0;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001439 size = p->size;
1440 ispackage = (size < 0);
1441 if (ispackage)
1442 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001444 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001445 name, ispackage ? " package" : "");
1446 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001447 if (co == NULL)
1448 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001449 if (!PyCode_Check(co)) {
1450 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001451 PyErr_Format(PyExc_TypeError,
1452 "frozen object %.200s is not a code object",
1453 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001454 return -1;
1455 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001456 if (ispackage) {
1457 /* Set __path__ to the package name */
1458 PyObject *d, *s;
1459 int err;
1460 m = PyImport_AddModule(name);
1461 if (m == NULL)
1462 return -1;
1463 d = PyModule_GetDict(m);
1464 s = PyString_InternFromString(name);
1465 if (s == NULL)
1466 return -1;
1467 err = PyDict_SetItemString(d, "__path__", s);
1468 Py_DECREF(s);
1469 if (err != 0)
1470 return err;
1471 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001472 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001473 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001474 if (m == NULL)
1475 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001476 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001477 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001478}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001479
1480
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001481/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001482 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001483
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001485PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001486{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001487 PyObject *pname;
1488 PyObject *result;
1489
1490 pname = PyString_FromString(name);
1491 result = PyImport_Import(pname);
1492 Py_DECREF(pname);
1493 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001494}
1495
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001496/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001497static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1498static PyObject *load_next(PyObject *mod, PyObject *altmod,
1499 char **p_name, char *buf, int *p_buflen);
1500static int mark_miss(char *name);
1501static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1502 char *buf, int buflen, int recursive);
1503static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001504
1505/* The Magnum Opus of dotted-name import :-) */
1506
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001507static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001508import_module_ex(char *name, PyObject *globals, PyObject *locals,
1509 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001510{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001511 char buf[MAXPATHLEN+1];
1512 int buflen = 0;
1513 PyObject *parent, *head, *next, *tail;
1514
1515 parent = get_parent(globals, buf, &buflen);
1516 if (parent == NULL)
1517 return NULL;
1518
1519 head = load_next(parent, Py_None, &name, buf, &buflen);
1520 if (head == NULL)
1521 return NULL;
1522
1523 tail = head;
1524 Py_INCREF(tail);
1525 while (name) {
1526 next = load_next(tail, tail, &name, buf, &buflen);
1527 Py_DECREF(tail);
1528 if (next == NULL) {
1529 Py_DECREF(head);
1530 return NULL;
1531 }
1532 tail = next;
1533 }
1534
1535 if (fromlist != NULL) {
1536 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1537 fromlist = NULL;
1538 }
1539
1540 if (fromlist == NULL) {
1541 Py_DECREF(tail);
1542 return head;
1543 }
1544
1545 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001546 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001547 Py_DECREF(tail);
1548 return NULL;
1549 }
1550
1551 return tail;
1552}
1553
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001554PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001555PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1556 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001557{
1558 PyObject *result;
1559 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001560 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001561 unlock_import();
1562 return result;
1563}
1564
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001565static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001566get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001567{
1568 static PyObject *namestr = NULL;
1569 static PyObject *pathstr = NULL;
1570 PyObject *modname, *modpath, *modules, *parent;
1571
1572 if (globals == NULL || !PyDict_Check(globals))
1573 return Py_None;
1574
1575 if (namestr == NULL) {
1576 namestr = PyString_InternFromString("__name__");
1577 if (namestr == NULL)
1578 return NULL;
1579 }
1580 if (pathstr == NULL) {
1581 pathstr = PyString_InternFromString("__path__");
1582 if (pathstr == NULL)
1583 return NULL;
1584 }
1585
1586 *buf = '\0';
1587 *p_buflen = 0;
1588 modname = PyDict_GetItem(globals, namestr);
1589 if (modname == NULL || !PyString_Check(modname))
1590 return Py_None;
1591
1592 modpath = PyDict_GetItem(globals, pathstr);
1593 if (modpath != NULL) {
1594 int len = PyString_GET_SIZE(modname);
1595 if (len > MAXPATHLEN) {
1596 PyErr_SetString(PyExc_ValueError,
1597 "Module name too long");
1598 return NULL;
1599 }
1600 strcpy(buf, PyString_AS_STRING(modname));
1601 *p_buflen = len;
1602 }
1603 else {
1604 char *start = PyString_AS_STRING(modname);
1605 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001606 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001607 if (lastdot == NULL)
1608 return Py_None;
1609 len = lastdot - start;
1610 if (len >= MAXPATHLEN) {
1611 PyErr_SetString(PyExc_ValueError,
1612 "Module name too long");
1613 return NULL;
1614 }
1615 strncpy(buf, start, len);
1616 buf[len] = '\0';
1617 *p_buflen = len;
1618 }
1619
1620 modules = PyImport_GetModuleDict();
1621 parent = PyDict_GetItemString(modules, buf);
1622 if (parent == NULL)
1623 parent = Py_None;
1624 return parent;
1625 /* We expect, but can't guarantee, if parent != None, that:
1626 - parent.__name__ == buf
1627 - parent.__dict__ is globals
1628 If this is violated... Who cares? */
1629}
1630
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001631/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001632static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001633load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1634 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001635{
1636 char *name = *p_name;
1637 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001638 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001639 char *p;
1640 PyObject *result;
1641
1642 if (dot == NULL) {
1643 *p_name = NULL;
1644 len = strlen(name);
1645 }
1646 else {
1647 *p_name = dot+1;
1648 len = dot-name;
1649 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00001650 if (len == 0) {
1651 PyErr_SetString(PyExc_ValueError,
1652 "Empty module name");
1653 return NULL;
1654 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001655
1656 p = buf + *p_buflen;
1657 if (p != buf)
1658 *p++ = '.';
1659 if (p+len-buf >= MAXPATHLEN) {
1660 PyErr_SetString(PyExc_ValueError,
1661 "Module name too long");
1662 return NULL;
1663 }
1664 strncpy(p, name, len);
1665 p[len] = '\0';
1666 *p_buflen = p+len-buf;
1667
1668 result = import_submodule(mod, p, buf);
1669 if (result == Py_None && altmod != mod) {
1670 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001671 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00001672 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001673 if (result != NULL && result != Py_None) {
1674 if (mark_miss(buf) != 0) {
1675 Py_DECREF(result);
1676 return NULL;
1677 }
1678 strncpy(buf, name, len);
1679 buf[len] = '\0';
1680 *p_buflen = len;
1681 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001682 }
1683 if (result == NULL)
1684 return NULL;
1685
1686 if (result == Py_None) {
1687 Py_DECREF(result);
1688 PyErr_Format(PyExc_ImportError,
1689 "No module named %.200s", name);
1690 return NULL;
1691 }
1692
1693 return result;
1694}
1695
1696static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001697mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001698{
1699 PyObject *modules = PyImport_GetModuleDict();
1700 return PyDict_SetItemString(modules, name, Py_None);
1701}
1702
1703static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001704ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1705 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001706{
1707 int i;
1708
1709 if (!PyObject_HasAttrString(mod, "__path__"))
1710 return 1;
1711
1712 for (i = 0; ; i++) {
1713 PyObject *item = PySequence_GetItem(fromlist, i);
1714 int hasit;
1715 if (item == NULL) {
1716 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1717 PyErr_Clear();
1718 return 1;
1719 }
1720 return 0;
1721 }
1722 if (!PyString_Check(item)) {
1723 PyErr_SetString(PyExc_TypeError,
1724 "Item in ``from list'' not a string");
1725 Py_DECREF(item);
1726 return 0;
1727 }
1728 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00001729 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001730 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001731 /* See if the package defines __all__ */
1732 if (recursive)
1733 continue; /* Avoid endless recursion */
1734 all = PyObject_GetAttrString(mod, "__all__");
1735 if (all == NULL)
1736 PyErr_Clear();
1737 else {
1738 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1739 return 0;
1740 Py_DECREF(all);
1741 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001742 continue;
1743 }
1744 hasit = PyObject_HasAttr(mod, item);
1745 if (!hasit) {
1746 char *subname = PyString_AS_STRING(item);
1747 PyObject *submod;
1748 char *p;
1749 if (buflen + strlen(subname) >= MAXPATHLEN) {
1750 PyErr_SetString(PyExc_ValueError,
1751 "Module name too long");
1752 Py_DECREF(item);
1753 return 0;
1754 }
1755 p = buf + buflen;
1756 *p++ = '.';
1757 strcpy(p, subname);
1758 submod = import_submodule(mod, subname, buf);
1759 Py_XDECREF(submod);
1760 if (submod == NULL) {
1761 Py_DECREF(item);
1762 return 0;
1763 }
1764 }
1765 Py_DECREF(item);
1766 }
1767
Guido van Rossuma7f2e811997-10-03 15:33:32 +00001768 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001769}
1770
1771static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001772import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001773{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001774 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 PyObject *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001776
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001777 /* Require:
1778 if mod == None: subname == fullname
1779 else: mod.__name__ + "." + subname == fullname
1780 */
1781
Tim Peters50d8d372001-02-28 05:34:27 +00001782 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001784 }
1785 else {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001786 PyObject *path;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001787 char buf[MAXPATHLEN+1];
1788 struct filedescr *fdp;
1789 FILE *fp = NULL;
1790
Guido van Rossum9c0afe51998-05-19 15:09:05 +00001791 if (mod == Py_None)
1792 path = NULL;
1793 else {
1794 path = PyObject_GetAttrString(mod, "__path__");
1795 if (path == NULL) {
1796 PyErr_Clear();
1797 Py_INCREF(Py_None);
1798 return Py_None;
1799 }
1800 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001801
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001802 buf[0] = '\0';
Guido van Rossumb68cd421998-07-01 17:36:26 +00001803 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1804 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001805 if (fdp == NULL) {
1806 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1807 return NULL;
1808 PyErr_Clear();
1809 Py_INCREF(Py_None);
1810 return Py_None;
1811 }
1812 m = load_module(fullname, fp, buf, fdp->type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001813 if (fp)
1814 fclose(fp);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001815 if (m != NULL && mod != Py_None) {
1816 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1817 Py_DECREF(m);
1818 m = NULL;
1819 }
1820 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00001821 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001822
1823 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001824}
1825
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826
1827/* Re-import a module of any kind and return its module object, WITH
1828 INCREMENTED REFERENCE COUNT */
1829
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001831PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001832{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001833 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00001834 PyObject *path = NULL;
1835 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001836 char buf[MAXPATHLEN+1];
1837 struct filedescr *fdp;
1838 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001839
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 if (m == NULL || !PyModule_Check(m)) {
1841 PyErr_SetString(PyExc_TypeError,
1842 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843 return NULL;
1844 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001846 if (name == NULL)
1847 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001848 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001849 PyErr_Format(PyExc_ImportError,
1850 "reload(): module %.200s not in sys.modules",
1851 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001852 return NULL;
1853 }
Guido van Rossum222ef561997-09-06 19:41:09 +00001854 subname = strrchr(name, '.');
1855 if (subname == NULL)
1856 subname = name;
1857 else {
1858 PyObject *parentname, *parent;
1859 parentname = PyString_FromStringAndSize(name, (subname-name));
1860 if (parentname == NULL)
1861 return NULL;
1862 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00001863 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00001864 if (parent == NULL) {
1865 PyErr_Format(PyExc_ImportError,
1866 "reload(): parent %.200s not in sys.modules",
1867 name);
1868 return NULL;
1869 }
1870 subname++;
1871 path = PyObject_GetAttrString(parent, "__path__");
1872 if (path == NULL)
1873 PyErr_Clear();
1874 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001875 buf[0] = '\0';
Guido van Rossum222ef561997-09-06 19:41:09 +00001876 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1877 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001878 if (fdp == NULL)
1879 return NULL;
1880 m = load_module(name, fp, buf, fdp->type);
1881 if (fp)
1882 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001883 return m;
1884}
1885
1886
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001887/* Higher-level import emulator which emulates the "import" statement
1888 more accurately -- it invokes the __import__() function from the
1889 builtins of the current globals. This means that the import is
1890 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00001891 environment, e.g. by "rexec".
1892 A dummy list ["__doc__"] is passed as the 4th argument so that
1893 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1894 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001895
1896PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001897PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001898{
1899 static PyObject *silly_list = NULL;
1900 static PyObject *builtins_str = NULL;
1901 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001902 PyObject *globals = NULL;
1903 PyObject *import = NULL;
1904 PyObject *builtins = NULL;
1905 PyObject *r = NULL;
1906
1907 /* Initialize constant string objects */
1908 if (silly_list == NULL) {
1909 import_str = PyString_InternFromString("__import__");
1910 if (import_str == NULL)
1911 return NULL;
1912 builtins_str = PyString_InternFromString("__builtins__");
1913 if (builtins_str == NULL)
1914 return NULL;
1915 silly_list = Py_BuildValue("[s]", "__doc__");
1916 if (silly_list == NULL)
1917 return NULL;
1918 }
1919
1920 /* Get the builtins from current globals */
1921 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00001922 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00001923 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001924 builtins = PyObject_GetItem(globals, builtins_str);
1925 if (builtins == NULL)
1926 goto err;
1927 }
1928 else {
1929 /* No globals -- use standard builtins, and fake globals */
1930 PyErr_Clear();
1931
Guido van Rossum85cd1d62001-02-20 21:43:24 +00001932 builtins = PyImport_ImportModuleEx("__builtin__",
1933 NULL, NULL, NULL);
1934 if (builtins == NULL)
1935 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001936 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1937 if (globals == NULL)
1938 goto err;
1939 }
1940
1941 /* Get the __import__ function from the builtins */
1942 if (PyDict_Check(builtins))
Fred Drakea76ba6e2001-03-06 06:31:15 +00001943 import = PyObject_GetItem(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001944 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00001945 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001946 if (import == NULL)
1947 goto err;
1948
1949 /* Call the _import__ function with the proper argument list */
1950 r = PyObject_CallFunction(import, "OOOO",
1951 module_name, globals, globals, silly_list);
1952
1953 err:
1954 Py_XDECREF(globals);
1955 Py_XDECREF(builtins);
1956 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001957
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001958 return r;
1959}
1960
1961
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001962/* Module 'imp' provides Python access to the primitives used for
1963 importing modules.
1964*/
1965
Guido van Rossum79f25d91997-04-29 20:08:16 +00001966static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001967imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001968{
1969 char buf[4];
1970
Guido van Rossum43713e52000-02-29 13:59:29 +00001971 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001972 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00001973 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1974 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1975 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1976 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001977
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001979}
1980
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001982imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001983{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001985 struct filedescr *fdp;
1986
Guido van Rossum43713e52000-02-29 13:59:29 +00001987 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001988 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001990 if (list == NULL)
1991 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1993 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001994 fdp->suffix, fdp->mode, fdp->type);
1995 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997 return NULL;
1998 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 if (PyList_Append(list, item) < 0) {
2000 Py_DECREF(list);
2001 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002002 return NULL;
2003 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002004 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002005 }
2006 return list;
2007}
2008
Guido van Rossum79f25d91997-04-29 20:08:16 +00002009static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002010call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002011{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002012 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002013 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002014 struct filedescr *fdp;
2015 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002016 FILE *fp = NULL;
2017
2018 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002019 if (path == Py_None)
2020 path = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002021 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
2022 if (fdp == NULL)
2023 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002024 if (fp != NULL) {
2025 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2026 if (fob == NULL) {
2027 fclose(fp);
2028 return NULL;
2029 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002030 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002031 else {
2032 fob = Py_None;
2033 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002034 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002035 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002036 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002037 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002038 return ret;
2039}
2040
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002042imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002043{
2044 char *name;
2045 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002046 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002047 return NULL;
2048 return call_find_module(name, path);
2049}
2050
2051static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002052imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002053{
2054 char *name;
2055 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002056 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002057 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002058 return NULL;
2059 ret = init_builtin(name);
2060 if (ret < 0)
2061 return NULL;
2062 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002063 Py_INCREF(Py_None);
2064 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002065 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066 m = PyImport_AddModule(name);
2067 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002068 return m;
2069}
2070
Guido van Rossum79f25d91997-04-29 20:08:16 +00002071static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002072imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002073{
2074 char *name;
2075 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002076 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002077 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002078 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002080 if (ret < 0)
2081 return NULL;
2082 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002083 Py_INCREF(Py_None);
2084 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002085 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002086 m = PyImport_AddModule(name);
2087 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002088 return m;
2089}
2090
Guido van Rossum79f25d91997-04-29 20:08:16 +00002091static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002092imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002093{
2094 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002095
Guido van Rossum43713e52000-02-29 13:59:29 +00002096 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002097 return NULL;
2098 return get_frozen_object(name);
2099}
2100
Guido van Rossum79f25d91997-04-29 20:08:16 +00002101static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002102imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002103{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002104 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002105 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002106 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002107 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002108}
2109
Guido van Rossum79f25d91997-04-29 20:08:16 +00002110static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002111imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002112{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002113 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002114 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002115 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002116 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002117 p = find_frozen(name);
2118 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002119}
2120
2121static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002122get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002123{
2124 FILE *fp;
2125 if (fob == NULL) {
2126 fp = fopen(pathname, mode);
2127 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002128 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002129 }
2130 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002131 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002132 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133 PyErr_SetString(PyExc_ValueError,
2134 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002135 }
2136 return fp;
2137}
2138
Guido van Rossum79f25d91997-04-29 20:08:16 +00002139static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002140imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002141{
2142 char *name;
2143 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002144 PyObject *fob = NULL;
2145 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002146 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002147 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002148 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002149 return NULL;
2150 fp = get_file(pathname, fob, "rb");
2151 if (fp == NULL)
2152 return NULL;
2153 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002154 if (fob == NULL)
2155 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002156 return m;
2157}
2158
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002159#ifdef HAVE_DYNAMIC_LOADING
2160
Guido van Rossum79f25d91997-04-29 20:08:16 +00002161static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002162imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002163{
2164 char *name;
2165 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002166 PyObject *fob = NULL;
2167 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002168 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002169 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002170 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002171 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002172 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002173 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002174 if (fp == NULL)
2175 return NULL;
2176 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002177 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002178 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002179}
2180
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002181#endif /* HAVE_DYNAMIC_LOADING */
2182
Guido van Rossum79f25d91997-04-29 20:08:16 +00002183static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002184imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002185{
2186 char *name;
2187 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002188 PyObject *fob = NULL;
2189 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002190 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002191 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002192 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002193 return NULL;
2194 fp = get_file(pathname, fob, "r");
2195 if (fp == NULL)
2196 return NULL;
2197 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002198 if (fob == NULL)
2199 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002200 return m;
2201}
2202
Jack Jansen9c96a921995-02-15 22:57:06 +00002203#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002204static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002205imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002206{
2207 char *name;
2208 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002209 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002210
Guido van Rossum43713e52000-02-29 13:59:29 +00002211 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002212 return NULL;
2213 m = PyMac_LoadResourceModule(name, pathname);
2214 return m;
2215}
2216#endif /* macintosh */
2217
Guido van Rossum79f25d91997-04-29 20:08:16 +00002218static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002219imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002220{
2221 char *name;
2222 PyObject *fob;
2223 char *pathname;
2224 char *suffix; /* Unused */
2225 char *mode;
2226 int type;
2227 FILE *fp;
2228
Guido van Rossum43713e52000-02-29 13:59:29 +00002229 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002230 &name, &fob, &pathname,
2231 &suffix, &mode, &type))
2232 return NULL;
2233 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2234 PyErr_Format(PyExc_ValueError,
2235 "invalid file open mode %.200s", mode);
2236 return NULL;
2237 }
2238 if (fob == Py_None)
2239 fp = NULL;
2240 else {
2241 if (!PyFile_Check(fob)) {
2242 PyErr_SetString(PyExc_ValueError,
2243 "load_module arg#2 should be a file or None");
2244 return NULL;
2245 }
2246 fp = get_file(pathname, fob, mode);
2247 if (fp == NULL)
2248 return NULL;
2249 }
2250 return load_module(name, fp, pathname, type);
2251}
2252
2253static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002254imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002255{
2256 char *name;
2257 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002258 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002259 return NULL;
2260 return load_package(name, pathname);
2261}
2262
2263static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002264imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002265{
2266 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002267 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002268 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002269 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002270}
2271
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002272/* Doc strings */
2273
2274static char doc_imp[] = "\
2275This module provides the components needed to build your own\n\
2276__import__ function. Undocumented functions are obsolete.\n\
2277";
2278
2279static char doc_find_module[] = "\
2280find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2281Search for a module. If path is omitted or None, search for a\n\
2282built-in, frozen or special module and continue search in sys.path.\n\
2283The module name cannot contain '.'; to search for a submodule of a\n\
2284package, pass the submodule name and the package's __path__.\
2285";
2286
2287static char doc_load_module[] = "\
2288load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2289Load a module, given information returned by find_module().\n\
2290The module name must include the full package name, if any.\
2291";
2292
2293static char doc_get_magic[] = "\
2294get_magic() -> string\n\
2295Return the magic number for .pyc or .pyo files.\
2296";
2297
2298static char doc_get_suffixes[] = "\
2299get_suffixes() -> [(suffix, mode, type), ...]\n\
2300Return a list of (suffix, mode, type) tuples describing the files\n\
2301that find_module() looks for.\
2302";
2303
2304static char doc_new_module[] = "\
2305new_module(name) -> module\n\
2306Create a new module. Do not enter it in sys.modules.\n\
2307The module name must include the full package name, if any.\
2308";
2309
Guido van Rossum79f25d91997-04-29 20:08:16 +00002310static PyMethodDef imp_methods[] = {
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002311 {"find_module", imp_find_module, 1, doc_find_module},
2312 {"get_magic", imp_get_magic, 1, doc_get_magic},
2313 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2314 {"load_module", imp_load_module, 1, doc_load_module},
2315 {"new_module", imp_new_module, 1, doc_new_module},
2316 /* The rest are obsolete */
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002317 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002318 {"init_builtin", imp_init_builtin, 1},
2319 {"init_frozen", imp_init_frozen, 1},
2320 {"is_builtin", imp_is_builtin, 1},
2321 {"is_frozen", imp_is_frozen, 1},
2322 {"load_compiled", imp_load_compiled, 1},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002323#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002324 {"load_dynamic", imp_load_dynamic, 1},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002325#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002326 {"load_package", imp_load_package, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00002327#ifdef macintosh
2328 {"load_resource", imp_load_resource, 1},
2329#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002330 {"load_source", imp_load_source, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002331 {NULL, NULL} /* sentinel */
2332};
2333
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002334static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002335setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002336{
2337 PyObject *v;
2338 int err;
2339
2340 v = PyInt_FromLong((long)value);
2341 err = PyDict_SetItemString(d, name, v);
2342 Py_XDECREF(v);
2343 return err;
2344}
2345
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002346void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002347initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002348{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002349 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002350
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002351 m = Py_InitModule4("imp", imp_methods, doc_imp,
2352 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002353 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002354
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002355 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2356 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2357 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2358 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2359 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2360 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2361 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2362 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002363 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002364
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002365 failure:
2366 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002367}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002368
2369
Guido van Rossumb18618d2000-05-03 23:44:39 +00002370/* API for embedding applications that want to add their own entries
2371 to the table of built-in modules. This should normally be called
2372 *before* Py_Initialize(). When the table resize fails, -1 is
2373 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002374
2375 After a similar function by Just van Rossum. */
2376
2377int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002378PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002379{
2380 static struct _inittab *our_copy = NULL;
2381 struct _inittab *p;
2382 int i, n;
2383
2384 /* Count the number of entries in both tables */
2385 for (n = 0; newtab[n].name != NULL; n++)
2386 ;
2387 if (n == 0)
2388 return 0; /* Nothing to do */
2389 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2390 ;
2391
2392 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002393 p = our_copy;
2394 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002395 if (p == NULL)
2396 return -1;
2397
2398 /* Copy the tables into the new memory */
2399 if (our_copy != PyImport_Inittab)
2400 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2401 PyImport_Inittab = our_copy = p;
2402 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2403
2404 return 0;
2405}
2406
2407/* Shorthand to add a single entry given a name and a function */
2408
2409int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002410PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002411{
2412 struct _inittab newtab[2];
2413
2414 memset(newtab, '\0', sizeof newtab);
2415
2416 newtab[0].name = name;
2417 newtab[0].initfunc = initfunc;
2418
2419 return PyImport_ExtendInittab(newtab);
2420}