blob: 07fff18d0b45293981a848e7c2447a9ba6f2dc86 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
10#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000011#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000014#ifdef macintosh
15#include "macglue.h"
16#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000017
Guido van Rossum55a83382000-09-20 20:31:38 +000018#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif
21
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000022extern time_t PyOS_GetLastModificationTime(char *, FILE *);
23 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000024
Guido van Rossum6c849691994-09-26 15:47:17 +000025/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000026/* Change for each incompatible change */
27/* The value of CR and LF is incorporated so if you ever read or write
28 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000029 Apple MPW compiler swaps their values, botching string constants.
30 XXX That probably isn't important anymore.
31*/
Guido van Rossum7faeab31995-07-07 22:50:36 +000032/* XXX Perhaps the magic number should be frozen and a version field
33 added to the .pyc file header? */
Tim Peters36515e22001-11-18 04:06:29 +000034/* New way to come up with the low 16 bits of the magic number:
35 (YEAR-1995) * 10000 + MONTH * 100 + DAY
36 where MONTH and DAY are 1-based.
37 XXX Whatever the "old way" may have been isn't documented.
38 XXX This scheme breaks in 2002, as (2002-1995)*10000 = 70000 doesn't
39 fit in 16 bits.
40 XXX Later, sometimes 1 gets added to MAGIC in order to record that
41 the Unicode -U option is in use. IMO (Tim's), that's a Bad Idea
42 (quite apart from that the -U option doesn't work so isn't used
43 anyway).
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000044
45 XXX MAL, 2002-02-07: I had to modify the MAGIC due to a fix of the
46 UTF-8 encoder (it previously produced invalid UTF-8 for unpaired
47 high surrogates), so I simply bumped the month value to 20 (invalid
48 month) and set the day to 1. This should be recognizable by any
49 algorithm relying on the above scheme. Perhaps we should simply
50 start counting in increments of 10 from now on ?!
51
Michael W. Hudsondd32a912002-08-15 14:59:02 +000052 MWH, 2002-08-03: Removed SET_LINENO. Couldn't be bothered figuring
53 out the MAGIC schemes, so just incremented it by 10.
54
Guido van Rossumf6894922002-08-31 15:16:14 +000055 GvR, 2002-08-31: Because MWH changed the bytecode again, moved the
56 magic number *back* to 62011. This should get the snake-farm to
57 throw away its old .pyc files, amongst others.
58
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000059 Known values:
60 Python 1.5: 20121
61 Python 1.5.1: 20121
62 Python 1.5.2: 20121
63 Python 2.0: 50823
64 Python 2.0.1: 50823
65 Python 2.1: 60202
66 Python 2.1.1: 60202
67 Python 2.1.2: 60202
68 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000069 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000070 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000071 Python 2.3a0: 62011 (!)
Tim Peters36515e22001-11-18 04:06:29 +000072*/
Guido van Rossumf6894922002-08-31 15:16:14 +000073#define MAGIC (62011 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000074
Guido van Rossum96774c12000-05-01 20:19:08 +000075/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000076 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000077 compiler works which are enabled by command line switches. */
78static long pyc_magic = MAGIC;
79
Guido van Rossum25ce5661997-08-02 03:10:38 +000080/* See _PyImport_FixupExtension() below */
81static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000082
Guido van Rossum771c6c81997-10-31 18:37:24 +000083/* This table is defined in config.c: */
84extern struct _inittab _PyImport_Inittab[];
85
86struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000087
Guido van Rossumed1170e1999-12-20 21:23:41 +000088/* these tables define the module suffixes that Python recognizes */
89struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000090
91#ifdef RISCOS
92static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000093 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000094 {"/pyc", "rb", PY_COMPILED},
95 {0, 0}
96};
97#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000098static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000099 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000100#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000101 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000102#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000103 {".pyc", "rb", PY_COMPILED},
104 {0, 0}
105};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000106#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000107
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000108/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
110void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000113 const struct filedescr *scan;
114 struct filedescr *filetab;
115 int countD = 0;
116 int countS = 0;
117
118 /* prepare _PyImport_Filetab: copy entries from
119 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
120 */
121 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
122 ++countD;
123 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
124 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000125 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000126 memcpy(filetab, _PyImport_DynLoadFiletab,
127 countD * sizeof(struct filedescr));
128 memcpy(filetab + countD, _PyImport_StandardFiletab,
129 countS * sizeof(struct filedescr));
130 filetab[countD + countS].suffix = NULL;
131
132 _PyImport_Filetab = filetab;
133
Guido van Rossum0824f631997-03-11 18:37:35 +0000134 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000135 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
136 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000137#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000138 if (strcmp(filetab->suffix, ".pyc") == 0)
139 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000140#else
141 if (strcmp(filetab->suffix, "/pyc") == 0)
142 filetab->suffix = "/pyo";
143#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000144 }
145 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000146
147 if (Py_UnicodeFlag) {
148 /* Fix the pyc_magic so that byte compiled code created
149 using the all-Unicode method doesn't interfere with
150 code created in normal operation mode. */
151 pyc_magic = MAGIC + 1;
152 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
Guido van Rossum25ce5661997-08-02 03:10:38 +0000155void
Just van Rossum52e14d62002-12-30 22:08:05 +0000156_PyImportHooks_Init(void)
157{
158 PyObject *v, *path_hooks = NULL, *zimpimport;
159 int err = 0;
160
161 /* adding sys.path_hooks and sys.path_importer_cache, setting up
162 zipimport */
163
164 if (Py_VerboseFlag)
165 PySys_WriteStderr("# installing zipimport hook\n");
166
167 v = PyList_New(0);
168 if (v == NULL)
169 goto error;
170 err = PySys_SetObject("meta_path", v);
171 Py_DECREF(v);
172 if (err)
173 goto error;
174 v = PyDict_New();
175 if (v == NULL)
176 goto error;
177 err = PySys_SetObject("path_importer_cache", v);
178 Py_DECREF(v);
179 if (err)
180 goto error;
181 path_hooks = PyList_New(0);
182 if (path_hooks == NULL)
183 goto error;
184 err = PySys_SetObject("path_hooks", path_hooks);
185 if (err) {
186 error:
187 PyErr_Print();
188 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
189 "path_importer_cache failed");
190 }
191 zimpimport = PyImport_ImportModule("zipimport");
192 if (zimpimport == NULL) {
193 PyErr_Clear(); /* No zip import module -- okay */
194 if (Py_VerboseFlag)
195 PySys_WriteStderr("# can't import zipimport\n");
196 }
197 else {
198 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
199 "zipimporter");
200 Py_DECREF(zimpimport);
201 if (zipimporter == NULL) {
202 PyErr_Clear(); /* No zipimporter object -- okay */
203 if (Py_VerboseFlag)
204 PySys_WriteStderr(
205 "# can't import zipimport.zimimporter\n");
206 }
207 else {
208 /* sys.path_hooks.append(zipimporter) */
209 err = PyList_Append(path_hooks, zipimporter);
210 Py_DECREF(zipimporter);
211 if (err)
212 goto error;
213 if (Py_VerboseFlag)
214 PySys_WriteStderr(
215 "# installed zipimport hook\n");
216 }
217 }
218 Py_DECREF(path_hooks);
219}
220
221void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000222_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223{
224 Py_XDECREF(extensions);
225 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000226 PyMem_DEL(_PyImport_Filetab);
227 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228}
229
230
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000231/* Locking primitives to prevent parallel imports of the same module
232 in different threads to return with a partially loaded module.
233 These calls are serialized by the global interpreter lock. */
234
235#ifdef WITH_THREAD
236
Guido van Rossum49b56061998-10-01 20:42:43 +0000237#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000238
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000240static long import_lock_thread = -1;
241static int import_lock_level = 0;
242
243static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000245{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000247 if (me == -1)
248 return; /* Too bad */
249 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251 if (import_lock_thread == me) {
252 import_lock_level++;
253 return;
254 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000255 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
256 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000257 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259 PyEval_RestoreThread(tstate);
260 }
261 import_lock_thread = me;
262 import_lock_level = 1;
263}
264
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000265static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000267{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000269 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000270 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000271 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000272 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000273 import_lock_level--;
274 if (import_lock_level == 0) {
275 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000276 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000277 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000279}
280
281#else
282
283#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000284#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000285
286#endif
287
Tim Peters69232342001-08-30 05:16:13 +0000288static PyObject *
289imp_lock_held(PyObject *self, PyObject *args)
290{
291 if (!PyArg_ParseTuple(args, ":lock_held"))
292 return NULL;
293#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000294 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000295#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000296 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000297#endif
298}
299
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000300static PyObject *
301imp_acquire_lock(PyObject *self, PyObject *args)
302{
303 if (!PyArg_ParseTuple(args, ":acquire_lock"))
304 return NULL;
305#ifdef WITH_THREAD
306 lock_import();
307#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000308 Py_INCREF(Py_None);
309 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000310}
311
312static PyObject *
313imp_release_lock(PyObject *self, PyObject *args)
314{
315 if (!PyArg_ParseTuple(args, ":release_lock"))
316 return NULL;
317#ifdef WITH_THREAD
318 if (unlock_import() < 0) {
319 PyErr_SetString(PyExc_RuntimeError,
320 "not holding the import lock");
321 return NULL;
322 }
323#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000324 Py_INCREF(Py_None);
325 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000326}
327
Guido van Rossum25ce5661997-08-02 03:10:38 +0000328/* Helper for sys */
329
330PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000332{
333 PyInterpreterState *interp = PyThreadState_Get()->interp;
334 if (interp->modules == NULL)
335 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
336 return interp->modules;
337}
338
Guido van Rossum3f5da241990-12-20 15:06:42 +0000339
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000340/* List of names to clear in sys */
341static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000342 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000343 "exc_type", "exc_value", "exc_traceback",
344 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000345 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000346 NULL
347};
348
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000349static char* sys_files[] = {
350 "stdin", "__stdin__",
351 "stdout", "__stdout__",
352 "stderr", "__stderr__",
353 NULL
354};
355
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000356
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000357/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000358
Guido van Rossum3f5da241990-12-20 15:06:42 +0000359void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000360PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000361{
Guido van Rossum758eec01998-01-19 21:58:26 +0000362 int pos, ndone;
363 char *name;
364 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000366 PyObject *modules = interp->modules;
367
368 if (modules == NULL)
369 return; /* Already done */
370
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371 /* Delete some special variables first. These are common
372 places where user values hide and people complain when their
373 destructors fail. Since the modules containing them are
374 deleted *last* of all, they would come too late in the normal
375 destruction order. Sigh. */
376
377 value = PyDict_GetItemString(modules, "__builtin__");
378 if (value != NULL && PyModule_Check(value)) {
379 dict = PyModule_GetDict(value);
380 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000381 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000382 PyDict_SetItemString(dict, "_", Py_None);
383 }
384 value = PyDict_GetItemString(modules, "sys");
385 if (value != NULL && PyModule_Check(value)) {
386 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000387 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000388 dict = PyModule_GetDict(value);
389 for (p = sys_deletes; *p != NULL; p++) {
390 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000391 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000392 PyDict_SetItemString(dict, *p, Py_None);
393 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000394 for (p = sys_files; *p != NULL; p+=2) {
395 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000396 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000397 v = PyDict_GetItemString(dict, *(p+1));
398 if (v == NULL)
399 v = Py_None;
400 PyDict_SetItemString(dict, *p, v);
401 }
402 }
403
404 /* First, delete __main__ */
405 value = PyDict_GetItemString(modules, "__main__");
406 if (value != NULL && PyModule_Check(value)) {
407 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000408 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000409 _PyModule_Clear(value);
410 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000411 }
412
Guido van Rossum758eec01998-01-19 21:58:26 +0000413 /* The special treatment of __builtin__ here is because even
414 when it's not referenced as a module, its dictionary is
415 referenced by almost every module's __builtins__. Since
416 deleting a module clears its dictionary (even if there are
417 references left to it), we need to delete the __builtin__
418 module last. Likewise, we don't delete sys until the very
419 end because it is implicitly referenced (e.g. by print).
420
421 Also note that we 'delete' modules by replacing their entry
422 in the modules dict with None, rather than really deleting
423 them; this avoids a rehash of the modules dictionary and
424 also marks them as "non existent" so they won't be
425 re-imported. */
426
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000427 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000428 one (skipping __builtin__ and sys) and delete them */
429 do {
430 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000431 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000432 while (PyDict_Next(modules, &pos, &key, &value)) {
433 if (value->ob_refcnt != 1)
434 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000435 if (PyString_Check(key) && PyModule_Check(value)) {
436 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000437 if (strcmp(name, "__builtin__") == 0)
438 continue;
439 if (strcmp(name, "sys") == 0)
440 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000441 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000442 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000443 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000444 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000445 PyDict_SetItem(modules, key, Py_None);
446 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000447 }
448 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000449 } while (ndone > 0);
450
Guido van Rossum758eec01998-01-19 21:58:26 +0000451 /* Next, delete all modules (still skipping __builtin__ and sys) */
452 pos = 0;
453 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000454 if (PyString_Check(key) && PyModule_Check(value)) {
455 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000456 if (strcmp(name, "__builtin__") == 0)
457 continue;
458 if (strcmp(name, "sys") == 0)
459 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000460 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000461 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000462 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000463 PyDict_SetItem(modules, key, Py_None);
464 }
465 }
466
467 /* Next, delete sys and __builtin__ (in that order) */
468 value = PyDict_GetItemString(modules, "sys");
469 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000470 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000471 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000472 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000473 PyDict_SetItemString(modules, "sys", Py_None);
474 }
475 value = PyDict_GetItemString(modules, "__builtin__");
476 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000477 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000478 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000479 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000480 PyDict_SetItemString(modules, "__builtin__", Py_None);
481 }
482
483 /* Finally, clear and delete the modules directory */
484 PyDict_Clear(modules);
485 interp->modules = NULL;
486 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000487}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000488
489
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000490/* Helper for pythonrun.c -- return magic number */
491
492long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000493PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000494{
Guido van Rossum96774c12000-05-01 20:19:08 +0000495 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496}
497
498
Guido van Rossum25ce5661997-08-02 03:10:38 +0000499/* Magic for extension modules (built-in as well as dynamically
500 loaded). To prevent initializing an extension module more than
501 once, we keep a static dictionary 'extensions' keyed by module name
502 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000503 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000504 dictionary is stored by calling _PyImport_FixupExtension()
505 immediately after the module initialization function succeeds. A
506 copy can be retrieved from there by calling
507 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508
Guido van Rossum79f25d91997-04-29 20:08:16 +0000509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000512 PyObject *modules, *mod, *dict, *copy;
513 if (extensions == NULL) {
514 extensions = PyDict_New();
515 if (extensions == NULL)
516 return NULL;
517 }
518 modules = PyImport_GetModuleDict();
519 mod = PyDict_GetItemString(modules, name);
520 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000521 PyErr_Format(PyExc_SystemError,
522 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000523 return NULL;
524 }
525 dict = PyModule_GetDict(mod);
526 if (dict == NULL)
527 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000528 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529 if (copy == NULL)
530 return NULL;
531 PyDict_SetItemString(extensions, filename, copy);
532 Py_DECREF(copy);
533 return copy;
534}
535
536PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000537_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000538{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000539 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540 if (extensions == NULL)
541 return NULL;
542 dict = PyDict_GetItemString(extensions, filename);
543 if (dict == NULL)
544 return NULL;
545 mod = PyImport_AddModule(name);
546 if (mod == NULL)
547 return NULL;
548 mdict = PyModule_GetDict(mod);
549 if (mdict == NULL)
550 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000551 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000552 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000554 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000555 name, filename);
556 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557}
558
559
560/* Get the module object corresponding to a module name.
561 First check the modules dictionary if there's one there,
562 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000563 Because the former action is most common, THIS DOES NOT RETURN A
564 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000567PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000569 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
Guido van Rossum25ce5661997-08-02 03:10:38 +0000572 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576 if (m == NULL)
577 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000580 return NULL;
581 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583
584 return m;
585}
586
587
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000588/* Execute a code object in a module and return the module object
589 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000592PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000594 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
595}
596
597PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000598PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000599{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000602
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604 if (m == NULL)
605 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 d = PyModule_GetDict(m);
607 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
608 if (PyDict_SetItemString(d, "__builtins__",
609 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000610 return NULL;
611 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000612 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000613 v = NULL;
614 if (pathname != NULL) {
615 v = PyString_FromString(pathname);
616 if (v == NULL)
617 PyErr_Clear();
618 }
619 if (v == NULL) {
620 v = ((PyCodeObject *)co)->co_filename;
621 Py_INCREF(v);
622 }
623 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000624 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000625 Py_DECREF(v);
626
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000627 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628 if (v == NULL)
629 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000631
Guido van Rossum25ce5661997-08-02 03:10:38 +0000632 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000633 PyErr_Format(PyExc_ImportError,
634 "Loaded module %.200s not found in sys.modules",
635 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000636 return NULL;
637 }
638
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640
641 return m;
642}
643
644
645/* Given a pathname for a Python source file, fill a buffer with the
646 pathname for the corresponding compiled file. Return the pathname
647 for the compiled file, or NULL if there's no space in the buffer.
648 Doesn't set an exception. */
649
650static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000652{
Tim Petersc1731372001-08-04 08:12:36 +0000653 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654 if (len+2 > buflen)
655 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000656
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000657#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000658 /* Treat .pyw as if it were .py. The case of ".pyw" must match
659 that used in _PyImport_StandardFiletab. */
660 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
661 --len; /* pretend 'w' isn't there */
662#endif
663 memcpy(buf, pathname, len);
664 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
665 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666
667 return buf;
668}
669
670
671/* Given a pathname for a Python source file, its time of last
672 modification, and a pathname for a compiled file, check whether the
673 compiled file represents the same version of the source. If so,
674 return a FILE pointer for the compiled file, positioned just after
675 the header; if not, return NULL.
676 Doesn't set an exception. */
677
678static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000679check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680{
681 FILE *fp;
682 long magic;
683 long pyc_mtime;
684
685 fp = fopen(cpathname, "rb");
686 if (fp == NULL)
687 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000688 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000689 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000691 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 fclose(fp);
693 return NULL;
694 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000698 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 fclose(fp);
700 return NULL;
701 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000703 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000704 return fp;
705}
706
707
708/* Read a code object from a file and check it for validity */
709
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000711read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714
Tim Petersd9b9ac82001-01-28 00:27:39 +0000715 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 if (co == NULL || !PyCode_Check(co)) {
718 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000719 PyErr_Format(PyExc_ImportError,
720 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722 return NULL;
723 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725}
726
727
728/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000729 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000732load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000733{
734 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 PyCodeObject *co;
736 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000739 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000740 PyErr_Format(PyExc_ImportError,
741 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 return NULL;
743 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000745 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 if (co == NULL)
747 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000749 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000750 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000751 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000753
754 return m;
755}
756
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757/* Parse a source file and return the corresponding code object */
758
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000760parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763 node *n;
764
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000765 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766 if (n == NULL)
767 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 co = PyNode_Compile(n, pathname);
769 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770
771 return co;
772}
773
774
Guido van Rossum55a83382000-09-20 20:31:38 +0000775/* Helper to open a bytecode file for writing in exclusive mode */
776
777static FILE *
778open_exclusive(char *filename)
779{
780#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
781 /* Use O_EXCL to avoid a race condition when another process tries to
782 write the same file. When that happens, our open() call fails,
783 which is just fine (since it's only a cache).
784 XXX If the file exists and is writable but the directory is not
785 writable, the file will never be written. Oh well.
786 */
787 int fd;
788 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000789 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
790#ifdef O_BINARY
791 |O_BINARY /* necessary for Windows */
792#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000793#ifdef __VMS
794 , 0666, "ctxt=bin", "shr=nil");
795#else
796 , 0666);
797#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000798 if (fd < 0)
799 return NULL;
800 return fdopen(fd, "wb");
801#else
802 /* Best we can do -- on Windows this can't happen anyway */
803 return fopen(filename, "wb");
804#endif
805}
806
807
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808/* Write a compiled module to a file, placing the time of last
809 modification of its source into the header.
810 Errors are ignored, if a write error occurs an attempt is made to
811 remove the file. */
812
813static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000814write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815{
816 FILE *fp;
817
Guido van Rossum55a83382000-09-20 20:31:38 +0000818 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000821 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 "# can't create %s\n", cpathname);
823 return;
824 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000825 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 PyMarshal_WriteLongToFile(0L, fp);
828 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000831 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000832 /* Don't keep partial file */
833 fclose(fp);
834 (void) unlink(cpathname);
835 return;
836 }
837 /* Now write the true mtime */
838 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840 fflush(fp);
841 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000843 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844#ifdef macintosh
Jack Jansencbf630f2000-07-11 21:59:16 +0000845 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846#endif
847}
848
849
850/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000851 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
852 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000855load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856{
Fred Drake4c82b232000-06-30 16:18:57 +0000857 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858 FILE *fpc;
859 char buf[MAXPATHLEN+1];
860 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyCodeObject *co;
862 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000863
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000864 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000865 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000866 return NULL;
867#if SIZEOF_TIME_T > 4
868 /* Python's .pyc timestamp handling presumes that the timestamp fits
869 in 4 bytes. This will be fine until sometime in the year 2038,
870 when a 4-byte signed time_t will overflow.
871 */
872 if (mtime >> 32) {
873 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000874 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000875 return NULL;
876 }
877#endif
Tim Peters36515e22001-11-18 04:06:29 +0000878 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000879 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000880 if (cpathname != NULL &&
881 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000882 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 fclose(fpc);
884 if (co == NULL)
885 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000887 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000888 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000889 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890 }
891 else {
892 co = parse_source_module(pathname, fp);
893 if (co == NULL)
894 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000896 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000897 name, pathname);
898 write_compiled_module(co, cpathname, mtime);
899 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000900 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000901 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000902
903 return m;
904}
905
906
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000907/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000908static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
909static struct filedescr *find_module(char *, char *, PyObject *,
910 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000911static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000912
913/* Load a package and return its module object WITH INCREMENTED
914 REFERENCE COUNT */
915
916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000917load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000918{
919 PyObject *m, *d, *file, *path;
920 int err;
921 char buf[MAXPATHLEN+1];
922 FILE *fp = NULL;
923 struct filedescr *fdp;
924
925 m = PyImport_AddModule(name);
926 if (m == NULL)
927 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000928 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000929 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000930 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931 d = PyModule_GetDict(m);
932 file = PyString_FromString(pathname);
933 if (file == NULL)
934 return NULL;
935 path = Py_BuildValue("[O]", file);
936 if (path == NULL) {
937 Py_DECREF(file);
938 return NULL;
939 }
940 err = PyDict_SetItemString(d, "__file__", file);
941 if (err == 0)
942 err = PyDict_SetItemString(d, "__path__", path);
943 if (err != 0) {
944 m = NULL;
945 goto cleanup;
946 }
947 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000948 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949 if (fdp == NULL) {
950 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
951 PyErr_Clear();
952 }
953 else
954 m = NULL;
955 goto cleanup;
956 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000957 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000958 if (fp != NULL)
959 fclose(fp);
960 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000961 Py_XDECREF(path);
962 Py_XDECREF(file);
963 return m;
964}
965
966
967/* Helper to test for built-in module */
968
969static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000970is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000971{
972 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000973 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
974 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
975 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000976 return -1;
977 else
978 return 1;
979 }
980 }
981 return 0;
982}
983
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000984
Just van Rossum52e14d62002-12-30 22:08:05 +0000985/* Return an importer object for a sys.path/pkg.__path__ item 'p',
986 possibly by fetching it from the path_importer_cache dict. If it
987 wasn't yet cached, traverse path_hooks until it a hook is found
988 that can handle the path item. Return None if no hook could;
989 this tells our caller it should fall back to the builtin
990 import mechanism. Cache the result in path_importer_cache.
991 Returns a borrowed reference. */
992
993static PyObject *
994get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
995 PyObject *p)
996{
997 PyObject *importer;
998 int j, nhooks;
999
1000 /* These conditions are the caller's responsibility: */
1001 assert(PyList_Check(path_hooks));
1002 assert(PyDict_Check(path_importer_cache));
1003
1004 nhooks = PyList_Size(path_hooks);
1005 if (nhooks < 0)
1006 return NULL; /* Shouldn't happen */
1007
1008 importer = PyDict_GetItem(path_importer_cache, p);
1009 if (importer != NULL)
1010 return importer;
1011
1012 /* set path_importer_cache[p] to None to avoid recursion */
1013 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1014 return NULL;
1015
1016 for (j = 0; j < nhooks; j++) {
1017 PyObject *hook = PyList_GetItem(path_hooks, j);
1018 if (hook == NULL)
1019 return NULL;
1020 importer = PyObject_CallFunction(hook, "O", p);
1021 if (importer != NULL)
1022 break;
1023
1024 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1025 return NULL;
1026 }
1027 PyErr_Clear();
1028 }
1029 if (importer == NULL)
1030 importer = Py_None;
1031 else if (importer != Py_None) {
1032 int err = PyDict_SetItem(path_importer_cache, p, importer);
1033 Py_DECREF(importer);
1034 if (err != 0)
1035 return NULL;
1036 }
1037 return importer;
1038}
1039
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040/* Search the path (default sys.path) for a module. Return the
1041 corresponding filedescr struct, and (via return arguments) the
1042 pathname and an open file. Return NULL if the module is not found. */
1043
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001044#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001045extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1046 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001047#endif
1048
Tim Peters50d8d372001-02-28 05:34:27 +00001049static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001050static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001051static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001052
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001054find_module(char *fullname, char *subname, PyObject *path, char *buf,
1055 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001056{
Fred Drake4c82b232000-06-30 16:18:57 +00001057 int i, npath;
1058 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001059 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001060 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001061 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001062 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001063#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001064 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001065#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001066 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1067 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1068 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001069 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001070#if defined(PYOS_OS2)
1071 size_t saved_len;
1072 size_t saved_namelen;
1073 char *saved_buf = NULL;
1074#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001075 if (p_loader != NULL)
1076 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001077
Just van Rossum52e14d62002-12-30 22:08:05 +00001078 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001079 PyErr_SetString(PyExc_OverflowError,
1080 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001081 return NULL;
1082 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001083 strcpy(name, subname);
1084
1085 /* sys.meta_path import hook */
1086 if (p_loader != NULL) {
1087 PyObject *meta_path;
1088
1089 meta_path = PySys_GetObject("meta_path");
1090 if (meta_path == NULL || !PyList_Check(meta_path)) {
1091 PyErr_SetString(PyExc_ImportError,
1092 "sys.meta_path must be a list of "
1093 "import hooks");
1094 return NULL;
1095 }
1096 Py_INCREF(meta_path); /* zap guard */
1097 npath = PyList_Size(meta_path);
1098 for (i = 0; i < npath; i++) {
1099 PyObject *loader;
1100 PyObject *hook = PyList_GetItem(meta_path, i);
1101 loader = PyObject_CallMethod(hook, "find_module",
1102 "sO", fullname,
1103 path != NULL ?
1104 path : Py_None);
1105 if (loader == NULL) {
1106 Py_DECREF(meta_path);
1107 return NULL; /* true error */
1108 }
1109 if (loader != Py_None) {
1110 /* a loader was found */
1111 *p_loader = loader;
1112 Py_DECREF(meta_path);
1113 return &importhookdescr;
1114 }
1115 Py_DECREF(loader);
1116 }
1117 Py_DECREF(meta_path);
1118 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001119
1120 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001121 /* The only type of submodule allowed inside a "frozen"
1122 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001123 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1124 PyErr_SetString(PyExc_ImportError,
1125 "full frozen module name too long");
1126 return NULL;
1127 }
1128 strcpy(buf, PyString_AsString(path));
1129 strcat(buf, ".");
1130 strcat(buf, name);
1131 strcpy(name, buf);
Jack Jansen550fdae2001-10-30 13:08:39 +00001132#ifdef macintosh
1133 /* Freezing on the mac works different, and the modules are
1134 ** actually on sys.path. So we don't take the quick exit but
1135 ** continue with the normal flow.
1136 */
1137 path = NULL;
1138#else
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001139 if (find_frozen(name) != NULL) {
1140 strcpy(buf, name);
1141 return &fd_frozen;
1142 }
1143 PyErr_Format(PyExc_ImportError,
1144 "No frozen submodule named %.200s", name);
1145 return NULL;
Jack Jansen550fdae2001-10-30 13:08:39 +00001146#endif
Guido van Rossum0506a431998-08-11 15:07:39 +00001147 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001148 if (path == NULL) {
1149 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001150 strcpy(buf, name);
1151 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001152 }
Greg Ward201baee2001-10-04 14:52:06 +00001153 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001154 strcpy(buf, name);
1155 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001156 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157
Guido van Rossumac279101996-08-22 23:10:58 +00001158#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001159 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1160 if (fp != NULL) {
1161 *p_fp = fp;
1162 return fdp;
1163 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001164#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001165 path = PySys_GetObject("path");
1166 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 if (path == NULL || !PyList_Check(path)) {
1168 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001169 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001170 return NULL;
1171 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001172
1173 path_hooks = PySys_GetObject("path_hooks");
1174 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1175 PyErr_SetString(PyExc_ImportError,
1176 "sys.path_hooks must be a list of "
1177 "import hooks");
1178 return NULL;
1179 }
1180 path_importer_cache = PySys_GetObject("path_importer_cache");
1181 if (path_importer_cache == NULL ||
1182 !PyDict_Check(path_importer_cache)) {
1183 PyErr_SetString(PyExc_ImportError,
1184 "sys.path_importer_cache must be a dict");
1185 return NULL;
1186 }
1187
Guido van Rossum79f25d91997-04-29 20:08:16 +00001188 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001189 namelen = strlen(name);
1190 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001191 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001193#ifdef Py_USING_UNICODE
1194 if (PyUnicode_Check(v)) {
1195 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1196 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1197 if (copy == NULL)
1198 return NULL;
1199 v = copy;
1200 }
1201 else
1202#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001203 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001204 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001206 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1207 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001208 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001209 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001211 if (strlen(buf) != len) {
1212 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001213 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001214 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001215
1216 /* sys.path_hooks import hook */
1217 if (p_loader != NULL) {
1218 PyObject *importer;
1219
1220 importer = get_path_importer(path_importer_cache,
1221 path_hooks, v);
1222 if (importer == NULL)
1223 return NULL;
1224 /* Note: importer is a borrowed reference */
1225 if (importer != Py_None) {
1226 PyObject *loader;
1227 loader = PyObject_CallMethod(importer,
1228 "find_module",
1229 "s", fullname);
1230 if (loader == NULL)
1231 return NULL; /* error */
1232 if (loader != Py_None) {
1233 /* a loader was found */
1234 *p_loader = loader;
1235 return &importhookdescr;
1236 }
1237 Py_DECREF(loader);
1238 }
1239 /* no hook was successful, use builtin import */
1240 }
1241
Jack Jansen9c96a921995-02-15 22:57:06 +00001242#ifdef macintosh
Tim Peters50d8d372001-02-28 05:34:27 +00001243 /*
Guido van Rossum741689d1997-08-12 14:53:39 +00001244 ** Speedup: each sys.path item is interned, and
1245 ** FindResourceModule remembers which items refer to
1246 ** folders (so we don't have to bother trying to look
Jack Jansen9363dca2003-01-24 16:15:45 +00001247 ** into them for resources). We only do this for string
1248 ** items.
Guido van Rossum741689d1997-08-12 14:53:39 +00001249 */
Jack Jansen9363dca2003-01-24 16:15:45 +00001250 if (PyString_Check(PyList_GET_ITEM(path, i))) {
1251 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
1252 v = PyList_GET_ITEM(path, i);
1253 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
1254 static struct filedescr resfiledescr =
1255 {"", "", PY_RESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +00001256
Jack Jansen9363dca2003-01-24 16:15:45 +00001257 Py_XDECREF(copy);
1258 return &resfiledescr;
1259 }
1260 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
1261 static struct filedescr resfiledescr =
1262 {"", "", PY_CODERESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +00001263
Jack Jansen9363dca2003-01-24 16:15:45 +00001264 Py_XDECREF(copy);
1265 return &resfiledescr;
1266 }
Guido van Rossum0f84a341998-08-06 13:36:01 +00001267 }
Jack Jansen9c96a921995-02-15 22:57:06 +00001268#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001269 if (len > 0 && buf[len-1] != SEP
1270#ifdef ALTSEP
1271 && buf[len-1] != ALTSEP
1272#endif
1273 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001274 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001275 strcpy(buf+len, name);
1276 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001277
1278 /* Check for package import (buf holds a directory name,
1279 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001280#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001281 if (stat(buf, &statbuf) == 0 && /* it exists */
1282 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1283 find_init_module(buf) && /* it has __init__.py */
1284 case_ok(buf, len, namelen, name)) { /* and case matches */
1285 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001286 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001287 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001288#else
1289 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001290#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001291 if (isdir(buf) &&
1292 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001293 case_ok(buf, len, namelen, name)) {
1294 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001295 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001296 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001297#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001298#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001299#ifdef macintosh
1300 fdp = PyMac_FindModuleExtension(buf, &len, name);
Jack Jansen4df3c522001-03-20 23:09:54 +00001301 if (fdp) {
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001302#else
Andrew MacIntyred9400542002-02-26 11:41:34 +00001303#if defined(PYOS_OS2)
1304 /* take a snapshot of the module spec for restoration
1305 * after the 8 character DLL hackery
1306 */
1307 saved_buf = strdup(buf);
1308 saved_len = len;
1309 saved_namelen = namelen;
1310#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001312#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001313 /* OS/2 limits DLLs to 8 character names (w/o
1314 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001315 * so if the name is longer than that and its a
1316 * dynamically loaded module we're going to try,
1317 * truncate the name before trying
1318 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001319 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001320 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001321 const struct filedescr *scan;
1322 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001323 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001324 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001325 break;
1326 else
1327 scan++;
1328 }
1329 if (scan->suffix != NULL) {
1330 /* yes, so truncate the name */
1331 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001332 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001333 buf[len] = '\0';
1334 }
1335 }
1336#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001337 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001339 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansen4df3c522001-03-20 23:09:54 +00001340#endif /* !macintosh */
Jack Jansenc88da1f2002-05-28 10:58:19 +00001341 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001342 if (filemode[0] == 'U')
1343 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001344 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001345 if (fp != NULL) {
1346 if (case_ok(buf, len, namelen, name))
1347 break;
1348 else { /* continue search */
1349 fclose(fp);
1350 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001351 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001352 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001353#if defined(PYOS_OS2)
1354 /* restore the saved snapshot */
1355 strcpy(buf, saved_buf);
1356 len = saved_len;
1357 namelen = saved_namelen;
1358#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001359 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001360#if defined(PYOS_OS2)
1361 /* don't need/want the module name snapshot anymore */
1362 if (saved_buf)
1363 {
1364 free(saved_buf);
1365 saved_buf = NULL;
1366 }
1367#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001368 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001369 if (fp != NULL)
1370 break;
1371 }
1372 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001373 PyErr_Format(PyExc_ImportError,
1374 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001375 return NULL;
1376 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001377 *p_fp = fp;
1378 return fdp;
1379}
1380
Tim Petersd1e87a82001-03-01 18:12:00 +00001381/* case_ok(char* buf, int len, int namelen, char* name)
1382 * The arguments here are tricky, best shown by example:
1383 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1384 * ^ ^ ^ ^
1385 * |--------------------- buf ---------------------|
1386 * |------------------- len ------------------|
1387 * |------ name -------|
1388 * |----- namelen -----|
1389 * buf is the full path, but len only counts up to (& exclusive of) the
1390 * extension. name is the module name, also exclusive of extension.
1391 *
1392 * We've already done a successful stat() or fopen() on buf, so know that
1393 * there's some match, possibly case-insensitive.
1394 *
Tim Peters50d8d372001-02-28 05:34:27 +00001395 * case_ok() is to return 1 if there's a case-sensitive match for
1396 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1397 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001398 *
Tim Peters50d8d372001-02-28 05:34:27 +00001399 * case_ok() is used to implement case-sensitive import semantics even
1400 * on platforms with case-insensitive filesystems. It's trivial to implement
1401 * for case-sensitive filesystems. It's pretty much a cross-platform
1402 * nightmare for systems with case-insensitive filesystems.
1403 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001404
Tim Peters50d8d372001-02-28 05:34:27 +00001405/* First we may need a pile of platform-specific header files; the sequence
1406 * of #if's here should match the sequence in the body of case_ok().
1407 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001408#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001409#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001410#ifdef __CYGWIN__
1411#include <sys/cygwin.h>
1412#endif
1413
Tim Peters50d8d372001-02-28 05:34:27 +00001414#elif defined(DJGPP)
1415#include <dir.h>
1416
1417#elif defined(macintosh)
1418#include <TextUtils.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001419
Tim Petersd1e87a82001-03-01 18:12:00 +00001420#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001421#include <sys/types.h>
1422#include <dirent.h>
1423
Andrew MacIntyred9400542002-02-26 11:41:34 +00001424#elif defined(PYOS_OS2)
1425#define INCL_DOS
1426#define INCL_DOSERRORS
1427#define INCL_NOPMAPI
1428#include <os2.h>
1429
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001430#elif defined(RISCOS)
1431#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001432#endif
1433
Guido van Rossum0980bd91998-02-13 17:18:36 +00001434static int
Tim Peters50d8d372001-02-28 05:34:27 +00001435case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001436{
Tim Peters50d8d372001-02-28 05:34:27 +00001437/* Pick a platform-specific implementation; the sequence of #if's here should
1438 * match the sequence just above.
1439 */
1440
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001441/* MS_WINDOWS || __CYGWIN__ */
1442#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001443 WIN32_FIND_DATA data;
1444 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001445#ifdef __CYGWIN__
1446 char tempbuf[MAX_PATH];
1447#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001448
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001449 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001450 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001451
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001452#ifdef __CYGWIN__
1453 cygwin32_conv_to_win32_path(buf, tempbuf);
1454 h = FindFirstFile(tempbuf, &data);
1455#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001456 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001457#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001458 if (h == INVALID_HANDLE_VALUE) {
1459 PyErr_Format(PyExc_NameError,
1460 "Can't find file for module %.100s\n(filename %.300s)",
1461 name, buf);
1462 return 0;
1463 }
1464 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001465 return strncmp(data.cFileName, name, namelen) == 0;
1466
1467/* DJGPP */
1468#elif defined(DJGPP)
1469 struct ffblk ffblk;
1470 int done;
1471
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001472 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001473 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001474
1475 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1476 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001477 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001478 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001479 name, buf);
1480 return 0;
1481 }
Tim Peters50d8d372001-02-28 05:34:27 +00001482 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001483
Tim Peters50d8d372001-02-28 05:34:27 +00001484/* macintosh */
1485#elif defined(macintosh)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001486 FSSpec fss;
1487 OSErr err;
Tim Peters50d8d372001-02-28 05:34:27 +00001488
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001489 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Peters50d8d372001-02-28 05:34:27 +00001490 return 1;
1491
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001492 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001493 if (err) {
1494 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001495 "Can't find file for module %.100s\n(filename %.300s)",
1496 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001497 return 0;
1498 }
Tim Peters50d8d372001-02-28 05:34:27 +00001499 return fss.name[0] >= namelen &&
1500 strncmp(name, (char *)fss.name+1, namelen) == 0;
1501
Tim Peters677898a2001-03-02 03:28:03 +00001502/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001503#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001504 DIR *dirp;
1505 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001506 char dirname[MAXPATHLEN + 1];
1507 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001508
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001509 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001510 return 1;
1511
Tim Petersd1e87a82001-03-01 18:12:00 +00001512 /* Copy the dir component into dirname; substitute "." if empty */
1513 if (dirlen <= 0) {
1514 dirname[0] = '.';
1515 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001516 }
1517 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001518 assert(dirlen <= MAXPATHLEN);
1519 memcpy(dirname, buf, dirlen);
1520 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001521 }
1522 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001523 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001524 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001525 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001526 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001527 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001528#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001529 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001530#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001531 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001532#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001533 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001534 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001535 (void)closedir(dirp);
1536 return 1; /* Found */
1537 }
1538 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001539 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001540 }
Tim Peters430f5d42001-03-01 01:30:56 +00001541 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001542
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001543/* RISC OS */
1544#elif defined(RISCOS)
1545 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1546 char buf2[MAXPATHLEN+2];
1547 char *nameWithExt = buf+len-namelen;
1548 int canonlen;
1549 os_error *e;
1550
1551 if (Py_GETENV("PYTHONCASEOK") != NULL)
1552 return 1;
1553
1554 /* workaround:
1555 append wildcard, otherwise case of filename wouldn't be touched */
1556 strcpy(buf2, buf);
1557 strcat(buf2, "*");
1558
1559 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1560 canonlen = MAXPATHLEN+1-canonlen;
1561 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1562 return 0;
1563 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1564 return 1; /* match */
1565
1566 return 0;
1567
Andrew MacIntyred9400542002-02-26 11:41:34 +00001568/* OS/2 */
1569#elif defined(PYOS_OS2)
1570 HDIR hdir = 1;
1571 ULONG srchcnt = 1;
1572 FILEFINDBUF3 ffbuf;
1573 APIRET rc;
1574
1575 if (getenv("PYTHONCASEOK") != NULL)
1576 return 1;
1577
1578 rc = DosFindFirst(buf,
1579 &hdir,
1580 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1581 &ffbuf, sizeof(ffbuf),
1582 &srchcnt,
1583 FIL_STANDARD);
1584 if (rc != NO_ERROR)
1585 return 0;
1586 return strncmp(ffbuf.achName, name, namelen) == 0;
1587
Tim Peters50d8d372001-02-28 05:34:27 +00001588/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1589#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001590 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001591
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001592#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001593}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001594
Guido van Rossum0980bd91998-02-13 17:18:36 +00001595
Guido van Rossum197346f1997-10-31 18:38:52 +00001596#ifdef HAVE_STAT
1597/* Helper to look for __init__.py or __init__.py[co] in potential package */
1598static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001599find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001600{
Tim Peters0f9431f2001-07-05 03:47:53 +00001601 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001602 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001603 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001604 struct stat statbuf;
1605
Tim Peters0f9431f2001-07-05 03:47:53 +00001606/* For calling case_ok(buf, len, namelen, name):
1607 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1608 * ^ ^ ^ ^
1609 * |--------------------- buf ---------------------|
1610 * |------------------- len ------------------|
1611 * |------ name -------|
1612 * |----- namelen -----|
1613 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001614 if (save_len + 13 >= MAXPATHLEN)
1615 return 0;
1616 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001617 pname = buf + i;
1618 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001619 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001620 if (case_ok(buf,
1621 save_len + 9, /* len("/__init__") */
1622 8, /* len("__init__") */
1623 pname)) {
1624 buf[save_len] = '\0';
1625 return 1;
1626 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001627 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001628 i += strlen(pname);
1629 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001630 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001631 if (case_ok(buf,
1632 save_len + 9, /* len("/__init__") */
1633 8, /* len("__init__") */
1634 pname)) {
1635 buf[save_len] = '\0';
1636 return 1;
1637 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001638 }
1639 buf[save_len] = '\0';
1640 return 0;
1641}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001642
1643#else
1644
1645#ifdef RISCOS
1646static int
1647find_init_module(buf)
1648 char *buf;
1649{
1650 int save_len = strlen(buf);
1651 int i = save_len;
1652
1653 if (save_len + 13 >= MAXPATHLEN)
1654 return 0;
1655 buf[i++] = SEP;
1656 strcpy(buf+i, "__init__/py");
1657 if (isfile(buf)) {
1658 buf[save_len] = '\0';
1659 return 1;
1660 }
1661
1662 if (Py_OptimizeFlag)
1663 strcpy(buf+i, "o");
1664 else
1665 strcpy(buf+i, "c");
1666 if (isfile(buf)) {
1667 buf[save_len] = '\0';
1668 return 1;
1669 }
1670 buf[save_len] = '\0';
1671 return 0;
1672}
1673#endif /*RISCOS*/
1674
Guido van Rossum197346f1997-10-31 18:38:52 +00001675#endif /* HAVE_STAT */
1676
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001677
Tim Petersdbd9ba62000-07-09 03:09:57 +00001678static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001679
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001681 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001682
Guido van Rossum79f25d91997-04-29 20:08:16 +00001683static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001684load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001685{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001686 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001688 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001690 /* First check that there's an open file (if we need one) */
1691 switch (type) {
1692 case PY_SOURCE:
1693 case PY_COMPILED:
1694 if (fp == NULL) {
1695 PyErr_Format(PyExc_ValueError,
1696 "file object required for import (type code %d)",
1697 type);
1698 return NULL;
1699 }
1700 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001701
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001702 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001703
1704 case PY_SOURCE:
1705 m = load_source_module(name, buf, fp);
1706 break;
1707
1708 case PY_COMPILED:
1709 m = load_compiled_module(name, buf, fp);
1710 break;
1711
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001712#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001714 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001716#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001717
Jack Jansen9c96a921995-02-15 22:57:06 +00001718#ifdef macintosh
1719 case PY_RESOURCE:
1720 m = PyMac_LoadResourceModule(name, buf);
1721 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001722 case PY_CODERESOURCE:
1723 m = PyMac_LoadCodeResourceModule(name, buf);
1724 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001725#endif
1726
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001727 case PKG_DIRECTORY:
1728 m = load_package(name, buf);
1729 break;
1730
1731 case C_BUILTIN:
1732 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001733 if (buf != NULL && buf[0] != '\0')
1734 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001735 if (type == C_BUILTIN)
1736 err = init_builtin(name);
1737 else
1738 err = PyImport_ImportFrozenModule(name);
1739 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001740 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001741 if (err == 0) {
1742 PyErr_Format(PyExc_ImportError,
1743 "Purported %s module %.200s not found",
1744 type == C_BUILTIN ?
1745 "builtin" : "frozen",
1746 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001747 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001748 }
1749 modules = PyImport_GetModuleDict();
1750 m = PyDict_GetItemString(modules, name);
1751 if (m == NULL) {
1752 PyErr_Format(
1753 PyExc_ImportError,
1754 "%s module %.200s not properly initialized",
1755 type == C_BUILTIN ?
1756 "builtin" : "frozen",
1757 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001758 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001759 }
1760 Py_INCREF(m);
1761 break;
1762
Just van Rossum52e14d62002-12-30 22:08:05 +00001763 case IMP_HOOK: {
1764 if (loader == NULL) {
1765 PyErr_SetString(PyExc_ImportError,
1766 "import hook without loader");
1767 return NULL;
1768 }
1769 m = PyObject_CallMethod(loader, "load_module", "s", name);
1770 break;
1771 }
1772
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001774 PyErr_Format(PyExc_ImportError,
1775 "Don't know how to import %.200s (type code %d)",
1776 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001777 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001778
1779 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780
1781 return m;
1782}
1783
1784
1785/* Initialize a built-in module.
1786 Return 1 for succes, 0 if the module is not found, and -1 with
1787 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001788
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001789static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001790init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001791{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001792 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001793
Greg Ward201baee2001-10-04 14:52:06 +00001794 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001795 return 1;
1796
Guido van Rossum771c6c81997-10-31 18:37:24 +00001797 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001798 if (strcmp(name, p->name) == 0) {
1799 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001800 PyErr_Format(PyExc_ImportError,
1801 "Cannot re-init internal module %.200s",
1802 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001803 return -1;
1804 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001806 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001807 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001808 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001810 if (_PyImport_FixupExtension(name, name) == NULL)
1811 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001812 return 1;
1813 }
1814 }
1815 return 0;
1816}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001817
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001819/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001820
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001821static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001822find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001823{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001824 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001825
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001827 if (p->name == NULL)
1828 return NULL;
1829 if (strcmp(p->name, name) == 0)
1830 break;
1831 }
1832 return p;
1833}
1834
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001836get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001837{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001838 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001839 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001840
1841 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001842 PyErr_Format(PyExc_ImportError,
1843 "No such frozen object named %.200s",
1844 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001845 return NULL;
1846 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001847 if (p->code == NULL) {
1848 PyErr_Format(PyExc_ImportError,
1849 "Excluded frozen object named %.200s",
1850 name);
1851 return NULL;
1852 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001853 size = p->size;
1854 if (size < 0)
1855 size = -size;
1856 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001857}
1858
1859/* Initialize a frozen module.
1860 Return 1 for succes, 0 if the module is not found, and -1 with
1861 an exception set if the initialization failed.
1862 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001863
1864int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001865PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001866{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001867 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 PyObject *co;
1869 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001870 int ispackage;
1871 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001872
1873 if (p == NULL)
1874 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001875 if (p->code == NULL) {
1876 PyErr_Format(PyExc_ImportError,
1877 "Excluded frozen object named %.200s",
1878 name);
1879 return -1;
1880 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001881 size = p->size;
1882 ispackage = (size < 0);
1883 if (ispackage)
1884 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001886 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001887 name, ispackage ? " package" : "");
1888 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001889 if (co == NULL)
1890 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 if (!PyCode_Check(co)) {
1892 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001893 PyErr_Format(PyExc_TypeError,
1894 "frozen object %.200s is not a code object",
1895 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001896 return -1;
1897 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001898 if (ispackage) {
1899 /* Set __path__ to the package name */
1900 PyObject *d, *s;
1901 int err;
1902 m = PyImport_AddModule(name);
1903 if (m == NULL)
1904 return -1;
1905 d = PyModule_GetDict(m);
1906 s = PyString_InternFromString(name);
1907 if (s == NULL)
1908 return -1;
1909 err = PyDict_SetItemString(d, "__path__", s);
1910 Py_DECREF(s);
1911 if (err != 0)
1912 return err;
1913 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001914 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001916 if (m == NULL)
1917 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001918 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001919 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001920}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001921
1922
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001923/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001924 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001925
Guido van Rossum79f25d91997-04-29 20:08:16 +00001926PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001927PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001928{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001929 PyObject *pname;
1930 PyObject *result;
1931
1932 pname = PyString_FromString(name);
1933 result = PyImport_Import(pname);
1934 Py_DECREF(pname);
1935 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001936}
1937
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001938/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001939static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1940static PyObject *load_next(PyObject *mod, PyObject *altmod,
1941 char **p_name, char *buf, int *p_buflen);
1942static int mark_miss(char *name);
1943static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1944 char *buf, int buflen, int recursive);
1945static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001946
1947/* The Magnum Opus of dotted-name import :-) */
1948
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001949static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001950import_module_ex(char *name, PyObject *globals, PyObject *locals,
1951 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001952{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001953 char buf[MAXPATHLEN+1];
1954 int buflen = 0;
1955 PyObject *parent, *head, *next, *tail;
1956
1957 parent = get_parent(globals, buf, &buflen);
1958 if (parent == NULL)
1959 return NULL;
1960
1961 head = load_next(parent, Py_None, &name, buf, &buflen);
1962 if (head == NULL)
1963 return NULL;
1964
1965 tail = head;
1966 Py_INCREF(tail);
1967 while (name) {
1968 next = load_next(tail, tail, &name, buf, &buflen);
1969 Py_DECREF(tail);
1970 if (next == NULL) {
1971 Py_DECREF(head);
1972 return NULL;
1973 }
1974 tail = next;
1975 }
1976
1977 if (fromlist != NULL) {
1978 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1979 fromlist = NULL;
1980 }
1981
1982 if (fromlist == NULL) {
1983 Py_DECREF(tail);
1984 return head;
1985 }
1986
1987 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001988 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001989 Py_DECREF(tail);
1990 return NULL;
1991 }
1992
1993 return tail;
1994}
1995
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001996PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001997PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1998 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001999{
2000 PyObject *result;
2001 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00002002 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002003 if (unlock_import() < 0) {
2004 Py_XDECREF(result);
2005 PyErr_SetString(PyExc_RuntimeError,
2006 "not holding the import lock");
2007 return NULL;
2008 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002009 return result;
2010}
2011
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002012static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002013get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002014{
2015 static PyObject *namestr = NULL;
2016 static PyObject *pathstr = NULL;
2017 PyObject *modname, *modpath, *modules, *parent;
2018
2019 if (globals == NULL || !PyDict_Check(globals))
2020 return Py_None;
2021
2022 if (namestr == NULL) {
2023 namestr = PyString_InternFromString("__name__");
2024 if (namestr == NULL)
2025 return NULL;
2026 }
2027 if (pathstr == NULL) {
2028 pathstr = PyString_InternFromString("__path__");
2029 if (pathstr == NULL)
2030 return NULL;
2031 }
2032
2033 *buf = '\0';
2034 *p_buflen = 0;
2035 modname = PyDict_GetItem(globals, namestr);
2036 if (modname == NULL || !PyString_Check(modname))
2037 return Py_None;
2038
2039 modpath = PyDict_GetItem(globals, pathstr);
2040 if (modpath != NULL) {
2041 int len = PyString_GET_SIZE(modname);
2042 if (len > MAXPATHLEN) {
2043 PyErr_SetString(PyExc_ValueError,
2044 "Module name too long");
2045 return NULL;
2046 }
2047 strcpy(buf, PyString_AS_STRING(modname));
2048 *p_buflen = len;
2049 }
2050 else {
2051 char *start = PyString_AS_STRING(modname);
2052 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002053 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002054 if (lastdot == NULL)
2055 return Py_None;
2056 len = lastdot - start;
2057 if (len >= MAXPATHLEN) {
2058 PyErr_SetString(PyExc_ValueError,
2059 "Module name too long");
2060 return NULL;
2061 }
2062 strncpy(buf, start, len);
2063 buf[len] = '\0';
2064 *p_buflen = len;
2065 }
2066
2067 modules = PyImport_GetModuleDict();
2068 parent = PyDict_GetItemString(modules, buf);
2069 if (parent == NULL)
2070 parent = Py_None;
2071 return parent;
2072 /* We expect, but can't guarantee, if parent != None, that:
2073 - parent.__name__ == buf
2074 - parent.__dict__ is globals
2075 If this is violated... Who cares? */
2076}
2077
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002078/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002079static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002080load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2081 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002082{
2083 char *name = *p_name;
2084 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002085 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002086 char *p;
2087 PyObject *result;
2088
2089 if (dot == NULL) {
2090 *p_name = NULL;
2091 len = strlen(name);
2092 }
2093 else {
2094 *p_name = dot+1;
2095 len = dot-name;
2096 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002097 if (len == 0) {
2098 PyErr_SetString(PyExc_ValueError,
2099 "Empty module name");
2100 return NULL;
2101 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002102
2103 p = buf + *p_buflen;
2104 if (p != buf)
2105 *p++ = '.';
2106 if (p+len-buf >= MAXPATHLEN) {
2107 PyErr_SetString(PyExc_ValueError,
2108 "Module name too long");
2109 return NULL;
2110 }
2111 strncpy(p, name, len);
2112 p[len] = '\0';
2113 *p_buflen = p+len-buf;
2114
2115 result = import_submodule(mod, p, buf);
2116 if (result == Py_None && altmod != mod) {
2117 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002118 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002119 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002120 if (result != NULL && result != Py_None) {
2121 if (mark_miss(buf) != 0) {
2122 Py_DECREF(result);
2123 return NULL;
2124 }
2125 strncpy(buf, name, len);
2126 buf[len] = '\0';
2127 *p_buflen = len;
2128 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002129 }
2130 if (result == NULL)
2131 return NULL;
2132
2133 if (result == Py_None) {
2134 Py_DECREF(result);
2135 PyErr_Format(PyExc_ImportError,
2136 "No module named %.200s", name);
2137 return NULL;
2138 }
2139
2140 return result;
2141}
2142
2143static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002144mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002145{
2146 PyObject *modules = PyImport_GetModuleDict();
2147 return PyDict_SetItemString(modules, name, Py_None);
2148}
2149
2150static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002151ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2152 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002153{
2154 int i;
2155
2156 if (!PyObject_HasAttrString(mod, "__path__"))
2157 return 1;
2158
2159 for (i = 0; ; i++) {
2160 PyObject *item = PySequence_GetItem(fromlist, i);
2161 int hasit;
2162 if (item == NULL) {
2163 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2164 PyErr_Clear();
2165 return 1;
2166 }
2167 return 0;
2168 }
2169 if (!PyString_Check(item)) {
2170 PyErr_SetString(PyExc_TypeError,
2171 "Item in ``from list'' not a string");
2172 Py_DECREF(item);
2173 return 0;
2174 }
2175 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002176 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002177 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002178 /* See if the package defines __all__ */
2179 if (recursive)
2180 continue; /* Avoid endless recursion */
2181 all = PyObject_GetAttrString(mod, "__all__");
2182 if (all == NULL)
2183 PyErr_Clear();
2184 else {
2185 if (!ensure_fromlist(mod, all, buf, buflen, 1))
2186 return 0;
2187 Py_DECREF(all);
2188 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002189 continue;
2190 }
2191 hasit = PyObject_HasAttr(mod, item);
2192 if (!hasit) {
2193 char *subname = PyString_AS_STRING(item);
2194 PyObject *submod;
2195 char *p;
2196 if (buflen + strlen(subname) >= MAXPATHLEN) {
2197 PyErr_SetString(PyExc_ValueError,
2198 "Module name too long");
2199 Py_DECREF(item);
2200 return 0;
2201 }
2202 p = buf + buflen;
2203 *p++ = '.';
2204 strcpy(p, subname);
2205 submod = import_submodule(mod, subname, buf);
2206 Py_XDECREF(submod);
2207 if (submod == NULL) {
2208 Py_DECREF(item);
2209 return 0;
2210 }
2211 }
2212 Py_DECREF(item);
2213 }
2214
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002215 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002216}
2217
2218static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002219import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002220{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002221 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumf48f11c2001-07-23 13:27:49 +00002222 PyObject *m, *res = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002223
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002224 /* Require:
2225 if mod == None: subname == fullname
2226 else: mod.__name__ + "." + subname == fullname
2227 */
2228
Tim Peters50d8d372001-02-28 05:34:27 +00002229 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002230 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002231 }
2232 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002233 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002234 char buf[MAXPATHLEN+1];
2235 struct filedescr *fdp;
2236 FILE *fp = NULL;
2237
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002238 if (mod == Py_None)
2239 path = NULL;
2240 else {
2241 path = PyObject_GetAttrString(mod, "__path__");
2242 if (path == NULL) {
2243 PyErr_Clear();
2244 Py_INCREF(Py_None);
2245 return Py_None;
2246 }
2247 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002248
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002249 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002250 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2251 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002252 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002253 if (fdp == NULL) {
2254 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2255 return NULL;
2256 PyErr_Clear();
2257 Py_INCREF(Py_None);
2258 return Py_None;
2259 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002260 m = load_module(fullname, fp, buf, fdp->type, loader);
2261 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002262 if (fp)
2263 fclose(fp);
Guido van Rossumf48f11c2001-07-23 13:27:49 +00002264 if (mod != Py_None) {
2265 /* Irrespective of the success of this load, make a
2266 reference to it in the parent package module.
2267 A copy gets saved in the modules dictionary
2268 under the full name, so get a reference from
2269 there, if need be. (The exception is when
2270 the load failed with a SyntaxError -- then
2271 there's no trace in sys.modules. In that case,
2272 of course, do nothing extra.) */
2273 res = m;
2274 if (res == NULL)
2275 res = PyDict_GetItemString(modules, fullname);
2276 if (res != NULL &&
2277 PyObject_SetAttrString(mod, subname, res) < 0) {
2278 Py_XDECREF(m);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002279 m = NULL;
2280 }
2281 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002282 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002283
2284 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002285}
2286
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002287
2288/* Re-import a module of any kind and return its module object, WITH
2289 INCREMENTED REFERENCE COUNT */
2290
Guido van Rossum79f25d91997-04-29 20:08:16 +00002291PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002292PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002293{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002294 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002295 PyObject *path = NULL;
2296 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002297 char buf[MAXPATHLEN+1];
2298 struct filedescr *fdp;
2299 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002300
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 if (m == NULL || !PyModule_Check(m)) {
2302 PyErr_SetString(PyExc_TypeError,
2303 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002304 return NULL;
2305 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002306 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002307 if (name == NULL)
2308 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002309 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002310 PyErr_Format(PyExc_ImportError,
2311 "reload(): module %.200s not in sys.modules",
2312 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002313 return NULL;
2314 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002315 subname = strrchr(name, '.');
2316 if (subname == NULL)
2317 subname = name;
2318 else {
2319 PyObject *parentname, *parent;
2320 parentname = PyString_FromStringAndSize(name, (subname-name));
2321 if (parentname == NULL)
2322 return NULL;
2323 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002324 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002325 if (parent == NULL) {
2326 PyErr_Format(PyExc_ImportError,
2327 "reload(): parent %.200s not in sys.modules",
2328 name);
2329 return NULL;
2330 }
2331 subname++;
2332 path = PyObject_GetAttrString(parent, "__path__");
2333 if (path == NULL)
2334 PyErr_Clear();
2335 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002336 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002337 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002338 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002339 if (fdp == NULL)
2340 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002341 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002342 if (fp)
2343 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002344 return m;
2345}
2346
2347
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002348/* Higher-level import emulator which emulates the "import" statement
2349 more accurately -- it invokes the __import__() function from the
2350 builtins of the current globals. This means that the import is
2351 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002352 environment, e.g. by "rexec".
2353 A dummy list ["__doc__"] is passed as the 4th argument so that
2354 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2355 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002356
2357PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002358PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002359{
2360 static PyObject *silly_list = NULL;
2361 static PyObject *builtins_str = NULL;
2362 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002363 PyObject *globals = NULL;
2364 PyObject *import = NULL;
2365 PyObject *builtins = NULL;
2366 PyObject *r = NULL;
2367
2368 /* Initialize constant string objects */
2369 if (silly_list == NULL) {
2370 import_str = PyString_InternFromString("__import__");
2371 if (import_str == NULL)
2372 return NULL;
2373 builtins_str = PyString_InternFromString("__builtins__");
2374 if (builtins_str == NULL)
2375 return NULL;
2376 silly_list = Py_BuildValue("[s]", "__doc__");
2377 if (silly_list == NULL)
2378 return NULL;
2379 }
2380
2381 /* Get the builtins from current globals */
2382 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002383 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002384 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002385 builtins = PyObject_GetItem(globals, builtins_str);
2386 if (builtins == NULL)
2387 goto err;
2388 }
2389 else {
2390 /* No globals -- use standard builtins, and fake globals */
2391 PyErr_Clear();
2392
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002393 builtins = PyImport_ImportModuleEx("__builtin__",
2394 NULL, NULL, NULL);
2395 if (builtins == NULL)
2396 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002397 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2398 if (globals == NULL)
2399 goto err;
2400 }
2401
2402 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002403 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002404 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002405 if (import == NULL)
2406 PyErr_SetObject(PyExc_KeyError, import_str);
2407 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002408 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002409 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002410 if (import == NULL)
2411 goto err;
2412
2413 /* Call the _import__ function with the proper argument list */
2414 r = PyObject_CallFunction(import, "OOOO",
2415 module_name, globals, globals, silly_list);
2416
2417 err:
2418 Py_XDECREF(globals);
2419 Py_XDECREF(builtins);
2420 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002421
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002422 return r;
2423}
2424
2425
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002426/* Module 'imp' provides Python access to the primitives used for
2427 importing modules.
2428*/
2429
Guido van Rossum79f25d91997-04-29 20:08:16 +00002430static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002431imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002432{
2433 char buf[4];
2434
Guido van Rossum43713e52000-02-29 13:59:29 +00002435 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002436 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00002437 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2438 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2439 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2440 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441
Guido van Rossum79f25d91997-04-29 20:08:16 +00002442 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002443}
2444
Guido van Rossum79f25d91997-04-29 20:08:16 +00002445static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002446imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002447{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002448 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002449 struct filedescr *fdp;
2450
Guido van Rossum43713e52000-02-29 13:59:29 +00002451 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002452 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002454 if (list == NULL)
2455 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002456 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2457 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002458 fdp->suffix, fdp->mode, fdp->type);
2459 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002461 return NULL;
2462 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 if (PyList_Append(list, item) < 0) {
2464 Py_DECREF(list);
2465 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002466 return NULL;
2467 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002468 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002469 }
2470 return list;
2471}
2472
Guido van Rossum79f25d91997-04-29 20:08:16 +00002473static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002474call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002475{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002476 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002477 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002478 struct filedescr *fdp;
2479 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002480 FILE *fp = NULL;
2481
2482 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002483 if (path == Py_None)
2484 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002485 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002486 if (fdp == NULL)
2487 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002488 if (fp != NULL) {
2489 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2490 if (fob == NULL) {
2491 fclose(fp);
2492 return NULL;
2493 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002494 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002495 else {
2496 fob = Py_None;
2497 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002498 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002499 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002500 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002501 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002502 return ret;
2503}
2504
Guido van Rossum79f25d91997-04-29 20:08:16 +00002505static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002506imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002507{
2508 char *name;
2509 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002510 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002511 return NULL;
2512 return call_find_module(name, path);
2513}
2514
2515static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002516imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002517{
2518 char *name;
2519 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002520 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002521 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002522 return NULL;
2523 ret = init_builtin(name);
2524 if (ret < 0)
2525 return NULL;
2526 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527 Py_INCREF(Py_None);
2528 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002529 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 m = PyImport_AddModule(name);
2531 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002532 return m;
2533}
2534
Guido van Rossum79f25d91997-04-29 20:08:16 +00002535static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002536imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002537{
2538 char *name;
2539 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002540 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002541 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002542 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002543 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002544 if (ret < 0)
2545 return NULL;
2546 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002547 Py_INCREF(Py_None);
2548 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002549 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002550 m = PyImport_AddModule(name);
2551 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002552 return m;
2553}
2554
Guido van Rossum79f25d91997-04-29 20:08:16 +00002555static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002556imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002557{
2558 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002559
Guido van Rossum43713e52000-02-29 13:59:29 +00002560 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002561 return NULL;
2562 return get_frozen_object(name);
2563}
2564
Guido van Rossum79f25d91997-04-29 20:08:16 +00002565static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002566imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002567{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002568 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002569 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002571 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572}
2573
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002575imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002576{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002578 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002579 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002580 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002581 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002582 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002583}
2584
2585static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002586get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002587{
2588 FILE *fp;
2589 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002590 if (mode[0] == 'U')
2591 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592 fp = fopen(pathname, mode);
2593 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595 }
2596 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002597 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002598 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002599 PyErr_SetString(PyExc_ValueError,
2600 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002601 }
2602 return fp;
2603}
2604
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002606imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002607{
2608 char *name;
2609 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002610 PyObject *fob = NULL;
2611 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002612 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002613 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002615 return NULL;
2616 fp = get_file(pathname, fob, "rb");
2617 if (fp == NULL)
2618 return NULL;
2619 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002620 if (fob == NULL)
2621 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002622 return m;
2623}
2624
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002625#ifdef HAVE_DYNAMIC_LOADING
2626
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002628imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002629{
2630 char *name;
2631 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002632 PyObject *fob = NULL;
2633 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002634 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002635 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002636 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002637 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002638 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002639 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002640 if (fp == NULL)
2641 return NULL;
2642 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002643 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002644 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002645}
2646
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002647#endif /* HAVE_DYNAMIC_LOADING */
2648
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002650imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002651{
2652 char *name;
2653 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002654 PyObject *fob = NULL;
2655 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002657 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002658 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002659 return NULL;
2660 fp = get_file(pathname, fob, "r");
2661 if (fp == NULL)
2662 return NULL;
2663 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002664 if (fob == NULL)
2665 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002666 return m;
2667}
2668
Jack Jansen9c96a921995-02-15 22:57:06 +00002669#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002670static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002671imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002672{
2673 char *name;
2674 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002675 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002676
Guido van Rossum43713e52000-02-29 13:59:29 +00002677 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002678 return NULL;
2679 m = PyMac_LoadResourceModule(name, pathname);
2680 return m;
2681}
2682#endif /* macintosh */
2683
Guido van Rossum79f25d91997-04-29 20:08:16 +00002684static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002685imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002686{
2687 char *name;
2688 PyObject *fob;
2689 char *pathname;
2690 char *suffix; /* Unused */
2691 char *mode;
2692 int type;
2693 FILE *fp;
2694
Guido van Rossum43713e52000-02-29 13:59:29 +00002695 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002696 &name, &fob, &pathname,
2697 &suffix, &mode, &type))
2698 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002699 if (*mode) {
2700 /* Mode must start with 'r' or 'U' and must not contain '+'.
2701 Implicit in this test is the assumption that the mode
2702 may contain other modifiers like 'b' or 't'. */
2703
2704 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002705 PyErr_Format(PyExc_ValueError,
2706 "invalid file open mode %.200s", mode);
2707 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002708 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002709 }
2710 if (fob == Py_None)
2711 fp = NULL;
2712 else {
2713 if (!PyFile_Check(fob)) {
2714 PyErr_SetString(PyExc_ValueError,
2715 "load_module arg#2 should be a file or None");
2716 return NULL;
2717 }
2718 fp = get_file(pathname, fob, mode);
2719 if (fp == NULL)
2720 return NULL;
2721 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002722 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002723}
2724
2725static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002726imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002727{
2728 char *name;
2729 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002730 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002731 return NULL;
2732 return load_package(name, pathname);
2733}
2734
2735static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002736imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002737{
2738 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002739 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002740 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002741 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002742}
2743
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002744/* Doc strings */
2745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(doc_imp,
2747"This module provides the components needed to build your own\n\
2748__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002750PyDoc_STRVAR(doc_find_module,
2751"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002752Search for a module. If path is omitted or None, search for a\n\
2753built-in, frozen or special module and continue search in sys.path.\n\
2754The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002757PyDoc_STRVAR(doc_load_module,
2758"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002759Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002760The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002762PyDoc_STRVAR(doc_get_magic,
2763"get_magic() -> string\n\
2764Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002765
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002766PyDoc_STRVAR(doc_get_suffixes,
2767"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002768Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002769that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771PyDoc_STRVAR(doc_new_module,
2772"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002773Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002775
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776PyDoc_STRVAR(doc_lock_held,
2777"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002778Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002780
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002781PyDoc_STRVAR(doc_acquire_lock,
2782"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002783Acquires the interpreter's import lock for the current thread.\n\
2784This lock should be used by import hooks to ensure thread-safety\n\
2785when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002786On platforms without threads, this function does nothing.");
2787
2788PyDoc_STRVAR(doc_release_lock,
2789"release_lock() -> None\n\
2790Release the interpreter's import lock.\n\
2791On platforms without threads, this function does nothing.");
2792
Guido van Rossum79f25d91997-04-29 20:08:16 +00002793static PyMethodDef imp_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00002794 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2795 {"get_magic", imp_get_magic, METH_VARARGS, doc_get_magic},
2796 {"get_suffixes", imp_get_suffixes, METH_VARARGS, doc_get_suffixes},
2797 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2798 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2799 {"lock_held", imp_lock_held, METH_VARARGS, doc_lock_held},
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002800 {"acquire_lock", imp_acquire_lock, METH_VARARGS, doc_acquire_lock},
2801 {"release_lock", imp_release_lock, METH_VARARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002802 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002803 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2804 {"init_builtin", imp_init_builtin, METH_VARARGS},
2805 {"init_frozen", imp_init_frozen, METH_VARARGS},
2806 {"is_builtin", imp_is_builtin, METH_VARARGS},
2807 {"is_frozen", imp_is_frozen, METH_VARARGS},
2808 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002809#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002810 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002811#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002812 {"load_package", imp_load_package, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002813#ifdef macintosh
Neal Norwitz031829d2002-03-31 14:37:44 +00002814 {"load_resource", imp_load_resource, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002815#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002816 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002817 {NULL, NULL} /* sentinel */
2818};
2819
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002820static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002821setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002822{
2823 PyObject *v;
2824 int err;
2825
2826 v = PyInt_FromLong((long)value);
2827 err = PyDict_SetItemString(d, name, v);
2828 Py_XDECREF(v);
2829 return err;
2830}
2831
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002832void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002833initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002834{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002835 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002836
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002837 m = Py_InitModule4("imp", imp_methods, doc_imp,
2838 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002839 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002840
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002841 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2842 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2843 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2844 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2845 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2846 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2847 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2848 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002849 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002850 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002851
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002852 failure:
2853 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002854}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002855
2856
Guido van Rossumb18618d2000-05-03 23:44:39 +00002857/* API for embedding applications that want to add their own entries
2858 to the table of built-in modules. This should normally be called
2859 *before* Py_Initialize(). When the table resize fails, -1 is
2860 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002861
2862 After a similar function by Just van Rossum. */
2863
2864int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002865PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002866{
2867 static struct _inittab *our_copy = NULL;
2868 struct _inittab *p;
2869 int i, n;
2870
2871 /* Count the number of entries in both tables */
2872 for (n = 0; newtab[n].name != NULL; n++)
2873 ;
2874 if (n == 0)
2875 return 0; /* Nothing to do */
2876 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2877 ;
2878
2879 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002880 p = our_copy;
2881 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002882 if (p == NULL)
2883 return -1;
2884
2885 /* Copy the tables into the new memory */
2886 if (our_copy != PyImport_Inittab)
2887 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2888 PyImport_Inittab = our_copy = p;
2889 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2890
2891 return 0;
2892}
2893
2894/* Shorthand to add a single entry given a name and a function */
2895
2896int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002897PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002898{
2899 struct _inittab newtab[2];
2900
2901 memset(newtab, '\0', sizeof newtab);
2902
2903 newtab[0].name = name;
2904 newtab[0].initfunc = initfunc;
2905
2906 return PyImport_ExtendInittab(newtab);
2907}