blob: c9cc9de9c9b4b1e81c170ce3424697d44eb76c90 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Module definition and import implementation */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000039#include "marshal.h"
40#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000041#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000042#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000044#ifdef macintosh
45#include "macglue.h"
46#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000047
Guido van Rossum80bb9651996-12-05 23:27:02 +000048#ifdef HAVE_UNISTD_H
49#include <unistd.h>
50#endif
51
Guido van Rossumaee0bad1997-09-05 07:33:22 +000052/* We expect that stat exists on most systems.
53 It's confirmed on Unix, Mac and Windows.
54 If you don't have it, add #define DONT_HAVE_STAT to your config.h. */
55#ifndef DONT_HAVE_STAT
56#define HAVE_STAT
57
58#include <sys/types.h>
59#include <sys/stat.h>
60
Guido van Rossum595d7ba1997-12-05 21:45:29 +000061#if defined(PYCC_VACPP)
62/* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
63#define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
64#endif
65
Guido van Rossumaee0bad1997-09-05 07:33:22 +000066#ifndef S_ISDIR
67#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
68#endif
69
70#endif
71
72
Guido van Rossum79f25d91997-04-29 20:08:16 +000073extern long PyOS_GetLastModificationTime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000074
Guido van Rossum6c849691994-09-26 15:47:17 +000075/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000076/* Change for each incompatible change */
77/* The value of CR and LF is incorporated so if you ever read or write
78 a .pyc file in text mode the magic number will be wrong; also, the
79 Apple MPW compiler swaps their values, botching string constants */
80/* XXX Perhaps the magic number should be frozen and a version field
81 added to the .pyc file header? */
Guido van Rossumdd5db431997-01-17 21:06:11 +000082/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
Guido van Rossum99d18251997-01-24 03:44:53 +000083#define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000084
Guido van Rossum25ce5661997-08-02 03:10:38 +000085/* See _PyImport_FixupExtension() below */
86static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000087
Guido van Rossum771c6c81997-10-31 18:37:24 +000088/* This table is defined in config.c: */
89extern struct _inittab _PyImport_Inittab[];
90
91struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000092
Guido van Rossum1ae940a1995-01-02 19:04:15 +000093/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094
95void
Guido van Rossum25ce5661997-08-02 03:10:38 +000096_PyImport_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossum0824f631997-03-11 18:37:35 +000098 if (Py_OptimizeFlag) {
99 /* Replace ".pyc" with ".pyo" in import_filetab */
100 struct filedescr *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000101 for (p = _PyImport_Filetab; p->suffix != NULL; p++) {
Guido van Rossum0824f631997-03-11 18:37:35 +0000102 if (strcmp(p->suffix, ".pyc") == 0)
103 p->suffix = ".pyo";
104 }
105 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106}
107
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108void
109_PyImport_Fini()
110{
111 Py_XDECREF(extensions);
112 extensions = NULL;
113}
114
115
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000116/* Locking primitives to prevent parallel imports of the same module
117 in different threads to return with a partially loaded module.
118 These calls are serialized by the global interpreter lock. */
119
120#ifdef WITH_THREAD
121
122#include "thread.h"
123
124static type_lock import_lock = 0;
125static long import_lock_thread = -1;
126static int import_lock_level = 0;
127
128static void
129lock_import()
130{
131 long me = get_thread_ident();
132 if (me == -1)
133 return; /* Too bad */
134 if (import_lock == NULL)
135 import_lock = allocate_lock();
136 if (import_lock_thread == me) {
137 import_lock_level++;
138 return;
139 }
140 if (import_lock_thread != -1 || !acquire_lock(import_lock, 0)) {
141 PyThreadState *tstate = PyEval_SaveThread();
142 acquire_lock(import_lock, 1);
143 PyEval_RestoreThread(tstate);
144 }
145 import_lock_thread = me;
146 import_lock_level = 1;
147}
148
149static void
150unlock_import()
151{
152 long me = get_thread_ident();
153 if (me == -1)
154 return; /* Too bad */
155 if (import_lock_thread != me)
156 Py_FatalError("unlock_import: not holding the import lock");
157 import_lock_level--;
158 if (import_lock_level == 0) {
159 import_lock_thread = -1;
160 release_lock(import_lock);
161 }
162}
163
164#else
165
166#define lock_import()
167#define unlock_import()
168
169#endif
170
Guido van Rossum25ce5661997-08-02 03:10:38 +0000171/* Helper for sys */
172
173PyObject *
174PyImport_GetModuleDict()
175{
176 PyInterpreterState *interp = PyThreadState_Get()->interp;
177 if (interp->modules == NULL)
178 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
179 return interp->modules;
180}
181
Guido van Rossum3f5da241990-12-20 15:06:42 +0000182
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000183/* List of names to clear in sys */
184static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000185 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000186 "exc_type", "exc_value", "exc_traceback",
187 "last_type", "last_value", "last_traceback",
188 NULL
189};
190
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000191static char* sys_files[] = {
192 "stdin", "__stdin__",
193 "stdout", "__stdout__",
194 "stderr", "__stderr__",
195 NULL
196};
197
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000198
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000199/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000200
Guido van Rossum3f5da241990-12-20 15:06:42 +0000201void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202PyImport_Cleanup()
Guido van Rossum3f5da241990-12-20 15:06:42 +0000203{
Guido van Rossum758eec01998-01-19 21:58:26 +0000204 int pos, ndone;
205 char *name;
206 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000207 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000208 PyObject *modules = interp->modules;
209
210 if (modules == NULL)
211 return; /* Already done */
212
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000213 /* Delete some special variables first. These are common
214 places where user values hide and people complain when their
215 destructors fail. Since the modules containing them are
216 deleted *last* of all, they would come too late in the normal
217 destruction order. Sigh. */
218
219 value = PyDict_GetItemString(modules, "__builtin__");
220 if (value != NULL && PyModule_Check(value)) {
221 dict = PyModule_GetDict(value);
222 if (Py_VerboseFlag)
223 fprintf(stderr, "# clear __builtin__._\n");
224 PyDict_SetItemString(dict, "_", Py_None);
225 }
226 value = PyDict_GetItemString(modules, "sys");
227 if (value != NULL && PyModule_Check(value)) {
228 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000229 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000230 dict = PyModule_GetDict(value);
231 for (p = sys_deletes; *p != NULL; p++) {
232 if (Py_VerboseFlag)
233 fprintf(stderr, "# clear sys.%s\n", *p);
234 PyDict_SetItemString(dict, *p, Py_None);
235 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000236 for (p = sys_files; *p != NULL; p+=2) {
237 if (Py_VerboseFlag)
238 fprintf(stderr, "# restore sys.%s\n", *p);
239 v = PyDict_GetItemString(dict, *(p+1));
240 if (v == NULL)
241 v = Py_None;
242 PyDict_SetItemString(dict, *p, v);
243 }
244 }
245
246 /* First, delete __main__ */
247 value = PyDict_GetItemString(modules, "__main__");
248 if (value != NULL && PyModule_Check(value)) {
249 if (Py_VerboseFlag)
250 fprintf(stderr, "# cleanup __main__\n");
251 _PyModule_Clear(value);
252 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000253 }
254
Guido van Rossum758eec01998-01-19 21:58:26 +0000255 /* The special treatment of __builtin__ here is because even
256 when it's not referenced as a module, its dictionary is
257 referenced by almost every module's __builtins__. Since
258 deleting a module clears its dictionary (even if there are
259 references left to it), we need to delete the __builtin__
260 module last. Likewise, we don't delete sys until the very
261 end because it is implicitly referenced (e.g. by print).
262
263 Also note that we 'delete' modules by replacing their entry
264 in the modules dict with None, rather than really deleting
265 them; this avoids a rehash of the modules dictionary and
266 also marks them as "non existent" so they won't be
267 re-imported. */
268
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000269 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000270 one (skipping __builtin__ and sys) and delete them */
271 do {
272 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000274 while (PyDict_Next(modules, &pos, &key, &value)) {
275 if (value->ob_refcnt != 1)
276 continue;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000277 if (PyModule_Check(value)) {
Guido van Rossum758eec01998-01-19 21:58:26 +0000278 name = PyString_AsString(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000279 if (strcmp(name, "__builtin__") == 0)
280 continue;
281 if (strcmp(name, "sys") == 0)
282 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000283 if (Py_VerboseFlag)
284 fprintf(stderr,
285 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000286 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000287 PyDict_SetItem(modules, key, Py_None);
288 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000289 }
290 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000291 } while (ndone > 0);
292
Guido van Rossum758eec01998-01-19 21:58:26 +0000293 /* Next, delete all modules (still skipping __builtin__ and sys) */
294 pos = 0;
295 while (PyDict_Next(modules, &pos, &key, &value)) {
296 if (PyModule_Check(value)) {
297 name = PyString_AsString(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)
303 fprintf(stderr, "# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000304 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000305 PyDict_SetItem(modules, key, Py_None);
306 }
307 }
308
309 /* Next, delete sys and __builtin__ (in that order) */
310 value = PyDict_GetItemString(modules, "sys");
311 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000312 if (Py_VerboseFlag)
313 fprintf(stderr, "# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000314 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000315 PyDict_SetItemString(modules, "sys", Py_None);
316 }
317 value = PyDict_GetItemString(modules, "__builtin__");
318 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000319 if (Py_VerboseFlag)
320 fprintf(stderr, "# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000321 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000322 PyDict_SetItemString(modules, "__builtin__", Py_None);
323 }
324
325 /* Finally, clear and delete the modules directory */
326 PyDict_Clear(modules);
327 interp->modules = NULL;
328 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000329}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000330
331
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000332/* Helper for pythonrun.c -- return magic number */
333
334long
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335PyImport_GetMagicNumber()
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000336{
337 return MAGIC;
338}
339
340
Guido van Rossum25ce5661997-08-02 03:10:38 +0000341/* Magic for extension modules (built-in as well as dynamically
342 loaded). To prevent initializing an extension module more than
343 once, we keep a static dictionary 'extensions' keyed by module name
344 (for built-in modules) or by filename (for dynamically loaded
345 modules), containing these modules. A copy od the module's
346 dictionary is stored by calling _PyImport_FixupExtension()
347 immediately after the module initialization function succeeds. A
348 copy can be retrieved from there by calling
349 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000350
Guido van Rossum79f25d91997-04-29 20:08:16 +0000351PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352_PyImport_FixupExtension(name, filename)
353 char *name;
354 char *filename;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000355{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356 PyObject *modules, *mod, *dict, *copy;
357 if (extensions == NULL) {
358 extensions = PyDict_New();
359 if (extensions == NULL)
360 return NULL;
361 }
362 modules = PyImport_GetModuleDict();
363 mod = PyDict_GetItemString(modules, name);
364 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000365 PyErr_Format(PyExc_SystemError,
366 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367 return NULL;
368 }
369 dict = PyModule_GetDict(mod);
370 if (dict == NULL)
371 return NULL;
372 copy = PyObject_CallMethod(dict, "copy", "");
373 if (copy == NULL)
374 return NULL;
375 PyDict_SetItemString(extensions, filename, copy);
376 Py_DECREF(copy);
377 return copy;
378}
379
380PyObject *
381_PyImport_FindExtension(name, filename)
382 char *name;
383 char *filename;
384{
385 PyObject *dict, *mod, *mdict, *result;
386 if (extensions == NULL)
387 return NULL;
388 dict = PyDict_GetItemString(extensions, filename);
389 if (dict == NULL)
390 return NULL;
391 mod = PyImport_AddModule(name);
392 if (mod == NULL)
393 return NULL;
394 mdict = PyModule_GetDict(mod);
395 if (mdict == NULL)
396 return NULL;
397 result = PyObject_CallMethod(mdict, "update", "O", dict);
398 if (result == NULL)
399 return NULL;
400 Py_DECREF(result);
401 if (Py_VerboseFlag)
402 fprintf(stderr, "import %s # previously loaded (%s)\n",
403 name, filename);
404 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000405}
406
407
408/* Get the module object corresponding to a module name.
409 First check the modules dictionary if there's one there,
410 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000411 Because the former action is most common, THIS DOES NOT RETURN A
412 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000413
Guido van Rossum79f25d91997-04-29 20:08:16 +0000414PyObject *
415PyImport_AddModule(name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000416 char *name;
417{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000419 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420
Guido van Rossum25ce5661997-08-02 03:10:38 +0000421 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000423 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425 if (m == NULL)
426 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000427 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000429 return NULL;
430 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000431 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000432
433 return m;
434}
435
436
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000437/* Execute a code object in a module and return the module object
438 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000439
Guido van Rossum79f25d91997-04-29 20:08:16 +0000440PyObject *
441PyImport_ExecCodeModule(name, co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000442 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000443 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000444{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000445 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
446}
447
448PyObject *
449PyImport_ExecCodeModuleEx(name, co, pathname)
450 char *name;
451 PyObject *co;
452 char *pathname;
453{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000454 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000456
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000458 if (m == NULL)
459 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000460 d = PyModule_GetDict(m);
461 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
462 if (PyDict_SetItemString(d, "__builtins__",
463 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000464 return NULL;
465 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000466 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000467 v = NULL;
468 if (pathname != NULL) {
469 v = PyString_FromString(pathname);
470 if (v == NULL)
471 PyErr_Clear();
472 }
473 if (v == NULL) {
474 v = ((PyCodeObject *)co)->co_filename;
475 Py_INCREF(v);
476 }
477 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000478 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000479 Py_DECREF(v);
480
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000481 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482 if (v == NULL)
483 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000485
Guido van Rossum25ce5661997-08-02 03:10:38 +0000486 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000487 PyErr_Format(PyExc_ImportError,
488 "Loaded module %.200s not found in sys.modules",
489 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000490 return NULL;
491 }
492
Guido van Rossum79f25d91997-04-29 20:08:16 +0000493 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000494
495 return m;
496}
497
498
499/* Given a pathname for a Python source file, fill a buffer with the
500 pathname for the corresponding compiled file. Return the pathname
501 for the compiled file, or NULL if there's no space in the buffer.
502 Doesn't set an exception. */
503
504static char *
505make_compiled_pathname(pathname, buf, buflen)
506 char *pathname;
507 char *buf;
508 int buflen;
509{
510 int len;
511
512 len = strlen(pathname);
513 if (len+2 > buflen)
514 return NULL;
515 strcpy(buf, pathname);
Guido van Rossum0824f631997-03-11 18:37:35 +0000516 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517
518 return buf;
519}
520
521
522/* Given a pathname for a Python source file, its time of last
523 modification, and a pathname for a compiled file, check whether the
524 compiled file represents the same version of the source. If so,
525 return a FILE pointer for the compiled file, positioned just after
526 the header; if not, return NULL.
527 Doesn't set an exception. */
528
529static FILE *
530check_compiled_module(pathname, mtime, cpathname)
531 char *pathname;
532 long mtime;
533 char *cpathname;
534{
535 FILE *fp;
536 long magic;
537 long pyc_mtime;
538
539 fp = fopen(cpathname, "rb");
540 if (fp == NULL)
541 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543 if (magic != MAGIC) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545 fprintf(stderr, "# %s has bad magic\n", cpathname);
546 fclose(fp);
547 return NULL;
548 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552 fprintf(stderr, "# %s has bad mtime\n", cpathname);
553 fclose(fp);
554 return NULL;
555 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
558 return fp;
559}
560
561
562/* Read a code object from a file and check it for validity */
563
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564static PyCodeObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000565read_compiled_module(cpathname, fp)
566 char *cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 FILE *fp;
568{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 co = PyMarshal_ReadObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 if (co == NULL || !PyCode_Check(co)) {
574 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000575 PyErr_Format(PyExc_ImportError,
576 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000578 return NULL;
579 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000580 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581}
582
583
584/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000585 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
Guido van Rossum79f25d91997-04-29 20:08:16 +0000587static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588load_compiled_module(name, cpathname, fp)
589 char *name;
590 char *cpathname;
591 FILE *fp;
592{
593 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyCodeObject *co;
595 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000598 if (magic != MAGIC) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000599 PyErr_Format(PyExc_ImportError,
600 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601 return NULL;
602 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000604 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 if (co == NULL)
606 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608 fprintf(stderr, "import %s # precompiled from %s\n",
609 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000610 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612
613 return m;
614}
615
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616/* Parse a source file and return the corresponding code object */
617
Guido van Rossum79f25d91997-04-29 20:08:16 +0000618static PyCodeObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619parse_source_module(pathname, fp)
620 char *pathname;
621 FILE *fp;
622{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624 node *n;
625
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000626 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627 if (n == NULL)
628 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 co = PyNode_Compile(n, pathname);
630 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631
632 return co;
633}
634
635
636/* Write a compiled module to a file, placing the time of last
637 modification of its source into the header.
638 Errors are ignored, if a write error occurs an attempt is made to
639 remove the file. */
640
641static void
642write_compiled_module(co, cpathname, mtime)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644 char *cpathname;
645 long mtime;
646{
647 FILE *fp;
648
649 fp = fopen(cpathname, "wb");
650 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000651 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000652 fprintf(stderr,
653 "# can't create %s\n", cpathname);
654 return;
655 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 PyMarshal_WriteLongToFile(MAGIC, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 PyMarshal_WriteLongToFile(0L, fp);
659 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662 fprintf(stderr, "# can't write %s\n", cpathname);
663 /* Don't keep partial file */
664 fclose(fp);
665 (void) unlink(cpathname);
666 return;
667 }
668 /* Now write the true mtime */
669 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000671 fflush(fp);
672 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000673 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 fprintf(stderr, "# wrote %s\n", cpathname);
675#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000676 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000677#endif
678}
679
680
681/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000682 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
683 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686load_source_module(name, pathname, fp)
687 char *name;
688 char *pathname;
689 FILE *fp;
690{
691 long mtime;
692 FILE *fpc;
693 char buf[MAXPATHLEN+1];
694 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 PyCodeObject *co;
696 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000697
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000698 mtime = PyOS_GetLastModificationTime(pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
700 if (cpathname != NULL &&
701 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000702 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703 fclose(fpc);
704 if (co == NULL)
705 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707 fprintf(stderr, "import %s # precompiled from %s\n",
708 name, cpathname);
709 }
710 else {
711 co = parse_source_module(pathname, fp);
712 if (co == NULL)
713 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000715 fprintf(stderr, "import %s # from %s\n",
716 name, pathname);
717 write_compiled_module(co, cpathname, mtime);
718 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000719 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721
722 return m;
723}
724
725
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000726/* Forward */
727static PyObject *load_module Py_PROTO((char *, FILE *, char *, int));
728static struct filedescr *find_module Py_PROTO((char *, PyObject *,
729 char *, int, FILE **));
Guido van Rossuma86f77d1997-09-09 18:53:47 +0000730static struct _frozen *find_frozen Py_PROTO((char *name));
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000731
732/* Load a package and return its module object WITH INCREMENTED
733 REFERENCE COUNT */
734
735static PyObject *
736load_package(name, pathname)
737 char *name;
738 char *pathname;
739{
740 PyObject *m, *d, *file, *path;
741 int err;
742 char buf[MAXPATHLEN+1];
743 FILE *fp = NULL;
744 struct filedescr *fdp;
745
746 m = PyImport_AddModule(name);
747 if (m == NULL)
748 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000749 if (Py_VerboseFlag)
750 fprintf(stderr, "import %s # directory %s\n",
751 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000752 d = PyModule_GetDict(m);
753 file = PyString_FromString(pathname);
754 if (file == NULL)
755 return NULL;
756 path = Py_BuildValue("[O]", file);
757 if (path == NULL) {
758 Py_DECREF(file);
759 return NULL;
760 }
761 err = PyDict_SetItemString(d, "__file__", file);
762 if (err == 0)
763 err = PyDict_SetItemString(d, "__path__", path);
764 if (err != 0) {
765 m = NULL;
766 goto cleanup;
767 }
768 buf[0] = '\0';
769 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
770 if (fdp == NULL) {
771 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
772 PyErr_Clear();
773 }
774 else
775 m = NULL;
776 goto cleanup;
777 }
778 m = load_module(name, fp, buf, fdp->type);
779 if (fp != NULL)
780 fclose(fp);
781 cleanup:
782 Py_XINCREF(m);
783 Py_XDECREF(path);
784 Py_XDECREF(file);
785 return m;
786}
787
788
789/* Helper to test for built-in module */
790
791static int
792is_builtin(name)
793 char *name;
794{
795 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000796 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
797 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
798 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000799 return -1;
800 else
801 return 1;
802 }
803 }
804 return 0;
805}
806
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000807
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808/* Search the path (default sys.path) for a module. Return the
809 corresponding filedescr struct, and (via return arguments) the
810 pathname and an open file. Return NULL if the module is not found. */
811
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000812#ifdef MS_COREDLL
813extern FILE *PyWin_FindRegisteredModule();
814#endif
815
Guido van Rossum0980bd91998-02-13 17:18:36 +0000816#ifdef CHECK_IMPORT_CASE
817static int check_case(char *, int, int, char *);
818#endif
819
Guido van Rossum197346f1997-10-31 18:38:52 +0000820static int find_init_module Py_PROTO((char *)); /* Forward */
821
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822static struct filedescr *
823find_module(name, path, buf, buflen, p_fp)
824 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 PyObject *path;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826 /* Output parameters: */
827 char *buf;
828 int buflen;
829 FILE **p_fp;
830{
831 int i, npath, len, namelen;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000832 struct _frozen *f;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000833 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000834 FILE *fp = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000835 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000836 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
837 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
838 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000839
840 if (path == NULL) {
841 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000842 strcpy(buf, name);
843 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000844 }
Guido van Rossuma5568d31998-03-05 03:45:08 +0000845 if ((f = find_frozen(name)) != NULL) {
846 strcpy(buf, name);
847 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000848 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849
Guido van Rossumac279101996-08-22 23:10:58 +0000850#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000851 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
852 if (fp != NULL) {
853 *p_fp = fp;
854 return fdp;
855 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000856#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000857 path = PySys_GetObject("path");
858 }
859 else if (PyString_Check(path)) {
860 /* Submodule of frozen package */
Guido van Rossum39b0f891998-04-10 21:52:06 +0000861 if (PyString_Size(path) + 1 + strlen(name) >= (unsigned int)buflen) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000862 PyErr_SetString(PyExc_ImportError,
863 "full frozen module name too long");
864 return NULL;
865 }
866 strcpy(buf, PyString_AsString(path));
867 strcat(buf, ".");
868 strcat(buf, name);
869 if (find_frozen(buf) != NULL)
870 return &fd_frozen;
871 PyErr_Format(PyExc_ImportError,
872 "frozen module %.200s not found", buf);
873 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000874 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000875
Guido van Rossum79f25d91997-04-29 20:08:16 +0000876 if (path == NULL || !PyList_Check(path)) {
877 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +0000878 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000879 return NULL;
880 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882 namelen = strlen(name);
883 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 PyObject *v = PyList_GetItem(path, i);
885 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000887 len = PyString_Size(v);
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000888 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000889 continue; /* Too long */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 strcpy(buf, PyString_AsString(v));
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000891 if ((int)strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000892 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000893#ifdef macintosh
Guido van Rossum741689d1997-08-12 14:53:39 +0000894#ifdef INTERN_STRINGS
895 /*
896 ** Speedup: each sys.path item is interned, and
897 ** FindResourceModule remembers which items refer to
898 ** folders (so we don't have to bother trying to look
899 ** into them for resources).
900 */
901 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
902 v = PyList_GET_ITEM(path, i);
903#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000904 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 static struct filedescr resfiledescr =
906 {"", "", PY_RESOURCE};
Jack Jansen9c96a921995-02-15 22:57:06 +0000907
908 return &resfiledescr;
909 }
910#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000911 if (len > 0 && buf[len-1] != SEP
912#ifdef ALTSEP
913 && buf[len-1] != ALTSEP
914#endif
915 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000917#ifdef IMPORT_8x3_NAMES
Guido van Rossume8c27bb1997-12-17 18:57:16 +0000918 /* see if we are searching in directory dos-8x3 */
919 if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){
Guido van Rossum40f470f1996-05-23 22:51:04 +0000920 int j;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 char ch; /* limit name to 8 lower-case characters */
Guido van Rossum40f470f1996-05-23 22:51:04 +0000922 for (j = 0; (ch = name[j]) && j < 8; j++)
923 if (isupper(ch))
924 buf[len++] = tolower(ch);
925 else
926 buf[len++] = ch;
927 }
Guido van Rossume8c27bb1997-12-17 18:57:16 +0000928 else /* Not in dos-8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000929#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000930 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000931 strcpy(buf+len, name);
932 len += namelen;
933 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000934#ifdef HAVE_STAT
935 if (stat(buf, &statbuf) == 0) {
Guido van Rossum197346f1997-10-31 18:38:52 +0000936 if (S_ISDIR(statbuf.st_mode)) {
Guido van Rossum0980bd91998-02-13 17:18:36 +0000937 if (find_init_module(buf)) {
938#ifdef CHECK_IMPORT_CASE
939 if (!check_case(buf, len, namelen,
940 name))
941 return NULL;
942#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000943 return &fd_package;
Guido van Rossum0980bd91998-02-13 17:18:36 +0000944 }
Guido van Rossum197346f1997-10-31 18:38:52 +0000945 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000946 }
947#else
948 /* XXX How are you going to test for directories? */
949#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +0000950#ifdef macintosh
951 fdp = PyMac_FindModuleExtension(buf, &len, name);
952 if (fdp)
953 fp = fopen(buf, fdp->mode);
954#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000957 if (Py_VerboseFlag > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958 fprintf(stderr, "# trying %s\n", buf);
959 fp = fopen(buf, fdp->mode);
960 if (fp != NULL)
961 break;
962 }
Guido van Rossum741689d1997-08-12 14:53:39 +0000963#endif /* !macintosh */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000964 if (fp != NULL)
965 break;
966 }
967 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000968 PyErr_Format(PyExc_ImportError,
969 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000970 return NULL;
971 }
Guido van Rossum0980bd91998-02-13 17:18:36 +0000972#ifdef CHECK_IMPORT_CASE
973 if (!check_case(buf, len, namelen, name)) {
974 fclose(fp);
975 return NULL;
976 }
977#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000978
979 *p_fp = fp;
980 return fdp;
981}
982
Guido van Rossum0980bd91998-02-13 17:18:36 +0000983#ifdef CHECK_IMPORT_CASE
984
985#ifdef MS_WIN32
986#include <windows.h>
987static int
988check_case(char *buf, int len, int namelen, char *name)
989{
990 WIN32_FIND_DATA data;
991 HANDLE h;
992 if (getenv("PYTHONCASEOK") != NULL)
993 return 1;
994 h = FindFirstFile(buf, &data);
995 if (h == INVALID_HANDLE_VALUE) {
996 PyErr_Format(PyExc_NameError,
997 "Can't find file for module %.100s\n(filename %.300s)",
998 name, buf);
999 return 0;
1000 }
1001 FindClose(h);
1002 if (strncmp(data.cFileName, name, namelen) != 0) {
1003 strcpy(buf+len-namelen, data.cFileName);
1004 PyErr_Format(PyExc_NameError,
1005 "Case mismatch for module name %.100s\n(filename %.300s)",
1006 name, buf);
1007 return 0;
1008 }
1009 return 1;
1010}
1011#endif /* MS_WIN32 */
1012
1013#ifdef macintosh
1014#include <TextUtils.h>
1015static int
1016check_case(char *buf, int len, int namelen, char *name)
1017{
1018 FSSpec fss;
1019 OSErr err;
1020 unsigned char mybuf[MAXPATHLEN+1];
1021
1022 strcpy((char *)mybuf, buf);
1023 c2pstr((char *)mybuf);
1024 err = FSMakeFSSpec(0, 0, mybuf, &fss);
1025 if (err) {
1026 PyErr_Format(PyExc_NameError,
1027 "Can't find file for module %.100s\n(filename %.300s)",
1028 name, buf);
1029 return 0;
1030 }
1031 p2cstr(fss.name);
1032 if ( strncmp(name, (char *)fss.name, namelen) != 0 ) {
1033 PyErr_Format(PyExc_NameError,
1034 "Case mismatch for module name %.100s\n(filename %.300s)",
1035 name, fss.name);
1036 return 0;
1037 }
1038 return 1;
1039}
1040#endif /* macintosh */
1041
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001042#ifdef DJGPP
Guido van Rossumaeca79b1998-02-18 16:21:00 +00001043#include <dir.h>
1044
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001045static int
1046check_case(char *buf, int len, int namelen, char *name)
1047{
1048 struct ffblk ffblk;
1049 int done;
1050
1051 if (getenv("PYTHONCASEOK") != NULL)
1052 return 1;
Guido van Rossumaeca79b1998-02-18 16:21:00 +00001053 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001054 if (done) {
1055 PyErr_Format(PyExc_NameError,
1056 "Can't find file for module %.100s\n(filename %.300s)",
1057 name, buf);
1058 return 0;
1059 }
1060
1061 if (strncmp(ffblk.ff_name, name, namelen) != 0) {
1062 strcpy(buf+len-namelen, ffblk.ff_name);
1063 PyErr_Format(PyExc_NameError,
1064 "Case mismatch for module name %.100s\n(filename %.300s)",
1065 name, buf);
1066 return 0;
1067 }
1068 return 1;
1069}
1070#endif
1071
Guido van Rossum0980bd91998-02-13 17:18:36 +00001072#endif CHECK_IMPORT_CASE
1073
Guido van Rossum197346f1997-10-31 18:38:52 +00001074#ifdef HAVE_STAT
1075/* Helper to look for __init__.py or __init__.py[co] in potential package */
1076static int
1077find_init_module(buf)
1078 char *buf;
1079{
1080 int save_len = strlen(buf);
1081 int i = save_len;
1082 struct stat statbuf;
1083
1084 if (save_len + 13 >= MAXPATHLEN)
1085 return 0;
1086 buf[i++] = SEP;
1087 strcpy(buf+i, "__init__.py");
1088 if (stat(buf, &statbuf) == 0) {
1089 buf[save_len] = '\0';
1090 return 1;
1091 }
1092 i += strlen(buf+i);
1093 if (Py_OptimizeFlag)
1094 strcpy(buf+i, "o");
1095 else
1096 strcpy(buf+i, "c");
1097 if (stat(buf, &statbuf) == 0) {
1098 buf[save_len] = '\0';
1099 return 1;
1100 }
1101 buf[save_len] = '\0';
1102 return 0;
1103}
1104#endif /* HAVE_STAT */
1105
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001106
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001107static int init_builtin Py_PROTO((char *)); /* Forward */
1108
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001109/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001110 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001111
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001113load_module(name, fp, buf, type)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001114 char *name;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001115 FILE *fp;
1116 char *buf;
1117 int type;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001118{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001119 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001121 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001122
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001123 /* First check that there's an open file (if we need one) */
1124 switch (type) {
1125 case PY_SOURCE:
1126 case PY_COMPILED:
1127 if (fp == NULL) {
1128 PyErr_Format(PyExc_ValueError,
1129 "file object required for import (type code %d)",
1130 type);
1131 return NULL;
1132 }
1133 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001134
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001135 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001136
1137 case PY_SOURCE:
1138 m = load_source_module(name, buf, fp);
1139 break;
1140
1141 case PY_COMPILED:
1142 m = load_compiled_module(name, buf, fp);
1143 break;
1144
1145 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001147 break;
1148
Jack Jansen9c96a921995-02-15 22:57:06 +00001149#ifdef macintosh
1150 case PY_RESOURCE:
1151 m = PyMac_LoadResourceModule(name, buf);
1152 break;
1153#endif
1154
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001155 case PKG_DIRECTORY:
1156 m = load_package(name, buf);
1157 break;
1158
1159 case C_BUILTIN:
1160 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001161 if (buf != NULL && buf[0] != '\0')
1162 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001163 if (type == C_BUILTIN)
1164 err = init_builtin(name);
1165 else
1166 err = PyImport_ImportFrozenModule(name);
1167 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001168 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001169 if (err == 0) {
1170 PyErr_Format(PyExc_ImportError,
1171 "Purported %s module %.200s not found",
1172 type == C_BUILTIN ?
1173 "builtin" : "frozen",
1174 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001175 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001176 }
1177 modules = PyImport_GetModuleDict();
1178 m = PyDict_GetItemString(modules, name);
1179 if (m == NULL) {
1180 PyErr_Format(
1181 PyExc_ImportError,
1182 "%s module %.200s not properly initialized",
1183 type == C_BUILTIN ?
1184 "builtin" : "frozen",
1185 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001186 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001187 }
1188 Py_INCREF(m);
1189 break;
1190
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001191 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001192 PyErr_Format(PyExc_ImportError,
1193 "Don't know how to import %.200s (type code %d)",
1194 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001195 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196
1197 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001198
1199 return m;
1200}
1201
1202
1203/* Initialize a built-in module.
1204 Return 1 for succes, 0 if the module is not found, and -1 with
1205 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001206
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001207static int
1208init_builtin(name)
1209 char *name;
1210{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001211 struct _inittab *p;
1212 PyObject *mod;
1213
1214 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1215 return 1;
1216
Guido van Rossum771c6c81997-10-31 18:37:24 +00001217 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001218 if (strcmp(name, p->name) == 0) {
1219 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001220 PyErr_Format(PyExc_ImportError,
1221 "Cannot re-init internal module %.200s",
1222 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001223 return -1;
1224 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 if (Py_VerboseFlag)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001226 fprintf(stderr, "import %s # builtin\n", name);
1227 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001229 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001230 if (_PyImport_FixupExtension(name, name) == NULL)
1231 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001232 return 1;
1233 }
1234 }
1235 return 0;
1236}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001237
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001238
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001239/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001240
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001241static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001242find_frozen(name)
1243 char *name;
1244{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001245 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001246
Guido van Rossum79f25d91997-04-29 20:08:16 +00001247 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001248 if (p->name == NULL)
1249 return NULL;
1250 if (strcmp(p->name, name) == 0)
1251 break;
1252 }
1253 return p;
1254}
1255
Guido van Rossum79f25d91997-04-29 20:08:16 +00001256static PyObject *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001257get_frozen_object(name)
1258 char *name;
1259{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001260 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001261 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001262
1263 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001264 PyErr_Format(PyExc_ImportError,
1265 "No such frozen object named %.200s",
1266 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001267 return NULL;
1268 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001269 size = p->size;
1270 if (size < 0)
1271 size = -size;
1272 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001273}
1274
1275/* Initialize a frozen module.
1276 Return 1 for succes, 0 if the module is not found, and -1 with
1277 an exception set if the initialization failed.
1278 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001279
1280int
Guido van Rossum79f25d91997-04-29 20:08:16 +00001281PyImport_ImportFrozenModule(name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001282 char *name;
1283{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001284 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285 PyObject *co;
1286 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001287 int ispackage;
1288 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001289
1290 if (p == NULL)
1291 return 0;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001292 size = p->size;
1293 ispackage = (size < 0);
1294 if (ispackage)
1295 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001296 if (Py_VerboseFlag)
Guido van Rossuma5568d31998-03-05 03:45:08 +00001297 fprintf(stderr, "import %s # frozen%s\n",
1298 name, ispackage ? " package" : "");
1299 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001300 if (co == NULL)
1301 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001302 if (!PyCode_Check(co)) {
1303 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001304 PyErr_Format(PyExc_TypeError,
1305 "frozen object %.200s is not a code object",
1306 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001307 return -1;
1308 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001309 if (ispackage) {
1310 /* Set __path__ to the package name */
1311 PyObject *d, *s;
1312 int err;
1313 m = PyImport_AddModule(name);
1314 if (m == NULL)
1315 return -1;
1316 d = PyModule_GetDict(m);
1317 s = PyString_InternFromString(name);
1318 if (s == NULL)
1319 return -1;
1320 err = PyDict_SetItemString(d, "__path__", s);
1321 Py_DECREF(s);
1322 if (err != 0)
1323 return err;
1324 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001325 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001326 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001327 if (m == NULL)
1328 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001329 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001330 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001331}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001332
1333
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001334/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001335 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001336
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337PyObject *
1338PyImport_ImportModule(name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001339 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001340{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001341 static PyObject *fromlist = NULL;
1342 if (fromlist == NULL && strchr(name, '.') != NULL) {
1343 fromlist = Py_BuildValue("[s]", "*");
1344 if (fromlist == NULL)
1345 return NULL;
1346 }
1347 return PyImport_ImportModuleEx(name, NULL, NULL, fromlist);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001348}
1349
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001350/* Forward declarations for helper routines */
1351static PyObject *get_parent Py_PROTO((PyObject *globals,
1352 char *buf, int *p_buflen));
1353static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod,
1354 char **p_name, char *buf, int *p_buflen));
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001355static int mark_miss Py_PROTO((char *name));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001356static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist,
Guido van Rossum9905ef91997-09-08 16:07:11 +00001357 char *buf, int buflen, int recursive));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001358static PyObject * import_submodule Py_PROTO((PyObject *mod,
1359 char *name, char *fullname));
1360
1361/* The Magnum Opus of dotted-name import :-) */
1362
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001363static PyObject *
1364import_module_ex(name, globals, locals, fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001365 char *name;
1366 PyObject *globals;
1367 PyObject *locals;
1368 PyObject *fromlist;
1369{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001370 char buf[MAXPATHLEN+1];
1371 int buflen = 0;
1372 PyObject *parent, *head, *next, *tail;
1373
1374 parent = get_parent(globals, buf, &buflen);
1375 if (parent == NULL)
1376 return NULL;
1377
1378 head = load_next(parent, Py_None, &name, buf, &buflen);
1379 if (head == NULL)
1380 return NULL;
1381
1382 tail = head;
1383 Py_INCREF(tail);
1384 while (name) {
1385 next = load_next(tail, tail, &name, buf, &buflen);
1386 Py_DECREF(tail);
1387 if (next == NULL) {
1388 Py_DECREF(head);
1389 return NULL;
1390 }
1391 tail = next;
1392 }
1393
1394 if (fromlist != NULL) {
1395 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1396 fromlist = NULL;
1397 }
1398
1399 if (fromlist == NULL) {
1400 Py_DECREF(tail);
1401 return head;
1402 }
1403
1404 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001405 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001406 Py_DECREF(tail);
1407 return NULL;
1408 }
1409
1410 return tail;
1411}
1412
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001413PyObject *
1414PyImport_ImportModuleEx(name, globals, locals, fromlist)
1415 char *name;
1416 PyObject *globals;
1417 PyObject *locals;
1418 PyObject *fromlist;
1419{
1420 PyObject *result;
1421 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001422 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001423 unlock_import();
1424 return result;
1425}
1426
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001427static PyObject *
1428get_parent(globals, buf, p_buflen)
1429 PyObject *globals;
1430 char *buf;
1431 int *p_buflen;
1432{
1433 static PyObject *namestr = NULL;
1434 static PyObject *pathstr = NULL;
1435 PyObject *modname, *modpath, *modules, *parent;
1436
1437 if (globals == NULL || !PyDict_Check(globals))
1438 return Py_None;
1439
1440 if (namestr == NULL) {
1441 namestr = PyString_InternFromString("__name__");
1442 if (namestr == NULL)
1443 return NULL;
1444 }
1445 if (pathstr == NULL) {
1446 pathstr = PyString_InternFromString("__path__");
1447 if (pathstr == NULL)
1448 return NULL;
1449 }
1450
1451 *buf = '\0';
1452 *p_buflen = 0;
1453 modname = PyDict_GetItem(globals, namestr);
1454 if (modname == NULL || !PyString_Check(modname))
1455 return Py_None;
1456
1457 modpath = PyDict_GetItem(globals, pathstr);
1458 if (modpath != NULL) {
1459 int len = PyString_GET_SIZE(modname);
1460 if (len > MAXPATHLEN) {
1461 PyErr_SetString(PyExc_ValueError,
1462 "Module name too long");
1463 return NULL;
1464 }
1465 strcpy(buf, PyString_AS_STRING(modname));
1466 *p_buflen = len;
1467 }
1468 else {
1469 char *start = PyString_AS_STRING(modname);
1470 char *lastdot = strrchr(start, '.');
1471 int len;
1472 if (lastdot == NULL)
1473 return Py_None;
1474 len = lastdot - start;
1475 if (len >= MAXPATHLEN) {
1476 PyErr_SetString(PyExc_ValueError,
1477 "Module name too long");
1478 return NULL;
1479 }
1480 strncpy(buf, start, len);
1481 buf[len] = '\0';
1482 *p_buflen = len;
1483 }
1484
1485 modules = PyImport_GetModuleDict();
1486 parent = PyDict_GetItemString(modules, buf);
1487 if (parent == NULL)
1488 parent = Py_None;
1489 return parent;
1490 /* We expect, but can't guarantee, if parent != None, that:
1491 - parent.__name__ == buf
1492 - parent.__dict__ is globals
1493 If this is violated... Who cares? */
1494}
1495
1496static PyObject *
1497load_next(mod, altmod, p_name, buf, p_buflen)
1498 PyObject *mod;
1499 PyObject *altmod; /* Either None or same as mod */
1500 char **p_name;
1501 char *buf;
1502 int *p_buflen;
1503{
1504 char *name = *p_name;
1505 char *dot = strchr(name, '.');
1506 int len;
1507 char *p;
1508 PyObject *result;
1509
1510 if (dot == NULL) {
1511 *p_name = NULL;
1512 len = strlen(name);
1513 }
1514 else {
1515 *p_name = dot+1;
1516 len = dot-name;
1517 }
1518
1519 p = buf + *p_buflen;
1520 if (p != buf)
1521 *p++ = '.';
1522 if (p+len-buf >= MAXPATHLEN) {
1523 PyErr_SetString(PyExc_ValueError,
1524 "Module name too long");
1525 return NULL;
1526 }
1527 strncpy(p, name, len);
1528 p[len] = '\0';
1529 *p_buflen = p+len-buf;
1530
1531 result = import_submodule(mod, p, buf);
1532 if (result == Py_None && altmod != mod) {
1533 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001534 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00001535 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001536 if (result != NULL && result != Py_None) {
1537 if (mark_miss(buf) != 0) {
1538 Py_DECREF(result);
1539 return NULL;
1540 }
1541 strncpy(buf, name, len);
1542 buf[len] = '\0';
1543 *p_buflen = len;
1544 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001545 }
1546 if (result == NULL)
1547 return NULL;
1548
1549 if (result == Py_None) {
1550 Py_DECREF(result);
1551 PyErr_Format(PyExc_ImportError,
1552 "No module named %.200s", name);
1553 return NULL;
1554 }
1555
1556 return result;
1557}
1558
1559static int
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001560mark_miss(name)
1561 char *name;
1562{
1563 PyObject *modules = PyImport_GetModuleDict();
1564 return PyDict_SetItemString(modules, name, Py_None);
1565}
1566
1567static int
Guido van Rossum9905ef91997-09-08 16:07:11 +00001568ensure_fromlist(mod, fromlist, buf, buflen, recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001569 PyObject *mod;
1570 PyObject *fromlist;
1571 char *buf;
1572 int buflen;
Guido van Rossum9905ef91997-09-08 16:07:11 +00001573 int recursive;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001574{
1575 int i;
1576
1577 if (!PyObject_HasAttrString(mod, "__path__"))
1578 return 1;
1579
1580 for (i = 0; ; i++) {
1581 PyObject *item = PySequence_GetItem(fromlist, i);
1582 int hasit;
1583 if (item == NULL) {
1584 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1585 PyErr_Clear();
1586 return 1;
1587 }
1588 return 0;
1589 }
1590 if (!PyString_Check(item)) {
1591 PyErr_SetString(PyExc_TypeError,
1592 "Item in ``from list'' not a string");
1593 Py_DECREF(item);
1594 return 0;
1595 }
1596 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00001597 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001598 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001599 /* See if the package defines __all__ */
1600 if (recursive)
1601 continue; /* Avoid endless recursion */
1602 all = PyObject_GetAttrString(mod, "__all__");
1603 if (all == NULL)
1604 PyErr_Clear();
1605 else {
1606 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1607 return 0;
1608 Py_DECREF(all);
1609 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001610 continue;
1611 }
1612 hasit = PyObject_HasAttr(mod, item);
1613 if (!hasit) {
1614 char *subname = PyString_AS_STRING(item);
1615 PyObject *submod;
1616 char *p;
1617 if (buflen + strlen(subname) >= MAXPATHLEN) {
1618 PyErr_SetString(PyExc_ValueError,
1619 "Module name too long");
1620 Py_DECREF(item);
1621 return 0;
1622 }
1623 p = buf + buflen;
1624 *p++ = '.';
1625 strcpy(p, subname);
1626 submod = import_submodule(mod, subname, buf);
1627 Py_XDECREF(submod);
1628 if (submod == NULL) {
1629 Py_DECREF(item);
1630 return 0;
1631 }
1632 }
1633 Py_DECREF(item);
1634 }
1635
Guido van Rossuma7f2e811997-10-03 15:33:32 +00001636 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001637}
1638
1639static PyObject *
1640import_submodule(mod, subname, fullname)
1641 PyObject *mod; /* May be None */
1642 char *subname;
1643 char *fullname;
1644{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001645 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001646 PyObject *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001647
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001648 /* Require:
1649 if mod == None: subname == fullname
1650 else: mod.__name__ + "." + subname == fullname
1651 */
1652
1653 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001654 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001655 }
1656 else {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001657 PyObject *path;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001658 char buf[MAXPATHLEN+1];
1659 struct filedescr *fdp;
1660 FILE *fp = NULL;
1661
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001662 path = PyObject_GetAttrString(mod, "__path__");
1663 if (path == NULL)
1664 PyErr_Clear();
1665
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001666 buf[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001667 fdp = find_module(subname, path,
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001668 buf, MAXPATHLEN+1, &fp);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001669 if (fdp == NULL) {
1670 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1671 return NULL;
1672 PyErr_Clear();
1673 Py_INCREF(Py_None);
1674 return Py_None;
1675 }
1676 m = load_module(fullname, fp, buf, fdp->type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001677 if (fp)
1678 fclose(fp);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001679 if (m != NULL && mod != Py_None) {
1680 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1681 Py_DECREF(m);
1682 m = NULL;
1683 }
1684 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00001685 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001686
1687 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001688}
1689
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001690
1691/* Re-import a module of any kind and return its module object, WITH
1692 INCREMENTED REFERENCE COUNT */
1693
Guido van Rossum79f25d91997-04-29 20:08:16 +00001694PyObject *
1695PyImport_ReloadModule(m)
1696 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001697{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001698 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00001699 PyObject *path = NULL;
1700 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001701 char buf[MAXPATHLEN+1];
1702 struct filedescr *fdp;
1703 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704
Guido van Rossum79f25d91997-04-29 20:08:16 +00001705 if (m == NULL || !PyModule_Check(m)) {
1706 PyErr_SetString(PyExc_TypeError,
1707 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001708 return NULL;
1709 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001711 if (name == NULL)
1712 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001713 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001714 PyErr_Format(PyExc_ImportError,
1715 "reload(): module %.200s not in sys.modules",
1716 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001717 return NULL;
1718 }
Guido van Rossum222ef561997-09-06 19:41:09 +00001719 subname = strrchr(name, '.');
1720 if (subname == NULL)
1721 subname = name;
1722 else {
1723 PyObject *parentname, *parent;
1724 parentname = PyString_FromStringAndSize(name, (subname-name));
1725 if (parentname == NULL)
1726 return NULL;
1727 parent = PyDict_GetItem(modules, parentname);
1728 if (parent == NULL) {
1729 PyErr_Format(PyExc_ImportError,
1730 "reload(): parent %.200s not in sys.modules",
1731 name);
1732 return NULL;
1733 }
1734 subname++;
1735 path = PyObject_GetAttrString(parent, "__path__");
1736 if (path == NULL)
1737 PyErr_Clear();
1738 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001739 buf[0] = '\0';
Guido van Rossum222ef561997-09-06 19:41:09 +00001740 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1741 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001742 if (fdp == NULL)
1743 return NULL;
1744 m = load_module(name, fp, buf, fdp->type);
1745 if (fp)
1746 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001747 return m;
1748}
1749
1750
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001751/* Higher-level import emulator which emulates the "import" statement
1752 more accurately -- it invokes the __import__() function from the
1753 builtins of the current globals. This means that the import is
1754 done using whatever import hooks are installed in the current
1755 environment, e.g. by "ni" or "rexec". */
1756
1757PyObject *
1758PyImport_Import(module_name)
1759 PyObject *module_name;
1760{
1761 static PyObject *silly_list = NULL;
1762 static PyObject *builtins_str = NULL;
1763 static PyObject *import_str = NULL;
1764 static PyObject *standard_builtins = NULL;
1765 PyObject *globals = NULL;
1766 PyObject *import = NULL;
1767 PyObject *builtins = NULL;
1768 PyObject *r = NULL;
1769
1770 /* Initialize constant string objects */
1771 if (silly_list == NULL) {
1772 import_str = PyString_InternFromString("__import__");
1773 if (import_str == NULL)
1774 return NULL;
1775 builtins_str = PyString_InternFromString("__builtins__");
1776 if (builtins_str == NULL)
1777 return NULL;
1778 silly_list = Py_BuildValue("[s]", "__doc__");
1779 if (silly_list == NULL)
1780 return NULL;
1781 }
1782
1783 /* Get the builtins from current globals */
1784 globals = PyEval_GetGlobals();
1785 if(globals != NULL) {
1786 builtins = PyObject_GetItem(globals, builtins_str);
1787 if (builtins == NULL)
1788 goto err;
1789 }
1790 else {
1791 /* No globals -- use standard builtins, and fake globals */
1792 PyErr_Clear();
1793
1794 if (standard_builtins == NULL) {
1795 standard_builtins =
1796 PyImport_ImportModule("__builtin__");
1797 if (standard_builtins == NULL)
1798 return NULL;
1799 }
1800
1801 builtins = standard_builtins;
1802 Py_INCREF(builtins);
1803 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1804 if (globals == NULL)
1805 goto err;
1806 }
1807
1808 /* Get the __import__ function from the builtins */
1809 if (PyDict_Check(builtins))
1810 import=PyObject_GetItem(builtins, import_str);
1811 else
1812 import=PyObject_GetAttr(builtins, import_str);
1813 if (import == NULL)
1814 goto err;
1815
1816 /* Call the _import__ function with the proper argument list */
1817 r = PyObject_CallFunction(import, "OOOO",
1818 module_name, globals, globals, silly_list);
1819
1820 err:
1821 Py_XDECREF(globals);
1822 Py_XDECREF(builtins);
1823 Py_XDECREF(import);
1824
1825 return r;
1826}
1827
1828
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001829/* Module 'imp' provides Python access to the primitives used for
1830 importing modules.
1831*/
1832
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001834imp_get_magic(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 PyObject *self;
1836 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001837{
1838 char buf[4];
1839
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001841 return NULL;
Guido van Rossum6bf62da1997-04-11 20:37:35 +00001842 buf[0] = (char) ((MAGIC >> 0) & 0xff);
1843 buf[1] = (char) ((MAGIC >> 8) & 0xff);
1844 buf[2] = (char) ((MAGIC >> 16) & 0xff);
1845 buf[3] = (char) ((MAGIC >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001846
Guido van Rossum79f25d91997-04-29 20:08:16 +00001847 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001848}
1849
Guido van Rossum79f25d91997-04-29 20:08:16 +00001850static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001851imp_get_suffixes(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyObject *self;
1853 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001854{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001856 struct filedescr *fdp;
1857
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001859 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001861 if (list == NULL)
1862 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1864 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001865 fdp->suffix, fdp->mode, fdp->type);
1866 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001868 return NULL;
1869 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870 if (PyList_Append(list, item) < 0) {
1871 Py_DECREF(list);
1872 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001873 return NULL;
1874 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001876 }
1877 return list;
1878}
1879
Guido van Rossum79f25d91997-04-29 20:08:16 +00001880static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001881call_find_module(name, path)
1882 char *name;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001883 PyObject *path; /* list or None or NULL */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001884{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 extern int fclose Py_PROTO((FILE *));
Guido van Rossum79f25d91997-04-29 20:08:16 +00001886 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001887 struct filedescr *fdp;
1888 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001889 FILE *fp = NULL;
1890
1891 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001892 if (path == Py_None)
1893 path = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001894 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
1895 if (fdp == NULL)
1896 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001897 if (fp != NULL) {
1898 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
1899 if (fob == NULL) {
1900 fclose(fp);
1901 return NULL;
1902 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001903 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001904 else {
1905 fob = Py_None;
1906 Py_INCREF(fob);
1907 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001908 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001909 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001911 return ret;
1912}
1913
Guido van Rossum79f25d91997-04-29 20:08:16 +00001914static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001915imp_find_module(self, args)
1916 PyObject *self;
1917 PyObject *args;
1918{
1919 char *name;
1920 PyObject *path = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001921 if (!PyArg_ParseTuple(args, "s|O", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001922 return NULL;
1923 return call_find_module(name, path);
1924}
1925
1926static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001927imp_init_builtin(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928 PyObject *self;
1929 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001930{
1931 char *name;
1932 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933 PyObject *m;
1934 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001935 return NULL;
1936 ret = init_builtin(name);
1937 if (ret < 0)
1938 return NULL;
1939 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001940 Py_INCREF(Py_None);
1941 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001942 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943 m = PyImport_AddModule(name);
1944 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001945 return m;
1946}
1947
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001949imp_init_frozen(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 PyObject *self;
1951 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001952{
1953 char *name;
1954 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001955 PyObject *m;
1956 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001957 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001959 if (ret < 0)
1960 return NULL;
1961 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 Py_INCREF(Py_None);
1963 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001964 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965 m = PyImport_AddModule(name);
1966 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001967 return m;
1968}
1969
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970static PyObject *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001971imp_get_frozen_object(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972 PyObject *self;
1973 PyObject *args;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001974{
1975 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001976
Guido van Rossum79f25d91997-04-29 20:08:16 +00001977 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001978 return NULL;
1979 return get_frozen_object(name);
1980}
1981
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001983imp_is_builtin(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984 PyObject *self;
1985 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001986{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001987 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001988 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001989 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001990 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001991}
1992
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001994imp_is_frozen(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995 PyObject *self;
1996 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001998 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002000 return NULL;
Guido van Rossuma86f77d1997-09-09 18:53:47 +00002001 return PyInt_FromLong(find_frozen(name) != NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002002}
2003
2004static FILE *
2005get_file(pathname, fob, mode)
2006 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 PyObject *fob;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002008 char *mode;
2009{
2010 FILE *fp;
2011 if (fob == NULL) {
2012 fp = fopen(pathname, mode);
2013 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002014 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002015 }
2016 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002018 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002019 PyErr_SetString(PyExc_ValueError,
2020 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002021 }
2022 return fp;
2023}
2024
Guido van Rossum79f25d91997-04-29 20:08:16 +00002025static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002026imp_load_compiled(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002027 PyObject *self;
2028 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002029{
2030 char *name;
2031 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002032 PyObject *fob = NULL;
2033 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002034 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002035 if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002036 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002037 return NULL;
2038 fp = get_file(pathname, fob, "rb");
2039 if (fp == NULL)
2040 return NULL;
2041 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002042 if (fob == NULL)
2043 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002044 return m;
2045}
2046
Guido van Rossum79f25d91997-04-29 20:08:16 +00002047static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002048imp_load_dynamic(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049 PyObject *self;
2050 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002051{
2052 char *name;
2053 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002054 PyObject *fob = NULL;
2055 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002056 FILE *fp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002057 if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
2058 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002059 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002060 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002061 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002062 if (fp == NULL)
2063 return NULL;
2064 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002065 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002066 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002067}
2068
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002070imp_load_source(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002071 PyObject *self;
2072 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002073{
2074 char *name;
2075 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002076 PyObject *fob = NULL;
2077 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002078 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002079 if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002080 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002081 return NULL;
2082 fp = get_file(pathname, fob, "r");
2083 if (fp == NULL)
2084 return NULL;
2085 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002086 if (fob == NULL)
2087 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002088 return m;
2089}
2090
Jack Jansen9c96a921995-02-15 22:57:06 +00002091#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002092static PyObject *
Jack Jansen9c96a921995-02-15 22:57:06 +00002093imp_load_resource(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002094 PyObject *self;
2095 PyObject *args;
Jack Jansen9c96a921995-02-15 22:57:06 +00002096{
2097 char *name;
2098 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002099 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002100
Guido van Rossum79f25d91997-04-29 20:08:16 +00002101 if (!PyArg_ParseTuple(args, "ss", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002102 return NULL;
2103 m = PyMac_LoadResourceModule(name, pathname);
2104 return m;
2105}
2106#endif /* macintosh */
2107
Guido van Rossum79f25d91997-04-29 20:08:16 +00002108static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002109imp_load_module(self, args)
2110 PyObject *self;
2111 PyObject *args;
2112{
2113 char *name;
2114 PyObject *fob;
2115 char *pathname;
2116 char *suffix; /* Unused */
2117 char *mode;
2118 int type;
2119 FILE *fp;
2120
2121 if (!PyArg_ParseTuple(args, "sOs(ssi)",
2122 &name, &fob, &pathname,
2123 &suffix, &mode, &type))
2124 return NULL;
2125 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2126 PyErr_Format(PyExc_ValueError,
2127 "invalid file open mode %.200s", mode);
2128 return NULL;
2129 }
2130 if (fob == Py_None)
2131 fp = NULL;
2132 else {
2133 if (!PyFile_Check(fob)) {
2134 PyErr_SetString(PyExc_ValueError,
2135 "load_module arg#2 should be a file or None");
2136 return NULL;
2137 }
2138 fp = get_file(pathname, fob, mode);
2139 if (fp == NULL)
2140 return NULL;
2141 }
2142 return load_module(name, fp, pathname, type);
2143}
2144
2145static PyObject *
2146imp_load_package(self, args)
2147 PyObject *self;
2148 PyObject *args;
2149{
2150 char *name;
2151 char *pathname;
2152 if (!PyArg_ParseTuple(args, "ss", &name, &pathname))
2153 return NULL;
2154 return load_package(name, pathname);
2155}
2156
2157static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002158imp_new_module(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002159 PyObject *self;
2160 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002161{
2162 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002163 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002164 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002165 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002166}
2167
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002168/* Doc strings */
2169
2170static char doc_imp[] = "\
2171This module provides the components needed to build your own\n\
2172__import__ function. Undocumented functions are obsolete.\n\
2173";
2174
2175static char doc_find_module[] = "\
2176find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2177Search for a module. If path is omitted or None, search for a\n\
2178built-in, frozen or special module and continue search in sys.path.\n\
2179The module name cannot contain '.'; to search for a submodule of a\n\
2180package, pass the submodule name and the package's __path__.\
2181";
2182
2183static char doc_load_module[] = "\
2184load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2185Load a module, given information returned by find_module().\n\
2186The module name must include the full package name, if any.\
2187";
2188
2189static char doc_get_magic[] = "\
2190get_magic() -> string\n\
2191Return the magic number for .pyc or .pyo files.\
2192";
2193
2194static char doc_get_suffixes[] = "\
2195get_suffixes() -> [(suffix, mode, type), ...]\n\
2196Return a list of (suffix, mode, type) tuples describing the files\n\
2197that find_module() looks for.\
2198";
2199
2200static char doc_new_module[] = "\
2201new_module(name) -> module\n\
2202Create a new module. Do not enter it in sys.modules.\n\
2203The module name must include the full package name, if any.\
2204";
2205
Guido van Rossum79f25d91997-04-29 20:08:16 +00002206static PyMethodDef imp_methods[] = {
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002207 {"find_module", imp_find_module, 1, doc_find_module},
2208 {"get_magic", imp_get_magic, 1, doc_get_magic},
2209 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2210 {"load_module", imp_load_module, 1, doc_load_module},
2211 {"new_module", imp_new_module, 1, doc_new_module},
2212 /* The rest are obsolete */
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002213 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002214 {"init_builtin", imp_init_builtin, 1},
2215 {"init_frozen", imp_init_frozen, 1},
2216 {"is_builtin", imp_is_builtin, 1},
2217 {"is_frozen", imp_is_frozen, 1},
2218 {"load_compiled", imp_load_compiled, 1},
2219 {"load_dynamic", imp_load_dynamic, 1},
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002220 {"load_package", imp_load_package, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00002221#ifdef macintosh
2222 {"load_resource", imp_load_resource, 1},
2223#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002224 {"load_source", imp_load_source, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002225 {NULL, NULL} /* sentinel */
2226};
2227
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002228int
2229setint(d, name, value)
2230 PyObject *d;
2231 char *name;
2232 int value;
2233{
2234 PyObject *v;
2235 int err;
2236
2237 v = PyInt_FromLong((long)value);
2238 err = PyDict_SetItemString(d, name, v);
2239 Py_XDECREF(v);
2240 return err;
2241}
2242
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002243void
2244initimp()
2245{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002246 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002247
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002248 m = Py_InitModule4("imp", imp_methods, doc_imp,
2249 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002250 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002251
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002252 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2253 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2254 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2255 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2256 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2257 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2258 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2259 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002260
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002261 failure:
2262 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002263}