blob: 8d06a53adf83288d46aeb06fdf30d70ed5f6c94a [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 Rossumaee0bad1997-09-05 07:33:22 +000022/* We expect that stat exists on most systems.
23 It's confirmed on Unix, Mac and Windows.
24 If you don't have it, add #define DONT_HAVE_STAT to your config.h. */
25#ifndef DONT_HAVE_STAT
26#define HAVE_STAT
27
Guido van Rossum2571cc81999-04-07 16:07:23 +000028#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossumaee0bad1997-09-05 07:33:22 +000029#include <sys/types.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000030#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000031
Guido van Rossum2571cc81999-04-07 16:07:23 +000032#ifndef DONT_HAVE_SYS_STAT_H
Guido van Rossumaee0bad1997-09-05 07:33:22 +000033#include <sys/stat.h>
Guido van Rossum63e97ad2000-07-01 01:06:56 +000034#elif defined(HAVE_STAT_H)
35#include <stat.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000036#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +000037
Guido van Rossum55a83382000-09-20 20:31:38 +000038#ifdef HAVE_FCNTL_H
39#include <fcntl.h>
40#endif
41
Guido van Rossum595d7ba1997-12-05 21:45:29 +000042#if defined(PYCC_VACPP)
43/* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
44#define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
45#endif
46
Guido van Rossumaee0bad1997-09-05 07:33:22 +000047#ifndef S_ISDIR
48#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
49#endif
50
51#endif
52
53
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000054extern time_t PyOS_GetLastModificationTime(char *, FILE *);
55 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000056
Guido van Rossum6c849691994-09-26 15:47:17 +000057/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000058/* Change for each incompatible change */
59/* The value of CR and LF is incorporated so if you ever read or write
60 a .pyc file in text mode the magic number will be wrong; also, the
61 Apple MPW compiler swaps their values, botching string constants */
62/* XXX Perhaps the magic number should be frozen and a version field
63 added to the .pyc file header? */
Guido van Rossumdd5db431997-01-17 21:06:11 +000064/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
Thomas Woutersdd13e4f2000-08-27 20:31:27 +000065#define MAGIC (50823 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000066
Guido van Rossum96774c12000-05-01 20:19:08 +000067/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000068 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000069 compiler works which are enabled by command line switches. */
70static long pyc_magic = MAGIC;
71
Guido van Rossum25ce5661997-08-02 03:10:38 +000072/* See _PyImport_FixupExtension() below */
73static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000074
Guido van Rossum771c6c81997-10-31 18:37:24 +000075/* This table is defined in config.c: */
76extern struct _inittab _PyImport_Inittab[];
77
78struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000079
Guido van Rossumed1170e1999-12-20 21:23:41 +000080/* these tables define the module suffixes that Python recognizes */
81struct filedescr * _PyImport_Filetab = NULL;
82static const struct filedescr _PyImport_StandardFiletab[] = {
83 {".py", "r", PY_SOURCE},
84 {".pyc", "rb", PY_COMPILED},
85 {0, 0}
86};
87
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089
90void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000091_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Guido van Rossumed1170e1999-12-20 21:23:41 +000093 const struct filedescr *scan;
94 struct filedescr *filetab;
95 int countD = 0;
96 int countS = 0;
97
98 /* prepare _PyImport_Filetab: copy entries from
99 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
100 */
101 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
102 ++countD;
103 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
104 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000105 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000106 memcpy(filetab, _PyImport_DynLoadFiletab,
107 countD * sizeof(struct filedescr));
108 memcpy(filetab + countD, _PyImport_StandardFiletab,
109 countS * sizeof(struct filedescr));
110 filetab[countD + countS].suffix = NULL;
111
112 _PyImport_Filetab = filetab;
113
Guido van Rossum0824f631997-03-11 18:37:35 +0000114 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000115 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
116 for (; filetab->suffix != NULL; filetab++) {
117 if (strcmp(filetab->suffix, ".pyc") == 0)
118 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000119 }
120 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000121
122 if (Py_UnicodeFlag) {
123 /* Fix the pyc_magic so that byte compiled code created
124 using the all-Unicode method doesn't interfere with
125 code created in normal operation mode. */
126 pyc_magic = MAGIC + 1;
127 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128}
129
Guido van Rossum25ce5661997-08-02 03:10:38 +0000130void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132{
133 Py_XDECREF(extensions);
134 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000135 PyMem_DEL(_PyImport_Filetab);
136 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137}
138
139
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000140/* Locking primitives to prevent parallel imports of the same module
141 in different threads to return with a partially loaded module.
142 These calls are serialized by the global interpreter lock. */
143
144#ifdef WITH_THREAD
145
Guido van Rossum49b56061998-10-01 20:42:43 +0000146#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000147
Guido van Rossum65d5b571998-12-21 19:32:43 +0000148static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000149static long import_lock_thread = -1;
150static int import_lock_level = 0;
151
152static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000154{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000155 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000156 if (me == -1)
157 return; /* Too bad */
158 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000159 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000160 if (import_lock_thread == me) {
161 import_lock_level++;
162 return;
163 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000164 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000165 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000166 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000167 PyEval_RestoreThread(tstate);
168 }
169 import_lock_thread = me;
170 import_lock_level = 1;
171}
172
173static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000174unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000175{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000176 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000177 if (me == -1)
178 return; /* Too bad */
179 if (import_lock_thread != me)
180 Py_FatalError("unlock_import: not holding the import lock");
181 import_lock_level--;
182 if (import_lock_level == 0) {
183 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000184 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000185 }
186}
187
188#else
189
190#define lock_import()
191#define unlock_import()
192
193#endif
194
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195/* Helper for sys */
196
197PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199{
200 PyInterpreterState *interp = PyThreadState_Get()->interp;
201 if (interp->modules == NULL)
202 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
203 return interp->modules;
204}
205
Guido van Rossum3f5da241990-12-20 15:06:42 +0000206
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000207/* List of names to clear in sys */
208static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000209 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000210 "exc_type", "exc_value", "exc_traceback",
211 "last_type", "last_value", "last_traceback",
212 NULL
213};
214
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000215static char* sys_files[] = {
216 "stdin", "__stdin__",
217 "stdout", "__stdout__",
218 "stderr", "__stderr__",
219 NULL
220};
221
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000222
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000223/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000227{
Guido van Rossum758eec01998-01-19 21:58:26 +0000228 int pos, ndone;
229 char *name;
230 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000232 PyObject *modules = interp->modules;
233
234 if (modules == NULL)
235 return; /* Already done */
236
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000237 /* Delete some special variables first. These are common
238 places where user values hide and people complain when their
239 destructors fail. Since the modules containing them are
240 deleted *last* of all, they would come too late in the normal
241 destruction order. Sigh. */
242
243 value = PyDict_GetItemString(modules, "__builtin__");
244 if (value != NULL && PyModule_Check(value)) {
245 dict = PyModule_GetDict(value);
246 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000247 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000248 PyDict_SetItemString(dict, "_", Py_None);
249 }
250 value = PyDict_GetItemString(modules, "sys");
251 if (value != NULL && PyModule_Check(value)) {
252 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000253 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000254 dict = PyModule_GetDict(value);
255 for (p = sys_deletes; *p != NULL; p++) {
256 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000257 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000258 PyDict_SetItemString(dict, *p, Py_None);
259 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000260 for (p = sys_files; *p != NULL; p+=2) {
261 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000262 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000263 v = PyDict_GetItemString(dict, *(p+1));
264 if (v == NULL)
265 v = Py_None;
266 PyDict_SetItemString(dict, *p, v);
267 }
268 }
269
270 /* First, delete __main__ */
271 value = PyDict_GetItemString(modules, "__main__");
272 if (value != NULL && PyModule_Check(value)) {
273 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000274 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000275 _PyModule_Clear(value);
276 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000277 }
278
Guido van Rossum758eec01998-01-19 21:58:26 +0000279 /* The special treatment of __builtin__ here is because even
280 when it's not referenced as a module, its dictionary is
281 referenced by almost every module's __builtins__. Since
282 deleting a module clears its dictionary (even if there are
283 references left to it), we need to delete the __builtin__
284 module last. Likewise, we don't delete sys until the very
285 end because it is implicitly referenced (e.g. by print).
286
287 Also note that we 'delete' modules by replacing their entry
288 in the modules dict with None, rather than really deleting
289 them; this avoids a rehash of the modules dictionary and
290 also marks them as "non existent" so they won't be
291 re-imported. */
292
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000293 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000294 one (skipping __builtin__ and sys) and delete them */
295 do {
296 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000297 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000298 while (PyDict_Next(modules, &pos, &key, &value)) {
299 if (value->ob_refcnt != 1)
300 continue;
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(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000309 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000310 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000311 PyDict_SetItem(modules, key, Py_None);
312 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000313 }
314 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000315 } while (ndone > 0);
316
Guido van Rossum758eec01998-01-19 21:58:26 +0000317 /* Next, delete all modules (still skipping __builtin__ and sys) */
318 pos = 0;
319 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000320 if (PyString_Check(key) && PyModule_Check(value)) {
321 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000322 if (strcmp(name, "__builtin__") == 0)
323 continue;
324 if (strcmp(name, "sys") == 0)
325 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000326 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000327 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000328 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000329 PyDict_SetItem(modules, key, Py_None);
330 }
331 }
332
333 /* Next, delete sys and __builtin__ (in that order) */
334 value = PyDict_GetItemString(modules, "sys");
335 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000336 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000337 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000338 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000339 PyDict_SetItemString(modules, "sys", Py_None);
340 }
341 value = PyDict_GetItemString(modules, "__builtin__");
342 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000343 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000344 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000345 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000346 PyDict_SetItemString(modules, "__builtin__", Py_None);
347 }
348
349 /* Finally, clear and delete the modules directory */
350 PyDict_Clear(modules);
351 interp->modules = NULL;
352 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000353}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000354
355
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000356/* Helper for pythonrun.c -- return magic number */
357
358long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000360{
Guido van Rossum96774c12000-05-01 20:19:08 +0000361 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000362}
363
364
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365/* Magic for extension modules (built-in as well as dynamically
366 loaded). To prevent initializing an extension module more than
367 once, we keep a static dictionary 'extensions' keyed by module name
368 (for built-in modules) or by filename (for dynamically loaded
369 modules), containing these modules. A copy od the module's
370 dictionary is stored by calling _PyImport_FixupExtension()
371 immediately after the module initialization function succeeds. A
372 copy can be retrieved from there by calling
373 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000374
Guido van Rossum79f25d91997-04-29 20:08:16 +0000375PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000377{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000378 PyObject *modules, *mod, *dict, *copy;
379 if (extensions == NULL) {
380 extensions = PyDict_New();
381 if (extensions == NULL)
382 return NULL;
383 }
384 modules = PyImport_GetModuleDict();
385 mod = PyDict_GetItemString(modules, name);
386 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000387 PyErr_Format(PyExc_SystemError,
388 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000389 return NULL;
390 }
391 dict = PyModule_GetDict(mod);
392 if (dict == NULL)
393 return NULL;
394 copy = PyObject_CallMethod(dict, "copy", "");
395 if (copy == NULL)
396 return NULL;
397 PyDict_SetItemString(extensions, filename, copy);
398 Py_DECREF(copy);
399 return copy;
400}
401
402PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404{
405 PyObject *dict, *mod, *mdict, *result;
406 if (extensions == NULL)
407 return NULL;
408 dict = PyDict_GetItemString(extensions, filename);
409 if (dict == NULL)
410 return NULL;
411 mod = PyImport_AddModule(name);
412 if (mod == NULL)
413 return NULL;
414 mdict = PyModule_GetDict(mod);
415 if (mdict == NULL)
416 return NULL;
417 result = PyObject_CallMethod(mdict, "update", "O", dict);
418 if (result == NULL)
419 return NULL;
420 Py_DECREF(result);
421 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000422 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423 name, filename);
424 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425}
426
427
428/* Get the module object corresponding to a module name.
429 First check the modules dictionary if there's one there,
430 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000431 Because the former action is most common, THIS DOES NOT RETURN A
432 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000433
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000436{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000437 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000439
Guido van Rossum25ce5661997-08-02 03:10:38 +0000440 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000442 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000443 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000444 if (m == NULL)
445 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000446 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000448 return NULL;
449 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451
452 return m;
453}
454
455
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000456/* Execute a code object in a module and return the module object
457 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000458
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000460PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000461{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000462 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
463}
464
465PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000466PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000467{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000468 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470
Guido van Rossum79f25d91997-04-29 20:08:16 +0000471 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472 if (m == NULL)
473 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474 d = PyModule_GetDict(m);
475 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
476 if (PyDict_SetItemString(d, "__builtins__",
477 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000478 return NULL;
479 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000480 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000481 v = NULL;
482 if (pathname != NULL) {
483 v = PyString_FromString(pathname);
484 if (v == NULL)
485 PyErr_Clear();
486 }
487 if (v == NULL) {
488 v = ((PyCodeObject *)co)->co_filename;
489 Py_INCREF(v);
490 }
491 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000492 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000493 Py_DECREF(v);
494
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000495 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496 if (v == NULL)
497 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000498 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000499
Guido van Rossum25ce5661997-08-02 03:10:38 +0000500 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000501 PyErr_Format(PyExc_ImportError,
502 "Loaded module %.200s not found in sys.modules",
503 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000504 return NULL;
505 }
506
Guido van Rossum79f25d91997-04-29 20:08:16 +0000507 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508
509 return m;
510}
511
512
513/* Given a pathname for a Python source file, fill a buffer with the
514 pathname for the corresponding compiled file. Return the pathname
515 for the compiled file, or NULL if there's no space in the buffer.
516 Doesn't set an exception. */
517
518static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000519make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520{
Fred Drake4c82b232000-06-30 16:18:57 +0000521 size_t len;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522
523 len = strlen(pathname);
524 if (len+2 > buflen)
525 return NULL;
526 strcpy(buf, pathname);
Guido van Rossum0824f631997-03-11 18:37:35 +0000527 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000528
529 return buf;
530}
531
532
533/* Given a pathname for a Python source file, its time of last
534 modification, and a pathname for a compiled file, check whether the
535 compiled file represents the same version of the source. If so,
536 return a FILE pointer for the compiled file, positioned just after
537 the header; if not, return NULL.
538 Doesn't set an exception. */
539
540static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000541check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542{
543 FILE *fp;
544 long magic;
545 long pyc_mtime;
546
547 fp = fopen(cpathname, "rb");
548 if (fp == NULL)
549 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000551 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000553 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 fclose(fp);
555 return NULL;
556 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000560 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000561 fclose(fp);
562 return NULL;
563 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000565 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 return fp;
567}
568
569
570/* Read a code object from a file and check it for validity */
571
Guido van Rossum79f25d91997-04-29 20:08:16 +0000572static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000573read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 co = PyMarshal_ReadObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000578 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 if (co == NULL || !PyCode_Check(co)) {
580 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000581 PyErr_Format(PyExc_ImportError,
582 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000583 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584 return NULL;
585 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000587}
588
589
590/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000591 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000594load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595{
596 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 PyCodeObject *co;
598 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000599
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000601 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000602 PyErr_Format(PyExc_ImportError,
603 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604 return NULL;
605 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000607 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608 if (co == NULL)
609 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000611 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000613 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615
616 return m;
617}
618
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619/* Parse a source file and return the corresponding code object */
620
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000622parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000624 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000625 node *n;
626
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000627 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628 if (n == NULL)
629 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 co = PyNode_Compile(n, pathname);
631 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632
633 return co;
634}
635
636
Guido van Rossum55a83382000-09-20 20:31:38 +0000637/* Helper to open a bytecode file for writing in exclusive mode */
638
639static FILE *
640open_exclusive(char *filename)
641{
642#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
643 /* Use O_EXCL to avoid a race condition when another process tries to
644 write the same file. When that happens, our open() call fails,
645 which is just fine (since it's only a cache).
646 XXX If the file exists and is writable but the directory is not
647 writable, the file will never be written. Oh well.
648 */
649 int fd;
650 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000651 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
652#ifdef O_BINARY
653 |O_BINARY /* necessary for Windows */
654#endif
655
656 , 0666);
Guido van Rossum55a83382000-09-20 20:31:38 +0000657 if (fd < 0)
658 return NULL;
659 return fdopen(fd, "wb");
660#else
661 /* Best we can do -- on Windows this can't happen anyway */
662 return fopen(filename, "wb");
663#endif
664}
665
666
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667/* Write a compiled module to a file, placing the time of last
668 modification of its source into the header.
669 Errors are ignored, if a write error occurs an attempt is made to
670 remove the file. */
671
672static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000673write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674{
675 FILE *fp;
676
Guido van Rossum55a83382000-09-20 20:31:38 +0000677 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000678 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000680 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681 "# can't create %s\n", cpathname);
682 return;
683 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000684 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyMarshal_WriteLongToFile(0L, fp);
687 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000690 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691 /* Don't keep partial file */
692 fclose(fp);
693 (void) unlink(cpathname);
694 return;
695 }
696 /* Now write the true mtime */
697 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 fflush(fp);
700 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000702 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703#ifdef macintosh
Jack Jansencbf630f2000-07-11 21:59:16 +0000704 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705#endif
706}
707
708
709/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000710 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
711 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000714load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000715{
Fred Drake4c82b232000-06-30 16:18:57 +0000716 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717 FILE *fpc;
718 char buf[MAXPATHLEN+1];
719 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 PyCodeObject *co;
721 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000723 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drake4c82b232000-06-30 16:18:57 +0000724 if (mtime == -1)
725 return NULL;
726#if SIZEOF_TIME_T > 4
727 /* Python's .pyc timestamp handling presumes that the timestamp fits
728 in 4 bytes. This will be fine until sometime in the year 2038,
729 when a 4-byte signed time_t will overflow.
730 */
731 if (mtime >> 32) {
732 PyErr_SetString(PyExc_OverflowError,
733 "modification time overflows a 4 bytes");
734 return NULL;
735 }
736#endif
737 cpathname = make_compiled_pathname(pathname, buf, (size_t)MAXPATHLEN+1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 if (cpathname != NULL &&
739 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000740 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 fclose(fpc);
742 if (co == NULL)
743 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000745 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000747 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748 }
749 else {
750 co = parse_source_module(pathname, fp);
751 if (co == NULL)
752 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000754 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755 name, pathname);
756 write_compiled_module(co, cpathname, mtime);
757 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000758 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760
761 return m;
762}
763
764
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000765/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000766static PyObject *load_module(char *, FILE *, char *, int);
767static struct filedescr *find_module(char *, PyObject *,
768 char *, size_t, FILE **);
769static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000770
771/* Load a package and return its module object WITH INCREMENTED
772 REFERENCE COUNT */
773
774static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000775load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000776{
777 PyObject *m, *d, *file, *path;
778 int err;
779 char buf[MAXPATHLEN+1];
780 FILE *fp = NULL;
781 struct filedescr *fdp;
782
783 m = PyImport_AddModule(name);
784 if (m == NULL)
785 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000786 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000787 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000788 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000789 d = PyModule_GetDict(m);
790 file = PyString_FromString(pathname);
791 if (file == NULL)
792 return NULL;
793 path = Py_BuildValue("[O]", file);
794 if (path == NULL) {
795 Py_DECREF(file);
796 return NULL;
797 }
798 err = PyDict_SetItemString(d, "__file__", file);
799 if (err == 0)
800 err = PyDict_SetItemString(d, "__path__", path);
801 if (err != 0) {
802 m = NULL;
803 goto cleanup;
804 }
805 buf[0] = '\0';
806 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
807 if (fdp == NULL) {
808 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
809 PyErr_Clear();
810 }
811 else
812 m = NULL;
813 goto cleanup;
814 }
815 m = load_module(name, fp, buf, fdp->type);
816 if (fp != NULL)
817 fclose(fp);
818 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000819 Py_XDECREF(path);
820 Py_XDECREF(file);
821 return m;
822}
823
824
825/* Helper to test for built-in module */
826
827static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000828is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000829{
830 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000831 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
832 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
833 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000834 return -1;
835 else
836 return 1;
837 }
838 }
839 return 0;
840}
841
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000842
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000843/* Search the path (default sys.path) for a module. Return the
844 corresponding filedescr struct, and (via return arguments) the
845 pathname and an open file. Return NULL if the module is not found. */
846
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000847#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +0000848extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
849 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000850#endif
851
Guido van Rossum0980bd91998-02-13 17:18:36 +0000852#ifdef CHECK_IMPORT_CASE
853static int check_case(char *, int, int, char *);
854#endif
855
Tim Petersdbd9ba62000-07-09 03:09:57 +0000856static int find_init_module(char *); /* Forward */
Guido van Rossum197346f1997-10-31 18:38:52 +0000857
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858static struct filedescr *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000859find_module(char *realname, PyObject *path, char *buf, size_t buflen,
860 FILE **p_fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000861{
Fred Drake4c82b232000-06-30 16:18:57 +0000862 int i, npath;
863 size_t len, namelen;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000864 struct _frozen *f;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000865 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000866 FILE *fp = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000867 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000868 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
869 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
870 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +0000871 char name[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000872
Fred Drake4c82b232000-06-30 16:18:57 +0000873 if (strlen(realname) > MAXPATHLEN) {
874 PyErr_SetString(PyExc_OverflowError, "module name is too long");
875 return NULL;
876 }
Guido van Rossum0506a431998-08-11 15:07:39 +0000877 strcpy(name, realname);
878
879 if (path != NULL && PyString_Check(path)) {
880 /* Submodule of "frozen" package:
881 Set name to the fullname, path to NULL
882 and continue as "usual" */
883 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
884 PyErr_SetString(PyExc_ImportError,
885 "full frozen module name too long");
886 return NULL;
887 }
888 strcpy(buf, PyString_AsString(path));
889 strcat(buf, ".");
890 strcat(buf, name);
891 strcpy(name, buf);
892 path = NULL;
893 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000894 if (path == NULL) {
895 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000896 strcpy(buf, name);
897 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000898 }
Guido van Rossuma5568d31998-03-05 03:45:08 +0000899 if ((f = find_frozen(name)) != NULL) {
900 strcpy(buf, name);
901 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000902 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000903
Guido van Rossumac279101996-08-22 23:10:58 +0000904#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000905 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
906 if (fp != NULL) {
907 *p_fp = fp;
908 return fdp;
909 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000910#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000911 path = PySys_GetObject("path");
912 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 if (path == NULL || !PyList_Check(path)) {
914 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +0000915 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916 return NULL;
917 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000918 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000919 namelen = strlen(name);
920 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 PyObject *v = PyList_GetItem(path, i);
922 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 len = PyString_Size(v);
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000925 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000926 continue; /* Too long */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 strcpy(buf, PyString_AsString(v));
Fred Drake4c82b232000-06-30 16:18:57 +0000928 if (strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000930#ifdef macintosh
Guido van Rossum741689d1997-08-12 14:53:39 +0000931#ifdef INTERN_STRINGS
932 /*
933 ** Speedup: each sys.path item is interned, and
934 ** FindResourceModule remembers which items refer to
935 ** folders (so we don't have to bother trying to look
936 ** into them for resources).
937 */
938 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
939 v = PyList_GET_ITEM(path, i);
940#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000941 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 static struct filedescr resfiledescr =
943 {"", "", PY_RESOURCE};
Jack Jansen9c96a921995-02-15 22:57:06 +0000944
945 return &resfiledescr;
946 }
Guido van Rossum0f84a341998-08-06 13:36:01 +0000947 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
948 static struct filedescr resfiledescr =
949 {"", "", PY_CODERESOURCE};
950
951 return &resfiledescr;
952 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000953#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000954 if (len > 0 && buf[len-1] != SEP
955#ifdef ALTSEP
956 && buf[len-1] != ALTSEP
957#endif
958 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000960#ifdef IMPORT_8x3_NAMES
Guido van Rossume8c27bb1997-12-17 18:57:16 +0000961 /* see if we are searching in directory dos-8x3 */
962 if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){
Guido van Rossum40f470f1996-05-23 22:51:04 +0000963 int j;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 char ch; /* limit name to 8 lower-case characters */
Guido van Rossum40f470f1996-05-23 22:51:04 +0000965 for (j = 0; (ch = name[j]) && j < 8; j++)
966 if (isupper(ch))
967 buf[len++] = tolower(ch);
968 else
969 buf[len++] = ch;
970 }
Guido van Rossume8c27bb1997-12-17 18:57:16 +0000971 else /* Not in dos-8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000972#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000973 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000974 strcpy(buf+len, name);
975 len += namelen;
976 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000977#ifdef HAVE_STAT
978 if (stat(buf, &statbuf) == 0) {
Guido van Rossum197346f1997-10-31 18:38:52 +0000979 if (S_ISDIR(statbuf.st_mode)) {
Guido van Rossum0980bd91998-02-13 17:18:36 +0000980 if (find_init_module(buf)) {
981#ifdef CHECK_IMPORT_CASE
982 if (!check_case(buf, len, namelen,
983 name))
984 return NULL;
985#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000986 return &fd_package;
Guido van Rossum0980bd91998-02-13 17:18:36 +0000987 }
Guido van Rossum197346f1997-10-31 18:38:52 +0000988 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000989 }
990#else
991 /* XXX How are you going to test for directories? */
992#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +0000993#ifdef macintosh
994 fdp = PyMac_FindModuleExtension(buf, &len, name);
995 if (fdp)
996 fp = fopen(buf, fdp->mode);
997#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000999 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001001 PySys_WriteStderr("# trying %s\n", buf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001002 fp = fopen(buf, fdp->mode);
1003 if (fp != NULL)
1004 break;
1005 }
Guido van Rossum741689d1997-08-12 14:53:39 +00001006#endif /* !macintosh */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007 if (fp != NULL)
1008 break;
1009 }
1010 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001011 PyErr_Format(PyExc_ImportError,
1012 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001013 return NULL;
1014 }
Guido van Rossum0980bd91998-02-13 17:18:36 +00001015#ifdef CHECK_IMPORT_CASE
1016 if (!check_case(buf, len, namelen, name)) {
1017 fclose(fp);
1018 return NULL;
1019 }
1020#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001021
1022 *p_fp = fp;
1023 return fdp;
1024}
1025
Guido van Rossum0980bd91998-02-13 17:18:36 +00001026#ifdef CHECK_IMPORT_CASE
1027
1028#ifdef MS_WIN32
1029#include <windows.h>
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001030#include <ctype.h>
1031
1032static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001033allcaps8x3(char *s)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001034{
1035 /* Return 1 if s is an 8.3 filename in ALLCAPS */
1036 char c;
1037 char *dot = strchr(s, '.');
1038 char *end = strchr(s, '\0');
1039 if (dot != NULL) {
1040 if (dot-s > 8)
Guido van Rossum22b65a82000-05-01 17:36:58 +00001041 return 0; /* More than 8 before '.' */
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001042 if (end-dot > 4)
Guido van Rossum22b65a82000-05-01 17:36:58 +00001043 return 0; /* More than 3 after '.' */
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001044 end = strchr(dot+1, '.');
1045 if (end != NULL)
Guido van Rossum22b65a82000-05-01 17:36:58 +00001046 return 0; /* More than one dot */
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001047 }
1048 else if (end-s > 8)
Guido van Rossum22b65a82000-05-01 17:36:58 +00001049 return 0; /* More than 8 and no dot */
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001050 while ((c = *s++)) {
1051 if (islower(c))
1052 return 0;
1053 }
1054 return 1;
1055}
1056
Guido van Rossum0980bd91998-02-13 17:18:36 +00001057static int
1058check_case(char *buf, int len, int namelen, char *name)
1059{
1060 WIN32_FIND_DATA data;
1061 HANDLE h;
1062 if (getenv("PYTHONCASEOK") != NULL)
1063 return 1;
1064 h = FindFirstFile(buf, &data);
1065 if (h == INVALID_HANDLE_VALUE) {
1066 PyErr_Format(PyExc_NameError,
1067 "Can't find file for module %.100s\n(filename %.300s)",
1068 name, buf);
1069 return 0;
1070 }
1071 FindClose(h);
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001072 if (allcaps8x3(data.cFileName)) {
1073 /* Skip the test if the filename is ALL.CAPS. This can
1074 happen in certain circumstances beyond our control,
1075 e.g. when software is installed under NT on a FAT
1076 filesystem and then the same FAT filesystem is used
1077 under Windows 95. */
1078 return 1;
1079 }
Guido van Rossum0980bd91998-02-13 17:18:36 +00001080 if (strncmp(data.cFileName, name, namelen) != 0) {
1081 strcpy(buf+len-namelen, data.cFileName);
1082 PyErr_Format(PyExc_NameError,
1083 "Case mismatch for module name %.100s\n(filename %.300s)",
1084 name, buf);
1085 return 0;
1086 }
1087 return 1;
1088}
1089#endif /* MS_WIN32 */
1090
1091#ifdef macintosh
1092#include <TextUtils.h>
Guido van Rossumb33aa1a2000-04-24 15:08:18 +00001093#ifdef USE_GUSI1
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001094#include "TFileSpec.h" /* for Path2FSSpec() */
1095#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001096static int
1097check_case(char *buf, int len, int namelen, char *name)
1098{
1099 FSSpec fss;
1100 OSErr err;
Guido van Rossumb33aa1a2000-04-24 15:08:18 +00001101#ifndef USE_GUSI1
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001102 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1103#else
1104 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1105 the way, which is fine for all directories, but here we need
1106 the original name of the alias file (say, Dlg.ppc.slb, not
1107 toolboxmodules.ppc.slb). */
1108 char *colon;
1109 err = Path2FSSpec(buf, &fss);
1110 if (err == noErr) {
1111 colon = strrchr(buf, ':'); /* find filename */
1112 if (colon != NULL)
1113 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1114 Pstring(colon+1), &fss);
1115 else
1116 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1117 fss.name, &fss);
1118 }
1119#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001120 if (err) {
1121 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001122 "Can't find file for module %.100s\n(filename %.300s)",
1123 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001124 return 0;
1125 }
Guido van Rossum63e97ad2000-07-01 01:06:56 +00001126 if ( namelen > fss.name[0] || strncmp(name, (char *)fss.name+1, namelen) != 0 ) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001127 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001128 "Case mismatch for module name %.100s\n(filename %.300s)",
1129 name, fss.name);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001130 return 0;
1131 }
1132 return 1;
1133}
1134#endif /* macintosh */
1135
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001136#ifdef DJGPP
Guido van Rossumaeca79b1998-02-18 16:21:00 +00001137#include <dir.h>
1138
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001139static int
1140check_case(char *buf, int len, int namelen, char *name)
1141{
1142 struct ffblk ffblk;
1143 int done;
1144
1145 if (getenv("PYTHONCASEOK") != NULL)
1146 return 1;
Guido van Rossumaeca79b1998-02-18 16:21:00 +00001147 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001148 if (done) {
1149 PyErr_Format(PyExc_NameError,
1150 "Can't find file for module %.100s\n(filename %.300s)",
1151 name, buf);
1152 return 0;
1153 }
1154
1155 if (strncmp(ffblk.ff_name, name, namelen) != 0) {
1156 strcpy(buf+len-namelen, ffblk.ff_name);
1157 PyErr_Format(PyExc_NameError,
1158 "Case mismatch for module name %.100s\n(filename %.300s)",
1159 name, buf);
1160 return 0;
1161 }
1162 return 1;
1163}
1164#endif
1165
Guido van Rossum8766a771998-04-10 23:44:32 +00001166#endif /* CHECK_IMPORT_CASE */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001167
Guido van Rossum197346f1997-10-31 18:38:52 +00001168#ifdef HAVE_STAT
1169/* Helper to look for __init__.py or __init__.py[co] in potential package */
1170static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001171find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001172{
Fred Drake4c82b232000-06-30 16:18:57 +00001173 size_t save_len = strlen(buf);
1174 size_t i = save_len;
Guido van Rossum197346f1997-10-31 18:38:52 +00001175 struct stat statbuf;
1176
1177 if (save_len + 13 >= MAXPATHLEN)
1178 return 0;
1179 buf[i++] = SEP;
1180 strcpy(buf+i, "__init__.py");
1181 if (stat(buf, &statbuf) == 0) {
1182 buf[save_len] = '\0';
1183 return 1;
1184 }
1185 i += strlen(buf+i);
1186 if (Py_OptimizeFlag)
1187 strcpy(buf+i, "o");
1188 else
1189 strcpy(buf+i, "c");
1190 if (stat(buf, &statbuf) == 0) {
1191 buf[save_len] = '\0';
1192 return 1;
1193 }
1194 buf[save_len] = '\0';
1195 return 0;
1196}
1197#endif /* HAVE_STAT */
1198
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001199
Tim Petersdbd9ba62000-07-09 03:09:57 +00001200static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001201
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001202/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001203 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001204
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001206load_module(char *name, FILE *fp, char *buf, int type)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001207{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001208 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001210 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001211
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001212 /* First check that there's an open file (if we need one) */
1213 switch (type) {
1214 case PY_SOURCE:
1215 case PY_COMPILED:
1216 if (fp == NULL) {
1217 PyErr_Format(PyExc_ValueError,
1218 "file object required for import (type code %d)",
1219 type);
1220 return NULL;
1221 }
1222 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001223
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001224 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001225
1226 case PY_SOURCE:
1227 m = load_source_module(name, buf, fp);
1228 break;
1229
1230 case PY_COMPILED:
1231 m = load_compiled_module(name, buf, fp);
1232 break;
1233
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001234#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001237 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001238#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001239
Jack Jansen9c96a921995-02-15 22:57:06 +00001240#ifdef macintosh
1241 case PY_RESOURCE:
1242 m = PyMac_LoadResourceModule(name, buf);
1243 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001244 case PY_CODERESOURCE:
1245 m = PyMac_LoadCodeResourceModule(name, buf);
1246 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001247#endif
1248
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001249 case PKG_DIRECTORY:
1250 m = load_package(name, buf);
1251 break;
1252
1253 case C_BUILTIN:
1254 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001255 if (buf != NULL && buf[0] != '\0')
1256 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001257 if (type == C_BUILTIN)
1258 err = init_builtin(name);
1259 else
1260 err = PyImport_ImportFrozenModule(name);
1261 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001262 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001263 if (err == 0) {
1264 PyErr_Format(PyExc_ImportError,
1265 "Purported %s module %.200s not found",
1266 type == C_BUILTIN ?
1267 "builtin" : "frozen",
1268 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001269 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001270 }
1271 modules = PyImport_GetModuleDict();
1272 m = PyDict_GetItemString(modules, name);
1273 if (m == NULL) {
1274 PyErr_Format(
1275 PyExc_ImportError,
1276 "%s module %.200s not properly initialized",
1277 type == C_BUILTIN ?
1278 "builtin" : "frozen",
1279 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001280 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001281 }
1282 Py_INCREF(m);
1283 break;
1284
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001285 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001286 PyErr_Format(PyExc_ImportError,
1287 "Don't know how to import %.200s (type code %d)",
1288 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001289 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001290
1291 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001292
1293 return m;
1294}
1295
1296
1297/* Initialize a built-in module.
1298 Return 1 for succes, 0 if the module is not found, and -1 with
1299 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001300
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001301static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001302init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001303{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001304 struct _inittab *p;
1305 PyObject *mod;
1306
1307 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1308 return 1;
1309
Guido van Rossum771c6c81997-10-31 18:37:24 +00001310 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001311 if (strcmp(name, p->name) == 0) {
1312 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001313 PyErr_Format(PyExc_ImportError,
1314 "Cannot re-init internal module %.200s",
1315 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001316 return -1;
1317 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001318 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001319 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001320 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001321 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001322 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001323 if (_PyImport_FixupExtension(name, name) == NULL)
1324 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001325 return 1;
1326 }
1327 }
1328 return 0;
1329}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001330
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001331
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001332/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001333
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001334static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001335find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001336{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001337 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001338
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001340 if (p->name == NULL)
1341 return NULL;
1342 if (strcmp(p->name, name) == 0)
1343 break;
1344 }
1345 return p;
1346}
1347
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001349get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001350{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001351 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001352 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001353
1354 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001355 PyErr_Format(PyExc_ImportError,
1356 "No such frozen object named %.200s",
1357 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001358 return NULL;
1359 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001360 size = p->size;
1361 if (size < 0)
1362 size = -size;
1363 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001364}
1365
1366/* Initialize a frozen module.
1367 Return 1 for succes, 0 if the module is not found, and -1 with
1368 an exception set if the initialization failed.
1369 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001370
1371int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001372PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001373{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001374 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375 PyObject *co;
1376 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001377 int ispackage;
1378 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001379
1380 if (p == NULL)
1381 return 0;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001382 size = p->size;
1383 ispackage = (size < 0);
1384 if (ispackage)
1385 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001387 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001388 name, ispackage ? " package" : "");
1389 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001390 if (co == NULL)
1391 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001392 if (!PyCode_Check(co)) {
1393 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001394 PyErr_Format(PyExc_TypeError,
1395 "frozen object %.200s is not a code object",
1396 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001397 return -1;
1398 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001399 if (ispackage) {
1400 /* Set __path__ to the package name */
1401 PyObject *d, *s;
1402 int err;
1403 m = PyImport_AddModule(name);
1404 if (m == NULL)
1405 return -1;
1406 d = PyModule_GetDict(m);
1407 s = PyString_InternFromString(name);
1408 if (s == NULL)
1409 return -1;
1410 err = PyDict_SetItemString(d, "__path__", s);
1411 Py_DECREF(s);
1412 if (err != 0)
1413 return err;
1414 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001415 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001416 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001417 if (m == NULL)
1418 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001420 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001421}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001422
1423
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001424/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001425 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001426
Guido van Rossum79f25d91997-04-29 20:08:16 +00001427PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001428PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001429{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001430 static PyObject *fromlist = NULL;
1431 if (fromlist == NULL && strchr(name, '.') != NULL) {
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00001432 fromlist = Py_BuildValue("(s)", "*");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001433 if (fromlist == NULL)
1434 return NULL;
1435 }
1436 return PyImport_ImportModuleEx(name, NULL, NULL, fromlist);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001437}
1438
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001439/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001440static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1441static PyObject *load_next(PyObject *mod, PyObject *altmod,
1442 char **p_name, char *buf, int *p_buflen);
1443static int mark_miss(char *name);
1444static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1445 char *buf, int buflen, int recursive);
1446static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001447
1448/* The Magnum Opus of dotted-name import :-) */
1449
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001450static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001451import_module_ex(char *name, PyObject *globals, PyObject *locals,
1452 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001453{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001454 char buf[MAXPATHLEN+1];
1455 int buflen = 0;
1456 PyObject *parent, *head, *next, *tail;
1457
1458 parent = get_parent(globals, buf, &buflen);
1459 if (parent == NULL)
1460 return NULL;
1461
1462 head = load_next(parent, Py_None, &name, buf, &buflen);
1463 if (head == NULL)
1464 return NULL;
1465
1466 tail = head;
1467 Py_INCREF(tail);
1468 while (name) {
1469 next = load_next(tail, tail, &name, buf, &buflen);
1470 Py_DECREF(tail);
1471 if (next == NULL) {
1472 Py_DECREF(head);
1473 return NULL;
1474 }
1475 tail = next;
1476 }
1477
1478 if (fromlist != NULL) {
1479 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1480 fromlist = NULL;
1481 }
1482
1483 if (fromlist == NULL) {
1484 Py_DECREF(tail);
1485 return head;
1486 }
1487
1488 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001489 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001490 Py_DECREF(tail);
1491 return NULL;
1492 }
1493
1494 return tail;
1495}
1496
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001497PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001498PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1499 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001500{
1501 PyObject *result;
1502 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001503 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001504 unlock_import();
1505 return result;
1506}
1507
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001508static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001509get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001510{
1511 static PyObject *namestr = NULL;
1512 static PyObject *pathstr = NULL;
1513 PyObject *modname, *modpath, *modules, *parent;
1514
1515 if (globals == NULL || !PyDict_Check(globals))
1516 return Py_None;
1517
1518 if (namestr == NULL) {
1519 namestr = PyString_InternFromString("__name__");
1520 if (namestr == NULL)
1521 return NULL;
1522 }
1523 if (pathstr == NULL) {
1524 pathstr = PyString_InternFromString("__path__");
1525 if (pathstr == NULL)
1526 return NULL;
1527 }
1528
1529 *buf = '\0';
1530 *p_buflen = 0;
1531 modname = PyDict_GetItem(globals, namestr);
1532 if (modname == NULL || !PyString_Check(modname))
1533 return Py_None;
1534
1535 modpath = PyDict_GetItem(globals, pathstr);
1536 if (modpath != NULL) {
1537 int len = PyString_GET_SIZE(modname);
1538 if (len > MAXPATHLEN) {
1539 PyErr_SetString(PyExc_ValueError,
1540 "Module name too long");
1541 return NULL;
1542 }
1543 strcpy(buf, PyString_AS_STRING(modname));
1544 *p_buflen = len;
1545 }
1546 else {
1547 char *start = PyString_AS_STRING(modname);
1548 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001549 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001550 if (lastdot == NULL)
1551 return Py_None;
1552 len = lastdot - start;
1553 if (len >= MAXPATHLEN) {
1554 PyErr_SetString(PyExc_ValueError,
1555 "Module name too long");
1556 return NULL;
1557 }
1558 strncpy(buf, start, len);
1559 buf[len] = '\0';
1560 *p_buflen = len;
1561 }
1562
1563 modules = PyImport_GetModuleDict();
1564 parent = PyDict_GetItemString(modules, buf);
1565 if (parent == NULL)
1566 parent = Py_None;
1567 return parent;
1568 /* We expect, but can't guarantee, if parent != None, that:
1569 - parent.__name__ == buf
1570 - parent.__dict__ is globals
1571 If this is violated... Who cares? */
1572}
1573
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001574/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001575static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001576load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1577 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001578{
1579 char *name = *p_name;
1580 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001581 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001582 char *p;
1583 PyObject *result;
1584
1585 if (dot == NULL) {
1586 *p_name = NULL;
1587 len = strlen(name);
1588 }
1589 else {
1590 *p_name = dot+1;
1591 len = dot-name;
1592 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00001593 if (len == 0) {
1594 PyErr_SetString(PyExc_ValueError,
1595 "Empty module name");
1596 return NULL;
1597 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001598
1599 p = buf + *p_buflen;
1600 if (p != buf)
1601 *p++ = '.';
1602 if (p+len-buf >= MAXPATHLEN) {
1603 PyErr_SetString(PyExc_ValueError,
1604 "Module name too long");
1605 return NULL;
1606 }
1607 strncpy(p, name, len);
1608 p[len] = '\0';
1609 *p_buflen = p+len-buf;
1610
1611 result = import_submodule(mod, p, buf);
1612 if (result == Py_None && altmod != mod) {
1613 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001614 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00001615 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001616 if (result != NULL && result != Py_None) {
1617 if (mark_miss(buf) != 0) {
1618 Py_DECREF(result);
1619 return NULL;
1620 }
1621 strncpy(buf, name, len);
1622 buf[len] = '\0';
1623 *p_buflen = len;
1624 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001625 }
1626 if (result == NULL)
1627 return NULL;
1628
1629 if (result == Py_None) {
1630 Py_DECREF(result);
1631 PyErr_Format(PyExc_ImportError,
1632 "No module named %.200s", name);
1633 return NULL;
1634 }
1635
1636 return result;
1637}
1638
1639static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001640mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001641{
1642 PyObject *modules = PyImport_GetModuleDict();
1643 return PyDict_SetItemString(modules, name, Py_None);
1644}
1645
1646static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001647ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1648 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001649{
1650 int i;
1651
1652 if (!PyObject_HasAttrString(mod, "__path__"))
1653 return 1;
1654
1655 for (i = 0; ; i++) {
1656 PyObject *item = PySequence_GetItem(fromlist, i);
1657 int hasit;
1658 if (item == NULL) {
1659 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1660 PyErr_Clear();
1661 return 1;
1662 }
1663 return 0;
1664 }
1665 if (!PyString_Check(item)) {
1666 PyErr_SetString(PyExc_TypeError,
1667 "Item in ``from list'' not a string");
1668 Py_DECREF(item);
1669 return 0;
1670 }
1671 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00001672 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001673 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001674 /* See if the package defines __all__ */
1675 if (recursive)
1676 continue; /* Avoid endless recursion */
1677 all = PyObject_GetAttrString(mod, "__all__");
1678 if (all == NULL)
1679 PyErr_Clear();
1680 else {
1681 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1682 return 0;
1683 Py_DECREF(all);
1684 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001685 continue;
1686 }
1687 hasit = PyObject_HasAttr(mod, item);
1688 if (!hasit) {
1689 char *subname = PyString_AS_STRING(item);
1690 PyObject *submod;
1691 char *p;
1692 if (buflen + strlen(subname) >= MAXPATHLEN) {
1693 PyErr_SetString(PyExc_ValueError,
1694 "Module name too long");
1695 Py_DECREF(item);
1696 return 0;
1697 }
1698 p = buf + buflen;
1699 *p++ = '.';
1700 strcpy(p, subname);
1701 submod = import_submodule(mod, subname, buf);
1702 Py_XDECREF(submod);
1703 if (submod == NULL) {
1704 Py_DECREF(item);
1705 return 0;
1706 }
1707 }
1708 Py_DECREF(item);
1709 }
1710
Guido van Rossuma7f2e811997-10-03 15:33:32 +00001711 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001712}
1713
1714static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001715import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001716{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001717 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001718 PyObject *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001719
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001720 /* Require:
1721 if mod == None: subname == fullname
1722 else: mod.__name__ + "." + subname == fullname
1723 */
1724
1725 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001727 }
1728 else {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001729 PyObject *path;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001730 char buf[MAXPATHLEN+1];
1731 struct filedescr *fdp;
1732 FILE *fp = NULL;
1733
Guido van Rossum9c0afe51998-05-19 15:09:05 +00001734 if (mod == Py_None)
1735 path = NULL;
1736 else {
1737 path = PyObject_GetAttrString(mod, "__path__");
1738 if (path == NULL) {
1739 PyErr_Clear();
1740 Py_INCREF(Py_None);
1741 return Py_None;
1742 }
1743 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001744
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001745 buf[0] = '\0';
Guido van Rossumb68cd421998-07-01 17:36:26 +00001746 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1747 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001748 if (fdp == NULL) {
1749 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1750 return NULL;
1751 PyErr_Clear();
1752 Py_INCREF(Py_None);
1753 return Py_None;
1754 }
1755 m = load_module(fullname, fp, buf, fdp->type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001756 if (fp)
1757 fclose(fp);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001758 if (m != NULL && mod != Py_None) {
1759 if (PyObject_SetAttrString(mod, subname, m) < 0) {
1760 Py_DECREF(m);
1761 m = NULL;
1762 }
1763 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00001764 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001765
1766 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001767}
1768
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769
1770/* Re-import a module of any kind and return its module object, WITH
1771 INCREMENTED REFERENCE COUNT */
1772
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001774PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001775{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001776 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00001777 PyObject *path = NULL;
1778 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001779 char buf[MAXPATHLEN+1];
1780 struct filedescr *fdp;
1781 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 if (m == NULL || !PyModule_Check(m)) {
1784 PyErr_SetString(PyExc_TypeError,
1785 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001786 return NULL;
1787 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001789 if (name == NULL)
1790 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001791 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001792 PyErr_Format(PyExc_ImportError,
1793 "reload(): module %.200s not in sys.modules",
1794 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001795 return NULL;
1796 }
Guido van Rossum222ef561997-09-06 19:41:09 +00001797 subname = strrchr(name, '.');
1798 if (subname == NULL)
1799 subname = name;
1800 else {
1801 PyObject *parentname, *parent;
1802 parentname = PyString_FromStringAndSize(name, (subname-name));
1803 if (parentname == NULL)
1804 return NULL;
1805 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00001806 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00001807 if (parent == NULL) {
1808 PyErr_Format(PyExc_ImportError,
1809 "reload(): parent %.200s not in sys.modules",
1810 name);
1811 return NULL;
1812 }
1813 subname++;
1814 path = PyObject_GetAttrString(parent, "__path__");
1815 if (path == NULL)
1816 PyErr_Clear();
1817 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001818 buf[0] = '\0';
Guido van Rossum222ef561997-09-06 19:41:09 +00001819 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1820 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001821 if (fdp == NULL)
1822 return NULL;
1823 m = load_module(name, fp, buf, fdp->type);
1824 if (fp)
1825 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826 return m;
1827}
1828
1829
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001830/* Higher-level import emulator which emulates the "import" statement
1831 more accurately -- it invokes the __import__() function from the
1832 builtins of the current globals. This means that the import is
1833 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00001834 environment, e.g. by "rexec".
1835 A dummy list ["__doc__"] is passed as the 4th argument so that
1836 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1837 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001838
1839PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001840PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001841{
1842 static PyObject *silly_list = NULL;
1843 static PyObject *builtins_str = NULL;
1844 static PyObject *import_str = NULL;
1845 static PyObject *standard_builtins = NULL;
1846 PyObject *globals = NULL;
1847 PyObject *import = NULL;
1848 PyObject *builtins = NULL;
1849 PyObject *r = NULL;
1850
1851 /* Initialize constant string objects */
1852 if (silly_list == NULL) {
1853 import_str = PyString_InternFromString("__import__");
1854 if (import_str == NULL)
1855 return NULL;
1856 builtins_str = PyString_InternFromString("__builtins__");
1857 if (builtins_str == NULL)
1858 return NULL;
1859 silly_list = Py_BuildValue("[s]", "__doc__");
1860 if (silly_list == NULL)
1861 return NULL;
1862 }
1863
1864 /* Get the builtins from current globals */
1865 globals = PyEval_GetGlobals();
1866 if(globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00001867 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001868 builtins = PyObject_GetItem(globals, builtins_str);
1869 if (builtins == NULL)
1870 goto err;
1871 }
1872 else {
1873 /* No globals -- use standard builtins, and fake globals */
1874 PyErr_Clear();
1875
1876 if (standard_builtins == NULL) {
1877 standard_builtins =
1878 PyImport_ImportModule("__builtin__");
1879 if (standard_builtins == NULL)
1880 return NULL;
1881 }
1882
1883 builtins = standard_builtins;
1884 Py_INCREF(builtins);
1885 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1886 if (globals == NULL)
1887 goto err;
1888 }
1889
1890 /* Get the __import__ function from the builtins */
1891 if (PyDict_Check(builtins))
1892 import=PyObject_GetItem(builtins, import_str);
1893 else
1894 import=PyObject_GetAttr(builtins, import_str);
1895 if (import == NULL)
1896 goto err;
1897
1898 /* Call the _import__ function with the proper argument list */
1899 r = PyObject_CallFunction(import, "OOOO",
1900 module_name, globals, globals, silly_list);
1901
1902 err:
1903 Py_XDECREF(globals);
1904 Py_XDECREF(builtins);
1905 Py_XDECREF(import);
1906
1907 return r;
1908}
1909
1910
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001911/* Module 'imp' provides Python access to the primitives used for
1912 importing modules.
1913*/
1914
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001916imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001917{
1918 char buf[4];
1919
Guido van Rossum43713e52000-02-29 13:59:29 +00001920 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001921 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00001922 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1923 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1924 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1925 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001926
Guido van Rossum79f25d91997-04-29 20:08:16 +00001927 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001928}
1929
Guido van Rossum79f25d91997-04-29 20:08:16 +00001930static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001931imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001932{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001934 struct filedescr *fdp;
1935
Guido van Rossum43713e52000-02-29 13:59:29 +00001936 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001937 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001938 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001939 if (list == NULL)
1940 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1942 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001943 fdp->suffix, fdp->mode, fdp->type);
1944 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001945 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001946 return NULL;
1947 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948 if (PyList_Append(list, item) < 0) {
1949 Py_DECREF(list);
1950 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001951 return NULL;
1952 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001953 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001954 }
1955 return list;
1956}
1957
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001959call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001960{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001961 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001963 struct filedescr *fdp;
1964 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001965 FILE *fp = NULL;
1966
1967 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001968 if (path == Py_None)
1969 path = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001970 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
1971 if (fdp == NULL)
1972 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001973 if (fp != NULL) {
1974 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
1975 if (fob == NULL) {
1976 fclose(fp);
1977 return NULL;
1978 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001979 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001980 else {
1981 fob = Py_None;
1982 Py_INCREF(fob);
1983 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001985 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001987 return ret;
1988}
1989
Guido van Rossum79f25d91997-04-29 20:08:16 +00001990static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001991imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001992{
1993 char *name;
1994 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00001995 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001996 return NULL;
1997 return call_find_module(name, path);
1998}
1999
2000static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002001imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002002{
2003 char *name;
2004 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002006 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007 return NULL;
2008 ret = init_builtin(name);
2009 if (ret < 0)
2010 return NULL;
2011 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 Py_INCREF(Py_None);
2013 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002014 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002015 m = PyImport_AddModule(name);
2016 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002017 return m;
2018}
2019
Guido van Rossum79f25d91997-04-29 20:08:16 +00002020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002021imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022{
2023 char *name;
2024 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002025 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002026 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002027 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002028 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002029 if (ret < 0)
2030 return NULL;
2031 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002032 Py_INCREF(Py_None);
2033 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002034 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002035 m = PyImport_AddModule(name);
2036 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002037 return m;
2038}
2039
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002041imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002042{
2043 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002044
Guido van Rossum43713e52000-02-29 13:59:29 +00002045 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002046 return NULL;
2047 return get_frozen_object(name);
2048}
2049
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002051imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002052{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002053 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002054 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002055 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002056 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002057}
2058
Guido van Rossum79f25d91997-04-29 20:08:16 +00002059static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002060imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002061{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002062 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002063 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002064 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002065 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002066 p = find_frozen(name);
2067 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002068}
2069
2070static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002071get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002072{
2073 FILE *fp;
2074 if (fob == NULL) {
2075 fp = fopen(pathname, mode);
2076 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002077 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002078 }
2079 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002080 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002081 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002082 PyErr_SetString(PyExc_ValueError,
2083 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002084 }
2085 return fp;
2086}
2087
Guido van Rossum79f25d91997-04-29 20:08:16 +00002088static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002089imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002090{
2091 char *name;
2092 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002093 PyObject *fob = NULL;
2094 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002095 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002096 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002097 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002098 return NULL;
2099 fp = get_file(pathname, fob, "rb");
2100 if (fp == NULL)
2101 return NULL;
2102 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002103 if (fob == NULL)
2104 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002105 return m;
2106}
2107
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002108#ifdef HAVE_DYNAMIC_LOADING
2109
Guido van Rossum79f25d91997-04-29 20:08:16 +00002110static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002111imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002112{
2113 char *name;
2114 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002115 PyObject *fob = NULL;
2116 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002117 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002118 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002119 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002120 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002121 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002122 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002123 if (fp == NULL)
2124 return NULL;
2125 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002126 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002127 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002128}
2129
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002130#endif /* HAVE_DYNAMIC_LOADING */
2131
Guido van Rossum79f25d91997-04-29 20:08:16 +00002132static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002133imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002134{
2135 char *name;
2136 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002137 PyObject *fob = NULL;
2138 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002139 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002140 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002141 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002142 return NULL;
2143 fp = get_file(pathname, fob, "r");
2144 if (fp == NULL)
2145 return NULL;
2146 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002147 if (fob == NULL)
2148 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002149 return m;
2150}
2151
Jack Jansen9c96a921995-02-15 22:57:06 +00002152#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002153static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002154imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002155{
2156 char *name;
2157 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002158 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002159
Guido van Rossum43713e52000-02-29 13:59:29 +00002160 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002161 return NULL;
2162 m = PyMac_LoadResourceModule(name, pathname);
2163 return m;
2164}
2165#endif /* macintosh */
2166
Guido van Rossum79f25d91997-04-29 20:08:16 +00002167static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002168imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002169{
2170 char *name;
2171 PyObject *fob;
2172 char *pathname;
2173 char *suffix; /* Unused */
2174 char *mode;
2175 int type;
2176 FILE *fp;
2177
Guido van Rossum43713e52000-02-29 13:59:29 +00002178 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002179 &name, &fob, &pathname,
2180 &suffix, &mode, &type))
2181 return NULL;
2182 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2183 PyErr_Format(PyExc_ValueError,
2184 "invalid file open mode %.200s", mode);
2185 return NULL;
2186 }
2187 if (fob == Py_None)
2188 fp = NULL;
2189 else {
2190 if (!PyFile_Check(fob)) {
2191 PyErr_SetString(PyExc_ValueError,
2192 "load_module arg#2 should be a file or None");
2193 return NULL;
2194 }
2195 fp = get_file(pathname, fob, mode);
2196 if (fp == NULL)
2197 return NULL;
2198 }
2199 return load_module(name, fp, pathname, type);
2200}
2201
2202static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002203imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002204{
2205 char *name;
2206 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002207 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002208 return NULL;
2209 return load_package(name, pathname);
2210}
2211
2212static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002213imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002214{
2215 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002216 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002217 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002218 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002219}
2220
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002221/* Doc strings */
2222
2223static char doc_imp[] = "\
2224This module provides the components needed to build your own\n\
2225__import__ function. Undocumented functions are obsolete.\n\
2226";
2227
2228static char doc_find_module[] = "\
2229find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2230Search for a module. If path is omitted or None, search for a\n\
2231built-in, frozen or special module and continue search in sys.path.\n\
2232The module name cannot contain '.'; to search for a submodule of a\n\
2233package, pass the submodule name and the package's __path__.\
2234";
2235
2236static char doc_load_module[] = "\
2237load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2238Load a module, given information returned by find_module().\n\
2239The module name must include the full package name, if any.\
2240";
2241
2242static char doc_get_magic[] = "\
2243get_magic() -> string\n\
2244Return the magic number for .pyc or .pyo files.\
2245";
2246
2247static char doc_get_suffixes[] = "\
2248get_suffixes() -> [(suffix, mode, type), ...]\n\
2249Return a list of (suffix, mode, type) tuples describing the files\n\
2250that find_module() looks for.\
2251";
2252
2253static char doc_new_module[] = "\
2254new_module(name) -> module\n\
2255Create a new module. Do not enter it in sys.modules.\n\
2256The module name must include the full package name, if any.\
2257";
2258
Guido van Rossum79f25d91997-04-29 20:08:16 +00002259static PyMethodDef imp_methods[] = {
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002260 {"find_module", imp_find_module, 1, doc_find_module},
2261 {"get_magic", imp_get_magic, 1, doc_get_magic},
2262 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2263 {"load_module", imp_load_module, 1, doc_load_module},
2264 {"new_module", imp_new_module, 1, doc_new_module},
2265 /* The rest are obsolete */
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002266 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002267 {"init_builtin", imp_init_builtin, 1},
2268 {"init_frozen", imp_init_frozen, 1},
2269 {"is_builtin", imp_is_builtin, 1},
2270 {"is_frozen", imp_is_frozen, 1},
2271 {"load_compiled", imp_load_compiled, 1},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002272#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002273 {"load_dynamic", imp_load_dynamic, 1},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002274#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002275 {"load_package", imp_load_package, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00002276#ifdef macintosh
2277 {"load_resource", imp_load_resource, 1},
2278#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002279 {"load_source", imp_load_source, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002280 {NULL, NULL} /* sentinel */
2281};
2282
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002283static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002284setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002285{
2286 PyObject *v;
2287 int err;
2288
2289 v = PyInt_FromLong((long)value);
2290 err = PyDict_SetItemString(d, name, v);
2291 Py_XDECREF(v);
2292 return err;
2293}
2294
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002295void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002296initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002297{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002298 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002299
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002300 m = Py_InitModule4("imp", imp_methods, doc_imp,
2301 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002302 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002303
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002304 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2305 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2306 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2307 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2308 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2309 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2310 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2311 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002312 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002313
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002314 failure:
2315 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002316}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002317
2318
Guido van Rossumb18618d2000-05-03 23:44:39 +00002319/* API for embedding applications that want to add their own entries
2320 to the table of built-in modules. This should normally be called
2321 *before* Py_Initialize(). When the table resize fails, -1 is
2322 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002323
2324 After a similar function by Just van Rossum. */
2325
2326int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002327PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002328{
2329 static struct _inittab *our_copy = NULL;
2330 struct _inittab *p;
2331 int i, n;
2332
2333 /* Count the number of entries in both tables */
2334 for (n = 0; newtab[n].name != NULL; n++)
2335 ;
2336 if (n == 0)
2337 return 0; /* Nothing to do */
2338 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2339 ;
2340
2341 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002342 p = our_copy;
2343 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002344 if (p == NULL)
2345 return -1;
2346
2347 /* Copy the tables into the new memory */
2348 if (our_copy != PyImport_Inittab)
2349 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2350 PyImport_Inittab = our_copy = p;
2351 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2352
2353 return 0;
2354}
2355
2356/* Shorthand to add a single entry given a name and a function */
2357
2358int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002359PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002360{
2361 struct _inittab newtab[2];
2362
2363 memset(newtab, '\0', sizeof newtab);
2364
2365 newtab[0].name = name;
2366 newtab[0].initfunc = initfunc;
2367
2368 return PyImport_ExtendInittab(newtab);
2369}