blob: 851fd069306aec0eb940af6c5e2c0cb13cf35840 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Module definition and import implementation */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000039#include "marshal.h"
40#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000041#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000042#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000044#ifdef macintosh
45#include "macglue.h"
46#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000047
Guido van Rossum80bb9651996-12-05 23:27:02 +000048#ifdef HAVE_UNISTD_H
49#include <unistd.h>
50#endif
51
Guido van Rossumaee0bad1997-09-05 07:33:22 +000052/* We expect that stat exists on most systems.
53 It's confirmed on Unix, Mac and Windows.
54 If you don't have it, add #define DONT_HAVE_STAT to your config.h. */
55#ifndef DONT_HAVE_STAT
56#define HAVE_STAT
57
Guido van Rossum2571cc81999-04-07 16:07:23 +000058#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossumaee0bad1997-09-05 07:33:22 +000059#include <sys/types.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000060#endif
61#ifndef DONT_HAVE_SYS_STAT_H
Guido van Rossumaee0bad1997-09-05 07:33:22 +000062#include <sys/stat.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000063#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +000064
Guido van Rossum595d7ba1997-12-05 21:45:29 +000065#if defined(PYCC_VACPP)
66/* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
67#define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
68#endif
69
Guido van Rossumaee0bad1997-09-05 07:33:22 +000070#ifndef S_ISDIR
71#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
72#endif
73
74#endif
75
76
Guido van Rossum79f25d91997-04-29 20:08:16 +000077extern long PyOS_GetLastModificationTime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000078
Guido van Rossum6c849691994-09-26 15:47:17 +000079/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000080/* Change for each incompatible change */
81/* The value of CR and LF is incorporated so if you ever read or write
82 a .pyc file in text mode the magic number will be wrong; also, the
83 Apple MPW compiler swaps their values, botching string constants */
84/* XXX Perhaps the magic number should be frozen and a version field
85 added to the .pyc file header? */
Guido van Rossumdd5db431997-01-17 21:06:11 +000086/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
Guido van Rossum99d18251997-01-24 03:44:53 +000087#define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000088
Guido van Rossum25ce5661997-08-02 03:10:38 +000089/* See _PyImport_FixupExtension() below */
90static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000091
Guido van Rossum771c6c81997-10-31 18:37:24 +000092/* This table is defined in config.c: */
93extern struct _inittab _PyImport_Inittab[];
94
95struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000096
Guido van Rossum1ae940a1995-01-02 19:04:15 +000097/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
99void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100_PyImport_Init()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
Guido van Rossum0824f631997-03-11 18:37:35 +0000102 if (Py_OptimizeFlag) {
103 /* Replace ".pyc" with ".pyo" in import_filetab */
104 struct filedescr *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000105 for (p = _PyImport_Filetab; p->suffix != NULL; p++) {
Guido van Rossum0824f631997-03-11 18:37:35 +0000106 if (strcmp(p->suffix, ".pyc") == 0)
107 p->suffix = ".pyo";
108 }
109 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110}
111
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112void
113_PyImport_Fini()
114{
115 Py_XDECREF(extensions);
116 extensions = NULL;
117}
118
119
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000120/* Locking primitives to prevent parallel imports of the same module
121 in different threads to return with a partially loaded module.
122 These calls are serialized by the global interpreter lock. */
123
124#ifdef WITH_THREAD
125
Guido van Rossum49b56061998-10-01 20:42:43 +0000126#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000127
Guido van Rossum65d5b571998-12-21 19:32:43 +0000128static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000129static long import_lock_thread = -1;
130static int import_lock_level = 0;
131
132static void
133lock_import()
134{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000135 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000136 if (me == -1)
137 return; /* Too bad */
138 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000139 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000140 if (import_lock_thread == me) {
141 import_lock_level++;
142 return;
143 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000144 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000145 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000146 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000147 PyEval_RestoreThread(tstate);
148 }
149 import_lock_thread = me;
150 import_lock_level = 1;
151}
152
153static void
154unlock_import()
155{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000156 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000157 if (me == -1)
158 return; /* Too bad */
159 if (import_lock_thread != me)
160 Py_FatalError("unlock_import: not holding the import lock");
161 import_lock_level--;
162 if (import_lock_level == 0) {
163 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000164 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000165 }
166}
167
168#else
169
170#define lock_import()
171#define unlock_import()
172
173#endif
174
Guido van Rossum25ce5661997-08-02 03:10:38 +0000175/* Helper for sys */
176
177PyObject *
178PyImport_GetModuleDict()
179{
180 PyInterpreterState *interp = PyThreadState_Get()->interp;
181 if (interp->modules == NULL)
182 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
183 return interp->modules;
184}
185
Guido van Rossum3f5da241990-12-20 15:06:42 +0000186
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000187/* List of names to clear in sys */
188static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000189 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000190 "exc_type", "exc_value", "exc_traceback",
191 "last_type", "last_value", "last_traceback",
192 NULL
193};
194
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000195static char* sys_files[] = {
196 "stdin", "__stdin__",
197 "stdout", "__stdout__",
198 "stderr", "__stderr__",
199 NULL
200};
201
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000202
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000203/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000204
Guido van Rossum3f5da241990-12-20 15:06:42 +0000205void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206PyImport_Cleanup()
Guido van Rossum3f5da241990-12-20 15:06:42 +0000207{
Guido van Rossum758eec01998-01-19 21:58:26 +0000208 int pos, ndone;
209 char *name;
210 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000212 PyObject *modules = interp->modules;
213
214 if (modules == NULL)
215 return; /* Already done */
216
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000217 /* Delete some special variables first. These are common
218 places where user values hide and people complain when their
219 destructors fail. Since the modules containing them are
220 deleted *last* of all, they would come too late in the normal
221 destruction order. Sigh. */
222
223 value = PyDict_GetItemString(modules, "__builtin__");
224 if (value != NULL && PyModule_Check(value)) {
225 dict = PyModule_GetDict(value);
226 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000227 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000228 PyDict_SetItemString(dict, "_", Py_None);
229 }
230 value = PyDict_GetItemString(modules, "sys");
231 if (value != NULL && PyModule_Check(value)) {
232 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000233 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000234 dict = PyModule_GetDict(value);
235 for (p = sys_deletes; *p != NULL; p++) {
236 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000237 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000238 PyDict_SetItemString(dict, *p, Py_None);
239 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000240 for (p = sys_files; *p != NULL; p+=2) {
241 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000242 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000243 v = PyDict_GetItemString(dict, *(p+1));
244 if (v == NULL)
245 v = Py_None;
246 PyDict_SetItemString(dict, *p, v);
247 }
248 }
249
250 /* First, delete __main__ */
251 value = PyDict_GetItemString(modules, "__main__");
252 if (value != NULL && PyModule_Check(value)) {
253 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000254 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000255 _PyModule_Clear(value);
256 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000257 }
258
Guido van Rossum758eec01998-01-19 21:58:26 +0000259 /* The special treatment of __builtin__ here is because even
260 when it's not referenced as a module, its dictionary is
261 referenced by almost every module's __builtins__. Since
262 deleting a module clears its dictionary (even if there are
263 references left to it), we need to delete the __builtin__
264 module last. Likewise, we don't delete sys until the very
265 end because it is implicitly referenced (e.g. by print).
266
267 Also note that we 'delete' modules by replacing their entry
268 in the modules dict with None, rather than really deleting
269 them; this avoids a rehash of the modules dictionary and
270 also marks them as "non existent" so they won't be
271 re-imported. */
272
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000273 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000274 one (skipping __builtin__ and sys) and delete them */
275 do {
276 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000277 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000278 while (PyDict_Next(modules, &pos, &key, &value)) {
279 if (value->ob_refcnt != 1)
280 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000281 if (PyString_Check(key) && PyModule_Check(value)) {
282 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000283 if (strcmp(name, "__builtin__") == 0)
284 continue;
285 if (strcmp(name, "sys") == 0)
286 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000287 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000288 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000289 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000290 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000291 PyDict_SetItem(modules, key, Py_None);
292 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000293 }
294 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000295 } while (ndone > 0);
296
Guido van Rossum758eec01998-01-19 21:58:26 +0000297 /* Next, delete all modules (still skipping __builtin__ and sys) */
298 pos = 0;
299 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000300 if (PyString_Check(key) && PyModule_Check(value)) {
301 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000302 if (strcmp(name, "__builtin__") == 0)
303 continue;
304 if (strcmp(name, "sys") == 0)
305 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000306 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000307 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000308 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000309 PyDict_SetItem(modules, key, Py_None);
310 }
311 }
312
313 /* Next, delete sys and __builtin__ (in that order) */
314 value = PyDict_GetItemString(modules, "sys");
315 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000316 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000317 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000318 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000319 PyDict_SetItemString(modules, "sys", Py_None);
320 }
321 value = PyDict_GetItemString(modules, "__builtin__");
322 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000323 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000324 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000325 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000326 PyDict_SetItemString(modules, "__builtin__", Py_None);
327 }
328
329 /* Finally, clear and delete the modules directory */
330 PyDict_Clear(modules);
331 interp->modules = NULL;
332 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000333}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000334
335
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000336/* Helper for pythonrun.c -- return magic number */
337
338long
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339PyImport_GetMagicNumber()
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000340{
341 return MAGIC;
342}
343
344
Guido van Rossum25ce5661997-08-02 03:10:38 +0000345/* Magic for extension modules (built-in as well as dynamically
346 loaded). To prevent initializing an extension module more than
347 once, we keep a static dictionary 'extensions' keyed by module name
348 (for built-in modules) or by filename (for dynamically loaded
349 modules), containing these modules. A copy od the module's
350 dictionary is stored by calling _PyImport_FixupExtension()
351 immediately after the module initialization function succeeds. A
352 copy can be retrieved from there by calling
353 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000354
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356_PyImport_FixupExtension(name, filename)
357 char *name;
358 char *filename;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000359{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000360 PyObject *modules, *mod, *dict, *copy;
361 if (extensions == NULL) {
362 extensions = PyDict_New();
363 if (extensions == NULL)
364 return NULL;
365 }
366 modules = PyImport_GetModuleDict();
367 mod = PyDict_GetItemString(modules, name);
368 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000369 PyErr_Format(PyExc_SystemError,
370 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000371 return NULL;
372 }
373 dict = PyModule_GetDict(mod);
374 if (dict == NULL)
375 return NULL;
376 copy = PyObject_CallMethod(dict, "copy", "");
377 if (copy == NULL)
378 return NULL;
379 PyDict_SetItemString(extensions, filename, copy);
380 Py_DECREF(copy);
381 return copy;
382}
383
384PyObject *
385_PyImport_FindExtension(name, filename)
386 char *name;
387 char *filename;
388{
389 PyObject *dict, *mod, *mdict, *result;
390 if (extensions == NULL)
391 return NULL;
392 dict = PyDict_GetItemString(extensions, filename);
393 if (dict == NULL)
394 return NULL;
395 mod = PyImport_AddModule(name);
396 if (mod == NULL)
397 return NULL;
398 mdict = PyModule_GetDict(mod);
399 if (mdict == NULL)
400 return NULL;
401 result = PyObject_CallMethod(mdict, "update", "O", dict);
402 if (result == NULL)
403 return NULL;
404 Py_DECREF(result);
405 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000406 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000407 name, filename);
408 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000409}
410
411
412/* Get the module object corresponding to a module name.
413 First check the modules dictionary if there's one there,
414 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000415 Because the former action is most common, THIS DOES NOT RETURN A
416 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418PyObject *
419PyImport_AddModule(name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420 char *name;
421{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000423 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000424
Guido van Rossum25ce5661997-08-02 03:10:38 +0000425 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000427 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000429 if (m == NULL)
430 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000431 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000433 return NULL;
434 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000436
437 return m;
438}
439
440
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000441/* Execute a code object in a module and return the module object
442 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000443
Guido van Rossum79f25d91997-04-29 20:08:16 +0000444PyObject *
445PyImport_ExecCodeModule(name, co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000448{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000449 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
450}
451
452PyObject *
453PyImport_ExecCodeModuleEx(name, co, pathname)
454 char *name;
455 PyObject *co;
456 char *pathname;
457{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000458 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000460
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000462 if (m == NULL)
463 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 d = PyModule_GetDict(m);
465 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
466 if (PyDict_SetItemString(d, "__builtins__",
467 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000468 return NULL;
469 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000470 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000471 v = NULL;
472 if (pathname != NULL) {
473 v = PyString_FromString(pathname);
474 if (v == NULL)
475 PyErr_Clear();
476 }
477 if (v == NULL) {
478 v = ((PyCodeObject *)co)->co_filename;
479 Py_INCREF(v);
480 }
481 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000482 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000483 Py_DECREF(v);
484
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000485 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486 if (v == NULL)
487 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000488 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000489
Guido van Rossum25ce5661997-08-02 03:10:38 +0000490 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000491 PyErr_Format(PyExc_ImportError,
492 "Loaded module %.200s not found in sys.modules",
493 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000494 return NULL;
495 }
496
Guido van Rossum79f25d91997-04-29 20:08:16 +0000497 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498
499 return m;
500}
501
502
503/* Given a pathname for a Python source file, fill a buffer with the
504 pathname for the corresponding compiled file. Return the pathname
505 for the compiled file, or NULL if there's no space in the buffer.
506 Doesn't set an exception. */
507
508static char *
509make_compiled_pathname(pathname, buf, buflen)
510 char *pathname;
511 char *buf;
512 int buflen;
513{
514 int len;
515
516 len = strlen(pathname);
517 if (len+2 > buflen)
518 return NULL;
519 strcpy(buf, pathname);
Guido van Rossum0824f631997-03-11 18:37:35 +0000520 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000521
522 return buf;
523}
524
525
526/* Given a pathname for a Python source file, its time of last
527 modification, and a pathname for a compiled file, check whether the
528 compiled file represents the same version of the source. If so,
529 return a FILE pointer for the compiled file, positioned just after
530 the header; if not, return NULL.
531 Doesn't set an exception. */
532
533static FILE *
534check_compiled_module(pathname, mtime, cpathname)
535 char *pathname;
536 long mtime;
537 char *cpathname;
538{
539 FILE *fp;
540 long magic;
541 long pyc_mtime;
542
543 fp = fopen(cpathname, "rb");
544 if (fp == NULL)
545 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547 if (magic != MAGIC) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000549 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 fclose(fp);
551 return NULL;
552 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000553 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000556 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557 fclose(fp);
558 return NULL;
559 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000560 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000561 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562 return fp;
563}
564
565
566/* Read a code object from a file and check it for validity */
567
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568static PyCodeObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000569read_compiled_module(cpathname, fp)
570 char *cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 FILE *fp;
572{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 co = PyMarshal_ReadObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 if (co == NULL || !PyCode_Check(co)) {
578 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000579 PyErr_Format(PyExc_ImportError,
580 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582 return NULL;
583 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000584 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585}
586
587
588/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000589 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592load_compiled_module(name, cpathname, fp)
593 char *name;
594 char *cpathname;
595 FILE *fp;
596{
597 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598 PyCodeObject *co;
599 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000602 if (magic != MAGIC) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000603 PyErr_Format(PyExc_ImportError,
604 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 return NULL;
606 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000608 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 if (co == NULL)
610 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000612 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000614 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616
617 return m;
618}
619
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620/* Parse a source file and return the corresponding code object */
621
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622static PyCodeObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623parse_source_module(pathname, fp)
624 char *pathname;
625 FILE *fp;
626{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628 node *n;
629
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000630 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631 if (n == NULL)
632 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 co = PyNode_Compile(n, pathname);
634 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635
636 return co;
637}
638
639
640/* Write a compiled module to a file, placing the time of last
641 modification of its source into the header.
642 Errors are ignored, if a write error occurs an attempt is made to
643 remove the file. */
644
645static void
646write_compiled_module(co, cpathname, mtime)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 char *cpathname;
649 long mtime;
650{
651 FILE *fp;
652
653 fp = fopen(cpathname, "wb");
654 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000655 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000656 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 "# can't create %s\n", cpathname);
658 return;
659 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 PyMarshal_WriteLongToFile(MAGIC, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 PyMarshal_WriteLongToFile(0L, fp);
663 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000666 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667 /* Don't keep partial file */
668 fclose(fp);
669 (void) unlink(cpathname);
670 return;
671 }
672 /* Now write the true mtime */
673 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000674 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000675 fflush(fp);
676 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000678 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000679#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000680 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681#endif
682}
683
684
685/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000686 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
687 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000690load_source_module(name, pathname, fp)
691 char *name;
692 char *pathname;
693 FILE *fp;
694{
695 long mtime;
696 FILE *fpc;
697 char buf[MAXPATHLEN+1];
698 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 PyCodeObject *co;
700 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000702 mtime = PyOS_GetLastModificationTime(pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
704 if (cpathname != NULL &&
705 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000706 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707 fclose(fpc);
708 if (co == NULL)
709 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000711 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000713 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 }
715 else {
716 co = parse_source_module(pathname, fp);
717 if (co == NULL)
718 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000720 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721 name, pathname);
722 write_compiled_module(co, cpathname, mtime);
723 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000724 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726
727 return m;
728}
729
730
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000731/* Forward */
732static PyObject *load_module Py_PROTO((char *, FILE *, char *, int));
733static struct filedescr *find_module Py_PROTO((char *, PyObject *,
734 char *, int, FILE **));
Guido van Rossuma86f77d1997-09-09 18:53:47 +0000735static struct _frozen *find_frozen Py_PROTO((char *name));
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000736
737/* Load a package and return its module object WITH INCREMENTED
738 REFERENCE COUNT */
739
740static PyObject *
741load_package(name, pathname)
742 char *name;
743 char *pathname;
744{
745 PyObject *m, *d, *file, *path;
746 int err;
747 char buf[MAXPATHLEN+1];
748 FILE *fp = NULL;
749 struct filedescr *fdp;
750
751 m = PyImport_AddModule(name);
752 if (m == NULL)
753 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000754 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000755 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000756 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000757 d = PyModule_GetDict(m);
758 file = PyString_FromString(pathname);
759 if (file == NULL)
760 return NULL;
761 path = Py_BuildValue("[O]", file);
762 if (path == NULL) {
763 Py_DECREF(file);
764 return NULL;
765 }
766 err = PyDict_SetItemString(d, "__file__", file);
767 if (err == 0)
768 err = PyDict_SetItemString(d, "__path__", path);
769 if (err != 0) {
770 m = NULL;
771 goto cleanup;
772 }
773 buf[0] = '\0';
774 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
775 if (fdp == NULL) {
776 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
777 PyErr_Clear();
778 }
779 else
780 m = NULL;
781 goto cleanup;
782 }
783 m = load_module(name, fp, buf, fdp->type);
784 if (fp != NULL)
785 fclose(fp);
786 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000787 Py_XDECREF(path);
788 Py_XDECREF(file);
789 return m;
790}
791
792
793/* Helper to test for built-in module */
794
795static int
796is_builtin(name)
797 char *name;
798{
799 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000800 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
801 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
802 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000803 return -1;
804 else
805 return 1;
806 }
807 }
808 return 0;
809}
810
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000811
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812/* Search the path (default sys.path) for a module. Return the
813 corresponding filedescr struct, and (via return arguments) the
814 pathname and an open file. Return NULL if the module is not found. */
815
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000816#ifdef MS_COREDLL
817extern FILE *PyWin_FindRegisteredModule();
818#endif
819
Guido van Rossum0980bd91998-02-13 17:18:36 +0000820#ifdef CHECK_IMPORT_CASE
821static int check_case(char *, int, int, char *);
822#endif
823
Guido van Rossum197346f1997-10-31 18:38:52 +0000824static int find_init_module Py_PROTO((char *)); /* Forward */
825
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826static struct filedescr *
Guido van Rossum0506a431998-08-11 15:07:39 +0000827find_module(realname, path, buf, buflen, p_fp)
828 char *realname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829 PyObject *path;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000830 /* Output parameters: */
831 char *buf;
832 int buflen;
833 FILE **p_fp;
834{
835 int i, npath, len, namelen;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000836 struct _frozen *f;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000837 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000838 FILE *fp = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000839 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +0000840 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
841 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
842 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +0000843 char name[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000844
Guido van Rossum0506a431998-08-11 15:07:39 +0000845 strcpy(name, realname);
846
847 if (path != NULL && PyString_Check(path)) {
848 /* Submodule of "frozen" package:
849 Set name to the fullname, path to NULL
850 and continue as "usual" */
851 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
852 PyErr_SetString(PyExc_ImportError,
853 "full frozen module name too long");
854 return NULL;
855 }
856 strcpy(buf, PyString_AsString(path));
857 strcat(buf, ".");
858 strcat(buf, name);
859 strcpy(name, buf);
860 path = NULL;
861 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000862 if (path == NULL) {
863 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000864 strcpy(buf, name);
865 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000866 }
Guido van Rossuma5568d31998-03-05 03:45:08 +0000867 if ((f = find_frozen(name)) != NULL) {
868 strcpy(buf, name);
869 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000870 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871
Guido van Rossumac279101996-08-22 23:10:58 +0000872#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000873 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
874 if (fp != NULL) {
875 *p_fp = fp;
876 return fdp;
877 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000878#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000879 path = PySys_GetObject("path");
880 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 if (path == NULL || !PyList_Check(path)) {
882 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +0000883 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000884 return NULL;
885 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000887 namelen = strlen(name);
888 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 PyObject *v = PyList_GetItem(path, i);
890 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 len = PyString_Size(v);
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000893 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894 continue; /* Too long */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 strcpy(buf, PyString_AsString(v));
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000896 if ((int)strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000897 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000898#ifdef macintosh
Guido van Rossum741689d1997-08-12 14:53:39 +0000899#ifdef INTERN_STRINGS
900 /*
901 ** Speedup: each sys.path item is interned, and
902 ** FindResourceModule remembers which items refer to
903 ** folders (so we don't have to bother trying to look
904 ** into them for resources).
905 */
906 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
907 v = PyList_GET_ITEM(path, i);
908#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000909 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000910 static struct filedescr resfiledescr =
911 {"", "", PY_RESOURCE};
Jack Jansen9c96a921995-02-15 22:57:06 +0000912
913 return &resfiledescr;
914 }
Guido van Rossum0f84a341998-08-06 13:36:01 +0000915 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
916 static struct filedescr resfiledescr =
917 {"", "", PY_CODERESOURCE};
918
919 return &resfiledescr;
920 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000921#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000922 if (len > 0 && buf[len-1] != SEP
923#ifdef ALTSEP
924 && buf[len-1] != ALTSEP
925#endif
926 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000928#ifdef IMPORT_8x3_NAMES
Guido van Rossume8c27bb1997-12-17 18:57:16 +0000929 /* see if we are searching in directory dos-8x3 */
930 if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){
Guido van Rossum40f470f1996-05-23 22:51:04 +0000931 int j;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 char ch; /* limit name to 8 lower-case characters */
Guido van Rossum40f470f1996-05-23 22:51:04 +0000933 for (j = 0; (ch = name[j]) && j < 8; j++)
934 if (isupper(ch))
935 buf[len++] = tolower(ch);
936 else
937 buf[len++] = ch;
938 }
Guido van Rossume8c27bb1997-12-17 18:57:16 +0000939 else /* Not in dos-8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000940#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000941 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000942 strcpy(buf+len, name);
943 len += namelen;
944 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000945#ifdef HAVE_STAT
946 if (stat(buf, &statbuf) == 0) {
Guido van Rossum197346f1997-10-31 18:38:52 +0000947 if (S_ISDIR(statbuf.st_mode)) {
Guido van Rossum0980bd91998-02-13 17:18:36 +0000948 if (find_init_module(buf)) {
949#ifdef CHECK_IMPORT_CASE
950 if (!check_case(buf, len, namelen,
951 name))
952 return NULL;
953#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000954 return &fd_package;
Guido van Rossum0980bd91998-02-13 17:18:36 +0000955 }
Guido van Rossum197346f1997-10-31 18:38:52 +0000956 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957 }
958#else
959 /* XXX How are you going to test for directories? */
960#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +0000961#ifdef macintosh
962 fdp = PyMac_FindModuleExtension(buf, &len, name);
963 if (fdp)
964 fp = fopen(buf, fdp->mode);
965#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000969 PySys_WriteStderr("# trying %s\n", buf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000970 fp = fopen(buf, fdp->mode);
971 if (fp != NULL)
972 break;
973 }
Guido van Rossum741689d1997-08-12 14:53:39 +0000974#endif /* !macintosh */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000975 if (fp != NULL)
976 break;
977 }
978 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000979 PyErr_Format(PyExc_ImportError,
980 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981 return NULL;
982 }
Guido van Rossum0980bd91998-02-13 17:18:36 +0000983#ifdef CHECK_IMPORT_CASE
984 if (!check_case(buf, len, namelen, name)) {
985 fclose(fp);
986 return NULL;
987 }
988#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000989
990 *p_fp = fp;
991 return fdp;
992}
993
Guido van Rossum0980bd91998-02-13 17:18:36 +0000994#ifdef CHECK_IMPORT_CASE
995
996#ifdef MS_WIN32
997#include <windows.h>
Guido van Rossum323bf5e1998-06-24 03:54:06 +0000998#include <ctype.h>
999
1000static int
1001allcaps8x3(s)
1002 char *s;
1003{
1004 /* Return 1 if s is an 8.3 filename in ALLCAPS */
1005 char c;
1006 char *dot = strchr(s, '.');
1007 char *end = strchr(s, '\0');
1008 if (dot != NULL) {
1009 if (dot-s > 8)
1010 return 1; /* More than 8 before '.' */
1011 if (end-dot > 4)
1012 return 1; /* More than 3 after '.' */
1013 end = strchr(dot+1, '.');
1014 if (end != NULL)
1015 return 1; /* More than one dot */
1016 }
1017 else if (end-s > 8)
1018 return 1; /* More than 8 and no dot */
1019 while ((c = *s++)) {
1020 if (islower(c))
1021 return 0;
1022 }
1023 return 1;
1024}
1025
Guido van Rossum0980bd91998-02-13 17:18:36 +00001026static int
1027check_case(char *buf, int len, int namelen, char *name)
1028{
1029 WIN32_FIND_DATA data;
1030 HANDLE h;
1031 if (getenv("PYTHONCASEOK") != NULL)
1032 return 1;
1033 h = FindFirstFile(buf, &data);
1034 if (h == INVALID_HANDLE_VALUE) {
1035 PyErr_Format(PyExc_NameError,
1036 "Can't find file for module %.100s\n(filename %.300s)",
1037 name, buf);
1038 return 0;
1039 }
1040 FindClose(h);
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001041 if (allcaps8x3(data.cFileName)) {
1042 /* Skip the test if the filename is ALL.CAPS. This can
1043 happen in certain circumstances beyond our control,
1044 e.g. when software is installed under NT on a FAT
1045 filesystem and then the same FAT filesystem is used
1046 under Windows 95. */
1047 return 1;
1048 }
Guido van Rossum0980bd91998-02-13 17:18:36 +00001049 if (strncmp(data.cFileName, name, namelen) != 0) {
1050 strcpy(buf+len-namelen, data.cFileName);
1051 PyErr_Format(PyExc_NameError,
1052 "Case mismatch for module name %.100s\n(filename %.300s)",
1053 name, buf);
1054 return 0;
1055 }
1056 return 1;
1057}
1058#endif /* MS_WIN32 */
1059
1060#ifdef macintosh
1061#include <TextUtils.h>
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001062#ifdef USE_GUSI
1063#include "TFileSpec.h" /* for Path2FSSpec() */
1064#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001065static int
1066check_case(char *buf, int len, int namelen, char *name)
1067{
1068 FSSpec fss;
1069 OSErr err;
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001070#ifndef USE_GUSI
1071 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
1072#else
1073 /* GUSI's Path2FSSpec() resolves all possible aliases nicely on
1074 the way, which is fine for all directories, but here we need
1075 the original name of the alias file (say, Dlg.ppc.slb, not
1076 toolboxmodules.ppc.slb). */
1077 char *colon;
1078 err = Path2FSSpec(buf, &fss);
1079 if (err == noErr) {
1080 colon = strrchr(buf, ':'); /* find filename */
1081 if (colon != NULL)
1082 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1083 Pstring(colon+1), &fss);
1084 else
1085 err = FSMakeFSSpec(fss.vRefNum, fss.parID,
1086 fss.name, &fss);
1087 }
1088#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001089 if (err) {
1090 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001091 "Can't find file for module %.100s\n(filename %.300s)",
1092 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001093 return 0;
1094 }
1095 p2cstr(fss.name);
1096 if ( strncmp(name, (char *)fss.name, namelen) != 0 ) {
1097 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001098 "Case mismatch for module name %.100s\n(filename %.300s)",
1099 name, fss.name);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001100 return 0;
1101 }
1102 return 1;
1103}
1104#endif /* macintosh */
1105
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001106#ifdef DJGPP
Guido van Rossumaeca79b1998-02-18 16:21:00 +00001107#include <dir.h>
1108
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001109static int
1110check_case(char *buf, int len, int namelen, char *name)
1111{
1112 struct ffblk ffblk;
1113 int done;
1114
1115 if (getenv("PYTHONCASEOK") != NULL)
1116 return 1;
Guido van Rossumaeca79b1998-02-18 16:21:00 +00001117 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001118 if (done) {
1119 PyErr_Format(PyExc_NameError,
1120 "Can't find file for module %.100s\n(filename %.300s)",
1121 name, buf);
1122 return 0;
1123 }
1124
1125 if (strncmp(ffblk.ff_name, name, namelen) != 0) {
1126 strcpy(buf+len-namelen, ffblk.ff_name);
1127 PyErr_Format(PyExc_NameError,
1128 "Case mismatch for module name %.100s\n(filename %.300s)",
1129 name, buf);
1130 return 0;
1131 }
1132 return 1;
1133}
1134#endif
1135
Guido van Rossum8766a771998-04-10 23:44:32 +00001136#endif /* CHECK_IMPORT_CASE */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001137
Guido van Rossum197346f1997-10-31 18:38:52 +00001138#ifdef HAVE_STAT
1139/* Helper to look for __init__.py or __init__.py[co] in potential package */
1140static int
1141find_init_module(buf)
1142 char *buf;
1143{
1144 int save_len = strlen(buf);
1145 int i = save_len;
1146 struct stat statbuf;
1147
1148 if (save_len + 13 >= MAXPATHLEN)
1149 return 0;
1150 buf[i++] = SEP;
1151 strcpy(buf+i, "__init__.py");
1152 if (stat(buf, &statbuf) == 0) {
1153 buf[save_len] = '\0';
1154 return 1;
1155 }
1156 i += strlen(buf+i);
1157 if (Py_OptimizeFlag)
1158 strcpy(buf+i, "o");
1159 else
1160 strcpy(buf+i, "c");
1161 if (stat(buf, &statbuf) == 0) {
1162 buf[save_len] = '\0';
1163 return 1;
1164 }
1165 buf[save_len] = '\0';
1166 return 0;
1167}
1168#endif /* HAVE_STAT */
1169
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001170
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001171static int init_builtin Py_PROTO((char *)); /* Forward */
1172
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001173/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001174 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001177load_module(name, fp, buf, type)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 char *name;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001179 FILE *fp;
1180 char *buf;
1181 int type;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001183 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001185 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001186
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001187 /* First check that there's an open file (if we need one) */
1188 switch (type) {
1189 case PY_SOURCE:
1190 case PY_COMPILED:
1191 if (fp == NULL) {
1192 PyErr_Format(PyExc_ValueError,
1193 "file object required for import (type code %d)",
1194 type);
1195 return NULL;
1196 }
1197 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001198
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001199 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200
1201 case PY_SOURCE:
1202 m = load_source_module(name, buf, fp);
1203 break;
1204
1205 case PY_COMPILED:
1206 m = load_compiled_module(name, buf, fp);
1207 break;
1208
1209 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001211 break;
1212
Jack Jansen9c96a921995-02-15 22:57:06 +00001213#ifdef macintosh
1214 case PY_RESOURCE:
1215 m = PyMac_LoadResourceModule(name, buf);
1216 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001217 case PY_CODERESOURCE:
1218 m = PyMac_LoadCodeResourceModule(name, buf);
1219 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001220#endif
1221
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001222 case PKG_DIRECTORY:
1223 m = load_package(name, buf);
1224 break;
1225
1226 case C_BUILTIN:
1227 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001228 if (buf != NULL && buf[0] != '\0')
1229 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001230 if (type == C_BUILTIN)
1231 err = init_builtin(name);
1232 else
1233 err = PyImport_ImportFrozenModule(name);
1234 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001235 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001236 if (err == 0) {
1237 PyErr_Format(PyExc_ImportError,
1238 "Purported %s module %.200s not found",
1239 type == C_BUILTIN ?
1240 "builtin" : "frozen",
1241 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001242 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001243 }
1244 modules = PyImport_GetModuleDict();
1245 m = PyDict_GetItemString(modules, name);
1246 if (m == NULL) {
1247 PyErr_Format(
1248 PyExc_ImportError,
1249 "%s module %.200s not properly initialized",
1250 type == C_BUILTIN ?
1251 "builtin" : "frozen",
1252 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001253 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001254 }
1255 Py_INCREF(m);
1256 break;
1257
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001258 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001259 PyErr_Format(PyExc_ImportError,
1260 "Don't know how to import %.200s (type code %d)",
1261 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001262 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001263
1264 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001265
1266 return m;
1267}
1268
1269
1270/* Initialize a built-in module.
1271 Return 1 for succes, 0 if the module is not found, and -1 with
1272 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001273
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001274static int
1275init_builtin(name)
1276 char *name;
1277{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001278 struct _inittab *p;
1279 PyObject *mod;
1280
1281 if ((mod = _PyImport_FindExtension(name, name)) != NULL)
1282 return 1;
1283
Guido van Rossum771c6c81997-10-31 18:37:24 +00001284 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001285 if (strcmp(name, p->name) == 0) {
1286 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001287 PyErr_Format(PyExc_ImportError,
1288 "Cannot re-init internal module %.200s",
1289 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001290 return -1;
1291 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001292 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001293 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001294 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001295 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001296 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001297 if (_PyImport_FixupExtension(name, name) == NULL)
1298 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001299 return 1;
1300 }
1301 }
1302 return 0;
1303}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001304
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001305
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001306/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001308static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001309find_frozen(name)
1310 char *name;
1311{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001312 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001315 if (p->name == NULL)
1316 return NULL;
1317 if (strcmp(p->name, name) == 0)
1318 break;
1319 }
1320 return p;
1321}
1322
Guido van Rossum79f25d91997-04-29 20:08:16 +00001323static PyObject *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001324get_frozen_object(name)
1325 char *name;
1326{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001327 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001328 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001329
1330 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001331 PyErr_Format(PyExc_ImportError,
1332 "No such frozen object named %.200s",
1333 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001334 return NULL;
1335 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001336 size = p->size;
1337 if (size < 0)
1338 size = -size;
1339 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001340}
1341
1342/* Initialize a frozen module.
1343 Return 1 for succes, 0 if the module is not found, and -1 with
1344 an exception set if the initialization failed.
1345 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001346
1347int
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348PyImport_ImportFrozenModule(name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001349 char *name;
1350{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001351 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001352 PyObject *co;
1353 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001354 int ispackage;
1355 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001356
1357 if (p == NULL)
1358 return 0;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001359 size = p->size;
1360 ispackage = (size < 0);
1361 if (ispackage)
1362 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001364 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001365 name, ispackage ? " package" : "");
1366 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001367 if (co == NULL)
1368 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001369 if (!PyCode_Check(co)) {
1370 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001371 PyErr_Format(PyExc_TypeError,
1372 "frozen object %.200s is not a code object",
1373 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001374 return -1;
1375 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001376 if (ispackage) {
1377 /* Set __path__ to the package name */
1378 PyObject *d, *s;
1379 int err;
1380 m = PyImport_AddModule(name);
1381 if (m == NULL)
1382 return -1;
1383 d = PyModule_GetDict(m);
1384 s = PyString_InternFromString(name);
1385 if (s == NULL)
1386 return -1;
1387 err = PyDict_SetItemString(d, "__path__", s);
1388 Py_DECREF(s);
1389 if (err != 0)
1390 return err;
1391 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001392 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001394 if (m == NULL)
1395 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001396 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001397 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001398}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001399
1400
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001401/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001402 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001403
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404PyObject *
1405PyImport_ImportModule(name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001406 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001407{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001408 static PyObject *fromlist = NULL;
1409 if (fromlist == NULL && strchr(name, '.') != NULL) {
1410 fromlist = Py_BuildValue("[s]", "*");
1411 if (fromlist == NULL)
1412 return NULL;
1413 }
1414 return PyImport_ImportModuleEx(name, NULL, NULL, fromlist);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001415}
1416
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001417/* Forward declarations for helper routines */
1418static PyObject *get_parent Py_PROTO((PyObject *globals,
1419 char *buf, int *p_buflen));
1420static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod,
1421 char **p_name, char *buf, int *p_buflen));
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001422static int mark_miss Py_PROTO((char *name));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001423static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist,
Guido van Rossum9905ef91997-09-08 16:07:11 +00001424 char *buf, int buflen, int recursive));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001425static PyObject * import_submodule Py_PROTO((PyObject *mod,
1426 char *name, char *fullname));
1427
1428/* The Magnum Opus of dotted-name import :-) */
1429
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001430static PyObject *
1431import_module_ex(name, globals, locals, fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001432 char *name;
1433 PyObject *globals;
1434 PyObject *locals;
1435 PyObject *fromlist;
1436{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001437 char buf[MAXPATHLEN+1];
1438 int buflen = 0;
1439 PyObject *parent, *head, *next, *tail;
1440
1441 parent = get_parent(globals, buf, &buflen);
1442 if (parent == NULL)
1443 return NULL;
1444
1445 head = load_next(parent, Py_None, &name, buf, &buflen);
1446 if (head == NULL)
1447 return NULL;
1448
1449 tail = head;
1450 Py_INCREF(tail);
1451 while (name) {
1452 next = load_next(tail, tail, &name, buf, &buflen);
1453 Py_DECREF(tail);
1454 if (next == NULL) {
1455 Py_DECREF(head);
1456 return NULL;
1457 }
1458 tail = next;
1459 }
1460
1461 if (fromlist != NULL) {
1462 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1463 fromlist = NULL;
1464 }
1465
1466 if (fromlist == NULL) {
1467 Py_DECREF(tail);
1468 return head;
1469 }
1470
1471 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001472 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001473 Py_DECREF(tail);
1474 return NULL;
1475 }
1476
1477 return tail;
1478}
1479
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001480PyObject *
1481PyImport_ImportModuleEx(name, globals, locals, fromlist)
1482 char *name;
1483 PyObject *globals;
1484 PyObject *locals;
1485 PyObject *fromlist;
1486{
1487 PyObject *result;
1488 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001489 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001490 unlock_import();
1491 return result;
1492}
1493
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001494static PyObject *
1495get_parent(globals, buf, p_buflen)
1496 PyObject *globals;
1497 char *buf;
1498 int *p_buflen;
1499{
1500 static PyObject *namestr = NULL;
1501 static PyObject *pathstr = NULL;
1502 PyObject *modname, *modpath, *modules, *parent;
1503
1504 if (globals == NULL || !PyDict_Check(globals))
1505 return Py_None;
1506
1507 if (namestr == NULL) {
1508 namestr = PyString_InternFromString("__name__");
1509 if (namestr == NULL)
1510 return NULL;
1511 }
1512 if (pathstr == NULL) {
1513 pathstr = PyString_InternFromString("__path__");
1514 if (pathstr == NULL)
1515 return NULL;
1516 }
1517
1518 *buf = '\0';
1519 *p_buflen = 0;
1520 modname = PyDict_GetItem(globals, namestr);
1521 if (modname == NULL || !PyString_Check(modname))
1522 return Py_None;
1523
1524 modpath = PyDict_GetItem(globals, pathstr);
1525 if (modpath != NULL) {
1526 int len = PyString_GET_SIZE(modname);
1527 if (len > MAXPATHLEN) {
1528 PyErr_SetString(PyExc_ValueError,
1529 "Module name too long");
1530 return NULL;
1531 }
1532 strcpy(buf, PyString_AS_STRING(modname));
1533 *p_buflen = len;
1534 }
1535 else {
1536 char *start = PyString_AS_STRING(modname);
1537 char *lastdot = strrchr(start, '.');
1538 int len;
1539 if (lastdot == NULL)
1540 return Py_None;
1541 len = lastdot - start;
1542 if (len >= MAXPATHLEN) {
1543 PyErr_SetString(PyExc_ValueError,
1544 "Module name too long");
1545 return NULL;
1546 }
1547 strncpy(buf, start, len);
1548 buf[len] = '\0';
1549 *p_buflen = len;
1550 }
1551
1552 modules = PyImport_GetModuleDict();
1553 parent = PyDict_GetItemString(modules, buf);
1554 if (parent == NULL)
1555 parent = Py_None;
1556 return parent;
1557 /* We expect, but can't guarantee, if parent != None, that:
1558 - parent.__name__ == buf
1559 - parent.__dict__ is globals
1560 If this is violated... Who cares? */
1561}
1562
1563static PyObject *
1564load_next(mod, altmod, p_name, buf, p_buflen)
1565 PyObject *mod;
1566 PyObject *altmod; /* Either None or same as mod */
1567 char **p_name;
1568 char *buf;
1569 int *p_buflen;
1570{
1571 char *name = *p_name;
1572 char *dot = strchr(name, '.');
1573 int len;
1574 char *p;
1575 PyObject *result;
1576
1577 if (dot == NULL) {
1578 *p_name = NULL;
1579 len = strlen(name);
1580 }
1581 else {
1582 *p_name = dot+1;
1583 len = dot-name;
1584 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00001585 if (len == 0) {
1586 PyErr_SetString(PyExc_ValueError,
1587 "Empty module name");
1588 return NULL;
1589 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001590
1591 p = buf + *p_buflen;
1592 if (p != buf)
1593 *p++ = '.';
1594 if (p+len-buf >= MAXPATHLEN) {
1595 PyErr_SetString(PyExc_ValueError,
1596 "Module name too long");
1597 return NULL;
1598 }
1599 strncpy(p, name, len);
1600 p[len] = '\0';
1601 *p_buflen = p+len-buf;
1602
1603 result = import_submodule(mod, p, buf);
1604 if (result == Py_None && altmod != mod) {
1605 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001606 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00001607 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001608 if (result != NULL && result != Py_None) {
1609 if (mark_miss(buf) != 0) {
1610 Py_DECREF(result);
1611 return NULL;
1612 }
1613 strncpy(buf, name, len);
1614 buf[len] = '\0';
1615 *p_buflen = len;
1616 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001617 }
1618 if (result == NULL)
1619 return NULL;
1620
1621 if (result == Py_None) {
1622 Py_DECREF(result);
1623 PyErr_Format(PyExc_ImportError,
1624 "No module named %.200s", name);
1625 return NULL;
1626 }
1627
1628 return result;
1629}
1630
1631static int
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001632mark_miss(name)
1633 char *name;
1634{
1635 PyObject *modules = PyImport_GetModuleDict();
1636 return PyDict_SetItemString(modules, name, Py_None);
1637}
1638
1639static int
Guido van Rossum9905ef91997-09-08 16:07:11 +00001640ensure_fromlist(mod, fromlist, buf, buflen, recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001641 PyObject *mod;
1642 PyObject *fromlist;
1643 char *buf;
1644 int buflen;
Guido van Rossum9905ef91997-09-08 16:07:11 +00001645 int recursive;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001646{
1647 int i;
1648
1649 if (!PyObject_HasAttrString(mod, "__path__"))
1650 return 1;
1651
1652 for (i = 0; ; i++) {
1653 PyObject *item = PySequence_GetItem(fromlist, i);
1654 int hasit;
1655 if (item == NULL) {
1656 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1657 PyErr_Clear();
1658 return 1;
1659 }
1660 return 0;
1661 }
1662 if (!PyString_Check(item)) {
1663 PyErr_SetString(PyExc_TypeError,
1664 "Item in ``from list'' not a string");
1665 Py_DECREF(item);
1666 return 0;
1667 }
1668 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00001669 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001670 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001671 /* See if the package defines __all__ */
1672 if (recursive)
1673 continue; /* Avoid endless recursion */
1674 all = PyObject_GetAttrString(mod, "__all__");
1675 if (all == NULL)
1676 PyErr_Clear();
1677 else {
1678 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1679 return 0;
1680 Py_DECREF(all);
1681 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001682 continue;
1683 }
1684 hasit = PyObject_HasAttr(mod, item);
1685 if (!hasit) {
1686 char *subname = PyString_AS_STRING(item);
1687 PyObject *submod;
1688 char *p;
1689 if (buflen + strlen(subname) >= MAXPATHLEN) {
1690 PyErr_SetString(PyExc_ValueError,
1691 "Module name too long");
1692 Py_DECREF(item);
1693 return 0;
1694 }
1695 p = buf + buflen;
1696 *p++ = '.';
1697 strcpy(p, subname);
1698 submod = import_submodule(mod, subname, buf);
1699 Py_XDECREF(submod);
1700 if (submod == NULL) {
1701 Py_DECREF(item);
1702 return 0;
1703 }
1704 }
1705 Py_DECREF(item);
1706 }
1707
Guido van Rossuma7f2e811997-10-03 15:33:32 +00001708 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001709}
1710
1711static PyObject *
1712import_submodule(mod, subname, fullname)
1713 PyObject *mod; /* May be None */
1714 char *subname;
1715 char *fullname;
1716{
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 *
1774PyImport_ReloadModule(m)
1775 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001777 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00001778 PyObject *path = NULL;
1779 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001780 char buf[MAXPATHLEN+1];
1781 struct filedescr *fdp;
1782 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001783
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784 if (m == NULL || !PyModule_Check(m)) {
1785 PyErr_SetString(PyExc_TypeError,
1786 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001787 return NULL;
1788 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001790 if (name == NULL)
1791 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001792 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001793 PyErr_Format(PyExc_ImportError,
1794 "reload(): module %.200s not in sys.modules",
1795 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796 return NULL;
1797 }
Guido van Rossum222ef561997-09-06 19:41:09 +00001798 subname = strrchr(name, '.');
1799 if (subname == NULL)
1800 subname = name;
1801 else {
1802 PyObject *parentname, *parent;
1803 parentname = PyString_FromStringAndSize(name, (subname-name));
1804 if (parentname == NULL)
1805 return NULL;
1806 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00001807 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00001808 if (parent == NULL) {
1809 PyErr_Format(PyExc_ImportError,
1810 "reload(): parent %.200s not in sys.modules",
1811 name);
1812 return NULL;
1813 }
1814 subname++;
1815 path = PyObject_GetAttrString(parent, "__path__");
1816 if (path == NULL)
1817 PyErr_Clear();
1818 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001819 buf[0] = '\0';
Guido van Rossum222ef561997-09-06 19:41:09 +00001820 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1821 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001822 if (fdp == NULL)
1823 return NULL;
1824 m = load_module(name, fp, buf, fdp->type);
1825 if (fp)
1826 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001827 return m;
1828}
1829
1830
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001831/* Higher-level import emulator which emulates the "import" statement
1832 more accurately -- it invokes the __import__() function from the
1833 builtins of the current globals. This means that the import is
1834 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00001835 environment, e.g. by "rexec".
1836 A dummy list ["__doc__"] is passed as the 4th argument so that
1837 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
1838 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001839
1840PyObject *
1841PyImport_Import(module_name)
1842 PyObject *module_name;
1843{
1844 static PyObject *silly_list = NULL;
1845 static PyObject *builtins_str = NULL;
1846 static PyObject *import_str = NULL;
1847 static PyObject *standard_builtins = NULL;
1848 PyObject *globals = NULL;
1849 PyObject *import = NULL;
1850 PyObject *builtins = NULL;
1851 PyObject *r = NULL;
1852
1853 /* Initialize constant string objects */
1854 if (silly_list == NULL) {
1855 import_str = PyString_InternFromString("__import__");
1856 if (import_str == NULL)
1857 return NULL;
1858 builtins_str = PyString_InternFromString("__builtins__");
1859 if (builtins_str == NULL)
1860 return NULL;
1861 silly_list = Py_BuildValue("[s]", "__doc__");
1862 if (silly_list == NULL)
1863 return NULL;
1864 }
1865
1866 /* Get the builtins from current globals */
1867 globals = PyEval_GetGlobals();
1868 if(globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00001869 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001870 builtins = PyObject_GetItem(globals, builtins_str);
1871 if (builtins == NULL)
1872 goto err;
1873 }
1874 else {
1875 /* No globals -- use standard builtins, and fake globals */
1876 PyErr_Clear();
1877
1878 if (standard_builtins == NULL) {
1879 standard_builtins =
1880 PyImport_ImportModule("__builtin__");
1881 if (standard_builtins == NULL)
1882 return NULL;
1883 }
1884
1885 builtins = standard_builtins;
1886 Py_INCREF(builtins);
1887 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1888 if (globals == NULL)
1889 goto err;
1890 }
1891
1892 /* Get the __import__ function from the builtins */
1893 if (PyDict_Check(builtins))
1894 import=PyObject_GetItem(builtins, import_str);
1895 else
1896 import=PyObject_GetAttr(builtins, import_str);
1897 if (import == NULL)
1898 goto err;
1899
1900 /* Call the _import__ function with the proper argument list */
1901 r = PyObject_CallFunction(import, "OOOO",
1902 module_name, globals, globals, silly_list);
1903
1904 err:
1905 Py_XDECREF(globals);
1906 Py_XDECREF(builtins);
1907 Py_XDECREF(import);
1908
1909 return r;
1910}
1911
1912
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001913/* Module 'imp' provides Python access to the primitives used for
1914 importing modules.
1915*/
1916
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001918imp_get_magic(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 PyObject *self;
1920 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001921{
1922 char buf[4];
1923
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001925 return NULL;
Guido van Rossum6bf62da1997-04-11 20:37:35 +00001926 buf[0] = (char) ((MAGIC >> 0) & 0xff);
1927 buf[1] = (char) ((MAGIC >> 8) & 0xff);
1928 buf[2] = (char) ((MAGIC >> 16) & 0xff);
1929 buf[3] = (char) ((MAGIC >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001930
Guido van Rossum79f25d91997-04-29 20:08:16 +00001931 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001932}
1933
Guido van Rossum79f25d91997-04-29 20:08:16 +00001934static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001935imp_get_suffixes(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936 PyObject *self;
1937 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001938{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001940 struct filedescr *fdp;
1941
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001943 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001945 if (list == NULL)
1946 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001947 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1948 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001949 fdp->suffix, fdp->mode, fdp->type);
1950 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001951 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001952 return NULL;
1953 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001954 if (PyList_Append(list, item) < 0) {
1955 Py_DECREF(list);
1956 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001957 return NULL;
1958 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001960 }
1961 return list;
1962}
1963
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001965call_find_module(name, path)
1966 char *name;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001967 PyObject *path; /* list or None or NULL */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001968{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001969 extern int fclose Py_PROTO((FILE *));
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001971 struct filedescr *fdp;
1972 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001973 FILE *fp = NULL;
1974
1975 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001976 if (path == Py_None)
1977 path = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001978 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
1979 if (fdp == NULL)
1980 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001981 if (fp != NULL) {
1982 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
1983 if (fob == NULL) {
1984 fclose(fp);
1985 return NULL;
1986 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001987 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001988 else {
1989 fob = Py_None;
1990 Py_INCREF(fob);
1991 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001993 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001994 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001995 return ret;
1996}
1997
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001999imp_find_module(self, args)
2000 PyObject *self;
2001 PyObject *args;
2002{
2003 char *name;
2004 PyObject *path = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002005 if (!PyArg_ParseTuple(args, "s|O", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002006 return NULL;
2007 return call_find_module(name, path);
2008}
2009
2010static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002011imp_init_builtin(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 PyObject *self;
2013 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002014{
2015 char *name;
2016 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017 PyObject *m;
2018 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002019 return NULL;
2020 ret = init_builtin(name);
2021 if (ret < 0)
2022 return NULL;
2023 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002024 Py_INCREF(Py_None);
2025 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002026 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002027 m = PyImport_AddModule(name);
2028 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002029 return m;
2030}
2031
Guido van Rossum79f25d91997-04-29 20:08:16 +00002032static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002033imp_init_frozen(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002034 PyObject *self;
2035 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002036{
2037 char *name;
2038 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002039 PyObject *m;
2040 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002041 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002042 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002043 if (ret < 0)
2044 return NULL;
2045 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002046 Py_INCREF(Py_None);
2047 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002048 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049 m = PyImport_AddModule(name);
2050 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002051 return m;
2052}
2053
Guido van Rossum79f25d91997-04-29 20:08:16 +00002054static PyObject *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002055imp_get_frozen_object(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002056 PyObject *self;
2057 PyObject *args;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002058{
2059 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002060
Guido van Rossum79f25d91997-04-29 20:08:16 +00002061 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002062 return NULL;
2063 return get_frozen_object(name);
2064}
2065
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002067imp_is_builtin(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002068 PyObject *self;
2069 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002070{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002071 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002072 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002073 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002074 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002075}
2076
Guido van Rossum79f25d91997-04-29 20:08:16 +00002077static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002078imp_is_frozen(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 PyObject *self;
2080 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002081{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002082 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002083 struct _frozen *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002084 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002085 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002086 p = find_frozen(name);
2087 return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002088}
2089
2090static FILE *
2091get_file(pathname, fob, mode)
2092 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002093 PyObject *fob;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002094 char *mode;
2095{
2096 FILE *fp;
2097 if (fob == NULL) {
2098 fp = fopen(pathname, mode);
2099 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002100 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002101 }
2102 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002103 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002104 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002105 PyErr_SetString(PyExc_ValueError,
2106 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002107 }
2108 return fp;
2109}
2110
Guido van Rossum79f25d91997-04-29 20:08:16 +00002111static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002112imp_load_compiled(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002113 PyObject *self;
2114 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002115{
2116 char *name;
2117 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002118 PyObject *fob = NULL;
2119 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002120 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002121 if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002122 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002123 return NULL;
2124 fp = get_file(pathname, fob, "rb");
2125 if (fp == NULL)
2126 return NULL;
2127 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002128 if (fob == NULL)
2129 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002130 return m;
2131}
2132
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002134imp_load_dynamic(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002135 PyObject *self;
2136 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002137{
2138 char *name;
2139 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002140 PyObject *fob = NULL;
2141 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002142 FILE *fp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002143 if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
2144 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002145 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002146 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002147 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002148 if (fp == NULL)
2149 return NULL;
2150 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002151 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002152 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002153}
2154
Guido van Rossum79f25d91997-04-29 20:08:16 +00002155static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002156imp_load_source(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002157 PyObject *self;
2158 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002159{
2160 char *name;
2161 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002162 PyObject *fob = NULL;
2163 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002164 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002165 if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002166 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002167 return NULL;
2168 fp = get_file(pathname, fob, "r");
2169 if (fp == NULL)
2170 return NULL;
2171 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002172 if (fob == NULL)
2173 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002174 return m;
2175}
2176
Jack Jansen9c96a921995-02-15 22:57:06 +00002177#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002178static PyObject *
Jack Jansen9c96a921995-02-15 22:57:06 +00002179imp_load_resource(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002180 PyObject *self;
2181 PyObject *args;
Jack Jansen9c96a921995-02-15 22:57:06 +00002182{
2183 char *name;
2184 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002185 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002186
Guido van Rossum79f25d91997-04-29 20:08:16 +00002187 if (!PyArg_ParseTuple(args, "ss", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002188 return NULL;
2189 m = PyMac_LoadResourceModule(name, pathname);
2190 return m;
2191}
2192#endif /* macintosh */
2193
Guido van Rossum79f25d91997-04-29 20:08:16 +00002194static PyObject *
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002195imp_load_module(self, args)
2196 PyObject *self;
2197 PyObject *args;
2198{
2199 char *name;
2200 PyObject *fob;
2201 char *pathname;
2202 char *suffix; /* Unused */
2203 char *mode;
2204 int type;
2205 FILE *fp;
2206
2207 if (!PyArg_ParseTuple(args, "sOs(ssi)",
2208 &name, &fob, &pathname,
2209 &suffix, &mode, &type))
2210 return NULL;
2211 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2212 PyErr_Format(PyExc_ValueError,
2213 "invalid file open mode %.200s", mode);
2214 return NULL;
2215 }
2216 if (fob == Py_None)
2217 fp = NULL;
2218 else {
2219 if (!PyFile_Check(fob)) {
2220 PyErr_SetString(PyExc_ValueError,
2221 "load_module arg#2 should be a file or None");
2222 return NULL;
2223 }
2224 fp = get_file(pathname, fob, mode);
2225 if (fp == NULL)
2226 return NULL;
2227 }
2228 return load_module(name, fp, pathname, type);
2229}
2230
2231static PyObject *
2232imp_load_package(self, args)
2233 PyObject *self;
2234 PyObject *args;
2235{
2236 char *name;
2237 char *pathname;
2238 if (!PyArg_ParseTuple(args, "ss", &name, &pathname))
2239 return NULL;
2240 return load_package(name, pathname);
2241}
2242
2243static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002244imp_new_module(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002245 PyObject *self;
2246 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002247{
2248 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249 if (!PyArg_ParseTuple(args, "s", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002250 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002251 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002252}
2253
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002254/* Doc strings */
2255
2256static char doc_imp[] = "\
2257This module provides the components needed to build your own\n\
2258__import__ function. Undocumented functions are obsolete.\n\
2259";
2260
2261static char doc_find_module[] = "\
2262find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2263Search for a module. If path is omitted or None, search for a\n\
2264built-in, frozen or special module and continue search in sys.path.\n\
2265The module name cannot contain '.'; to search for a submodule of a\n\
2266package, pass the submodule name and the package's __path__.\
2267";
2268
2269static char doc_load_module[] = "\
2270load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2271Load a module, given information returned by find_module().\n\
2272The module name must include the full package name, if any.\
2273";
2274
2275static char doc_get_magic[] = "\
2276get_magic() -> string\n\
2277Return the magic number for .pyc or .pyo files.\
2278";
2279
2280static char doc_get_suffixes[] = "\
2281get_suffixes() -> [(suffix, mode, type), ...]\n\
2282Return a list of (suffix, mode, type) tuples describing the files\n\
2283that find_module() looks for.\
2284";
2285
2286static char doc_new_module[] = "\
2287new_module(name) -> module\n\
2288Create a new module. Do not enter it in sys.modules.\n\
2289The module name must include the full package name, if any.\
2290";
2291
Guido van Rossum79f25d91997-04-29 20:08:16 +00002292static PyMethodDef imp_methods[] = {
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002293 {"find_module", imp_find_module, 1, doc_find_module},
2294 {"get_magic", imp_get_magic, 1, doc_get_magic},
2295 {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes},
2296 {"load_module", imp_load_module, 1, doc_load_module},
2297 {"new_module", imp_new_module, 1, doc_new_module},
2298 /* The rest are obsolete */
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002299 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002300 {"init_builtin", imp_init_builtin, 1},
2301 {"init_frozen", imp_init_frozen, 1},
2302 {"is_builtin", imp_is_builtin, 1},
2303 {"is_frozen", imp_is_frozen, 1},
2304 {"load_compiled", imp_load_compiled, 1},
2305 {"load_dynamic", imp_load_dynamic, 1},
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002306 {"load_package", imp_load_package, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00002307#ifdef macintosh
2308 {"load_resource", imp_load_resource, 1},
2309#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002310 {"load_source", imp_load_source, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002311 {NULL, NULL} /* sentinel */
2312};
2313
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002314static int
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002315setint(d, name, value)
2316 PyObject *d;
2317 char *name;
2318 int value;
2319{
2320 PyObject *v;
2321 int err;
2322
2323 v = PyInt_FromLong((long)value);
2324 err = PyDict_SetItemString(d, name, v);
2325 Py_XDECREF(v);
2326 return err;
2327}
2328
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002329void
2330initimp()
2331{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002332 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002333
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002334 m = Py_InitModule4("imp", imp_methods, doc_imp,
2335 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002336 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002337
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002338 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2339 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2340 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2341 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2342 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2343 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2344 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2345 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002346 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002347
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002348 failure:
2349 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002350}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002351
2352
2353/* API for embedding applications that want to add their own entries to the
2354 table of built-in modules. This should normally be called *before*
2355 Py_Initialize(). When the malloc() or realloc() call fails, -1 is returned
2356 and the existing table is unchanged.
2357
2358 After a similar function by Just van Rossum. */
2359
2360int
2361PyImport_ExtendInittab(newtab)
2362 struct _inittab *newtab;
2363{
2364 static struct _inittab *our_copy = NULL;
2365 struct _inittab *p;
2366 int i, n;
2367
2368 /* Count the number of entries in both tables */
2369 for (n = 0; newtab[n].name != NULL; n++)
2370 ;
2371 if (n == 0)
2372 return 0; /* Nothing to do */
2373 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2374 ;
2375
2376 /* Allocate new memory for the combined table */
2377 if (our_copy == NULL)
2378 p = malloc((i+n+1) * sizeof(struct _inittab));
2379 else
2380 p = realloc(our_copy, (i+n+1) * sizeof(struct _inittab));
2381 if (p == NULL)
2382 return -1;
2383
2384 /* Copy the tables into the new memory */
2385 if (our_copy != PyImport_Inittab)
2386 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2387 PyImport_Inittab = our_copy = p;
2388 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2389
2390 return 0;
2391}
2392
2393/* Shorthand to add a single entry given a name and a function */
2394
2395int
2396PyImport_AppendInittab(name, initfunc)
2397 char *name;
2398 void (*initfunc)();
2399{
2400 struct _inittab newtab[2];
2401
2402 memset(newtab, '\0', sizeof newtab);
2403
2404 newtab[0].name = name;
2405 newtab[0].initfunc = initfunc;
2406
2407 return PyImport_ExtendInittab(newtab);
2408}