blob: 77a5605fe0fd8b5f4a2927d2e0d6538e1cbd0175 [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"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000014
Guido van Rossum55a83382000-09-20 20:31:38 +000015#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
18
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000019extern time_t PyOS_GetLastModificationTime(char *, FILE *);
20 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000021
Guido van Rossum6c849691994-09-26 15:47:17 +000022/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000023/* Change for each incompatible change */
24/* The value of CR and LF is incorporated so if you ever read or write
25 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000026 Apple MPW compiler swaps their values, botching string constants.
27 XXX That probably isn't important anymore.
28*/
Guido van Rossum7faeab31995-07-07 22:50:36 +000029/* XXX Perhaps the magic number should be frozen and a version field
30 added to the .pyc file header? */
Tim Peters36515e22001-11-18 04:06:29 +000031/* New way to come up with the low 16 bits of the magic number:
32 (YEAR-1995) * 10000 + MONTH * 100 + DAY
33 where MONTH and DAY are 1-based.
34 XXX Whatever the "old way" may have been isn't documented.
35 XXX This scheme breaks in 2002, as (2002-1995)*10000 = 70000 doesn't
36 fit in 16 bits.
37 XXX Later, sometimes 1 gets added to MAGIC in order to record that
38 the Unicode -U option is in use. IMO (Tim's), that's a Bad Idea
39 (quite apart from that the -U option doesn't work so isn't used
40 anyway).
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000041
42 XXX MAL, 2002-02-07: I had to modify the MAGIC due to a fix of the
43 UTF-8 encoder (it previously produced invalid UTF-8 for unpaired
44 high surrogates), so I simply bumped the month value to 20 (invalid
45 month) and set the day to 1. This should be recognizable by any
46 algorithm relying on the above scheme. Perhaps we should simply
47 start counting in increments of 10 from now on ?!
48
Michael W. Hudsondd32a912002-08-15 14:59:02 +000049 MWH, 2002-08-03: Removed SET_LINENO. Couldn't be bothered figuring
50 out the MAGIC schemes, so just incremented it by 10.
51
Guido van Rossumf6894922002-08-31 15:16:14 +000052 GvR, 2002-08-31: Because MWH changed the bytecode again, moved the
53 magic number *back* to 62011. This should get the snake-farm to
54 throw away its old .pyc files, amongst others.
55
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000056 Known values:
57 Python 1.5: 20121
58 Python 1.5.1: 20121
59 Python 1.5.2: 20121
60 Python 2.0: 50823
61 Python 2.0.1: 50823
62 Python 2.1: 60202
63 Python 2.1.1: 60202
64 Python 2.1.2: 60202
65 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000066 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000067 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000068 Python 2.3a0: 62011 (!)
Tim Peters36515e22001-11-18 04:06:29 +000069*/
Guido van Rossumf6894922002-08-31 15:16:14 +000070#define MAGIC (62011 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000071
Guido van Rossum96774c12000-05-01 20:19:08 +000072/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000073 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000074 compiler works which are enabled by command line switches. */
75static long pyc_magic = MAGIC;
76
Guido van Rossum25ce5661997-08-02 03:10:38 +000077/* See _PyImport_FixupExtension() below */
78static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000079
Guido van Rossum771c6c81997-10-31 18:37:24 +000080/* This table is defined in config.c: */
81extern struct _inittab _PyImport_Inittab[];
82
83struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000084
Guido van Rossumed1170e1999-12-20 21:23:41 +000085/* these tables define the module suffixes that Python recognizes */
86struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000087
88#ifdef RISCOS
89static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000090 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000091 {"/pyc", "rb", PY_COMPILED},
92 {0, 0}
93};
94#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000095static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000096 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000097#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000098 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000099#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000100 {".pyc", "rb", PY_COMPILED},
101 {0, 0}
102};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000103#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000104
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106
107void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000108_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000110 const struct filedescr *scan;
111 struct filedescr *filetab;
112 int countD = 0;
113 int countS = 0;
114
115 /* prepare _PyImport_Filetab: copy entries from
116 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
117 */
118 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
119 ++countD;
120 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
121 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000123 memcpy(filetab, _PyImport_DynLoadFiletab,
124 countD * sizeof(struct filedescr));
125 memcpy(filetab + countD, _PyImport_StandardFiletab,
126 countS * sizeof(struct filedescr));
127 filetab[countD + countS].suffix = NULL;
128
129 _PyImport_Filetab = filetab;
130
Guido van Rossum0824f631997-03-11 18:37:35 +0000131 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000132 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
133 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000134#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000135 if (strcmp(filetab->suffix, ".pyc") == 0)
136 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000137#else
138 if (strcmp(filetab->suffix, "/pyc") == 0)
139 filetab->suffix = "/pyo";
140#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000141 }
142 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000143
144 if (Py_UnicodeFlag) {
145 /* Fix the pyc_magic so that byte compiled code created
146 using the all-Unicode method doesn't interfere with
147 code created in normal operation mode. */
148 pyc_magic = MAGIC + 1;
149 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150}
151
Guido van Rossum25ce5661997-08-02 03:10:38 +0000152void
Just van Rossum52e14d62002-12-30 22:08:05 +0000153_PyImportHooks_Init(void)
154{
155 PyObject *v, *path_hooks = NULL, *zimpimport;
156 int err = 0;
157
158 /* adding sys.path_hooks and sys.path_importer_cache, setting up
159 zipimport */
160
161 if (Py_VerboseFlag)
162 PySys_WriteStderr("# installing zipimport hook\n");
163
164 v = PyList_New(0);
165 if (v == NULL)
166 goto error;
167 err = PySys_SetObject("meta_path", v);
168 Py_DECREF(v);
169 if (err)
170 goto error;
171 v = PyDict_New();
172 if (v == NULL)
173 goto error;
174 err = PySys_SetObject("path_importer_cache", v);
175 Py_DECREF(v);
176 if (err)
177 goto error;
178 path_hooks = PyList_New(0);
179 if (path_hooks == NULL)
180 goto error;
181 err = PySys_SetObject("path_hooks", path_hooks);
182 if (err) {
183 error:
184 PyErr_Print();
185 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
186 "path_importer_cache failed");
187 }
188 zimpimport = PyImport_ImportModule("zipimport");
189 if (zimpimport == NULL) {
190 PyErr_Clear(); /* No zip import module -- okay */
191 if (Py_VerboseFlag)
192 PySys_WriteStderr("# can't import zipimport\n");
193 }
194 else {
195 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
196 "zipimporter");
197 Py_DECREF(zimpimport);
198 if (zipimporter == NULL) {
199 PyErr_Clear(); /* No zipimporter object -- okay */
200 if (Py_VerboseFlag)
201 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000202 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000203 }
204 else {
205 /* sys.path_hooks.append(zipimporter) */
206 err = PyList_Append(path_hooks, zipimporter);
207 Py_DECREF(zipimporter);
208 if (err)
209 goto error;
210 if (Py_VerboseFlag)
211 PySys_WriteStderr(
212 "# installed zipimport hook\n");
213 }
214 }
215 Py_DECREF(path_hooks);
216}
217
218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000220{
221 Py_XDECREF(extensions);
222 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000223 PyMem_DEL(_PyImport_Filetab);
224 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000225}
226
227
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000228/* Locking primitives to prevent parallel imports of the same module
229 in different threads to return with a partially loaded module.
230 These calls are serialized by the global interpreter lock. */
231
232#ifdef WITH_THREAD
233
Guido van Rossum49b56061998-10-01 20:42:43 +0000234#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000235
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000237static long import_lock_thread = -1;
238static int import_lock_level = 0;
239
240static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000242{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000243 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000244 if (me == -1)
245 return; /* Too bad */
246 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000248 if (import_lock_thread == me) {
249 import_lock_level++;
250 return;
251 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000252 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
253 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256 PyEval_RestoreThread(tstate);
257 }
258 import_lock_thread = me;
259 import_lock_level = 1;
260}
261
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000262static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000264{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000265 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000267 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000268 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000269 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000270 import_lock_level--;
271 if (import_lock_level == 0) {
272 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000273 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000274 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000275 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276}
277
278#else
279
280#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000281#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000282
283#endif
284
Tim Peters69232342001-08-30 05:16:13 +0000285static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000286imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000287{
Tim Peters69232342001-08-30 05:16:13 +0000288#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000289 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000290#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000291 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000292#endif
293}
294
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000296imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000297{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000298#ifdef WITH_THREAD
299 lock_import();
300#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000301 Py_INCREF(Py_None);
302 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000303}
304
305static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000306imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000307{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000308#ifdef WITH_THREAD
309 if (unlock_import() < 0) {
310 PyErr_SetString(PyExc_RuntimeError,
311 "not holding the import lock");
312 return NULL;
313 }
314#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000315 Py_INCREF(Py_None);
316 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000317}
318
Guido van Rossum25ce5661997-08-02 03:10:38 +0000319/* Helper for sys */
320
321PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000323{
324 PyInterpreterState *interp = PyThreadState_Get()->interp;
325 if (interp->modules == NULL)
326 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
327 return interp->modules;
328}
329
Guido van Rossum3f5da241990-12-20 15:06:42 +0000330
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331/* List of names to clear in sys */
332static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000333 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000334 "exc_type", "exc_value", "exc_traceback",
335 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000336 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000337 NULL
338};
339
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000340static char* sys_files[] = {
341 "stdin", "__stdin__",
342 "stdout", "__stdout__",
343 "stderr", "__stderr__",
344 NULL
345};
346
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000347
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000348/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000349
Guido van Rossum3f5da241990-12-20 15:06:42 +0000350void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000352{
Guido van Rossum758eec01998-01-19 21:58:26 +0000353 int pos, ndone;
354 char *name;
355 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000357 PyObject *modules = interp->modules;
358
359 if (modules == NULL)
360 return; /* Already done */
361
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000362 /* Delete some special variables first. These are common
363 places where user values hide and people complain when their
364 destructors fail. Since the modules containing them are
365 deleted *last* of all, they would come too late in the normal
366 destruction order. Sigh. */
367
368 value = PyDict_GetItemString(modules, "__builtin__");
369 if (value != NULL && PyModule_Check(value)) {
370 dict = PyModule_GetDict(value);
371 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000372 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000373 PyDict_SetItemString(dict, "_", Py_None);
374 }
375 value = PyDict_GetItemString(modules, "sys");
376 if (value != NULL && PyModule_Check(value)) {
377 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000378 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000379 dict = PyModule_GetDict(value);
380 for (p = sys_deletes; *p != NULL; p++) {
381 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000382 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000383 PyDict_SetItemString(dict, *p, Py_None);
384 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000385 for (p = sys_files; *p != NULL; p+=2) {
386 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000387 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000388 v = PyDict_GetItemString(dict, *(p+1));
389 if (v == NULL)
390 v = Py_None;
391 PyDict_SetItemString(dict, *p, v);
392 }
393 }
394
395 /* First, delete __main__ */
396 value = PyDict_GetItemString(modules, "__main__");
397 if (value != NULL && PyModule_Check(value)) {
398 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000399 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000400 _PyModule_Clear(value);
401 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000402 }
403
Guido van Rossum758eec01998-01-19 21:58:26 +0000404 /* The special treatment of __builtin__ here is because even
405 when it's not referenced as a module, its dictionary is
406 referenced by almost every module's __builtins__. Since
407 deleting a module clears its dictionary (even if there are
408 references left to it), we need to delete the __builtin__
409 module last. Likewise, we don't delete sys until the very
410 end because it is implicitly referenced (e.g. by print).
411
412 Also note that we 'delete' modules by replacing their entry
413 in the modules dict with None, rather than really deleting
414 them; this avoids a rehash of the modules dictionary and
415 also marks them as "non existent" so they won't be
416 re-imported. */
417
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000418 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000419 one (skipping __builtin__ and sys) and delete them */
420 do {
421 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000423 while (PyDict_Next(modules, &pos, &key, &value)) {
424 if (value->ob_refcnt != 1)
425 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000426 if (PyString_Check(key) && PyModule_Check(value)) {
427 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000428 if (strcmp(name, "__builtin__") == 0)
429 continue;
430 if (strcmp(name, "sys") == 0)
431 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000432 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000433 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000434 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000435 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000436 PyDict_SetItem(modules, key, Py_None);
437 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000438 }
439 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000440 } while (ndone > 0);
441
Guido van Rossum758eec01998-01-19 21:58:26 +0000442 /* Next, delete all modules (still skipping __builtin__ and sys) */
443 pos = 0;
444 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000445 if (PyString_Check(key) && PyModule_Check(value)) {
446 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000447 if (strcmp(name, "__builtin__") == 0)
448 continue;
449 if (strcmp(name, "sys") == 0)
450 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000451 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000452 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000453 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000454 PyDict_SetItem(modules, key, Py_None);
455 }
456 }
457
458 /* Next, delete sys and __builtin__ (in that order) */
459 value = PyDict_GetItemString(modules, "sys");
460 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000461 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000462 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000463 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000464 PyDict_SetItemString(modules, "sys", Py_None);
465 }
466 value = PyDict_GetItemString(modules, "__builtin__");
467 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000468 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000469 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000470 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000471 PyDict_SetItemString(modules, "__builtin__", Py_None);
472 }
473
474 /* Finally, clear and delete the modules directory */
475 PyDict_Clear(modules);
476 interp->modules = NULL;
477 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000478}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000479
480
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481/* Helper for pythonrun.c -- return magic number */
482
483long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000484PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000485{
Guido van Rossum96774c12000-05-01 20:19:08 +0000486 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000487}
488
489
Guido van Rossum25ce5661997-08-02 03:10:38 +0000490/* Magic for extension modules (built-in as well as dynamically
491 loaded). To prevent initializing an extension module more than
492 once, we keep a static dictionary 'extensions' keyed by module name
493 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000494 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000495 dictionary is stored by calling _PyImport_FixupExtension()
496 immediately after the module initialization function succeeds. A
497 copy can be retrieved from there by calling
498 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499
Guido van Rossum79f25d91997-04-29 20:08:16 +0000500PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000502{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000503 PyObject *modules, *mod, *dict, *copy;
504 if (extensions == NULL) {
505 extensions = PyDict_New();
506 if (extensions == NULL)
507 return NULL;
508 }
509 modules = PyImport_GetModuleDict();
510 mod = PyDict_GetItemString(modules, name);
511 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000512 PyErr_Format(PyExc_SystemError,
513 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000514 return NULL;
515 }
516 dict = PyModule_GetDict(mod);
517 if (dict == NULL)
518 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000519 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000520 if (copy == NULL)
521 return NULL;
522 PyDict_SetItemString(extensions, filename, copy);
523 Py_DECREF(copy);
524 return copy;
525}
526
527PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000528_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000530 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000531 if (extensions == NULL)
532 return NULL;
533 dict = PyDict_GetItemString(extensions, filename);
534 if (dict == NULL)
535 return NULL;
536 mod = PyImport_AddModule(name);
537 if (mod == NULL)
538 return NULL;
539 mdict = PyModule_GetDict(mod);
540 if (mdict == NULL)
541 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000542 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000545 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546 name, filename);
547 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548}
549
550
551/* Get the module object corresponding to a module name.
552 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000553 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000554 Because the former action is most common, THIS DOES NOT RETURN A
555 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000558PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 if (m == NULL)
568 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000569 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 return NULL;
572 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574
575 return m;
576}
577
578
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000579/* Execute a code object in a module and return the module object
580 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000583PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000585 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
586}
587
588PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000589PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000590{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 if (m == NULL)
596 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 d = PyModule_GetDict(m);
598 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
599 if (PyDict_SetItemString(d, "__builtins__",
600 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000601 return NULL;
602 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000603 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000604 v = NULL;
605 if (pathname != NULL) {
606 v = PyString_FromString(pathname);
607 if (v == NULL)
608 PyErr_Clear();
609 }
610 if (v == NULL) {
611 v = ((PyCodeObject *)co)->co_filename;
612 Py_INCREF(v);
613 }
614 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000616 Py_DECREF(v);
617
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000618 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619 if (v == NULL)
620 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000622
Guido van Rossum25ce5661997-08-02 03:10:38 +0000623 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000624 PyErr_Format(PyExc_ImportError,
625 "Loaded module %.200s not found in sys.modules",
626 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000627 return NULL;
628 }
629
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631
632 return m;
633}
634
635
636/* Given a pathname for a Python source file, fill a buffer with the
637 pathname for the corresponding compiled file. Return the pathname
638 for the compiled file, or NULL if there's no space in the buffer.
639 Doesn't set an exception. */
640
641static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000642make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643{
Tim Petersc1731372001-08-04 08:12:36 +0000644 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645 if (len+2 > buflen)
646 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000647
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000648#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000649 /* Treat .pyw as if it were .py. The case of ".pyw" must match
650 that used in _PyImport_StandardFiletab. */
651 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
652 --len; /* pretend 'w' isn't there */
653#endif
654 memcpy(buf, pathname, len);
655 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
656 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657
658 return buf;
659}
660
661
662/* Given a pathname for a Python source file, its time of last
663 modification, and a pathname for a compiled file, check whether the
664 compiled file represents the same version of the source. If so,
665 return a FILE pointer for the compiled file, positioned just after
666 the header; if not, return NULL.
667 Doesn't set an exception. */
668
669static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000670check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000671{
672 FILE *fp;
673 long magic;
674 long pyc_mtime;
675
676 fp = fopen(cpathname, "rb");
677 if (fp == NULL)
678 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000680 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000682 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683 fclose(fp);
684 return NULL;
685 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000688 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000689 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000690 fclose(fp);
691 return NULL;
692 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000694 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 return fp;
696}
697
698
699/* Read a code object from a file and check it for validity */
700
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000702read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705
Tim Petersd9b9ac82001-01-28 00:27:39 +0000706 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 if (co == NULL || !PyCode_Check(co)) {
709 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000710 PyErr_Format(PyExc_ImportError,
711 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713 return NULL;
714 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716}
717
718
719/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000720 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000723load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000724{
725 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 PyCodeObject *co;
727 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000730 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000731 PyErr_Format(PyExc_ImportError,
732 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000733 return NULL;
734 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000736 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737 if (co == NULL)
738 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000740 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000742 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744
745 return m;
746}
747
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748/* Parse a source file and return the corresponding code object */
749
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000751parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000752{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754 node *n;
755
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000756 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757 if (n == NULL)
758 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 co = PyNode_Compile(n, pathname);
760 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761
762 return co;
763}
764
765
Guido van Rossum55a83382000-09-20 20:31:38 +0000766/* Helper to open a bytecode file for writing in exclusive mode */
767
768static FILE *
769open_exclusive(char *filename)
770{
771#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
772 /* Use O_EXCL to avoid a race condition when another process tries to
773 write the same file. When that happens, our open() call fails,
774 which is just fine (since it's only a cache).
775 XXX If the file exists and is writable but the directory is not
776 writable, the file will never be written. Oh well.
777 */
778 int fd;
779 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000780 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
781#ifdef O_BINARY
782 |O_BINARY /* necessary for Windows */
783#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000784#ifdef __VMS
785 , 0666, "ctxt=bin", "shr=nil");
786#else
787 , 0666);
788#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000789 if (fd < 0)
790 return NULL;
791 return fdopen(fd, "wb");
792#else
793 /* Best we can do -- on Windows this can't happen anyway */
794 return fopen(filename, "wb");
795#endif
796}
797
798
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000799/* Write a compiled module to a file, placing the time of last
800 modification of its source into the header.
801 Errors are ignored, if a write error occurs an attempt is made to
802 remove the file. */
803
804static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000805write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806{
807 FILE *fp;
808
Guido van Rossum55a83382000-09-20 20:31:38 +0000809 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000812 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000813 "# can't create %s\n", cpathname);
814 return;
815 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000816 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 PyMarshal_WriteLongToFile(0L, fp);
819 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000822 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000823 /* Don't keep partial file */
824 fclose(fp);
825 (void) unlink(cpathname);
826 return;
827 }
828 /* Now write the true mtime */
829 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 fflush(fp);
832 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000834 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835}
836
837
838/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000839 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
840 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000841
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000843load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844{
Fred Drake4c82b232000-06-30 16:18:57 +0000845 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846 FILE *fpc;
847 char buf[MAXPATHLEN+1];
848 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000849 PyCodeObject *co;
850 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000852 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000853 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000854 return NULL;
855#if SIZEOF_TIME_T > 4
856 /* Python's .pyc timestamp handling presumes that the timestamp fits
857 in 4 bytes. This will be fine until sometime in the year 2038,
858 when a 4-byte signed time_t will overflow.
859 */
860 if (mtime >> 32) {
861 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000862 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000863 return NULL;
864 }
865#endif
Tim Peters36515e22001-11-18 04:06:29 +0000866 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000867 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868 if (cpathname != NULL &&
869 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000870 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871 fclose(fpc);
872 if (co == NULL)
873 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000875 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000877 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000878 }
879 else {
880 co = parse_source_module(pathname, fp);
881 if (co == NULL)
882 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000883 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000884 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000885 name, pathname);
886 write_compiled_module(co, cpathname, mtime);
887 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000888 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890
891 return m;
892}
893
894
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000895/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000896static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
897static struct filedescr *find_module(char *, char *, PyObject *,
898 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000899static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000900
901/* Load a package and return its module object WITH INCREMENTED
902 REFERENCE COUNT */
903
904static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000906{
907 PyObject *m, *d, *file, *path;
908 int err;
909 char buf[MAXPATHLEN+1];
910 FILE *fp = NULL;
911 struct filedescr *fdp;
912
913 m = PyImport_AddModule(name);
914 if (m == NULL)
915 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000916 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000917 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000918 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000919 d = PyModule_GetDict(m);
920 file = PyString_FromString(pathname);
921 if (file == NULL)
922 return NULL;
923 path = Py_BuildValue("[O]", file);
924 if (path == NULL) {
925 Py_DECREF(file);
926 return NULL;
927 }
928 err = PyDict_SetItemString(d, "__file__", file);
929 if (err == 0)
930 err = PyDict_SetItemString(d, "__path__", path);
931 if (err != 0) {
932 m = NULL;
933 goto cleanup;
934 }
935 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000936 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000937 if (fdp == NULL) {
938 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
939 PyErr_Clear();
940 }
941 else
942 m = NULL;
943 goto cleanup;
944 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000945 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000946 if (fp != NULL)
947 fclose(fp);
948 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949 Py_XDECREF(path);
950 Py_XDECREF(file);
951 return m;
952}
953
954
955/* Helper to test for built-in module */
956
957static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000958is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000959{
960 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000961 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
962 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
963 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000964 return -1;
965 else
966 return 1;
967 }
968 }
969 return 0;
970}
971
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000972
Just van Rossum52e14d62002-12-30 22:08:05 +0000973/* Return an importer object for a sys.path/pkg.__path__ item 'p',
974 possibly by fetching it from the path_importer_cache dict. If it
975 wasn't yet cached, traverse path_hooks until it a hook is found
976 that can handle the path item. Return None if no hook could;
977 this tells our caller it should fall back to the builtin
978 import mechanism. Cache the result in path_importer_cache.
979 Returns a borrowed reference. */
980
981static PyObject *
982get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
983 PyObject *p)
984{
985 PyObject *importer;
986 int j, nhooks;
987
988 /* These conditions are the caller's responsibility: */
989 assert(PyList_Check(path_hooks));
990 assert(PyDict_Check(path_importer_cache));
991
992 nhooks = PyList_Size(path_hooks);
993 if (nhooks < 0)
994 return NULL; /* Shouldn't happen */
995
996 importer = PyDict_GetItem(path_importer_cache, p);
997 if (importer != NULL)
998 return importer;
999
1000 /* set path_importer_cache[p] to None to avoid recursion */
1001 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1002 return NULL;
1003
1004 for (j = 0; j < nhooks; j++) {
1005 PyObject *hook = PyList_GetItem(path_hooks, j);
1006 if (hook == NULL)
1007 return NULL;
1008 importer = PyObject_CallFunction(hook, "O", p);
1009 if (importer != NULL)
1010 break;
1011
1012 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1013 return NULL;
1014 }
1015 PyErr_Clear();
1016 }
1017 if (importer == NULL)
1018 importer = Py_None;
1019 else if (importer != Py_None) {
1020 int err = PyDict_SetItem(path_importer_cache, p, importer);
1021 Py_DECREF(importer);
1022 if (err != 0)
1023 return NULL;
1024 }
1025 return importer;
1026}
1027
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001028/* Search the path (default sys.path) for a module. Return the
1029 corresponding filedescr struct, and (via return arguments) the
1030 pathname and an open file. Return NULL if the module is not found. */
1031
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001032#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001033extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1034 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001035#endif
1036
Tim Peters50d8d372001-02-28 05:34:27 +00001037static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001038static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001039static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001040
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001041static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001042find_module(char *fullname, char *subname, PyObject *path, char *buf,
1043 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001044{
Fred Drake4c82b232000-06-30 16:18:57 +00001045 int i, npath;
1046 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001047 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001048 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001049 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001050 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001051#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001052 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001053#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001054 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1055 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1056 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001057 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001058#if defined(PYOS_OS2)
1059 size_t saved_len;
1060 size_t saved_namelen;
1061 char *saved_buf = NULL;
1062#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001063 if (p_loader != NULL)
1064 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001065
Just van Rossum52e14d62002-12-30 22:08:05 +00001066 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001067 PyErr_SetString(PyExc_OverflowError,
1068 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001069 return NULL;
1070 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001071 strcpy(name, subname);
1072
1073 /* sys.meta_path import hook */
1074 if (p_loader != NULL) {
1075 PyObject *meta_path;
1076
1077 meta_path = PySys_GetObject("meta_path");
1078 if (meta_path == NULL || !PyList_Check(meta_path)) {
1079 PyErr_SetString(PyExc_ImportError,
1080 "sys.meta_path must be a list of "
1081 "import hooks");
1082 return NULL;
1083 }
1084 Py_INCREF(meta_path); /* zap guard */
1085 npath = PyList_Size(meta_path);
1086 for (i = 0; i < npath; i++) {
1087 PyObject *loader;
1088 PyObject *hook = PyList_GetItem(meta_path, i);
1089 loader = PyObject_CallMethod(hook, "find_module",
1090 "sO", fullname,
1091 path != NULL ?
1092 path : Py_None);
1093 if (loader == NULL) {
1094 Py_DECREF(meta_path);
1095 return NULL; /* true error */
1096 }
1097 if (loader != Py_None) {
1098 /* a loader was found */
1099 *p_loader = loader;
1100 Py_DECREF(meta_path);
1101 return &importhookdescr;
1102 }
1103 Py_DECREF(loader);
1104 }
1105 Py_DECREF(meta_path);
1106 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001107
1108 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001109 /* The only type of submodule allowed inside a "frozen"
1110 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001111 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1112 PyErr_SetString(PyExc_ImportError,
1113 "full frozen module name too long");
1114 return NULL;
1115 }
1116 strcpy(buf, PyString_AsString(path));
1117 strcat(buf, ".");
1118 strcat(buf, name);
1119 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001120 if (find_frozen(name) != NULL) {
1121 strcpy(buf, name);
1122 return &fd_frozen;
1123 }
1124 PyErr_Format(PyExc_ImportError,
1125 "No frozen submodule named %.200s", name);
1126 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001127 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001128 if (path == NULL) {
1129 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001130 strcpy(buf, name);
1131 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001132 }
Greg Ward201baee2001-10-04 14:52:06 +00001133 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001134 strcpy(buf, name);
1135 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001136 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001137
Guido van Rossumac279101996-08-22 23:10:58 +00001138#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001139 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1140 if (fp != NULL) {
1141 *p_fp = fp;
1142 return fdp;
1143 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001144#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001145 path = PySys_GetObject("path");
1146 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001147 if (path == NULL || !PyList_Check(path)) {
1148 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001149 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001150 return NULL;
1151 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001152
1153 path_hooks = PySys_GetObject("path_hooks");
1154 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1155 PyErr_SetString(PyExc_ImportError,
1156 "sys.path_hooks must be a list of "
1157 "import hooks");
1158 return NULL;
1159 }
1160 path_importer_cache = PySys_GetObject("path_importer_cache");
1161 if (path_importer_cache == NULL ||
1162 !PyDict_Check(path_importer_cache)) {
1163 PyErr_SetString(PyExc_ImportError,
1164 "sys.path_importer_cache must be a dict");
1165 return NULL;
1166 }
1167
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001169 namelen = strlen(name);
1170 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001171 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001173#ifdef Py_USING_UNICODE
1174 if (PyUnicode_Check(v)) {
1175 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1176 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1177 if (copy == NULL)
1178 return NULL;
1179 v = copy;
1180 }
1181 else
1182#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001184 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001186 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1187 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001188 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001189 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001191 if (strlen(buf) != len) {
1192 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001193 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001194 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001195
1196 /* sys.path_hooks import hook */
1197 if (p_loader != NULL) {
1198 PyObject *importer;
1199
1200 importer = get_path_importer(path_importer_cache,
1201 path_hooks, v);
1202 if (importer == NULL)
1203 return NULL;
1204 /* Note: importer is a borrowed reference */
1205 if (importer != Py_None) {
1206 PyObject *loader;
1207 loader = PyObject_CallMethod(importer,
1208 "find_module",
1209 "s", fullname);
1210 if (loader == NULL)
1211 return NULL; /* error */
1212 if (loader != Py_None) {
1213 /* a loader was found */
1214 *p_loader = loader;
1215 return &importhookdescr;
1216 }
1217 Py_DECREF(loader);
1218 }
1219 /* no hook was successful, use builtin import */
1220 }
1221
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001222 if (len > 0 && buf[len-1] != SEP
1223#ifdef ALTSEP
1224 && buf[len-1] != ALTSEP
1225#endif
1226 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001227 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001228 strcpy(buf+len, name);
1229 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001230
1231 /* Check for package import (buf holds a directory name,
1232 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001233#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001234 if (stat(buf, &statbuf) == 0 && /* it exists */
1235 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1236 find_init_module(buf) && /* it has __init__.py */
1237 case_ok(buf, len, namelen, name)) { /* and case matches */
1238 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001239 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001240 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001241#else
1242 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001243#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001244 if (isdir(buf) &&
1245 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001246 case_ok(buf, len, namelen, name)) {
1247 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001248 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001249 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001250#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001251#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001252#if defined(PYOS_OS2)
1253 /* take a snapshot of the module spec for restoration
1254 * after the 8 character DLL hackery
1255 */
1256 saved_buf = strdup(buf);
1257 saved_len = len;
1258 saved_namelen = namelen;
1259#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001260 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001261#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001262 /* OS/2 limits DLLs to 8 character names (w/o
1263 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001264 * so if the name is longer than that and its a
1265 * dynamically loaded module we're going to try,
1266 * truncate the name before trying
1267 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001268 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001269 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001270 const struct filedescr *scan;
1271 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001272 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001273 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001274 break;
1275 else
1276 scan++;
1277 }
1278 if (scan->suffix != NULL) {
1279 /* yes, so truncate the name */
1280 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001281 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001282 buf[len] = '\0';
1283 }
1284 }
1285#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001286 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001287 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001288 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001289 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001290 if (filemode[0] == 'U')
1291 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001292 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001293 if (fp != NULL) {
1294 if (case_ok(buf, len, namelen, name))
1295 break;
1296 else { /* continue search */
1297 fclose(fp);
1298 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001299 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001300 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001301#if defined(PYOS_OS2)
1302 /* restore the saved snapshot */
1303 strcpy(buf, saved_buf);
1304 len = saved_len;
1305 namelen = saved_namelen;
1306#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001308#if defined(PYOS_OS2)
1309 /* don't need/want the module name snapshot anymore */
1310 if (saved_buf)
1311 {
1312 free(saved_buf);
1313 saved_buf = NULL;
1314 }
1315#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001316 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001317 if (fp != NULL)
1318 break;
1319 }
1320 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001321 PyErr_Format(PyExc_ImportError,
1322 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001323 return NULL;
1324 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325 *p_fp = fp;
1326 return fdp;
1327}
1328
Tim Petersd1e87a82001-03-01 18:12:00 +00001329/* case_ok(char* buf, int len, int namelen, char* name)
1330 * The arguments here are tricky, best shown by example:
1331 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1332 * ^ ^ ^ ^
1333 * |--------------------- buf ---------------------|
1334 * |------------------- len ------------------|
1335 * |------ name -------|
1336 * |----- namelen -----|
1337 * buf is the full path, but len only counts up to (& exclusive of) the
1338 * extension. name is the module name, also exclusive of extension.
1339 *
1340 * We've already done a successful stat() or fopen() on buf, so know that
1341 * there's some match, possibly case-insensitive.
1342 *
Tim Peters50d8d372001-02-28 05:34:27 +00001343 * case_ok() is to return 1 if there's a case-sensitive match for
1344 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1345 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001346 *
Tim Peters50d8d372001-02-28 05:34:27 +00001347 * case_ok() is used to implement case-sensitive import semantics even
1348 * on platforms with case-insensitive filesystems. It's trivial to implement
1349 * for case-sensitive filesystems. It's pretty much a cross-platform
1350 * nightmare for systems with case-insensitive filesystems.
1351 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001352
Tim Peters50d8d372001-02-28 05:34:27 +00001353/* First we may need a pile of platform-specific header files; the sequence
1354 * of #if's here should match the sequence in the body of case_ok().
1355 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001356#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001357#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001358#ifdef __CYGWIN__
1359#include <sys/cygwin.h>
1360#endif
1361
Tim Peters50d8d372001-02-28 05:34:27 +00001362#elif defined(DJGPP)
1363#include <dir.h>
1364
Tim Petersd1e87a82001-03-01 18:12:00 +00001365#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001366#include <sys/types.h>
1367#include <dirent.h>
1368
Andrew MacIntyred9400542002-02-26 11:41:34 +00001369#elif defined(PYOS_OS2)
1370#define INCL_DOS
1371#define INCL_DOSERRORS
1372#define INCL_NOPMAPI
1373#include <os2.h>
1374
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001375#elif defined(RISCOS)
1376#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001377#endif
1378
Guido van Rossum0980bd91998-02-13 17:18:36 +00001379static int
Tim Peters50d8d372001-02-28 05:34:27 +00001380case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001381{
Tim Peters50d8d372001-02-28 05:34:27 +00001382/* Pick a platform-specific implementation; the sequence of #if's here should
1383 * match the sequence just above.
1384 */
1385
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001386/* MS_WINDOWS || __CYGWIN__ */
1387#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001388 WIN32_FIND_DATA data;
1389 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001390#ifdef __CYGWIN__
1391 char tempbuf[MAX_PATH];
1392#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001393
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001394 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001395 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001396
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001397#ifdef __CYGWIN__
1398 cygwin32_conv_to_win32_path(buf, tempbuf);
1399 h = FindFirstFile(tempbuf, &data);
1400#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001401 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001402#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001403 if (h == INVALID_HANDLE_VALUE) {
1404 PyErr_Format(PyExc_NameError,
1405 "Can't find file for module %.100s\n(filename %.300s)",
1406 name, buf);
1407 return 0;
1408 }
1409 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001410 return strncmp(data.cFileName, name, namelen) == 0;
1411
1412/* DJGPP */
1413#elif defined(DJGPP)
1414 struct ffblk ffblk;
1415 int done;
1416
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001417 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001418 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001419
1420 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1421 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001422 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001423 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001424 name, buf);
1425 return 0;
1426 }
Tim Peters50d8d372001-02-28 05:34:27 +00001427 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001428
Tim Peters677898a2001-03-02 03:28:03 +00001429/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001430#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001431 DIR *dirp;
1432 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001433 char dirname[MAXPATHLEN + 1];
1434 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001435
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001436 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001437 return 1;
1438
Tim Petersd1e87a82001-03-01 18:12:00 +00001439 /* Copy the dir component into dirname; substitute "." if empty */
1440 if (dirlen <= 0) {
1441 dirname[0] = '.';
1442 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001443 }
1444 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001445 assert(dirlen <= MAXPATHLEN);
1446 memcpy(dirname, buf, dirlen);
1447 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001448 }
1449 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001450 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001451 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001452 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001453 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001454 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001455#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001456 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001457#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001458 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001459#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001460 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001461 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001462 (void)closedir(dirp);
1463 return 1; /* Found */
1464 }
1465 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001466 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001467 }
Tim Peters430f5d42001-03-01 01:30:56 +00001468 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001469
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001470/* RISC OS */
1471#elif defined(RISCOS)
1472 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1473 char buf2[MAXPATHLEN+2];
1474 char *nameWithExt = buf+len-namelen;
1475 int canonlen;
1476 os_error *e;
1477
1478 if (Py_GETENV("PYTHONCASEOK") != NULL)
1479 return 1;
1480
1481 /* workaround:
1482 append wildcard, otherwise case of filename wouldn't be touched */
1483 strcpy(buf2, buf);
1484 strcat(buf2, "*");
1485
1486 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1487 canonlen = MAXPATHLEN+1-canonlen;
1488 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1489 return 0;
1490 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1491 return 1; /* match */
1492
1493 return 0;
1494
Andrew MacIntyred9400542002-02-26 11:41:34 +00001495/* OS/2 */
1496#elif defined(PYOS_OS2)
1497 HDIR hdir = 1;
1498 ULONG srchcnt = 1;
1499 FILEFINDBUF3 ffbuf;
1500 APIRET rc;
1501
1502 if (getenv("PYTHONCASEOK") != NULL)
1503 return 1;
1504
1505 rc = DosFindFirst(buf,
1506 &hdir,
1507 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1508 &ffbuf, sizeof(ffbuf),
1509 &srchcnt,
1510 FIL_STANDARD);
1511 if (rc != NO_ERROR)
1512 return 0;
1513 return strncmp(ffbuf.achName, name, namelen) == 0;
1514
Tim Peters50d8d372001-02-28 05:34:27 +00001515/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1516#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001517 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001518
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001519#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001520}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001521
Guido van Rossum0980bd91998-02-13 17:18:36 +00001522
Guido van Rossum197346f1997-10-31 18:38:52 +00001523#ifdef HAVE_STAT
1524/* Helper to look for __init__.py or __init__.py[co] in potential package */
1525static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001526find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001527{
Tim Peters0f9431f2001-07-05 03:47:53 +00001528 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001529 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001530 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001531 struct stat statbuf;
1532
Tim Peters0f9431f2001-07-05 03:47:53 +00001533/* For calling case_ok(buf, len, namelen, name):
1534 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1535 * ^ ^ ^ ^
1536 * |--------------------- buf ---------------------|
1537 * |------------------- len ------------------|
1538 * |------ name -------|
1539 * |----- namelen -----|
1540 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001541 if (save_len + 13 >= MAXPATHLEN)
1542 return 0;
1543 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001544 pname = buf + i;
1545 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001546 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001547 if (case_ok(buf,
1548 save_len + 9, /* len("/__init__") */
1549 8, /* len("__init__") */
1550 pname)) {
1551 buf[save_len] = '\0';
1552 return 1;
1553 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001554 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001555 i += strlen(pname);
1556 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001557 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001558 if (case_ok(buf,
1559 save_len + 9, /* len("/__init__") */
1560 8, /* len("__init__") */
1561 pname)) {
1562 buf[save_len] = '\0';
1563 return 1;
1564 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001565 }
1566 buf[save_len] = '\0';
1567 return 0;
1568}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001569
1570#else
1571
1572#ifdef RISCOS
1573static int
1574find_init_module(buf)
1575 char *buf;
1576{
1577 int save_len = strlen(buf);
1578 int i = save_len;
1579
1580 if (save_len + 13 >= MAXPATHLEN)
1581 return 0;
1582 buf[i++] = SEP;
1583 strcpy(buf+i, "__init__/py");
1584 if (isfile(buf)) {
1585 buf[save_len] = '\0';
1586 return 1;
1587 }
1588
1589 if (Py_OptimizeFlag)
1590 strcpy(buf+i, "o");
1591 else
1592 strcpy(buf+i, "c");
1593 if (isfile(buf)) {
1594 buf[save_len] = '\0';
1595 return 1;
1596 }
1597 buf[save_len] = '\0';
1598 return 0;
1599}
1600#endif /*RISCOS*/
1601
Guido van Rossum197346f1997-10-31 18:38:52 +00001602#endif /* HAVE_STAT */
1603
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001604
Tim Petersdbd9ba62000-07-09 03:09:57 +00001605static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001606
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001607/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001608 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001609
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001611load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001612{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001613 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001614 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001615 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001616
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001617 /* First check that there's an open file (if we need one) */
1618 switch (type) {
1619 case PY_SOURCE:
1620 case PY_COMPILED:
1621 if (fp == NULL) {
1622 PyErr_Format(PyExc_ValueError,
1623 "file object required for import (type code %d)",
1624 type);
1625 return NULL;
1626 }
1627 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001628
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001629 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001630
1631 case PY_SOURCE:
1632 m = load_source_module(name, buf, fp);
1633 break;
1634
1635 case PY_COMPILED:
1636 m = load_compiled_module(name, buf, fp);
1637 break;
1638
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001639#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001640 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001642 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001643#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001644
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001645 case PKG_DIRECTORY:
1646 m = load_package(name, buf);
1647 break;
1648
1649 case C_BUILTIN:
1650 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001651 if (buf != NULL && buf[0] != '\0')
1652 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001653 if (type == C_BUILTIN)
1654 err = init_builtin(name);
1655 else
1656 err = PyImport_ImportFrozenModule(name);
1657 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001658 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001659 if (err == 0) {
1660 PyErr_Format(PyExc_ImportError,
1661 "Purported %s module %.200s not found",
1662 type == C_BUILTIN ?
1663 "builtin" : "frozen",
1664 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001665 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001666 }
1667 modules = PyImport_GetModuleDict();
1668 m = PyDict_GetItemString(modules, name);
1669 if (m == NULL) {
1670 PyErr_Format(
1671 PyExc_ImportError,
1672 "%s module %.200s not properly initialized",
1673 type == C_BUILTIN ?
1674 "builtin" : "frozen",
1675 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001676 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001677 }
1678 Py_INCREF(m);
1679 break;
1680
Just van Rossum52e14d62002-12-30 22:08:05 +00001681 case IMP_HOOK: {
1682 if (loader == NULL) {
1683 PyErr_SetString(PyExc_ImportError,
1684 "import hook without loader");
1685 return NULL;
1686 }
1687 m = PyObject_CallMethod(loader, "load_module", "s", name);
1688 break;
1689 }
1690
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001691 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001692 PyErr_Format(PyExc_ImportError,
1693 "Don't know how to import %.200s (type code %d)",
1694 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001695 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001696
1697 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001698
1699 return m;
1700}
1701
1702
1703/* Initialize a built-in module.
1704 Return 1 for succes, 0 if the module is not found, and -1 with
1705 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001706
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001707static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001708init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001709{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001710 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001711
Greg Ward201baee2001-10-04 14:52:06 +00001712 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001713 return 1;
1714
Guido van Rossum771c6c81997-10-31 18:37:24 +00001715 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001716 if (strcmp(name, p->name) == 0) {
1717 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001718 PyErr_Format(PyExc_ImportError,
1719 "Cannot re-init internal module %.200s",
1720 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001721 return -1;
1722 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001724 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001725 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001727 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001728 if (_PyImport_FixupExtension(name, name) == NULL)
1729 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001730 return 1;
1731 }
1732 }
1733 return 0;
1734}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001735
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001736
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001737/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001738
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001739static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001740find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001741{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001742 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001743
Guido van Rossum79f25d91997-04-29 20:08:16 +00001744 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001745 if (p->name == NULL)
1746 return NULL;
1747 if (strcmp(p->name, name) == 0)
1748 break;
1749 }
1750 return p;
1751}
1752
Guido van Rossum79f25d91997-04-29 20:08:16 +00001753static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001754get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001755{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001756 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001757 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001758
1759 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001760 PyErr_Format(PyExc_ImportError,
1761 "No such frozen object named %.200s",
1762 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001763 return NULL;
1764 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001765 if (p->code == NULL) {
1766 PyErr_Format(PyExc_ImportError,
1767 "Excluded frozen object named %.200s",
1768 name);
1769 return NULL;
1770 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001771 size = p->size;
1772 if (size < 0)
1773 size = -size;
1774 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001775}
1776
1777/* Initialize a frozen module.
1778 Return 1 for succes, 0 if the module is not found, and -1 with
1779 an exception set if the initialization failed.
1780 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001781
1782int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001783PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001784{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001785 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786 PyObject *co;
1787 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001788 int ispackage;
1789 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001790
1791 if (p == NULL)
1792 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001793 if (p->code == NULL) {
1794 PyErr_Format(PyExc_ImportError,
1795 "Excluded frozen object named %.200s",
1796 name);
1797 return -1;
1798 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001799 size = p->size;
1800 ispackage = (size < 0);
1801 if (ispackage)
1802 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001803 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001804 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001805 name, ispackage ? " package" : "");
1806 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001807 if (co == NULL)
1808 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 if (!PyCode_Check(co)) {
1810 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001811 PyErr_Format(PyExc_TypeError,
1812 "frozen object %.200s is not a code object",
1813 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001814 return -1;
1815 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001816 if (ispackage) {
1817 /* Set __path__ to the package name */
1818 PyObject *d, *s;
1819 int err;
1820 m = PyImport_AddModule(name);
1821 if (m == NULL)
1822 return -1;
1823 d = PyModule_GetDict(m);
1824 s = PyString_InternFromString(name);
1825 if (s == NULL)
1826 return -1;
1827 err = PyDict_SetItemString(d, "__path__", s);
1828 Py_DECREF(s);
1829 if (err != 0)
1830 return err;
1831 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001832 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001834 if (m == NULL)
1835 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001837 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001838}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001839
1840
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001841/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001842 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001843
Guido van Rossum79f25d91997-04-29 20:08:16 +00001844PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001845PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001846{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001847 PyObject *pname;
1848 PyObject *result;
1849
1850 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001851 if (pname == NULL)
1852 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001853 result = PyImport_Import(pname);
1854 Py_DECREF(pname);
1855 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001856}
1857
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001858/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001859static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1860static PyObject *load_next(PyObject *mod, PyObject *altmod,
1861 char **p_name, char *buf, int *p_buflen);
1862static int mark_miss(char *name);
1863static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1864 char *buf, int buflen, int recursive);
1865static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001866
1867/* The Magnum Opus of dotted-name import :-) */
1868
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001869static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001870import_module_ex(char *name, PyObject *globals, PyObject *locals,
1871 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001872{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001873 char buf[MAXPATHLEN+1];
1874 int buflen = 0;
1875 PyObject *parent, *head, *next, *tail;
1876
1877 parent = get_parent(globals, buf, &buflen);
1878 if (parent == NULL)
1879 return NULL;
1880
1881 head = load_next(parent, Py_None, &name, buf, &buflen);
1882 if (head == NULL)
1883 return NULL;
1884
1885 tail = head;
1886 Py_INCREF(tail);
1887 while (name) {
1888 next = load_next(tail, tail, &name, buf, &buflen);
1889 Py_DECREF(tail);
1890 if (next == NULL) {
1891 Py_DECREF(head);
1892 return NULL;
1893 }
1894 tail = next;
1895 }
1896
1897 if (fromlist != NULL) {
1898 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1899 fromlist = NULL;
1900 }
1901
1902 if (fromlist == NULL) {
1903 Py_DECREF(tail);
1904 return head;
1905 }
1906
1907 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001908 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001909 Py_DECREF(tail);
1910 return NULL;
1911 }
1912
1913 return tail;
1914}
1915
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001916PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001917PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1918 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001919{
1920 PyObject *result;
1921 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001922 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001923 if (unlock_import() < 0) {
1924 Py_XDECREF(result);
1925 PyErr_SetString(PyExc_RuntimeError,
1926 "not holding the import lock");
1927 return NULL;
1928 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001929 return result;
1930}
1931
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001932static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001933get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001934{
1935 static PyObject *namestr = NULL;
1936 static PyObject *pathstr = NULL;
1937 PyObject *modname, *modpath, *modules, *parent;
1938
1939 if (globals == NULL || !PyDict_Check(globals))
1940 return Py_None;
1941
1942 if (namestr == NULL) {
1943 namestr = PyString_InternFromString("__name__");
1944 if (namestr == NULL)
1945 return NULL;
1946 }
1947 if (pathstr == NULL) {
1948 pathstr = PyString_InternFromString("__path__");
1949 if (pathstr == NULL)
1950 return NULL;
1951 }
1952
1953 *buf = '\0';
1954 *p_buflen = 0;
1955 modname = PyDict_GetItem(globals, namestr);
1956 if (modname == NULL || !PyString_Check(modname))
1957 return Py_None;
1958
1959 modpath = PyDict_GetItem(globals, pathstr);
1960 if (modpath != NULL) {
1961 int len = PyString_GET_SIZE(modname);
1962 if (len > MAXPATHLEN) {
1963 PyErr_SetString(PyExc_ValueError,
1964 "Module name too long");
1965 return NULL;
1966 }
1967 strcpy(buf, PyString_AS_STRING(modname));
1968 *p_buflen = len;
1969 }
1970 else {
1971 char *start = PyString_AS_STRING(modname);
1972 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001973 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001974 if (lastdot == NULL)
1975 return Py_None;
1976 len = lastdot - start;
1977 if (len >= MAXPATHLEN) {
1978 PyErr_SetString(PyExc_ValueError,
1979 "Module name too long");
1980 return NULL;
1981 }
1982 strncpy(buf, start, len);
1983 buf[len] = '\0';
1984 *p_buflen = len;
1985 }
1986
1987 modules = PyImport_GetModuleDict();
1988 parent = PyDict_GetItemString(modules, buf);
1989 if (parent == NULL)
1990 parent = Py_None;
1991 return parent;
1992 /* We expect, but can't guarantee, if parent != None, that:
1993 - parent.__name__ == buf
1994 - parent.__dict__ is globals
1995 If this is violated... Who cares? */
1996}
1997
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001998/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001999static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002000load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2001 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002002{
2003 char *name = *p_name;
2004 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002005 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002006 char *p;
2007 PyObject *result;
2008
2009 if (dot == NULL) {
2010 *p_name = NULL;
2011 len = strlen(name);
2012 }
2013 else {
2014 *p_name = dot+1;
2015 len = dot-name;
2016 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002017 if (len == 0) {
2018 PyErr_SetString(PyExc_ValueError,
2019 "Empty module name");
2020 return NULL;
2021 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002022
2023 p = buf + *p_buflen;
2024 if (p != buf)
2025 *p++ = '.';
2026 if (p+len-buf >= MAXPATHLEN) {
2027 PyErr_SetString(PyExc_ValueError,
2028 "Module name too long");
2029 return NULL;
2030 }
2031 strncpy(p, name, len);
2032 p[len] = '\0';
2033 *p_buflen = p+len-buf;
2034
2035 result = import_submodule(mod, p, buf);
2036 if (result == Py_None && altmod != mod) {
2037 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002038 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002039 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002040 if (result != NULL && result != Py_None) {
2041 if (mark_miss(buf) != 0) {
2042 Py_DECREF(result);
2043 return NULL;
2044 }
2045 strncpy(buf, name, len);
2046 buf[len] = '\0';
2047 *p_buflen = len;
2048 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002049 }
2050 if (result == NULL)
2051 return NULL;
2052
2053 if (result == Py_None) {
2054 Py_DECREF(result);
2055 PyErr_Format(PyExc_ImportError,
2056 "No module named %.200s", name);
2057 return NULL;
2058 }
2059
2060 return result;
2061}
2062
2063static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002064mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002065{
2066 PyObject *modules = PyImport_GetModuleDict();
2067 return PyDict_SetItemString(modules, name, Py_None);
2068}
2069
2070static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002071ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2072 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002073{
2074 int i;
2075
2076 if (!PyObject_HasAttrString(mod, "__path__"))
2077 return 1;
2078
2079 for (i = 0; ; i++) {
2080 PyObject *item = PySequence_GetItem(fromlist, i);
2081 int hasit;
2082 if (item == NULL) {
2083 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2084 PyErr_Clear();
2085 return 1;
2086 }
2087 return 0;
2088 }
2089 if (!PyString_Check(item)) {
2090 PyErr_SetString(PyExc_TypeError,
2091 "Item in ``from list'' not a string");
2092 Py_DECREF(item);
2093 return 0;
2094 }
2095 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002096 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002097 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002098 /* See if the package defines __all__ */
2099 if (recursive)
2100 continue; /* Avoid endless recursion */
2101 all = PyObject_GetAttrString(mod, "__all__");
2102 if (all == NULL)
2103 PyErr_Clear();
2104 else {
2105 if (!ensure_fromlist(mod, all, buf, buflen, 1))
2106 return 0;
2107 Py_DECREF(all);
2108 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002109 continue;
2110 }
2111 hasit = PyObject_HasAttr(mod, item);
2112 if (!hasit) {
2113 char *subname = PyString_AS_STRING(item);
2114 PyObject *submod;
2115 char *p;
2116 if (buflen + strlen(subname) >= MAXPATHLEN) {
2117 PyErr_SetString(PyExc_ValueError,
2118 "Module name too long");
2119 Py_DECREF(item);
2120 return 0;
2121 }
2122 p = buf + buflen;
2123 *p++ = '.';
2124 strcpy(p, subname);
2125 submod = import_submodule(mod, subname, buf);
2126 Py_XDECREF(submod);
2127 if (submod == NULL) {
2128 Py_DECREF(item);
2129 return 0;
2130 }
2131 }
2132 Py_DECREF(item);
2133 }
2134
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002135 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002136}
2137
Neil Schemenauer00b09662003-06-16 21:03:07 +00002138static int
2139add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2140 PyObject *modules)
2141{
2142 if (mod == Py_None)
2143 return 1;
2144 /* Irrespective of the success of this load, make a
2145 reference to it in the parent package module. A copy gets
2146 saved in the modules dictionary under the full name, so get a
2147 reference from there, if need be. (The exception is when the
2148 load failed with a SyntaxError -- then there's no trace in
2149 sys.modules. In that case, of course, do nothing extra.) */
2150 if (submod == NULL) {
2151 submod = PyDict_GetItemString(modules, fullname);
2152 if (submod == NULL)
2153 return 1;
2154 }
2155 if (PyModule_Check(mod)) {
2156 /* We can't use setattr here since it can give a
2157 * spurious warning if the submodule name shadows a
2158 * builtin name */
2159 PyObject *dict = PyModule_GetDict(mod);
2160 if (!dict)
2161 return 0;
2162 if (PyDict_SetItemString(dict, subname, submod) < 0)
2163 return 0;
2164 }
2165 else {
2166 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2167 return 0;
2168 }
2169 return 1;
2170}
2171
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002172static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002173import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002174{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002175 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002176 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002177
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002178 /* Require:
2179 if mod == None: subname == fullname
2180 else: mod.__name__ + "." + subname == fullname
2181 */
2182
Tim Peters50d8d372001-02-28 05:34:27 +00002183 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002184 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002185 }
2186 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002187 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002188 char buf[MAXPATHLEN+1];
2189 struct filedescr *fdp;
2190 FILE *fp = NULL;
2191
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002192 if (mod == Py_None)
2193 path = NULL;
2194 else {
2195 path = PyObject_GetAttrString(mod, "__path__");
2196 if (path == NULL) {
2197 PyErr_Clear();
2198 Py_INCREF(Py_None);
2199 return Py_None;
2200 }
2201 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002202
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002203 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002204 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2205 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002206 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002207 if (fdp == NULL) {
2208 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2209 return NULL;
2210 PyErr_Clear();
2211 Py_INCREF(Py_None);
2212 return Py_None;
2213 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002214 m = load_module(fullname, fp, buf, fdp->type, loader);
2215 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002216 if (fp)
2217 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002218 if (!add_submodule(mod, m, fullname, subname, modules)) {
2219 Py_XDECREF(m);
2220 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002221 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002222 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002223
2224 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002225}
2226
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002227
2228/* Re-import a module of any kind and return its module object, WITH
2229 INCREMENTED REFERENCE COUNT */
2230
Guido van Rossum79f25d91997-04-29 20:08:16 +00002231PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002232PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002233{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002234 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002235 PyObject *path = NULL;
2236 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002237 char buf[MAXPATHLEN+1];
2238 struct filedescr *fdp;
2239 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002240
Guido van Rossum79f25d91997-04-29 20:08:16 +00002241 if (m == NULL || !PyModule_Check(m)) {
2242 PyErr_SetString(PyExc_TypeError,
2243 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002244 return NULL;
2245 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002246 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002247 if (name == NULL)
2248 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002249 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002250 PyErr_Format(PyExc_ImportError,
2251 "reload(): module %.200s not in sys.modules",
2252 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002253 return NULL;
2254 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002255 subname = strrchr(name, '.');
2256 if (subname == NULL)
2257 subname = name;
2258 else {
2259 PyObject *parentname, *parent;
2260 parentname = PyString_FromStringAndSize(name, (subname-name));
2261 if (parentname == NULL)
2262 return NULL;
2263 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002264 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002265 if (parent == NULL) {
2266 PyErr_Format(PyExc_ImportError,
2267 "reload(): parent %.200s not in sys.modules",
2268 name);
2269 return NULL;
2270 }
2271 subname++;
2272 path = PyObject_GetAttrString(parent, "__path__");
2273 if (path == NULL)
2274 PyErr_Clear();
2275 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002276 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002277 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002278 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002279 if (fdp == NULL)
2280 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002281 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002282 if (fp)
2283 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002284 return m;
2285}
2286
2287
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002288/* Higher-level import emulator which emulates the "import" statement
2289 more accurately -- it invokes the __import__() function from the
2290 builtins of the current globals. This means that the import is
2291 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002292 environment, e.g. by "rexec".
2293 A dummy list ["__doc__"] is passed as the 4th argument so that
2294 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2295 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002296
2297PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002298PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002299{
2300 static PyObject *silly_list = NULL;
2301 static PyObject *builtins_str = NULL;
2302 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002303 PyObject *globals = NULL;
2304 PyObject *import = NULL;
2305 PyObject *builtins = NULL;
2306 PyObject *r = NULL;
2307
2308 /* Initialize constant string objects */
2309 if (silly_list == NULL) {
2310 import_str = PyString_InternFromString("__import__");
2311 if (import_str == NULL)
2312 return NULL;
2313 builtins_str = PyString_InternFromString("__builtins__");
2314 if (builtins_str == NULL)
2315 return NULL;
2316 silly_list = Py_BuildValue("[s]", "__doc__");
2317 if (silly_list == NULL)
2318 return NULL;
2319 }
2320
2321 /* Get the builtins from current globals */
2322 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002323 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002324 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002325 builtins = PyObject_GetItem(globals, builtins_str);
2326 if (builtins == NULL)
2327 goto err;
2328 }
2329 else {
2330 /* No globals -- use standard builtins, and fake globals */
2331 PyErr_Clear();
2332
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002333 builtins = PyImport_ImportModuleEx("__builtin__",
2334 NULL, NULL, NULL);
2335 if (builtins == NULL)
2336 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002337 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2338 if (globals == NULL)
2339 goto err;
2340 }
2341
2342 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002343 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002344 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002345 if (import == NULL)
2346 PyErr_SetObject(PyExc_KeyError, import_str);
2347 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002348 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002349 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002350 if (import == NULL)
2351 goto err;
2352
2353 /* Call the _import__ function with the proper argument list */
2354 r = PyObject_CallFunction(import, "OOOO",
2355 module_name, globals, globals, silly_list);
2356
2357 err:
2358 Py_XDECREF(globals);
2359 Py_XDECREF(builtins);
2360 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002361
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002362 return r;
2363}
2364
2365
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002366/* Module 'imp' provides Python access to the primitives used for
2367 importing modules.
2368*/
2369
Guido van Rossum79f25d91997-04-29 20:08:16 +00002370static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002371imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002372{
2373 char buf[4];
2374
Guido van Rossum96774c12000-05-01 20:19:08 +00002375 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2376 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2377 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2378 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002379
Guido van Rossum79f25d91997-04-29 20:08:16 +00002380 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002381}
2382
Guido van Rossum79f25d91997-04-29 20:08:16 +00002383static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002384imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002385{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002386 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002387 struct filedescr *fdp;
2388
Guido van Rossum79f25d91997-04-29 20:08:16 +00002389 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002390 if (list == NULL)
2391 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002392 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2393 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002394 fdp->suffix, fdp->mode, fdp->type);
2395 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002396 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002397 return NULL;
2398 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002399 if (PyList_Append(list, item) < 0) {
2400 Py_DECREF(list);
2401 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002402 return NULL;
2403 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002404 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002405 }
2406 return list;
2407}
2408
Guido van Rossum79f25d91997-04-29 20:08:16 +00002409static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002410call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002411{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002412 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002413 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002414 struct filedescr *fdp;
2415 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002416 FILE *fp = NULL;
2417
2418 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002419 if (path == Py_None)
2420 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002421 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002422 if (fdp == NULL)
2423 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002424 if (fp != NULL) {
2425 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2426 if (fob == NULL) {
2427 fclose(fp);
2428 return NULL;
2429 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002430 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002431 else {
2432 fob = Py_None;
2433 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002434 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002435 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002436 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438 return ret;
2439}
2440
Guido van Rossum79f25d91997-04-29 20:08:16 +00002441static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002442imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002443{
2444 char *name;
2445 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002446 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002447 return NULL;
2448 return call_find_module(name, path);
2449}
2450
2451static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002452imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002453{
2454 char *name;
2455 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002456 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002457 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002458 return NULL;
2459 ret = init_builtin(name);
2460 if (ret < 0)
2461 return NULL;
2462 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 Py_INCREF(Py_None);
2464 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002466 m = PyImport_AddModule(name);
2467 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002468 return m;
2469}
2470
Guido van Rossum79f25d91997-04-29 20:08:16 +00002471static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002472imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002473{
2474 char *name;
2475 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002476 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002477 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002478 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002479 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002480 if (ret < 0)
2481 return NULL;
2482 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002483 Py_INCREF(Py_None);
2484 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002485 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002486 m = PyImport_AddModule(name);
2487 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002488 return m;
2489}
2490
Guido van Rossum79f25d91997-04-29 20:08:16 +00002491static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002492imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002493{
2494 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002495
Guido van Rossum43713e52000-02-29 13:59:29 +00002496 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002497 return NULL;
2498 return get_frozen_object(name);
2499}
2500
Guido van Rossum79f25d91997-04-29 20:08:16 +00002501static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002502imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002503{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002504 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002505 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002506 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002507 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002508}
2509
Guido van Rossum79f25d91997-04-29 20:08:16 +00002510static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002511imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002512{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002513 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002514 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002515 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002517 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002518 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519}
2520
2521static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002522get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002523{
2524 FILE *fp;
2525 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002526 if (mode[0] == 'U')
2527 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002528 fp = fopen(pathname, mode);
2529 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531 }
2532 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002533 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002535 PyErr_SetString(PyExc_ValueError,
2536 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002537 }
2538 return fp;
2539}
2540
Guido van Rossum79f25d91997-04-29 20:08:16 +00002541static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002542imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002543{
2544 char *name;
2545 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002546 PyObject *fob = NULL;
2547 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002548 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002549 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002550 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002551 return NULL;
2552 fp = get_file(pathname, fob, "rb");
2553 if (fp == NULL)
2554 return NULL;
2555 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002556 if (fob == NULL)
2557 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558 return m;
2559}
2560
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002561#ifdef HAVE_DYNAMIC_LOADING
2562
Guido van Rossum79f25d91997-04-29 20:08:16 +00002563static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002564imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002565{
2566 char *name;
2567 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002568 PyObject *fob = NULL;
2569 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002570 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002571 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002572 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002573 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002574 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002575 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002576 if (fp == NULL)
2577 return NULL;
2578 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002579 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002580 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002581}
2582
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002583#endif /* HAVE_DYNAMIC_LOADING */
2584
Guido van Rossum79f25d91997-04-29 20:08:16 +00002585static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002586imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002587{
2588 char *name;
2589 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590 PyObject *fob = NULL;
2591 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002593 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595 return NULL;
2596 fp = get_file(pathname, fob, "r");
2597 if (fp == NULL)
2598 return NULL;
2599 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002600 if (fob == NULL)
2601 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602 return m;
2603}
2604
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002606imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002607{
2608 char *name;
2609 PyObject *fob;
2610 char *pathname;
2611 char *suffix; /* Unused */
2612 char *mode;
2613 int type;
2614 FILE *fp;
2615
Guido van Rossum43713e52000-02-29 13:59:29 +00002616 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002617 &name, &fob, &pathname,
2618 &suffix, &mode, &type))
2619 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002620 if (*mode) {
2621 /* Mode must start with 'r' or 'U' and must not contain '+'.
2622 Implicit in this test is the assumption that the mode
2623 may contain other modifiers like 'b' or 't'. */
2624
2625 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002626 PyErr_Format(PyExc_ValueError,
2627 "invalid file open mode %.200s", mode);
2628 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002629 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002630 }
2631 if (fob == Py_None)
2632 fp = NULL;
2633 else {
2634 if (!PyFile_Check(fob)) {
2635 PyErr_SetString(PyExc_ValueError,
2636 "load_module arg#2 should be a file or None");
2637 return NULL;
2638 }
2639 fp = get_file(pathname, fob, mode);
2640 if (fp == NULL)
2641 return NULL;
2642 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002643 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002644}
2645
2646static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002647imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002648{
2649 char *name;
2650 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002651 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002652 return NULL;
2653 return load_package(name, pathname);
2654}
2655
2656static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002657imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002658{
2659 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002660 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002661 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002662 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002663}
2664
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002665/* Doc strings */
2666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667PyDoc_STRVAR(doc_imp,
2668"This module provides the components needed to build your own\n\
2669__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002671PyDoc_STRVAR(doc_find_module,
2672"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002673Search for a module. If path is omitted or None, search for a\n\
2674built-in, frozen or special module and continue search in sys.path.\n\
2675The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002676package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(doc_load_module,
2679"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002680Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002682
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002683PyDoc_STRVAR(doc_get_magic,
2684"get_magic() -> string\n\
2685Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002686
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002687PyDoc_STRVAR(doc_get_suffixes,
2688"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002689Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002690that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002692PyDoc_STRVAR(doc_new_module,
2693"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002694Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002697PyDoc_STRVAR(doc_lock_held,
2698"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002699Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002701
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002702PyDoc_STRVAR(doc_acquire_lock,
2703"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002704Acquires the interpreter's import lock for the current thread.\n\
2705This lock should be used by import hooks to ensure thread-safety\n\
2706when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002707On platforms without threads, this function does nothing.");
2708
2709PyDoc_STRVAR(doc_release_lock,
2710"release_lock() -> None\n\
2711Release the interpreter's import lock.\n\
2712On platforms without threads, this function does nothing.");
2713
Guido van Rossum79f25d91997-04-29 20:08:16 +00002714static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002715 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2716 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2717 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2718 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2719 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2720 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2721 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2722 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002723 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002724 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2725 {"init_builtin", imp_init_builtin, METH_VARARGS},
2726 {"init_frozen", imp_init_frozen, METH_VARARGS},
2727 {"is_builtin", imp_is_builtin, METH_VARARGS},
2728 {"is_frozen", imp_is_frozen, METH_VARARGS},
2729 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002730#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002731 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002732#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002733 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002734 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002735 {NULL, NULL} /* sentinel */
2736};
2737
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002738static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002739setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002740{
2741 PyObject *v;
2742 int err;
2743
2744 v = PyInt_FromLong((long)value);
2745 err = PyDict_SetItemString(d, name, v);
2746 Py_XDECREF(v);
2747 return err;
2748}
2749
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002750PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002751initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002752{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002753 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002754
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002755 m = Py_InitModule4("imp", imp_methods, doc_imp,
2756 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002757 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002758
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002759 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2760 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2761 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2762 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2763 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2764 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2765 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2766 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002767 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002768 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002769
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002770 failure:
2771 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002772}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002773
2774
Guido van Rossumb18618d2000-05-03 23:44:39 +00002775/* API for embedding applications that want to add their own entries
2776 to the table of built-in modules. This should normally be called
2777 *before* Py_Initialize(). When the table resize fails, -1 is
2778 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002779
2780 After a similar function by Just van Rossum. */
2781
2782int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002783PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002784{
2785 static struct _inittab *our_copy = NULL;
2786 struct _inittab *p;
2787 int i, n;
2788
2789 /* Count the number of entries in both tables */
2790 for (n = 0; newtab[n].name != NULL; n++)
2791 ;
2792 if (n == 0)
2793 return 0; /* Nothing to do */
2794 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2795 ;
2796
2797 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002798 p = our_copy;
2799 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002800 if (p == NULL)
2801 return -1;
2802
2803 /* Copy the tables into the new memory */
2804 if (our_copy != PyImport_Inittab)
2805 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2806 PyImport_Inittab = our_copy = p;
2807 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2808
2809 return 0;
2810}
2811
2812/* Shorthand to add a single entry given a name and a function */
2813
2814int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002815PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002816{
2817 struct _inittab newtab[2];
2818
2819 memset(newtab, '\0', sizeof newtab);
2820
2821 newtab[0].name = name;
2822 newtab[0].initfunc = initfunc;
2823
2824 return PyImport_ExtendInittab(newtab);
2825}