blob: 52b80740885e5bc610dcdff5a217b14de6ed2a53 [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 */
Jeremy Hyltonb1cbc1e2001-02-02 20:13:24 +000046#define MAGIC (60202 | ((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;
63static const struct filedescr _PyImport_StandardFiletab[] = {
64 {".py", "r", PY_SOURCE},
65 {".pyc", "rb", PY_COMPILED},
66 {0, 0}
67};
68
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070
71void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073{
Guido van Rossumed1170e1999-12-20 21:23:41 +000074 const struct filedescr *scan;
75 struct filedescr *filetab;
76 int countD = 0;
77 int countS = 0;
78
79 /* prepare _PyImport_Filetab: copy entries from
80 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
81 */
82 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
83 ++countD;
84 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
85 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +000086 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +000087 memcpy(filetab, _PyImport_DynLoadFiletab,
88 countD * sizeof(struct filedescr));
89 memcpy(filetab + countD, _PyImport_StandardFiletab,
90 countS * sizeof(struct filedescr));
91 filetab[countD + countS].suffix = NULL;
92
93 _PyImport_Filetab = filetab;
94
Guido van Rossum0824f631997-03-11 18:37:35 +000095 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +000096 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
97 for (; filetab->suffix != NULL; filetab++) {
98 if (strcmp(filetab->suffix, ".pyc") == 0)
99 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000100 }
101 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000102
103 if (Py_UnicodeFlag) {
104 /* Fix the pyc_magic so that byte compiled code created
105 using the all-Unicode method doesn't interfere with
106 code created in normal operation mode. */
107 pyc_magic = MAGIC + 1;
108 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109}
110
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113{
114 Py_XDECREF(extensions);
115 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000116 PyMem_DEL(_PyImport_Filetab);
117 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118}
119
120
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000121/* Locking primitives to prevent parallel imports of the same module
122 in different threads to return with a partially loaded module.
123 These calls are serialized by the global interpreter lock. */
124
125#ifdef WITH_THREAD
126
Guido van Rossum49b56061998-10-01 20:42:43 +0000127#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000128
Guido van Rossum65d5b571998-12-21 19:32:43 +0000129static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000130static long import_lock_thread = -1;
131static int import_lock_level = 0;
132
133static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000134lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000135{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000136 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000137 if (me == -1)
138 return; /* Too bad */
139 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000140 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000141 if (import_lock_thread == me) {
142 import_lock_level++;
143 return;
144 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000145 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000146 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000147 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000148 PyEval_RestoreThread(tstate);
149 }
150 import_lock_thread = me;
151 import_lock_level = 1;
152}
153
154static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000156{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000157 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000158 if (me == -1)
159 return; /* Too bad */
160 if (import_lock_thread != me)
161 Py_FatalError("unlock_import: not holding the import lock");
162 import_lock_level--;
163 if (import_lock_level == 0) {
164 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000165 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000166 }
167}
168
169#else
170
171#define lock_import()
172#define unlock_import()
173
174#endif
175
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176/* Helper for sys */
177
178PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000179PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000180{
181 PyInterpreterState *interp = PyThreadState_Get()->interp;
182 if (interp->modules == NULL)
183 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
184 return interp->modules;
185}
186
Guido van Rossum3f5da241990-12-20 15:06:42 +0000187
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000188/* List of names to clear in sys */
189static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000190 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000191 "exc_type", "exc_value", "exc_traceback",
192 "last_type", "last_value", "last_traceback",
193 NULL
194};
195
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000196static char* sys_files[] = {
197 "stdin", "__stdin__",
198 "stdout", "__stdout__",
199 "stderr", "__stderr__",
200 NULL
201};
202
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000203
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000204/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000205
Guido van Rossum3f5da241990-12-20 15:06:42 +0000206void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000208{
Guido van Rossum758eec01998-01-19 21:58:26 +0000209 int pos, ndone;
210 char *name;
211 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000213 PyObject *modules = interp->modules;
214
215 if (modules == NULL)
216 return; /* Already done */
217
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000218 /* Delete some special variables first. These are common
219 places where user values hide and people complain when their
220 destructors fail. Since the modules containing them are
221 deleted *last* of all, they would come too late in the normal
222 destruction order. Sigh. */
223
224 value = PyDict_GetItemString(modules, "__builtin__");
225 if (value != NULL && PyModule_Check(value)) {
226 dict = PyModule_GetDict(value);
227 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000228 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000229 PyDict_SetItemString(dict, "_", Py_None);
230 }
231 value = PyDict_GetItemString(modules, "sys");
232 if (value != NULL && PyModule_Check(value)) {
233 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000234 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000235 dict = PyModule_GetDict(value);
236 for (p = sys_deletes; *p != NULL; p++) {
237 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000238 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000239 PyDict_SetItemString(dict, *p, Py_None);
240 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000241 for (p = sys_files; *p != NULL; p+=2) {
242 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000243 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000244 v = PyDict_GetItemString(dict, *(p+1));
245 if (v == NULL)
246 v = Py_None;
247 PyDict_SetItemString(dict, *p, v);
248 }
249 }
250
251 /* First, delete __main__ */
252 value = PyDict_GetItemString(modules, "__main__");
253 if (value != NULL && PyModule_Check(value)) {
254 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000255 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000256 _PyModule_Clear(value);
257 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000258 }
259
Guido van Rossum758eec01998-01-19 21:58:26 +0000260 /* The special treatment of __builtin__ here is because even
261 when it's not referenced as a module, its dictionary is
262 referenced by almost every module's __builtins__. Since
263 deleting a module clears its dictionary (even if there are
264 references left to it), we need to delete the __builtin__
265 module last. Likewise, we don't delete sys until the very
266 end because it is implicitly referenced (e.g. by print).
267
268 Also note that we 'delete' modules by replacing their entry
269 in the modules dict with None, rather than really deleting
270 them; this avoids a rehash of the modules dictionary and
271 also marks them as "non existent" so they won't be
272 re-imported. */
273
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000274 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000275 one (skipping __builtin__ and sys) and delete them */
276 do {
277 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000278 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000279 while (PyDict_Next(modules, &pos, &key, &value)) {
280 if (value->ob_refcnt != 1)
281 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000282 if (PyString_Check(key) && PyModule_Check(value)) {
283 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000284 if (strcmp(name, "__builtin__") == 0)
285 continue;
286 if (strcmp(name, "sys") == 0)
287 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000288 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000289 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000290 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000291 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000292 PyDict_SetItem(modules, key, Py_None);
293 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000294 }
295 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000296 } while (ndone > 0);
297
Guido van Rossum758eec01998-01-19 21:58:26 +0000298 /* Next, delete all modules (still skipping __builtin__ and sys) */
299 pos = 0;
300 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000301 if (PyString_Check(key) && PyModule_Check(value)) {
302 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000303 if (strcmp(name, "__builtin__") == 0)
304 continue;
305 if (strcmp(name, "sys") == 0)
306 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000307 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000308 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000309 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000310 PyDict_SetItem(modules, key, Py_None);
311 }
312 }
313
314 /* Next, delete sys and __builtin__ (in that order) */
315 value = PyDict_GetItemString(modules, "sys");
316 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000317 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000318 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000319 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000320 PyDict_SetItemString(modules, "sys", Py_None);
321 }
322 value = PyDict_GetItemString(modules, "__builtin__");
323 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000324 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000325 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000326 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000327 PyDict_SetItemString(modules, "__builtin__", Py_None);
328 }
329
330 /* Finally, clear and delete the modules directory */
331 PyDict_Clear(modules);
332 interp->modules = NULL;
333 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000334}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000335
336
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000337/* Helper for pythonrun.c -- return magic number */
338
339long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000341{
Guido van Rossum96774c12000-05-01 20:19:08 +0000342 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000343}
344
345
Guido van Rossum25ce5661997-08-02 03:10:38 +0000346/* Magic for extension modules (built-in as well as dynamically
347 loaded). To prevent initializing an extension module more than
348 once, we keep a static dictionary 'extensions' keyed by module name
349 (for built-in modules) or by filename (for dynamically loaded
350 modules), containing these modules. A copy od the module's
351 dictionary is stored by calling _PyImport_FixupExtension()
352 immediately after the module initialization function succeeds. A
353 copy can be retrieved from there by calling
354 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000355
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000358{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000359 PyObject *modules, *mod, *dict, *copy;
360 if (extensions == NULL) {
361 extensions = PyDict_New();
362 if (extensions == NULL)
363 return NULL;
364 }
365 modules = PyImport_GetModuleDict();
366 mod = PyDict_GetItemString(modules, name);
367 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000368 PyErr_Format(PyExc_SystemError,
369 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000370 return NULL;
371 }
372 dict = PyModule_GetDict(mod);
373 if (dict == NULL)
374 return NULL;
375 copy = PyObject_CallMethod(dict, "copy", "");
376 if (copy == NULL)
377 return NULL;
378 PyDict_SetItemString(extensions, filename, copy);
379 Py_DECREF(copy);
380 return copy;
381}
382
383PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000385{
386 PyObject *dict, *mod, *mdict, *result;
387 if (extensions == NULL)
388 return NULL;
389 dict = PyDict_GetItemString(extensions, filename);
390 if (dict == NULL)
391 return NULL;
392 mod = PyImport_AddModule(name);
393 if (mod == NULL)
394 return NULL;
395 mdict = PyModule_GetDict(mod);
396 if (mdict == NULL)
397 return NULL;
398 result = PyObject_CallMethod(mdict, "update", "O", dict);
399 if (result == NULL)
400 return NULL;
401 Py_DECREF(result);
402 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000403 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404 name, filename);
405 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000406}
407
408
409/* Get the module object corresponding to a module name.
410 First check the modules dictionary if there's one there,
411 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000412 Because the former action is most common, THIS DOES NOT RETURN A
413 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000414
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417{
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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000441PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000442{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000443 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
444}
445
446PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000447PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000448{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000449 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453 if (m == NULL)
454 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 d = PyModule_GetDict(m);
456 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
457 if (PyDict_SetItemString(d, "__builtins__",
458 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000459 return NULL;
460 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000461 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000462 v = NULL;
463 if (pathname != NULL) {
464 v = PyString_FromString(pathname);
465 if (v == NULL)
466 PyErr_Clear();
467 }
468 if (v == NULL) {
469 v = ((PyCodeObject *)co)->co_filename;
470 Py_INCREF(v);
471 }
472 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000473 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000474 Py_DECREF(v);
475
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000476 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477 if (v == NULL)
478 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000479 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000480
Guido van Rossum25ce5661997-08-02 03:10:38 +0000481 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000482 PyErr_Format(PyExc_ImportError,
483 "Loaded module %.200s not found in sys.modules",
484 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000485 return NULL;
486 }
487
Guido van Rossum79f25d91997-04-29 20:08:16 +0000488 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489
490 return m;
491}
492
493
494/* Given a pathname for a Python source file, fill a buffer with the
495 pathname for the corresponding compiled file. Return the pathname
496 for the compiled file, or NULL if there's no space in the buffer.
497 Doesn't set an exception. */
498
499static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000500make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000501{
Fred Drake4c82b232000-06-30 16:18:57 +0000502 size_t len;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503
504 len = strlen(pathname);
505 if (len+2 > buflen)
506 return NULL;
507 strcpy(buf, pathname);
Guido van Rossum0824f631997-03-11 18:37:35 +0000508 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000509
510 return buf;
511}
512
513
514/* Given a pathname for a Python source file, its time of last
515 modification, and a pathname for a compiled file, check whether the
516 compiled file represents the same version of the source. If so,
517 return a FILE pointer for the compiled file, positioned just after
518 the header; if not, return NULL.
519 Doesn't set an exception. */
520
521static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523{
524 FILE *fp;
525 long magic;
526 long pyc_mtime;
527
528 fp = fopen(cpathname, "rb");
529 if (fp == NULL)
530 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000531 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000532 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000534 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000535 fclose(fp);
536 return NULL;
537 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000541 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542 fclose(fp);
543 return NULL;
544 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000546 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547 return fp;
548}
549
550
551/* Read a code object from a file and check it for validity */
552
Guido van Rossum79f25d91997-04-29 20:08:16 +0000553static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000554read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557
Tim Petersd9b9ac82001-01-28 00:27:39 +0000558 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000560 if (co == NULL || !PyCode_Check(co)) {
561 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000562 PyErr_Format(PyExc_ImportError,
563 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565 return NULL;
566 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568}
569
570
571/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000572 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573
Guido van Rossum79f25d91997-04-29 20:08:16 +0000574static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000575load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576{
577 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 PyCodeObject *co;
579 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000580
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000582 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000583 PyErr_Format(PyExc_ImportError,
584 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585 return NULL;
586 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000587 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000588 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589 if (co == NULL)
590 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000592 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000594 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596
597 return m;
598}
599
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600/* Parse a source file and return the corresponding code object */
601
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000603parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606 node *n;
607
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000608 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 if (n == NULL)
610 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 co = PyNode_Compile(n, pathname);
612 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613
614 return co;
615}
616
617
Guido van Rossum55a83382000-09-20 20:31:38 +0000618/* Helper to open a bytecode file for writing in exclusive mode */
619
620static FILE *
621open_exclusive(char *filename)
622{
623#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
624 /* Use O_EXCL to avoid a race condition when another process tries to
625 write the same file. When that happens, our open() call fails,
626 which is just fine (since it's only a cache).
627 XXX If the file exists and is writable but the directory is not
628 writable, the file will never be written. Oh well.
629 */
630 int fd;
631 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000632 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
633#ifdef O_BINARY
634 |O_BINARY /* necessary for Windows */
635#endif
Tim Peters50d8d372001-02-28 05:34:27 +0000636
Tim Peters42c83af2000-09-29 04:03:10 +0000637 , 0666);
Guido van Rossum55a83382000-09-20 20:31:38 +0000638 if (fd < 0)
639 return NULL;
640 return fdopen(fd, "wb");
641#else
642 /* Best we can do -- on Windows this can't happen anyway */
643 return fopen(filename, "wb");
644#endif
645}
646
647
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648/* Write a compiled module to a file, placing the time of last
649 modification of its source into the header.
650 Errors are ignored, if a write error occurs an attempt is made to
651 remove the file. */
652
653static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655{
656 FILE *fp;
657
Guido van Rossum55a83382000-09-20 20:31:38 +0000658 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000661 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662 "# can't create %s\n", cpathname);
663 return;
664 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000665 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyMarshal_WriteLongToFile(0L, fp);
668 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000671 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000672 /* Don't keep partial file */
673 fclose(fp);
674 (void) unlink(cpathname);
675 return;
676 }
677 /* Now write the true mtime */
678 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680 fflush(fp);
681 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000683 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684#ifdef macintosh
Jack Jansencbf630f2000-07-11 21:59:16 +0000685 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686#endif
687}
688
689
690/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000691 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
692 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000695load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696{
Fred Drake4c82b232000-06-30 16:18:57 +0000697 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 FILE *fpc;
699 char buf[MAXPATHLEN+1];
700 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 PyCodeObject *co;
702 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000704 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drake4c82b232000-06-30 16:18:57 +0000705 if (mtime == -1)
706 return NULL;
707#if SIZEOF_TIME_T > 4
708 /* Python's .pyc timestamp handling presumes that the timestamp fits
709 in 4 bytes. This will be fine until sometime in the year 2038,
710 when a 4-byte signed time_t will overflow.
711 */
712 if (mtime >> 32) {
713 PyErr_SetString(PyExc_OverflowError,
714 "modification time overflows a 4 bytes");
715 return NULL;
716 }
717#endif
718 cpathname = make_compiled_pathname(pathname, buf, (size_t)MAXPATHLEN+1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719 if (cpathname != NULL &&
720 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000721 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722 fclose(fpc);
723 if (co == NULL)
724 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000726 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000727 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000728 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729 }
730 else {
731 co = parse_source_module(pathname, fp);
732 if (co == NULL)
733 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000735 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736 name, pathname);
737 write_compiled_module(co, cpathname, mtime);
738 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000739 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741
742 return m;
743}
744
745
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000746/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000747static PyObject *load_module(char *, FILE *, char *, int);
748static struct filedescr *find_module(char *, PyObject *,
749 char *, size_t, FILE **);
750static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000751
752/* Load a package and return its module object WITH INCREMENTED
753 REFERENCE COUNT */
754
755static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000757{
758 PyObject *m, *d, *file, *path;
759 int err;
760 char buf[MAXPATHLEN+1];
761 FILE *fp = NULL;
762 struct filedescr *fdp;
763
764 m = PyImport_AddModule(name);
765 if (m == NULL)
766 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000767 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000768 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000769 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000770 d = PyModule_GetDict(m);
771 file = PyString_FromString(pathname);
772 if (file == NULL)
773 return NULL;
774 path = Py_BuildValue("[O]", file);
775 if (path == NULL) {
776 Py_DECREF(file);
777 return NULL;
778 }
779 err = PyDict_SetItemString(d, "__file__", file);
780 if (err == 0)
781 err = PyDict_SetItemString(d, "__path__", path);
782 if (err != 0) {
783 m = NULL;
784 goto cleanup;
785 }
786 buf[0] = '\0';
787 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
788 if (fdp == NULL) {
789 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
790 PyErr_Clear();
791 }
792 else
793 m = NULL;
794 goto cleanup;
795 }
796 m = load_module(name, fp, buf, fdp->type);
797 if (fp != NULL)
798 fclose(fp);
799 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000800 Py_XDECREF(path);
801 Py_XDECREF(file);
802 return m;
803}
804
805
806/* Helper to test for built-in module */
807
808static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000809is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000810{
811 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000812 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
813 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
814 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000815 return -1;
816 else
817 return 1;
818 }
819 }
820 return 0;
821}
822
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000823
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824/* Search the path (default sys.path) for a module. Return the
825 corresponding filedescr struct, and (via return arguments) the
826 pathname and an open file. Return NULL if the module is not found. */
827
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000828#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +0000829extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
830 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000831#endif
832
Tim Peters50d8d372001-02-28 05:34:27 +0000833static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000834static int find_init_module(char *); /* Forward */
Guido van Rossum197346f1997-10-31 18:38:52 +0000835
Tim Peters50d8d372001-02-28 05:34:27 +0000836#if 0 /* XXX was #ifdef HAVE_DIRENT_H; resolve whether we really need this */
Barry Warsaw914a0b12001-02-02 19:12:16 +0000837
838static int MatchFilename(char *pathname, char *filename);
839
840#include <sys/types.h>
841#include <dirent.h>
842
843static int MatchFilename(char *pathname, char *filename)
844{
845 DIR *dirp;
846 struct dirent *dp;
847 int len = strlen(filename);
848
849 if ((pathname == NULL) || (strlen(pathname) == 0))
850 pathname = ".";
851 dirp = opendir(pathname);
852 if (dirp) {
853 while ((dp = readdir(dirp)) != NULL) {
854#ifdef _DIRENT_HAVE_D_NAMELINE
855 int namelen = dp->d_namlen;
856#else /* !_DIRENT_HAVE_D_NAMELINE */
857 int namelen = strlen(dp->d_name);
858#endif /* _DIRENT_HAVE_D_NAMELINE */
859 if (namelen == len && !strcmp(dp->d_name, filename)) {
860 (void)closedir(dirp);
861 return 1; /* Found */
862 }
863 }
864 }
865 (void)closedir(dirp);
866 return 0 ; /* Not found */
867}
868#endif /* HAVE_DIRENT_H */
869
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000870static struct filedescr *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000871find_module(char *realname, PyObject *path, char *buf, size_t buflen,
872 FILE **p_fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873{
Fred Drake4c82b232000-06-30 16:18:57 +0000874 int i, npath;
875 size_t len, namelen;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000876 struct _frozen *f;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000877 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000878 FILE *fp = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000879 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000880 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
881 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
882 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +0000883 char name[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000884
Fred Drake4c82b232000-06-30 16:18:57 +0000885 if (strlen(realname) > MAXPATHLEN) {
886 PyErr_SetString(PyExc_OverflowError, "module name is too long");
887 return NULL;
888 }
Guido van Rossum0506a431998-08-11 15:07:39 +0000889 strcpy(name, realname);
890
891 if (path != NULL && PyString_Check(path)) {
892 /* Submodule of "frozen" package:
893 Set name to the fullname, path to NULL
894 and continue as "usual" */
895 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
896 PyErr_SetString(PyExc_ImportError,
897 "full frozen module name too long");
898 return NULL;
899 }
900 strcpy(buf, PyString_AsString(path));
901 strcat(buf, ".");
902 strcat(buf, name);
903 strcpy(name, buf);
904 path = NULL;
905 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000906 if (path == NULL) {
907 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000908 strcpy(buf, name);
909 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000910 }
Guido van Rossuma5568d31998-03-05 03:45:08 +0000911 if ((f = find_frozen(name)) != NULL) {
912 strcpy(buf, name);
913 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000914 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000915
Guido van Rossumac279101996-08-22 23:10:58 +0000916#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000917 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
918 if (fp != NULL) {
919 *p_fp = fp;
920 return fdp;
921 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000922#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000923 path = PySys_GetObject("path");
924 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000925 if (path == NULL || !PyList_Check(path)) {
926 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +0000927 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000928 return NULL;
929 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931 namelen = strlen(name);
932 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 PyObject *v = PyList_GetItem(path, i);
934 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000935 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936 len = PyString_Size(v);
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000937 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000938 continue; /* Too long */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939 strcpy(buf, PyString_AsString(v));
Fred Drake4c82b232000-06-30 16:18:57 +0000940 if (strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000941 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000942#ifdef macintosh
Guido van Rossum741689d1997-08-12 14:53:39 +0000943#ifdef INTERN_STRINGS
Tim Peters50d8d372001-02-28 05:34:27 +0000944 /*
Guido van Rossum741689d1997-08-12 14:53:39 +0000945 ** Speedup: each sys.path item is interned, and
946 ** FindResourceModule remembers which items refer to
947 ** folders (so we don't have to bother trying to look
Tim Peters50d8d372001-02-28 05:34:27 +0000948 ** into them for resources).
Guido van Rossum741689d1997-08-12 14:53:39 +0000949 */
950 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
951 v = PyList_GET_ITEM(path, i);
952#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000953 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 static struct filedescr resfiledescr =
955 {"", "", PY_RESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +0000956
Jack Jansen9c96a921995-02-15 22:57:06 +0000957 return &resfiledescr;
958 }
Guido van Rossum0f84a341998-08-06 13:36:01 +0000959 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
960 static struct filedescr resfiledescr =
961 {"", "", PY_CODERESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +0000962
Guido van Rossum0f84a341998-08-06 13:36:01 +0000963 return &resfiledescr;
964 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000965#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000966 if (len > 0 && buf[len-1] != SEP
967#ifdef ALTSEP
968 && buf[len-1] != ALTSEP
969#endif
970 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000971 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +0000972 strcpy(buf+len, name);
973 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +0000974
975 /* Check for package import (buf holds a directory name,
976 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000977#ifdef HAVE_STAT
Tim Peters50d8d372001-02-28 05:34:27 +0000978 if (stat(buf, &statbuf) == 0 &&
979 S_ISDIR(statbuf.st_mode) &&
980 find_init_module(buf)) {
981 if (case_ok(buf, len, namelen, name))
982 return &fd_package;
983 else
984 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000985 }
986#else
987 /* XXX How are you going to test for directories? */
988#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +0000989#ifdef macintosh
990 fdp = PyMac_FindModuleExtension(buf, &len, name);
991 if (fdp)
992 fp = fopen(buf, fdp->mode);
993#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000994 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000997 PySys_WriteStderr("# trying %s\n", buf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000998 fp = fopen(buf, fdp->mode);
Tim Peters50d8d372001-02-28 05:34:27 +0000999 if (fp != NULL) {
1000 if (case_ok(buf, len, namelen, name))
1001 break;
1002 else { /* continue search */
1003 fclose(fp);
1004 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001005 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001006 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007 }
Guido van Rossum741689d1997-08-12 14:53:39 +00001008#endif /* !macintosh */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009 if (fp != NULL)
1010 break;
1011 }
1012 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001013 PyErr_Format(PyExc_ImportError,
1014 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 return NULL;
1016 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001017 *p_fp = fp;
1018 return fdp;
1019}
1020
Tim Peters50d8d372001-02-28 05:34:27 +00001021/* case_ok(buf, len, namelen, name)
1022 * We've already done a successful stat() or fopen() on buf (a path of length
1023 * len; can not assume there's a trailing null). name is the last component
1024 * of then path (a string of length namelen, exclusive of trailing null).
1025 * case_ok() is to return 1 if there's a case-sensitive match for
1026 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1027 * exists.
1028 * case_ok() is used to implement case-sensitive import semantics even
1029 * on platforms with case-insensitive filesystems. It's trivial to implement
1030 * for case-sensitive filesystems. It's pretty much a cross-platform
1031 * nightmare for systems with case-insensitive filesystems.
1032 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001033
Tim Peters50d8d372001-02-28 05:34:27 +00001034/* First we may need a pile of platform-specific header files; the sequence
1035 * of #if's here should match the sequence in the body of case_ok().
1036 */
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001037#if defined(MS_WIN32) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001038#include <windows.h>
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001039#include <ctype.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001040#ifdef __CYGWIN__
1041#include <sys/cygwin.h>
1042#endif
1043
Tim Peters50d8d372001-02-28 05:34:27 +00001044#elif defined(DJGPP)
1045#include <dir.h>
1046
1047#elif defined(macintosh)
1048#include <TextUtils.h>
1049#ifdef USE_GUSI1
1050#include "TFileSpec.h" /* for Path2FSSpec() */
1051#endif
1052
1053#endif
1054
Guido van Rossum0980bd91998-02-13 17:18:36 +00001055static int
Tim Peters50d8d372001-02-28 05:34:27 +00001056case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001057{
Tim Peters50d8d372001-02-28 05:34:27 +00001058/* Pick a platform-specific implementation; the sequence of #if's here should
1059 * match the sequence just above.
1060 */
1061
1062/* MS_WIN32 || __CYGWIN__ */
1063#if defined(MS_WIN32) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001064 WIN32_FIND_DATA data;
1065 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001066#ifdef __CYGWIN__
1067 char tempbuf[MAX_PATH];
1068#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001069
Guido van Rossum0980bd91998-02-13 17:18:36 +00001070 if (getenv("PYTHONCASEOK") != NULL)
1071 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001072
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001073#ifdef __CYGWIN__
1074 cygwin32_conv_to_win32_path(buf, tempbuf);
1075 h = FindFirstFile(tempbuf, &data);
1076#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001077 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001078#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001079 if (h == INVALID_HANDLE_VALUE) {
1080 PyErr_Format(PyExc_NameError,
1081 "Can't find file for module %.100s\n(filename %.300s)",
1082 name, buf);
1083 return 0;
1084 }
1085 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001086 return strncmp(data.cFileName, name, namelen) == 0;
1087
1088/* DJGPP */
1089#elif defined(DJGPP)
1090 struct ffblk ffblk;
1091 int done;
1092
1093 if (getenv("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001094 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001095
1096 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1097 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001098 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001099 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001100 name, buf);
1101 return 0;
1102 }
Tim Peters50d8d372001-02-28 05:34:27 +00001103 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001104
Tim Peters50d8d372001-02-28 05:34:27 +00001105/* macintosh */
1106#elif defined(macintosh)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001107 FSSpec fss;
1108 OSErr err;
Tim Peters50d8d372001-02-28 05:34:27 +00001109
1110 if (getenv("PYTHONCASEOK") != NULL)
1111 return 1;
1112
Guido van Rossumb33aa1a2000-04-24 15:08:18 +00001113#ifndef USE_GUSI1
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001114 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1115#else
1116 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1117 the way, which is fine for all directories, but here we need
1118 the original name of the alias file (say, Dlg.ppc.slb, not
1119 toolboxmodules.ppc.slb). */
1120 char *colon;
1121 err = Path2FSSpec(buf, &fss);
1122 if (err == noErr) {
1123 colon = strrchr(buf, ':'); /* find filename */
1124 if (colon != NULL)
1125 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1126 Pstring(colon+1), &fss);
1127 else
1128 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1129 fss.name, &fss);
1130 }
1131#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001132 if (err) {
1133 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001134 "Can't find file for module %.100s\n(filename %.300s)",
1135 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001136 return 0;
1137 }
Tim Peters50d8d372001-02-28 05:34:27 +00001138 return fss.name[0] >= namelen &&
1139 strncmp(name, (char *)fss.name+1, namelen) == 0;
1140
1141/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1142#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001143 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001144
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001145#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001146}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001147
Guido van Rossum0980bd91998-02-13 17:18:36 +00001148
Guido van Rossum197346f1997-10-31 18:38:52 +00001149#ifdef HAVE_STAT
1150/* Helper to look for __init__.py or __init__.py[co] in potential package */
1151static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001152find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001153{
Fred Drake4c82b232000-06-30 16:18:57 +00001154 size_t save_len = strlen(buf);
1155 size_t i = save_len;
Guido van Rossum197346f1997-10-31 18:38:52 +00001156 struct stat statbuf;
1157
1158 if (save_len + 13 >= MAXPATHLEN)
1159 return 0;
1160 buf[i++] = SEP;
1161 strcpy(buf+i, "__init__.py");
1162 if (stat(buf, &statbuf) == 0) {
1163 buf[save_len] = '\0';
1164 return 1;
1165 }
1166 i += strlen(buf+i);
1167 if (Py_OptimizeFlag)
1168 strcpy(buf+i, "o");
1169 else
1170 strcpy(buf+i, "c");
1171 if (stat(buf, &statbuf) == 0) {
1172 buf[save_len] = '\0';
1173 return 1;
1174 }
1175 buf[save_len] = '\0';
1176 return 0;
1177}
1178#endif /* HAVE_STAT */
1179
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180
Tim Petersdbd9ba62000-07-09 03:09:57 +00001181static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001182
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001184 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001185
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001187load_module(char *name, FILE *fp, char *buf, int type)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001188{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001189 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001191 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001192
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001193 /* First check that there's an open file (if we need one) */
1194 switch (type) {
1195 case PY_SOURCE:
1196 case PY_COMPILED:
1197 if (fp == NULL) {
1198 PyErr_Format(PyExc_ValueError,
1199 "file object required for import (type code %d)",
1200 type);
1201 return NULL;
1202 }
1203 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001204
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001205 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001206
1207 case PY_SOURCE:
1208 m = load_source_module(name, buf, fp);
1209 break;
1210
1211 case PY_COMPILED:
1212 m = load_compiled_module(name, buf, fp);
1213 break;
1214
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001215#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001216 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001218 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001219#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001220
Jack Jansen9c96a921995-02-15 22:57:06 +00001221#ifdef macintosh
1222 case PY_RESOURCE:
1223 m = PyMac_LoadResourceModule(name, buf);
1224 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001225 case PY_CODERESOURCE:
1226 m = PyMac_LoadCodeResourceModule(name, buf);
1227 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001228#endif
1229
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001230 case PKG_DIRECTORY:
1231 m = load_package(name, buf);
1232 break;
1233
1234 case C_BUILTIN:
1235 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001236 if (buf != NULL && buf[0] != '\0')
1237 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001238 if (type == C_BUILTIN)
1239 err = init_builtin(name);
1240 else
1241 err = PyImport_ImportFrozenModule(name);
1242 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001243 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001244 if (err == 0) {
1245 PyErr_Format(PyExc_ImportError,
1246 "Purported %s module %.200s not found",
1247 type == C_BUILTIN ?
1248 "builtin" : "frozen",
1249 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001250 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001251 }
1252 modules = PyImport_GetModuleDict();
1253 m = PyDict_GetItemString(modules, name);
1254 if (m == NULL) {
1255 PyErr_Format(
1256 PyExc_ImportError,
1257 "%s module %.200s not properly initialized",
1258 type == C_BUILTIN ?
1259 "builtin" : "frozen",
1260 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001261 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001262 }
1263 Py_INCREF(m);
1264 break;
1265
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001266 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001267 PyErr_Format(PyExc_ImportError,
1268 "Don't know how to import %.200s (type code %d)",
1269 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001270 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001271
1272 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001273
1274 return m;
1275}
1276
1277
1278/* Initialize a built-in module.
1279 Return 1 for succes, 0 if the module is not found, and -1 with
1280 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001281
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001282static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001283init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001284{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001285 struct _inittab *p;
1286 PyObject *mod;
1287
1288 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1289 return 1;
1290
Guido van Rossum771c6c81997-10-31 18:37:24 +00001291 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001292 if (strcmp(name, p->name) == 0) {
1293 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001294 PyErr_Format(PyExc_ImportError,
1295 "Cannot re-init internal module %.200s",
1296 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001297 return -1;
1298 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001299 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001300 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001301 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001302 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001303 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001304 if (_PyImport_FixupExtension(name, name) == NULL)
1305 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001306 return 1;
1307 }
1308 }
1309 return 0;
1310}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001311
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001312
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001313/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001314
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001315static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001316find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001317{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001318 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001319
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001321 if (p->name == NULL)
1322 return NULL;
1323 if (strcmp(p->name, name) == 0)
1324 break;
1325 }
1326 return p;
1327}
1328
Guido van Rossum79f25d91997-04-29 20:08:16 +00001329static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001330get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001331{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001332 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001333 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001334
1335 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001336 PyErr_Format(PyExc_ImportError,
1337 "No such frozen object named %.200s",
1338 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001339 return NULL;
1340 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001341 size = p->size;
1342 if (size < 0)
1343 size = -size;
1344 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001345}
1346
1347/* Initialize a frozen module.
1348 Return 1 for succes, 0 if the module is not found, and -1 with
1349 an exception set if the initialization failed.
1350 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001351
1352int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001353PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001354{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001355 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356 PyObject *co;
1357 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001358 int ispackage;
1359 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001360
1361 if (p == NULL)
1362 return 0;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001363 size = p->size;
1364 ispackage = (size < 0);
1365 if (ispackage)
1366 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001368 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001369 name, ispackage ? " package" : "");
1370 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001371 if (co == NULL)
1372 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373 if (!PyCode_Check(co)) {
1374 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001375 PyErr_Format(PyExc_TypeError,
1376 "frozen object %.200s is not a code object",
1377 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001378 return -1;
1379 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001380 if (ispackage) {
1381 /* Set __path__ to the package name */
1382 PyObject *d, *s;
1383 int err;
1384 m = PyImport_AddModule(name);
1385 if (m == NULL)
1386 return -1;
1387 d = PyModule_GetDict(m);
1388 s = PyString_InternFromString(name);
1389 if (s == NULL)
1390 return -1;
1391 err = PyDict_SetItemString(d, "__path__", s);
1392 Py_DECREF(s);
1393 if (err != 0)
1394 return err;
1395 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001396 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001398 if (m == NULL)
1399 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001401 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001402}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001403
1404
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001405/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001406 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001407
Guido van Rossum79f25d91997-04-29 20:08:16 +00001408PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001409PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001410{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001411 PyObject *pname;
1412 PyObject *result;
1413
1414 pname = PyString_FromString(name);
1415 result = PyImport_Import(pname);
1416 Py_DECREF(pname);
1417 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001418}
1419
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001420/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001421static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1422static PyObject *load_next(PyObject *mod, PyObject *altmod,
1423 char **p_name, char *buf, int *p_buflen);
1424static int mark_miss(char *name);
1425static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1426 char *buf, int buflen, int recursive);
1427static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001428
1429/* The Magnum Opus of dotted-name import :-) */
1430
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001431static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001432import_module_ex(char *name, PyObject *globals, PyObject *locals,
1433 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001434{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001435 char buf[MAXPATHLEN+1];
1436 int buflen = 0;
1437 PyObject *parent, *head, *next, *tail;
1438
1439 parent = get_parent(globals, buf, &buflen);
1440 if (parent == NULL)
1441 return NULL;
1442
1443 head = load_next(parent, Py_None, &name, buf, &buflen);
1444 if (head == NULL)
1445 return NULL;
1446
1447 tail = head;
1448 Py_INCREF(tail);
1449 while (name) {
1450 next = load_next(tail, tail, &name, buf, &buflen);
1451 Py_DECREF(tail);
1452 if (next == NULL) {
1453 Py_DECREF(head);
1454 return NULL;
1455 }
1456 tail = next;
1457 }
1458
1459 if (fromlist != NULL) {
1460 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1461 fromlist = NULL;
1462 }
1463
1464 if (fromlist == NULL) {
1465 Py_DECREF(tail);
1466 return head;
1467 }
1468
1469 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001470 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001471 Py_DECREF(tail);
1472 return NULL;
1473 }
1474
1475 return tail;
1476}
1477
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001478PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001479PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1480 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001481{
1482 PyObject *result;
1483 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001484 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001485 unlock_import();
1486 return result;
1487}
1488
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001489static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001490get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001491{
1492 static PyObject *namestr = NULL;
1493 static PyObject *pathstr = NULL;
1494 PyObject *modname, *modpath, *modules, *parent;
1495
1496 if (globals == NULL || !PyDict_Check(globals))
1497 return Py_None;
1498
1499 if (namestr == NULL) {
1500 namestr = PyString_InternFromString("__name__");
1501 if (namestr == NULL)
1502 return NULL;
1503 }
1504 if (pathstr == NULL) {
1505 pathstr = PyString_InternFromString("__path__");
1506 if (pathstr == NULL)
1507 return NULL;
1508 }
1509
1510 *buf = '\0';
1511 *p_buflen = 0;
1512 modname = PyDict_GetItem(globals, namestr);
1513 if (modname == NULL || !PyString_Check(modname))
1514 return Py_None;
1515
1516 modpath = PyDict_GetItem(globals, pathstr);
1517 if (modpath != NULL) {
1518 int len = PyString_GET_SIZE(modname);
1519 if (len > MAXPATHLEN) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "Module name too long");
1522 return NULL;
1523 }
1524 strcpy(buf, PyString_AS_STRING(modname));
1525 *p_buflen = len;
1526 }
1527 else {
1528 char *start = PyString_AS_STRING(modname);
1529 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001530 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001531 if (lastdot == NULL)
1532 return Py_None;
1533 len = lastdot - start;
1534 if (len >= MAXPATHLEN) {
1535 PyErr_SetString(PyExc_ValueError,
1536 "Module name too long");
1537 return NULL;
1538 }
1539 strncpy(buf, start, len);
1540 buf[len] = '\0';
1541 *p_buflen = len;
1542 }
1543
1544 modules = PyImport_GetModuleDict();
1545 parent = PyDict_GetItemString(modules, buf);
1546 if (parent == NULL)
1547 parent = Py_None;
1548 return parent;
1549 /* We expect, but can't guarantee, if parent != None, that:
1550 - parent.__name__ == buf
1551 - parent.__dict__ is globals
1552 If this is violated... Who cares? */
1553}
1554
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001555/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001556static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001557load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1558 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001559{
1560 char *name = *p_name;
1561 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001562 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001563 char *p;
1564 PyObject *result;
1565
1566 if (dot == NULL) {
1567 *p_name = NULL;
1568 len = strlen(name);
1569 }
1570 else {
1571 *p_name = dot+1;
1572 len = dot-name;
1573 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00001574 if (len == 0) {
1575 PyErr_SetString(PyExc_ValueError,
1576 "Empty module name");
1577 return NULL;
1578 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001579
1580 p = buf + *p_buflen;
1581 if (p != buf)
1582 *p++ = '.';
1583 if (p+len-buf >= MAXPATHLEN) {
1584 PyErr_SetString(PyExc_ValueError,
1585 "Module name too long");
1586 return NULL;
1587 }
1588 strncpy(p, name, len);
1589 p[len] = '\0';
1590 *p_buflen = p+len-buf;
1591
1592 result = import_submodule(mod, p, buf);
1593 if (result == Py_None && altmod != mod) {
1594 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001595 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00001596 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001597 if (result != NULL && result != Py_None) {
1598 if (mark_miss(buf) != 0) {
1599 Py_DECREF(result);
1600 return NULL;
1601 }
1602 strncpy(buf, name, len);
1603 buf[len] = '\0';
1604 *p_buflen = len;
1605 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001606 }
1607 if (result == NULL)
1608 return NULL;
1609
1610 if (result == Py_None) {
1611 Py_DECREF(result);
1612 PyErr_Format(PyExc_ImportError,
1613 "No module named %.200s", name);
1614 return NULL;
1615 }
1616
1617 return result;
1618}
1619
1620static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001621mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001622{
1623 PyObject *modules = PyImport_GetModuleDict();
1624 return PyDict_SetItemString(modules, name, Py_None);
1625}
1626
1627static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001628ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1629 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001630{
1631 int i;
1632
1633 if (!PyObject_HasAttrString(mod, "__path__"))
1634 return 1;
1635
1636 for (i = 0; ; i++) {
1637 PyObject *item = PySequence_GetItem(fromlist, i);
1638 int hasit;
1639 if (item == NULL) {
1640 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1641 PyErr_Clear();
1642 return 1;
1643 }
1644 return 0;
1645 }
1646 if (!PyString_Check(item)) {
1647 PyErr_SetString(PyExc_TypeError,
1648 "Item in ``from list'' not a string");
1649 Py_DECREF(item);
1650 return 0;
1651 }
1652 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00001653 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001654 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001655 /* See if the package defines __all__ */
1656 if (recursive)
1657 continue; /* Avoid endless recursion */
1658 all = PyObject_GetAttrString(mod, "__all__");
1659 if (all == NULL)
1660 PyErr_Clear();
1661 else {
1662 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1663 return 0;
1664 Py_DECREF(all);
1665 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001666 continue;
1667 }
1668 hasit = PyObject_HasAttr(mod, item);
1669 if (!hasit) {
1670 char *subname = PyString_AS_STRING(item);
1671 PyObject *submod;
1672 char *p;
1673 if (buflen + strlen(subname) >= MAXPATHLEN) {
1674 PyErr_SetString(PyExc_ValueError,
1675 "Module name too long");
1676 Py_DECREF(item);
1677 return 0;
1678 }
1679 p = buf + buflen;
1680 *p++ = '.';
1681 strcpy(p, subname);
1682 submod = import_submodule(mod, subname, buf);
1683 Py_XDECREF(submod);
1684 if (submod == NULL) {
1685 Py_DECREF(item);
1686 return 0;
1687 }
1688 }
1689 Py_DECREF(item);
1690 }
1691
Guido van Rossuma7f2e811997-10-03 15:33:32 +00001692 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001693}
1694
1695static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001696import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001697{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001698 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001699 PyObject *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001700
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001701 /* Require:
1702 if mod == None: subname == fullname
1703 else: mod.__name__ + "." + subname == fullname
1704 */
1705
Tim Peters50d8d372001-02-28 05:34:27 +00001706 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001708 }
1709 else {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001710 PyObject *path;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001711 char buf[MAXPATHLEN+1];
1712 struct filedescr *fdp;
1713 FILE *fp = NULL;
1714
Guido van Rossum9c0afe51998-05-19 15:09:05 +00001715 if (mod == Py_None)
1716 path = NULL;
1717 else {
1718 path = PyObject_GetAttrString(mod, "__path__");
1719 if (path == NULL) {
1720 PyErr_Clear();
1721 Py_INCREF(Py_None);
1722 return Py_None;
1723 }
1724 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001725
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001726 buf[0] = '\0';
Guido van Rossumb68cd421998-07-01 17:36:26 +00001727 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1728 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001729 if (fdp == NULL) {
1730 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1731 return NULL;
1732 PyErr_Clear();
1733 Py_INCREF(Py_None);
1734 return Py_None;
1735 }
1736 m = load_module(fullname, fp, buf, fdp->type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001737 if (fp)
1738 fclose(fp);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001739 if (m != NULL && mod != Py_None) {
1740 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1741 Py_DECREF(m);
1742 m = NULL;
1743 }
1744 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00001745 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001746
1747 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001748}
1749
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750
1751/* Re-import a module of any kind and return its module object, WITH
1752 INCREMENTED REFERENCE COUNT */
1753
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001755PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001756{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001757 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00001758 PyObject *path = NULL;
1759 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001760 char buf[MAXPATHLEN+1];
1761 struct filedescr *fdp;
1762 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 if (m == NULL || !PyModule_Check(m)) {
1765 PyErr_SetString(PyExc_TypeError,
1766 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001767 return NULL;
1768 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001770 if (name == NULL)
1771 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001772 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001773 PyErr_Format(PyExc_ImportError,
1774 "reload(): module %.200s not in sys.modules",
1775 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776 return NULL;
1777 }
Guido van Rossum222ef561997-09-06 19:41:09 +00001778 subname = strrchr(name, '.');
1779 if (subname == NULL)
1780 subname = name;
1781 else {
1782 PyObject *parentname, *parent;
1783 parentname = PyString_FromStringAndSize(name, (subname-name));
1784 if (parentname == NULL)
1785 return NULL;
1786 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00001787 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00001788 if (parent == NULL) {
1789 PyErr_Format(PyExc_ImportError,
1790 "reload(): parent %.200s not in sys.modules",
1791 name);
1792 return NULL;
1793 }
1794 subname++;
1795 path = PyObject_GetAttrString(parent, "__path__");
1796 if (path == NULL)
1797 PyErr_Clear();
1798 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001799 buf[0] = '\0';
Guido van Rossum222ef561997-09-06 19:41:09 +00001800 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1801 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001802 if (fdp == NULL)
1803 return NULL;
1804 m = load_module(name, fp, buf, fdp->type);
1805 if (fp)
1806 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807 return m;
1808}
1809
1810
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001811/* Higher-level import emulator which emulates the "import" statement
1812 more accurately -- it invokes the __import__() function from the
1813 builtins of the current globals. This means that the import is
1814 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00001815 environment, e.g. by "rexec".
1816 A dummy list ["__doc__"] is passed as the 4th argument so that
1817 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1818 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001819
1820PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001821PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001822{
1823 static PyObject *silly_list = NULL;
1824 static PyObject *builtins_str = NULL;
1825 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001826 PyObject *globals = NULL;
1827 PyObject *import = NULL;
1828 PyObject *builtins = NULL;
1829 PyObject *r = NULL;
1830
1831 /* Initialize constant string objects */
1832 if (silly_list == NULL) {
1833 import_str = PyString_InternFromString("__import__");
1834 if (import_str == NULL)
1835 return NULL;
1836 builtins_str = PyString_InternFromString("__builtins__");
1837 if (builtins_str == NULL)
1838 return NULL;
1839 silly_list = Py_BuildValue("[s]", "__doc__");
1840 if (silly_list == NULL)
1841 return NULL;
1842 }
1843
1844 /* Get the builtins from current globals */
1845 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00001846 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00001847 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001848 builtins = PyObject_GetItem(globals, builtins_str);
1849 if (builtins == NULL)
1850 goto err;
1851 }
1852 else {
1853 /* No globals -- use standard builtins, and fake globals */
1854 PyErr_Clear();
1855
Guido van Rossum85cd1d62001-02-20 21:43:24 +00001856 builtins = PyImport_ImportModuleEx("__builtin__",
1857 NULL, NULL, NULL);
1858 if (builtins == NULL)
1859 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001860 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1861 if (globals == NULL)
1862 goto err;
1863 }
1864
1865 /* Get the __import__ function from the builtins */
1866 if (PyDict_Check(builtins))
1867 import=PyObject_GetItem(builtins, import_str);
1868 else
1869 import=PyObject_GetAttr(builtins, import_str);
1870 if (import == NULL)
1871 goto err;
1872
1873 /* Call the _import__ function with the proper argument list */
1874 r = PyObject_CallFunction(import, "OOOO",
1875 module_name, globals, globals, silly_list);
1876
1877 err:
1878 Py_XDECREF(globals);
1879 Py_XDECREF(builtins);
1880 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001881
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001882 return r;
1883}
1884
1885
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001886/* Module 'imp' provides Python access to the primitives used for
1887 importing modules.
1888*/
1889
Guido van Rossum79f25d91997-04-29 20:08:16 +00001890static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001891imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001892{
1893 char buf[4];
1894
Guido van Rossum43713e52000-02-29 13:59:29 +00001895 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001896 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00001897 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1898 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1899 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1900 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001901
Guido van Rossum79f25d91997-04-29 20:08:16 +00001902 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001903}
1904
Guido van Rossum79f25d91997-04-29 20:08:16 +00001905static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001906imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001907{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001908 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001909 struct filedescr *fdp;
1910
Guido van Rossum43713e52000-02-29 13:59:29 +00001911 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001912 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001914 if (list == NULL)
1915 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1917 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001918 fdp->suffix, fdp->mode, fdp->type);
1919 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001921 return NULL;
1922 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923 if (PyList_Append(list, item) < 0) {
1924 Py_DECREF(list);
1925 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001926 return NULL;
1927 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001929 }
1930 return list;
1931}
1932
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001934call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001935{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001936 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001937 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001938 struct filedescr *fdp;
1939 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001940 FILE *fp = NULL;
1941
1942 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001943 if (path == Py_None)
1944 path = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001945 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
1946 if (fdp == NULL)
1947 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001948 if (fp != NULL) {
1949 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
1950 if (fob == NULL) {
1951 fclose(fp);
1952 return NULL;
1953 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001954 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001955 else {
1956 fob = Py_None;
1957 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00001958 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001960 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001962 return ret;
1963}
1964
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001966imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001967{
1968 char *name;
1969 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00001970 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001971 return NULL;
1972 return call_find_module(name, path);
1973}
1974
1975static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001976imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001977{
1978 char *name;
1979 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001980 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00001981 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001982 return NULL;
1983 ret = init_builtin(name);
1984 if (ret < 0)
1985 return NULL;
1986 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987 Py_INCREF(Py_None);
1988 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001989 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001990 m = PyImport_AddModule(name);
1991 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992 return m;
1993}
1994
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001996imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997{
1998 char *name;
1999 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002001 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002002 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002004 if (ret < 0)
2005 return NULL;
2006 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 Py_INCREF(Py_None);
2008 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002009 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010 m = PyImport_AddModule(name);
2011 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002012 return m;
2013}
2014
Guido van Rossum79f25d91997-04-29 20:08:16 +00002015static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002016imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002017{
2018 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002019
Guido van Rossum43713e52000-02-29 13:59:29 +00002020 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002021 return NULL;
2022 return get_frozen_object(name);
2023}
2024
Guido van Rossum79f25d91997-04-29 20:08:16 +00002025static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002026imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002027{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002028 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002029 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002030 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002031 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002032}
2033
Guido van Rossum79f25d91997-04-29 20:08:16 +00002034static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002035imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002036{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002037 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002038 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002039 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002040 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002041 p = find_frozen(name);
2042 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002043}
2044
2045static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002046get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002047{
2048 FILE *fp;
2049 if (fob == NULL) {
2050 fp = fopen(pathname, mode);
2051 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002052 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002053 }
2054 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002056 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002057 PyErr_SetString(PyExc_ValueError,
2058 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002059 }
2060 return fp;
2061}
2062
Guido van Rossum79f25d91997-04-29 20:08:16 +00002063static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002064imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002065{
2066 char *name;
2067 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002068 PyObject *fob = NULL;
2069 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002070 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002071 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002072 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002073 return NULL;
2074 fp = get_file(pathname, fob, "rb");
2075 if (fp == NULL)
2076 return NULL;
2077 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002078 if (fob == NULL)
2079 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002080 return m;
2081}
2082
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002083#ifdef HAVE_DYNAMIC_LOADING
2084
Guido van Rossum79f25d91997-04-29 20:08:16 +00002085static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002086imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002087{
2088 char *name;
2089 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002090 PyObject *fob = NULL;
2091 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002092 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002093 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002094 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002095 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002096 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002097 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002098 if (fp == NULL)
2099 return NULL;
2100 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002101 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002102 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002103}
2104
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002105#endif /* HAVE_DYNAMIC_LOADING */
2106
Guido van Rossum79f25d91997-04-29 20:08:16 +00002107static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002108imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002109{
2110 char *name;
2111 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002112 PyObject *fob = NULL;
2113 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002114 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002115 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002116 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002117 return NULL;
2118 fp = get_file(pathname, fob, "r");
2119 if (fp == NULL)
2120 return NULL;
2121 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002122 if (fob == NULL)
2123 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002124 return m;
2125}
2126
Jack Jansen9c96a921995-02-15 22:57:06 +00002127#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002128static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002129imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002130{
2131 char *name;
2132 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002134
Guido van Rossum43713e52000-02-29 13:59:29 +00002135 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002136 return NULL;
2137 m = PyMac_LoadResourceModule(name, pathname);
2138 return m;
2139}
2140#endif /* macintosh */
2141
Guido van Rossum79f25d91997-04-29 20:08:16 +00002142static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002143imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002144{
2145 char *name;
2146 PyObject *fob;
2147 char *pathname;
2148 char *suffix; /* Unused */
2149 char *mode;
2150 int type;
2151 FILE *fp;
2152
Guido van Rossum43713e52000-02-29 13:59:29 +00002153 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002154 &name, &fob, &pathname,
2155 &suffix, &mode, &type))
2156 return NULL;
2157 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2158 PyErr_Format(PyExc_ValueError,
2159 "invalid file open mode %.200s", mode);
2160 return NULL;
2161 }
2162 if (fob == Py_None)
2163 fp = NULL;
2164 else {
2165 if (!PyFile_Check(fob)) {
2166 PyErr_SetString(PyExc_ValueError,
2167 "load_module arg#2 should be a file or None");
2168 return NULL;
2169 }
2170 fp = get_file(pathname, fob, mode);
2171 if (fp == NULL)
2172 return NULL;
2173 }
2174 return load_module(name, fp, pathname, type);
2175}
2176
2177static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002178imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002179{
2180 char *name;
2181 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002182 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002183 return NULL;
2184 return load_package(name, pathname);
2185}
2186
2187static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002188imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002189{
2190 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002191 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002192 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002193 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002194}
2195
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002196/* Doc strings */
2197
2198static char doc_imp[] = "\
2199This module provides the components needed to build your own\n\
2200__import__ function. Undocumented functions are obsolete.\n\
2201";
2202
2203static char doc_find_module[] = "\
2204find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2205Search for a module. If path is omitted or None, search for a\n\
2206built-in, frozen or special module and continue search in sys.path.\n\
2207The module name cannot contain '.'; to search for a submodule of a\n\
2208package, pass the submodule name and the package's __path__.\
2209";
2210
2211static char doc_load_module[] = "\
2212load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2213Load a module, given information returned by find_module().\n\
2214The module name must include the full package name, if any.\
2215";
2216
2217static char doc_get_magic[] = "\
2218get_magic() -> string\n\
2219Return the magic number for .pyc or .pyo files.\
2220";
2221
2222static char doc_get_suffixes[] = "\
2223get_suffixes() -> [(suffix, mode, type), ...]\n\
2224Return a list of (suffix, mode, type) tuples describing the files\n\
2225that find_module() looks for.\
2226";
2227
2228static char doc_new_module[] = "\
2229new_module(name) -> module\n\
2230Create a new module. Do not enter it in sys.modules.\n\
2231The module name must include the full package name, if any.\
2232";
2233
Guido van Rossum79f25d91997-04-29 20:08:16 +00002234static PyMethodDef imp_methods[] = {
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002235 {"find_module", imp_find_module, 1, doc_find_module},
2236 {"get_magic", imp_get_magic, 1, doc_get_magic},
2237 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2238 {"load_module", imp_load_module, 1, doc_load_module},
2239 {"new_module", imp_new_module, 1, doc_new_module},
2240 /* The rest are obsolete */
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002241 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002242 {"init_builtin", imp_init_builtin, 1},
2243 {"init_frozen", imp_init_frozen, 1},
2244 {"is_builtin", imp_is_builtin, 1},
2245 {"is_frozen", imp_is_frozen, 1},
2246 {"load_compiled", imp_load_compiled, 1},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002247#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002248 {"load_dynamic", imp_load_dynamic, 1},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002249#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002250 {"load_package", imp_load_package, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00002251#ifdef macintosh
2252 {"load_resource", imp_load_resource, 1},
2253#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002254 {"load_source", imp_load_source, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002255 {NULL, NULL} /* sentinel */
2256};
2257
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002258static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002259setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002260{
2261 PyObject *v;
2262 int err;
2263
2264 v = PyInt_FromLong((long)value);
2265 err = PyDict_SetItemString(d, name, v);
2266 Py_XDECREF(v);
2267 return err;
2268}
2269
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002271initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002272{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002273 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002274
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002275 m = Py_InitModule4("imp", imp_methods, doc_imp,
2276 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002277 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002278
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002279 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2280 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2281 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2282 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2283 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2284 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2285 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2286 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002287 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002288
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002289 failure:
2290 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002291}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002292
2293
Guido van Rossumb18618d2000-05-03 23:44:39 +00002294/* API for embedding applications that want to add their own entries
2295 to the table of built-in modules. This should normally be called
2296 *before* Py_Initialize(). When the table resize fails, -1 is
2297 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002298
2299 After a similar function by Just van Rossum. */
2300
2301int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002302PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002303{
2304 static struct _inittab *our_copy = NULL;
2305 struct _inittab *p;
2306 int i, n;
2307
2308 /* Count the number of entries in both tables */
2309 for (n = 0; newtab[n].name != NULL; n++)
2310 ;
2311 if (n == 0)
2312 return 0; /* Nothing to do */
2313 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2314 ;
2315
2316 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002317 p = our_copy;
2318 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002319 if (p == NULL)
2320 return -1;
2321
2322 /* Copy the tables into the new memory */
2323 if (our_copy != PyImport_Inittab)
2324 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2325 PyImport_Inittab = our_copy = p;
2326 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2327
2328 return 0;
2329}
2330
2331/* Shorthand to add a single entry given a name and a function */
2332
2333int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002334PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002335{
2336 struct _inittab newtab[2];
2337
2338 memset(newtab, '\0', sizeof newtab);
2339
2340 newtab[0].name = name;
2341 newtab[0].initfunc = initfunc;
2342
2343 return PyImport_ExtendInittab(newtab);
2344}