blob: 5479677aa3dd14d9e3ae58aee30dbada5a943092 [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{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000324 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000325 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;
Nicholas Bastine5662ae2004-03-24 22:22:12 +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;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000597 /* If the module is being reloaded, we get the old module back
598 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 d = PyModule_GetDict(m);
600 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
601 if (PyDict_SetItemString(d, "__builtins__",
602 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000603 return NULL;
604 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000605 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000606 v = NULL;
607 if (pathname != NULL) {
608 v = PyString_FromString(pathname);
609 if (v == NULL)
610 PyErr_Clear();
611 }
612 if (v == NULL) {
613 v = ((PyCodeObject *)co)->co_filename;
614 Py_INCREF(v);
615 }
616 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000618 Py_DECREF(v);
619
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000620 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621 if (v == NULL)
622 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000624
Guido van Rossum25ce5661997-08-02 03:10:38 +0000625 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000626 PyErr_Format(PyExc_ImportError,
627 "Loaded module %.200s not found in sys.modules",
628 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000629 return NULL;
630 }
631
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633
634 return m;
635}
636
637
638/* Given a pathname for a Python source file, fill a buffer with the
639 pathname for the corresponding compiled file. Return the pathname
640 for the compiled file, or NULL if there's no space in the buffer.
641 Doesn't set an exception. */
642
643static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000644make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645{
Tim Petersc1731372001-08-04 08:12:36 +0000646 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647 if (len+2 > buflen)
648 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000649
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000650#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000651 /* Treat .pyw as if it were .py. The case of ".pyw" must match
652 that used in _PyImport_StandardFiletab. */
653 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
654 --len; /* pretend 'w' isn't there */
655#endif
656 memcpy(buf, pathname, len);
657 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
658 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659
660 return buf;
661}
662
663
664/* Given a pathname for a Python source file, its time of last
665 modification, and a pathname for a compiled file, check whether the
666 compiled file represents the same version of the source. If so,
667 return a FILE pointer for the compiled file, positioned just after
668 the header; if not, return NULL.
669 Doesn't set an exception. */
670
671static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000672check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673{
674 FILE *fp;
675 long magic;
676 long pyc_mtime;
677
678 fp = fopen(cpathname, "rb");
679 if (fp == NULL)
680 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000682 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000684 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685 fclose(fp);
686 return NULL;
687 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000688 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000691 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 fclose(fp);
693 return NULL;
694 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000696 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000697 return fp;
698}
699
700
701/* Read a code object from a file and check it for validity */
702
Guido van Rossum79f25d91997-04-29 20:08:16 +0000703static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000704read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Tim Petersd9b9ac82001-01-28 00:27:39 +0000708 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000709 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 if (co == NULL || !PyCode_Check(co)) {
711 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000712 PyErr_Format(PyExc_ImportError,
713 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000715 return NULL;
716 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718}
719
720
721/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000722 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000725load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726{
727 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000728 PyCodeObject *co;
729 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000732 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000733 PyErr_Format(PyExc_ImportError,
734 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000735 return NULL;
736 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000738 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739 if (co == NULL)
740 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000742 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000744 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746
747 return m;
748}
749
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000750/* Parse a source file and return the corresponding code object */
751
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000753parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756 node *n;
757
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000758 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 if (n == NULL)
760 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 co = PyNode_Compile(n, pathname);
762 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763
764 return co;
765}
766
767
Guido van Rossum55a83382000-09-20 20:31:38 +0000768/* Helper to open a bytecode file for writing in exclusive mode */
769
770static FILE *
771open_exclusive(char *filename)
772{
773#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
774 /* Use O_EXCL to avoid a race condition when another process tries to
775 write the same file. When that happens, our open() call fails,
776 which is just fine (since it's only a cache).
777 XXX If the file exists and is writable but the directory is not
778 writable, the file will never be written. Oh well.
779 */
780 int fd;
781 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000782 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
783#ifdef O_BINARY
784 |O_BINARY /* necessary for Windows */
785#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000786#ifdef __VMS
787 , 0666, "ctxt=bin", "shr=nil");
788#else
789 , 0666);
790#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000791 if (fd < 0)
792 return NULL;
793 return fdopen(fd, "wb");
794#else
795 /* Best we can do -- on Windows this can't happen anyway */
796 return fopen(filename, "wb");
797#endif
798}
799
800
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801/* Write a compiled module to a file, placing the time of last
802 modification of its source into the header.
803 Errors are ignored, if a write error occurs an attempt is made to
804 remove the file. */
805
806static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000807write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808{
809 FILE *fp;
810
Guido van Rossum55a83382000-09-20 20:31:38 +0000811 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000814 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815 "# can't create %s\n", cpathname);
816 return;
817 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000818 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 PyMarshal_WriteLongToFile(0L, fp);
821 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000824 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000825 /* Don't keep partial file */
826 fclose(fp);
827 (void) unlink(cpathname);
828 return;
829 }
830 /* Now write the true mtime */
831 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000833 fflush(fp);
834 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000836 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837}
838
839
840/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000841 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
842 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000843
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000845load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846{
Fred Drake4c82b232000-06-30 16:18:57 +0000847 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848 FILE *fpc;
849 char buf[MAXPATHLEN+1];
850 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 PyCodeObject *co;
852 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000854 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000855 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000856 return NULL;
857#if SIZEOF_TIME_T > 4
858 /* Python's .pyc timestamp handling presumes that the timestamp fits
859 in 4 bytes. This will be fine until sometime in the year 2038,
860 when a 4-byte signed time_t will overflow.
861 */
862 if (mtime >> 32) {
863 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000864 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000865 return NULL;
866 }
867#endif
Tim Peters36515e22001-11-18 04:06:29 +0000868 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000869 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000870 if (cpathname != NULL &&
871 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000872 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 fclose(fpc);
874 if (co == NULL)
875 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000876 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000877 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000878 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000879 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000880 }
881 else {
882 co = parse_source_module(pathname, fp);
883 if (co == NULL)
884 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000886 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000887 name, pathname);
888 write_compiled_module(co, cpathname, mtime);
889 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000890 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000892
893 return m;
894}
895
896
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000897/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000898static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
899static struct filedescr *find_module(char *, char *, PyObject *,
900 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000901static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000902
903/* Load a package and return its module object WITH INCREMENTED
904 REFERENCE COUNT */
905
906static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000907load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000908{
909 PyObject *m, *d, *file, *path;
910 int err;
911 char buf[MAXPATHLEN+1];
912 FILE *fp = NULL;
913 struct filedescr *fdp;
914
915 m = PyImport_AddModule(name);
916 if (m == NULL)
917 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000918 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000919 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000920 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000921 d = PyModule_GetDict(m);
922 file = PyString_FromString(pathname);
923 if (file == NULL)
924 return NULL;
925 path = Py_BuildValue("[O]", file);
926 if (path == NULL) {
927 Py_DECREF(file);
928 return NULL;
929 }
930 err = PyDict_SetItemString(d, "__file__", file);
931 if (err == 0)
932 err = PyDict_SetItemString(d, "__path__", path);
933 if (err != 0) {
934 m = NULL;
935 goto cleanup;
936 }
937 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000938 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000939 if (fdp == NULL) {
940 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
941 PyErr_Clear();
942 }
943 else
944 m = NULL;
945 goto cleanup;
946 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000947 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000948 if (fp != NULL)
949 fclose(fp);
950 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000951 Py_XDECREF(path);
952 Py_XDECREF(file);
953 return m;
954}
955
956
957/* Helper to test for built-in module */
958
959static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000960is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000961{
962 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000963 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
964 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
965 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000966 return -1;
967 else
968 return 1;
969 }
970 }
971 return 0;
972}
973
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000974
Just van Rossum52e14d62002-12-30 22:08:05 +0000975/* Return an importer object for a sys.path/pkg.__path__ item 'p',
976 possibly by fetching it from the path_importer_cache dict. If it
977 wasn't yet cached, traverse path_hooks until it a hook is found
978 that can handle the path item. Return None if no hook could;
979 this tells our caller it should fall back to the builtin
980 import mechanism. Cache the result in path_importer_cache.
981 Returns a borrowed reference. */
982
983static PyObject *
984get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
985 PyObject *p)
986{
987 PyObject *importer;
988 int j, nhooks;
989
990 /* These conditions are the caller's responsibility: */
991 assert(PyList_Check(path_hooks));
992 assert(PyDict_Check(path_importer_cache));
993
994 nhooks = PyList_Size(path_hooks);
995 if (nhooks < 0)
996 return NULL; /* Shouldn't happen */
997
998 importer = PyDict_GetItem(path_importer_cache, p);
999 if (importer != NULL)
1000 return importer;
1001
1002 /* set path_importer_cache[p] to None to avoid recursion */
1003 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1004 return NULL;
1005
1006 for (j = 0; j < nhooks; j++) {
1007 PyObject *hook = PyList_GetItem(path_hooks, j);
1008 if (hook == NULL)
1009 return NULL;
1010 importer = PyObject_CallFunction(hook, "O", p);
1011 if (importer != NULL)
1012 break;
1013
1014 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1015 return NULL;
1016 }
1017 PyErr_Clear();
1018 }
1019 if (importer == NULL)
1020 importer = Py_None;
1021 else if (importer != Py_None) {
1022 int err = PyDict_SetItem(path_importer_cache, p, importer);
1023 Py_DECREF(importer);
1024 if (err != 0)
1025 return NULL;
1026 }
1027 return importer;
1028}
1029
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001030/* Search the path (default sys.path) for a module. Return the
1031 corresponding filedescr struct, and (via return arguments) the
1032 pathname and an open file. Return NULL if the module is not found. */
1033
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001034#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001035extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1036 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001037#endif
1038
Tim Peters50d8d372001-02-28 05:34:27 +00001039static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001040static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001041static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001042
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001043static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001044find_module(char *fullname, char *subname, PyObject *path, char *buf,
1045 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001046{
Fred Drake4c82b232000-06-30 16:18:57 +00001047 int i, npath;
1048 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001049 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001050 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001051 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001052 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001053#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001054 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001055#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001056 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1057 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1058 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001059 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001060#if defined(PYOS_OS2)
1061 size_t saved_len;
1062 size_t saved_namelen;
1063 char *saved_buf = NULL;
1064#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001065 if (p_loader != NULL)
1066 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001067
Just van Rossum52e14d62002-12-30 22:08:05 +00001068 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001069 PyErr_SetString(PyExc_OverflowError,
1070 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001071 return NULL;
1072 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001073 strcpy(name, subname);
1074
1075 /* sys.meta_path import hook */
1076 if (p_loader != NULL) {
1077 PyObject *meta_path;
1078
1079 meta_path = PySys_GetObject("meta_path");
1080 if (meta_path == NULL || !PyList_Check(meta_path)) {
1081 PyErr_SetString(PyExc_ImportError,
1082 "sys.meta_path must be a list of "
1083 "import hooks");
1084 return NULL;
1085 }
1086 Py_INCREF(meta_path); /* zap guard */
1087 npath = PyList_Size(meta_path);
1088 for (i = 0; i < npath; i++) {
1089 PyObject *loader;
1090 PyObject *hook = PyList_GetItem(meta_path, i);
1091 loader = PyObject_CallMethod(hook, "find_module",
1092 "sO", fullname,
1093 path != NULL ?
1094 path : Py_None);
1095 if (loader == NULL) {
1096 Py_DECREF(meta_path);
1097 return NULL; /* true error */
1098 }
1099 if (loader != Py_None) {
1100 /* a loader was found */
1101 *p_loader = loader;
1102 Py_DECREF(meta_path);
1103 return &importhookdescr;
1104 }
1105 Py_DECREF(loader);
1106 }
1107 Py_DECREF(meta_path);
1108 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001109
1110 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001111 /* The only type of submodule allowed inside a "frozen"
1112 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001113 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1114 PyErr_SetString(PyExc_ImportError,
1115 "full frozen module name too long");
1116 return NULL;
1117 }
1118 strcpy(buf, PyString_AsString(path));
1119 strcat(buf, ".");
1120 strcat(buf, name);
1121 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001122 if (find_frozen(name) != NULL) {
1123 strcpy(buf, name);
1124 return &fd_frozen;
1125 }
1126 PyErr_Format(PyExc_ImportError,
1127 "No frozen submodule named %.200s", name);
1128 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001129 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001130 if (path == NULL) {
1131 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001132 strcpy(buf, name);
1133 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001134 }
Greg Ward201baee2001-10-04 14:52:06 +00001135 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001136 strcpy(buf, name);
1137 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001138 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001139
Guido van Rossumac279101996-08-22 23:10:58 +00001140#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001141 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1142 if (fp != NULL) {
1143 *p_fp = fp;
1144 return fdp;
1145 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001146#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001147 path = PySys_GetObject("path");
1148 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001149 if (path == NULL || !PyList_Check(path)) {
1150 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001151 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001152 return NULL;
1153 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001154
1155 path_hooks = PySys_GetObject("path_hooks");
1156 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1157 PyErr_SetString(PyExc_ImportError,
1158 "sys.path_hooks must be a list of "
1159 "import hooks");
1160 return NULL;
1161 }
1162 path_importer_cache = PySys_GetObject("path_importer_cache");
1163 if (path_importer_cache == NULL ||
1164 !PyDict_Check(path_importer_cache)) {
1165 PyErr_SetString(PyExc_ImportError,
1166 "sys.path_importer_cache must be a dict");
1167 return NULL;
1168 }
1169
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001171 namelen = strlen(name);
1172 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001173 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001175#ifdef Py_USING_UNICODE
1176 if (PyUnicode_Check(v)) {
1177 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1178 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1179 if (copy == NULL)
1180 return NULL;
1181 v = copy;
1182 }
1183 else
1184#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001186 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001188 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1189 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001190 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001191 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001193 if (strlen(buf) != len) {
1194 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001195 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001196 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001197
1198 /* sys.path_hooks import hook */
1199 if (p_loader != NULL) {
1200 PyObject *importer;
1201
1202 importer = get_path_importer(path_importer_cache,
1203 path_hooks, v);
1204 if (importer == NULL)
1205 return NULL;
1206 /* Note: importer is a borrowed reference */
1207 if (importer != Py_None) {
1208 PyObject *loader;
1209 loader = PyObject_CallMethod(importer,
1210 "find_module",
1211 "s", fullname);
1212 if (loader == NULL)
1213 return NULL; /* error */
1214 if (loader != Py_None) {
1215 /* a loader was found */
1216 *p_loader = loader;
1217 return &importhookdescr;
1218 }
1219 Py_DECREF(loader);
1220 }
1221 /* no hook was successful, use builtin import */
1222 }
1223
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001224 if (len > 0 && buf[len-1] != SEP
1225#ifdef ALTSEP
1226 && buf[len-1] != ALTSEP
1227#endif
1228 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001229 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001230 strcpy(buf+len, name);
1231 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001232
1233 /* Check for package import (buf holds a directory name,
1234 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001235#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001236 if (stat(buf, &statbuf) == 0 && /* it exists */
1237 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1238 find_init_module(buf) && /* it has __init__.py */
1239 case_ok(buf, len, namelen, name)) { /* and case matches */
1240 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001241 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001242 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001243#else
1244 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001245#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001246 if (isdir(buf) &&
1247 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001248 case_ok(buf, len, namelen, name)) {
1249 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001250 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001251 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001252#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001253#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001254#if defined(PYOS_OS2)
1255 /* take a snapshot of the module spec for restoration
1256 * after the 8 character DLL hackery
1257 */
1258 saved_buf = strdup(buf);
1259 saved_len = len;
1260 saved_namelen = namelen;
1261#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001263#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001264 /* OS/2 limits DLLs to 8 character names (w/o
1265 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001266 * so if the name is longer than that and its a
1267 * dynamically loaded module we're going to try,
1268 * truncate the name before trying
1269 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001270 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001271 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001272 const struct filedescr *scan;
1273 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001274 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001275 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001276 break;
1277 else
1278 scan++;
1279 }
1280 if (scan->suffix != NULL) {
1281 /* yes, so truncate the name */
1282 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001283 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001284 buf[len] = '\0';
1285 }
1286 }
1287#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001288 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001289 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001290 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001291 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001292 if (filemode[0] == 'U')
1293 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001294 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001295 if (fp != NULL) {
1296 if (case_ok(buf, len, namelen, name))
1297 break;
1298 else { /* continue search */
1299 fclose(fp);
1300 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001301 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001302 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001303#if defined(PYOS_OS2)
1304 /* restore the saved snapshot */
1305 strcpy(buf, saved_buf);
1306 len = saved_len;
1307 namelen = saved_namelen;
1308#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001309 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001310#if defined(PYOS_OS2)
1311 /* don't need/want the module name snapshot anymore */
1312 if (saved_buf)
1313 {
1314 free(saved_buf);
1315 saved_buf = NULL;
1316 }
1317#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001318 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001319 if (fp != NULL)
1320 break;
1321 }
1322 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001323 PyErr_Format(PyExc_ImportError,
1324 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325 return NULL;
1326 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001327 *p_fp = fp;
1328 return fdp;
1329}
1330
Tim Petersd1e87a82001-03-01 18:12:00 +00001331/* case_ok(char* buf, int len, int namelen, char* name)
1332 * The arguments here are tricky, best shown by example:
1333 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1334 * ^ ^ ^ ^
1335 * |--------------------- buf ---------------------|
1336 * |------------------- len ------------------|
1337 * |------ name -------|
1338 * |----- namelen -----|
1339 * buf is the full path, but len only counts up to (& exclusive of) the
1340 * extension. name is the module name, also exclusive of extension.
1341 *
1342 * We've already done a successful stat() or fopen() on buf, so know that
1343 * there's some match, possibly case-insensitive.
1344 *
Tim Peters50d8d372001-02-28 05:34:27 +00001345 * case_ok() is to return 1 if there's a case-sensitive match for
1346 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1347 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001348 *
Tim Peters50d8d372001-02-28 05:34:27 +00001349 * case_ok() is used to implement case-sensitive import semantics even
1350 * on platforms with case-insensitive filesystems. It's trivial to implement
1351 * for case-sensitive filesystems. It's pretty much a cross-platform
1352 * nightmare for systems with case-insensitive filesystems.
1353 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001354
Tim Peters50d8d372001-02-28 05:34:27 +00001355/* First we may need a pile of platform-specific header files; the sequence
1356 * of #if's here should match the sequence in the body of case_ok().
1357 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001358#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001359#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001360#ifdef __CYGWIN__
1361#include <sys/cygwin.h>
1362#endif
1363
Tim Peters50d8d372001-02-28 05:34:27 +00001364#elif defined(DJGPP)
1365#include <dir.h>
1366
Tim Petersd1e87a82001-03-01 18:12:00 +00001367#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001368#include <sys/types.h>
1369#include <dirent.h>
1370
Andrew MacIntyred9400542002-02-26 11:41:34 +00001371#elif defined(PYOS_OS2)
1372#define INCL_DOS
1373#define INCL_DOSERRORS
1374#define INCL_NOPMAPI
1375#include <os2.h>
1376
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001377#elif defined(RISCOS)
1378#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001379#endif
1380
Guido van Rossum0980bd91998-02-13 17:18:36 +00001381static int
Tim Peters50d8d372001-02-28 05:34:27 +00001382case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001383{
Tim Peters50d8d372001-02-28 05:34:27 +00001384/* Pick a platform-specific implementation; the sequence of #if's here should
1385 * match the sequence just above.
1386 */
1387
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001388/* MS_WINDOWS || __CYGWIN__ */
1389#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001390 WIN32_FIND_DATA data;
1391 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001392#ifdef __CYGWIN__
1393 char tempbuf[MAX_PATH];
1394#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001395
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001396 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001397 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001398
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001399#ifdef __CYGWIN__
1400 cygwin32_conv_to_win32_path(buf, tempbuf);
1401 h = FindFirstFile(tempbuf, &data);
1402#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001403 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001404#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001405 if (h == INVALID_HANDLE_VALUE) {
1406 PyErr_Format(PyExc_NameError,
1407 "Can't find file for module %.100s\n(filename %.300s)",
1408 name, buf);
1409 return 0;
1410 }
1411 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001412 return strncmp(data.cFileName, name, namelen) == 0;
1413
1414/* DJGPP */
1415#elif defined(DJGPP)
1416 struct ffblk ffblk;
1417 int done;
1418
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001419 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001420 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001421
1422 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1423 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001424 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001425 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001426 name, buf);
1427 return 0;
1428 }
Tim Peters50d8d372001-02-28 05:34:27 +00001429 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001430
Tim Peters677898a2001-03-02 03:28:03 +00001431/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001432#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001433 DIR *dirp;
1434 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001435 char dirname[MAXPATHLEN + 1];
1436 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001437
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001438 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001439 return 1;
1440
Tim Petersd1e87a82001-03-01 18:12:00 +00001441 /* Copy the dir component into dirname; substitute "." if empty */
1442 if (dirlen <= 0) {
1443 dirname[0] = '.';
1444 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001445 }
1446 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001447 assert(dirlen <= MAXPATHLEN);
1448 memcpy(dirname, buf, dirlen);
1449 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001450 }
1451 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001452 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001453 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001454 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001455 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001456 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001457#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001458 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001459#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001460 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001461#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001462 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001463 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001464 (void)closedir(dirp);
1465 return 1; /* Found */
1466 }
1467 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001468 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001469 }
Tim Peters430f5d42001-03-01 01:30:56 +00001470 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001471
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001472/* RISC OS */
1473#elif defined(RISCOS)
1474 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1475 char buf2[MAXPATHLEN+2];
1476 char *nameWithExt = buf+len-namelen;
1477 int canonlen;
1478 os_error *e;
1479
1480 if (Py_GETENV("PYTHONCASEOK") != NULL)
1481 return 1;
1482
1483 /* workaround:
1484 append wildcard, otherwise case of filename wouldn't be touched */
1485 strcpy(buf2, buf);
1486 strcat(buf2, "*");
1487
1488 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1489 canonlen = MAXPATHLEN+1-canonlen;
1490 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1491 return 0;
1492 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1493 return 1; /* match */
1494
1495 return 0;
1496
Andrew MacIntyred9400542002-02-26 11:41:34 +00001497/* OS/2 */
1498#elif defined(PYOS_OS2)
1499 HDIR hdir = 1;
1500 ULONG srchcnt = 1;
1501 FILEFINDBUF3 ffbuf;
1502 APIRET rc;
1503
1504 if (getenv("PYTHONCASEOK") != NULL)
1505 return 1;
1506
1507 rc = DosFindFirst(buf,
1508 &hdir,
1509 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1510 &ffbuf, sizeof(ffbuf),
1511 &srchcnt,
1512 FIL_STANDARD);
1513 if (rc != NO_ERROR)
1514 return 0;
1515 return strncmp(ffbuf.achName, name, namelen) == 0;
1516
Tim Peters50d8d372001-02-28 05:34:27 +00001517/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1518#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001519 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001520
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001521#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001522}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001523
Guido van Rossum0980bd91998-02-13 17:18:36 +00001524
Guido van Rossum197346f1997-10-31 18:38:52 +00001525#ifdef HAVE_STAT
1526/* Helper to look for __init__.py or __init__.py[co] in potential package */
1527static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001528find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001529{
Tim Peters0f9431f2001-07-05 03:47:53 +00001530 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001531 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001532 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001533 struct stat statbuf;
1534
Tim Peters0f9431f2001-07-05 03:47:53 +00001535/* For calling case_ok(buf, len, namelen, name):
1536 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1537 * ^ ^ ^ ^
1538 * |--------------------- buf ---------------------|
1539 * |------------------- len ------------------|
1540 * |------ name -------|
1541 * |----- namelen -----|
1542 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001543 if (save_len + 13 >= MAXPATHLEN)
1544 return 0;
1545 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001546 pname = buf + i;
1547 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001548 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001549 if (case_ok(buf,
1550 save_len + 9, /* len("/__init__") */
1551 8, /* len("__init__") */
1552 pname)) {
1553 buf[save_len] = '\0';
1554 return 1;
1555 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001556 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001557 i += strlen(pname);
1558 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001559 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001560 if (case_ok(buf,
1561 save_len + 9, /* len("/__init__") */
1562 8, /* len("__init__") */
1563 pname)) {
1564 buf[save_len] = '\0';
1565 return 1;
1566 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001567 }
1568 buf[save_len] = '\0';
1569 return 0;
1570}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001571
1572#else
1573
1574#ifdef RISCOS
1575static int
1576find_init_module(buf)
1577 char *buf;
1578{
1579 int save_len = strlen(buf);
1580 int i = save_len;
1581
1582 if (save_len + 13 >= MAXPATHLEN)
1583 return 0;
1584 buf[i++] = SEP;
1585 strcpy(buf+i, "__init__/py");
1586 if (isfile(buf)) {
1587 buf[save_len] = '\0';
1588 return 1;
1589 }
1590
1591 if (Py_OptimizeFlag)
1592 strcpy(buf+i, "o");
1593 else
1594 strcpy(buf+i, "c");
1595 if (isfile(buf)) {
1596 buf[save_len] = '\0';
1597 return 1;
1598 }
1599 buf[save_len] = '\0';
1600 return 0;
1601}
1602#endif /*RISCOS*/
1603
Guido van Rossum197346f1997-10-31 18:38:52 +00001604#endif /* HAVE_STAT */
1605
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001606
Tim Petersdbd9ba62000-07-09 03:09:57 +00001607static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001608
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001609/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001610 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001611
Guido van Rossum79f25d91997-04-29 20:08:16 +00001612static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001613load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001614{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001615 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001617 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001618
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001619 /* First check that there's an open file (if we need one) */
1620 switch (type) {
1621 case PY_SOURCE:
1622 case PY_COMPILED:
1623 if (fp == NULL) {
1624 PyErr_Format(PyExc_ValueError,
1625 "file object required for import (type code %d)",
1626 type);
1627 return NULL;
1628 }
1629 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001630
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001631 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001632
1633 case PY_SOURCE:
1634 m = load_source_module(name, buf, fp);
1635 break;
1636
1637 case PY_COMPILED:
1638 m = load_compiled_module(name, buf, fp);
1639 break;
1640
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001641#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001642 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001643 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001644 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001645#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001647 case PKG_DIRECTORY:
1648 m = load_package(name, buf);
1649 break;
1650
1651 case C_BUILTIN:
1652 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001653 if (buf != NULL && buf[0] != '\0')
1654 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001655 if (type == C_BUILTIN)
1656 err = init_builtin(name);
1657 else
1658 err = PyImport_ImportFrozenModule(name);
1659 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001660 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001661 if (err == 0) {
1662 PyErr_Format(PyExc_ImportError,
1663 "Purported %s module %.200s not found",
1664 type == C_BUILTIN ?
1665 "builtin" : "frozen",
1666 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001667 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001668 }
1669 modules = PyImport_GetModuleDict();
1670 m = PyDict_GetItemString(modules, name);
1671 if (m == NULL) {
1672 PyErr_Format(
1673 PyExc_ImportError,
1674 "%s module %.200s not properly initialized",
1675 type == C_BUILTIN ?
1676 "builtin" : "frozen",
1677 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001678 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001679 }
1680 Py_INCREF(m);
1681 break;
1682
Just van Rossum52e14d62002-12-30 22:08:05 +00001683 case IMP_HOOK: {
1684 if (loader == NULL) {
1685 PyErr_SetString(PyExc_ImportError,
1686 "import hook without loader");
1687 return NULL;
1688 }
1689 m = PyObject_CallMethod(loader, "load_module", "s", name);
1690 break;
1691 }
1692
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001693 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001694 PyErr_Format(PyExc_ImportError,
1695 "Don't know how to import %.200s (type code %d)",
1696 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001697 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001698
1699 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001700
1701 return m;
1702}
1703
1704
1705/* Initialize a built-in module.
1706 Return 1 for succes, 0 if the module is not found, and -1 with
1707 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001708
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001709static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001710init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001711{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001712 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001713
Greg Ward201baee2001-10-04 14:52:06 +00001714 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001715 return 1;
1716
Guido van Rossum771c6c81997-10-31 18:37:24 +00001717 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001718 if (strcmp(name, p->name) == 0) {
1719 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001720 PyErr_Format(PyExc_ImportError,
1721 "Cannot re-init internal module %.200s",
1722 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001723 return -1;
1724 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001725 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001726 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001727 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001730 if (_PyImport_FixupExtension(name, name) == NULL)
1731 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001732 return 1;
1733 }
1734 }
1735 return 0;
1736}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001737
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001738
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001739/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001741static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001742find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001743{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001744 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001745
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001747 if (p->name == NULL)
1748 return NULL;
1749 if (strcmp(p->name, name) == 0)
1750 break;
1751 }
1752 return p;
1753}
1754
Guido van Rossum79f25d91997-04-29 20:08:16 +00001755static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001756get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001757{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001758 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001759 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001760
1761 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001762 PyErr_Format(PyExc_ImportError,
1763 "No such frozen object named %.200s",
1764 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001765 return NULL;
1766 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001767 if (p->code == NULL) {
1768 PyErr_Format(PyExc_ImportError,
1769 "Excluded frozen object named %.200s",
1770 name);
1771 return NULL;
1772 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001773 size = p->size;
1774 if (size < 0)
1775 size = -size;
1776 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001777}
1778
1779/* Initialize a frozen module.
1780 Return 1 for succes, 0 if the module is not found, and -1 with
1781 an exception set if the initialization failed.
1782 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001783
1784int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001785PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001786{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001787 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 PyObject *co;
1789 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001790 int ispackage;
1791 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001792
1793 if (p == NULL)
1794 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001795 if (p->code == NULL) {
1796 PyErr_Format(PyExc_ImportError,
1797 "Excluded frozen object named %.200s",
1798 name);
1799 return -1;
1800 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001801 size = p->size;
1802 ispackage = (size < 0);
1803 if (ispackage)
1804 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001806 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001807 name, ispackage ? " package" : "");
1808 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001809 if (co == NULL)
1810 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 if (!PyCode_Check(co)) {
1812 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001813 PyErr_Format(PyExc_TypeError,
1814 "frozen object %.200s is not a code object",
1815 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001816 return -1;
1817 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001818 if (ispackage) {
1819 /* Set __path__ to the package name */
1820 PyObject *d, *s;
1821 int err;
1822 m = PyImport_AddModule(name);
1823 if (m == NULL)
1824 return -1;
1825 d = PyModule_GetDict(m);
1826 s = PyString_InternFromString(name);
1827 if (s == NULL)
1828 return -1;
1829 err = PyDict_SetItemString(d, "__path__", s);
1830 Py_DECREF(s);
1831 if (err != 0)
1832 return err;
1833 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001834 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001836 if (m == NULL)
1837 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001839 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001840}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001841
1842
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001844 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001845
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001847PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001848{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001849 PyObject *pname;
1850 PyObject *result;
1851
1852 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001853 if (pname == NULL)
1854 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001855 result = PyImport_Import(pname);
1856 Py_DECREF(pname);
1857 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001858}
1859
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001860/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001861static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1862static PyObject *load_next(PyObject *mod, PyObject *altmod,
1863 char **p_name, char *buf, int *p_buflen);
1864static int mark_miss(char *name);
1865static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1866 char *buf, int buflen, int recursive);
1867static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001868
1869/* The Magnum Opus of dotted-name import :-) */
1870
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001871static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001872import_module_ex(char *name, PyObject *globals, PyObject *locals,
1873 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001874{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001875 char buf[MAXPATHLEN+1];
1876 int buflen = 0;
1877 PyObject *parent, *head, *next, *tail;
1878
1879 parent = get_parent(globals, buf, &buflen);
1880 if (parent == NULL)
1881 return NULL;
1882
1883 head = load_next(parent, Py_None, &name, buf, &buflen);
1884 if (head == NULL)
1885 return NULL;
1886
1887 tail = head;
1888 Py_INCREF(tail);
1889 while (name) {
1890 next = load_next(tail, tail, &name, buf, &buflen);
1891 Py_DECREF(tail);
1892 if (next == NULL) {
1893 Py_DECREF(head);
1894 return NULL;
1895 }
1896 tail = next;
1897 }
1898
1899 if (fromlist != NULL) {
1900 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1901 fromlist = NULL;
1902 }
1903
1904 if (fromlist == NULL) {
1905 Py_DECREF(tail);
1906 return head;
1907 }
1908
1909 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001910 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001911 Py_DECREF(tail);
1912 return NULL;
1913 }
1914
1915 return tail;
1916}
1917
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001918PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001919PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1920 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001921{
1922 PyObject *result;
1923 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001924 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001925 if (unlock_import() < 0) {
1926 Py_XDECREF(result);
1927 PyErr_SetString(PyExc_RuntimeError,
1928 "not holding the import lock");
1929 return NULL;
1930 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001931 return result;
1932}
1933
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001934static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001935get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001936{
1937 static PyObject *namestr = NULL;
1938 static PyObject *pathstr = NULL;
1939 PyObject *modname, *modpath, *modules, *parent;
1940
1941 if (globals == NULL || !PyDict_Check(globals))
1942 return Py_None;
1943
1944 if (namestr == NULL) {
1945 namestr = PyString_InternFromString("__name__");
1946 if (namestr == NULL)
1947 return NULL;
1948 }
1949 if (pathstr == NULL) {
1950 pathstr = PyString_InternFromString("__path__");
1951 if (pathstr == NULL)
1952 return NULL;
1953 }
1954
1955 *buf = '\0';
1956 *p_buflen = 0;
1957 modname = PyDict_GetItem(globals, namestr);
1958 if (modname == NULL || !PyString_Check(modname))
1959 return Py_None;
1960
1961 modpath = PyDict_GetItem(globals, pathstr);
1962 if (modpath != NULL) {
1963 int len = PyString_GET_SIZE(modname);
1964 if (len > MAXPATHLEN) {
1965 PyErr_SetString(PyExc_ValueError,
1966 "Module name too long");
1967 return NULL;
1968 }
1969 strcpy(buf, PyString_AS_STRING(modname));
1970 *p_buflen = len;
1971 }
1972 else {
1973 char *start = PyString_AS_STRING(modname);
1974 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001975 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001976 if (lastdot == NULL)
1977 return Py_None;
1978 len = lastdot - start;
1979 if (len >= MAXPATHLEN) {
1980 PyErr_SetString(PyExc_ValueError,
1981 "Module name too long");
1982 return NULL;
1983 }
1984 strncpy(buf, start, len);
1985 buf[len] = '\0';
1986 *p_buflen = len;
1987 }
1988
1989 modules = PyImport_GetModuleDict();
1990 parent = PyDict_GetItemString(modules, buf);
1991 if (parent == NULL)
1992 parent = Py_None;
1993 return parent;
1994 /* We expect, but can't guarantee, if parent != None, that:
1995 - parent.__name__ == buf
1996 - parent.__dict__ is globals
1997 If this is violated... Who cares? */
1998}
1999
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002000/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002001static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002002load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2003 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002004{
2005 char *name = *p_name;
2006 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002007 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002008 char *p;
2009 PyObject *result;
2010
2011 if (dot == NULL) {
2012 *p_name = NULL;
2013 len = strlen(name);
2014 }
2015 else {
2016 *p_name = dot+1;
2017 len = dot-name;
2018 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002019 if (len == 0) {
2020 PyErr_SetString(PyExc_ValueError,
2021 "Empty module name");
2022 return NULL;
2023 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002024
2025 p = buf + *p_buflen;
2026 if (p != buf)
2027 *p++ = '.';
2028 if (p+len-buf >= MAXPATHLEN) {
2029 PyErr_SetString(PyExc_ValueError,
2030 "Module name too long");
2031 return NULL;
2032 }
2033 strncpy(p, name, len);
2034 p[len] = '\0';
2035 *p_buflen = p+len-buf;
2036
2037 result = import_submodule(mod, p, buf);
2038 if (result == Py_None && altmod != mod) {
2039 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002040 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002041 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002042 if (result != NULL && result != Py_None) {
2043 if (mark_miss(buf) != 0) {
2044 Py_DECREF(result);
2045 return NULL;
2046 }
2047 strncpy(buf, name, len);
2048 buf[len] = '\0';
2049 *p_buflen = len;
2050 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002051 }
2052 if (result == NULL)
2053 return NULL;
2054
2055 if (result == Py_None) {
2056 Py_DECREF(result);
2057 PyErr_Format(PyExc_ImportError,
2058 "No module named %.200s", name);
2059 return NULL;
2060 }
2061
2062 return result;
2063}
2064
2065static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002066mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002067{
2068 PyObject *modules = PyImport_GetModuleDict();
2069 return PyDict_SetItemString(modules, name, Py_None);
2070}
2071
2072static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002073ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2074 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002075{
2076 int i;
2077
2078 if (!PyObject_HasAttrString(mod, "__path__"))
2079 return 1;
2080
2081 for (i = 0; ; i++) {
2082 PyObject *item = PySequence_GetItem(fromlist, i);
2083 int hasit;
2084 if (item == NULL) {
2085 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2086 PyErr_Clear();
2087 return 1;
2088 }
2089 return 0;
2090 }
2091 if (!PyString_Check(item)) {
2092 PyErr_SetString(PyExc_TypeError,
2093 "Item in ``from list'' not a string");
2094 Py_DECREF(item);
2095 return 0;
2096 }
2097 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002098 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002099 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002100 /* See if the package defines __all__ */
2101 if (recursive)
2102 continue; /* Avoid endless recursion */
2103 all = PyObject_GetAttrString(mod, "__all__");
2104 if (all == NULL)
2105 PyErr_Clear();
2106 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002107 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002108 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002109 if (!ret)
2110 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002111 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002112 continue;
2113 }
2114 hasit = PyObject_HasAttr(mod, item);
2115 if (!hasit) {
2116 char *subname = PyString_AS_STRING(item);
2117 PyObject *submod;
2118 char *p;
2119 if (buflen + strlen(subname) >= MAXPATHLEN) {
2120 PyErr_SetString(PyExc_ValueError,
2121 "Module name too long");
2122 Py_DECREF(item);
2123 return 0;
2124 }
2125 p = buf + buflen;
2126 *p++ = '.';
2127 strcpy(p, subname);
2128 submod = import_submodule(mod, subname, buf);
2129 Py_XDECREF(submod);
2130 if (submod == NULL) {
2131 Py_DECREF(item);
2132 return 0;
2133 }
2134 }
2135 Py_DECREF(item);
2136 }
2137
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002138 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002139}
2140
Neil Schemenauer00b09662003-06-16 21:03:07 +00002141static int
2142add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2143 PyObject *modules)
2144{
2145 if (mod == Py_None)
2146 return 1;
2147 /* Irrespective of the success of this load, make a
2148 reference to it in the parent package module. A copy gets
2149 saved in the modules dictionary under the full name, so get a
2150 reference from there, if need be. (The exception is when the
2151 load failed with a SyntaxError -- then there's no trace in
2152 sys.modules. In that case, of course, do nothing extra.) */
2153 if (submod == NULL) {
2154 submod = PyDict_GetItemString(modules, fullname);
2155 if (submod == NULL)
2156 return 1;
2157 }
2158 if (PyModule_Check(mod)) {
2159 /* We can't use setattr here since it can give a
2160 * spurious warning if the submodule name shadows a
2161 * builtin name */
2162 PyObject *dict = PyModule_GetDict(mod);
2163 if (!dict)
2164 return 0;
2165 if (PyDict_SetItemString(dict, subname, submod) < 0)
2166 return 0;
2167 }
2168 else {
2169 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2170 return 0;
2171 }
2172 return 1;
2173}
2174
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002175static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002176import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002177{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002178 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002179 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002180
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002181 /* Require:
2182 if mod == None: subname == fullname
2183 else: mod.__name__ + "." + subname == fullname
2184 */
2185
Tim Peters50d8d372001-02-28 05:34:27 +00002186 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002187 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002188 }
2189 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002190 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002191 char buf[MAXPATHLEN+1];
2192 struct filedescr *fdp;
2193 FILE *fp = NULL;
2194
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002195 if (mod == Py_None)
2196 path = NULL;
2197 else {
2198 path = PyObject_GetAttrString(mod, "__path__");
2199 if (path == NULL) {
2200 PyErr_Clear();
2201 Py_INCREF(Py_None);
2202 return Py_None;
2203 }
2204 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002205
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002206 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002207 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2208 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002209 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002210 if (fdp == NULL) {
2211 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2212 return NULL;
2213 PyErr_Clear();
2214 Py_INCREF(Py_None);
2215 return Py_None;
2216 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002217 m = load_module(fullname, fp, buf, fdp->type, loader);
2218 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002219 if (fp)
2220 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002221 if (!add_submodule(mod, m, fullname, subname, modules)) {
2222 Py_XDECREF(m);
2223 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002224 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002225 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002226
2227 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002228}
2229
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002230
2231/* Re-import a module of any kind and return its module object, WITH
2232 INCREMENTED REFERENCE COUNT */
2233
Guido van Rossum79f25d91997-04-29 20:08:16 +00002234PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002235PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002236{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002237 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002238 PyObject *path = NULL;
2239 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002240 char buf[MAXPATHLEN+1];
2241 struct filedescr *fdp;
2242 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002243
Guido van Rossum79f25d91997-04-29 20:08:16 +00002244 if (m == NULL || !PyModule_Check(m)) {
2245 PyErr_SetString(PyExc_TypeError,
2246 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002247 return NULL;
2248 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002250 if (name == NULL)
2251 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002252 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002253 PyErr_Format(PyExc_ImportError,
2254 "reload(): module %.200s not in sys.modules",
2255 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002256 return NULL;
2257 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002258 subname = strrchr(name, '.');
2259 if (subname == NULL)
2260 subname = name;
2261 else {
2262 PyObject *parentname, *parent;
2263 parentname = PyString_FromStringAndSize(name, (subname-name));
2264 if (parentname == NULL)
2265 return NULL;
2266 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002267 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002268 if (parent == NULL) {
2269 PyErr_Format(PyExc_ImportError,
2270 "reload(): parent %.200s not in sys.modules",
2271 name);
2272 return NULL;
2273 }
2274 subname++;
2275 path = PyObject_GetAttrString(parent, "__path__");
2276 if (path == NULL)
2277 PyErr_Clear();
2278 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002279 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002280 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002281 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002282 if (fdp == NULL)
2283 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002284 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002285 if (fp)
2286 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002287 return m;
2288}
2289
2290
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002291/* Higher-level import emulator which emulates the "import" statement
2292 more accurately -- it invokes the __import__() function from the
2293 builtins of the current globals. This means that the import is
2294 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002295 environment, e.g. by "rexec".
2296 A dummy list ["__doc__"] is passed as the 4th argument so that
2297 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2298 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002299
2300PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002301PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002302{
2303 static PyObject *silly_list = NULL;
2304 static PyObject *builtins_str = NULL;
2305 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002306 PyObject *globals = NULL;
2307 PyObject *import = NULL;
2308 PyObject *builtins = NULL;
2309 PyObject *r = NULL;
2310
2311 /* Initialize constant string objects */
2312 if (silly_list == NULL) {
2313 import_str = PyString_InternFromString("__import__");
2314 if (import_str == NULL)
2315 return NULL;
2316 builtins_str = PyString_InternFromString("__builtins__");
2317 if (builtins_str == NULL)
2318 return NULL;
2319 silly_list = Py_BuildValue("[s]", "__doc__");
2320 if (silly_list == NULL)
2321 return NULL;
2322 }
2323
2324 /* Get the builtins from current globals */
2325 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002326 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002327 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002328 builtins = PyObject_GetItem(globals, builtins_str);
2329 if (builtins == NULL)
2330 goto err;
2331 }
2332 else {
2333 /* No globals -- use standard builtins, and fake globals */
2334 PyErr_Clear();
2335
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002336 builtins = PyImport_ImportModuleEx("__builtin__",
2337 NULL, NULL, NULL);
2338 if (builtins == NULL)
2339 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002340 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2341 if (globals == NULL)
2342 goto err;
2343 }
2344
2345 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002346 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002347 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002348 if (import == NULL)
2349 PyErr_SetObject(PyExc_KeyError, import_str);
2350 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002351 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002352 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002353 if (import == NULL)
2354 goto err;
2355
2356 /* Call the _import__ function with the proper argument list */
2357 r = PyObject_CallFunction(import, "OOOO",
2358 module_name, globals, globals, silly_list);
2359
2360 err:
2361 Py_XDECREF(globals);
2362 Py_XDECREF(builtins);
2363 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002364
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002365 return r;
2366}
2367
2368
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002369/* Module 'imp' provides Python access to the primitives used for
2370 importing modules.
2371*/
2372
Guido van Rossum79f25d91997-04-29 20:08:16 +00002373static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002374imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002375{
2376 char buf[4];
2377
Guido van Rossum96774c12000-05-01 20:19:08 +00002378 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2379 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2380 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2381 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002382
Guido van Rossum79f25d91997-04-29 20:08:16 +00002383 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002384}
2385
Guido van Rossum79f25d91997-04-29 20:08:16 +00002386static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002387imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002388{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002389 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002390 struct filedescr *fdp;
2391
Guido van Rossum79f25d91997-04-29 20:08:16 +00002392 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002393 if (list == NULL)
2394 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002395 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2396 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002397 fdp->suffix, fdp->mode, fdp->type);
2398 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002399 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002400 return NULL;
2401 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002402 if (PyList_Append(list, item) < 0) {
2403 Py_DECREF(list);
2404 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002405 return NULL;
2406 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002408 }
2409 return list;
2410}
2411
Guido van Rossum79f25d91997-04-29 20:08:16 +00002412static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002413call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002414{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002415 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002416 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417 struct filedescr *fdp;
2418 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002419 FILE *fp = NULL;
2420
2421 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002422 if (path == Py_None)
2423 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002424 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002425 if (fdp == NULL)
2426 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002427 if (fp != NULL) {
2428 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2429 if (fob == NULL) {
2430 fclose(fp);
2431 return NULL;
2432 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002434 else {
2435 fob = Py_None;
2436 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002437 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002438 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002439 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002440 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441 return ret;
2442}
2443
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002445imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002446{
2447 char *name;
2448 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002449 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002450 return NULL;
2451 return call_find_module(name, path);
2452}
2453
2454static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002455imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002456{
2457 char *name;
2458 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002459 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002460 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002461 return NULL;
2462 ret = init_builtin(name);
2463 if (ret < 0)
2464 return NULL;
2465 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002466 Py_INCREF(Py_None);
2467 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002468 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002469 m = PyImport_AddModule(name);
2470 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002471 return m;
2472}
2473
Guido van Rossum79f25d91997-04-29 20:08:16 +00002474static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002475imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002476{
2477 char *name;
2478 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002479 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002480 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002481 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002482 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002483 if (ret < 0)
2484 return NULL;
2485 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002486 Py_INCREF(Py_None);
2487 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002488 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002489 m = PyImport_AddModule(name);
2490 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002491 return m;
2492}
2493
Guido van Rossum79f25d91997-04-29 20:08:16 +00002494static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002495imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002496{
2497 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002498
Guido van Rossum43713e52000-02-29 13:59:29 +00002499 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002500 return NULL;
2501 return get_frozen_object(name);
2502}
2503
Guido van Rossum79f25d91997-04-29 20:08:16 +00002504static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002505imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002506{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002507 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002508 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002509 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002510 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002511}
2512
Guido van Rossum79f25d91997-04-29 20:08:16 +00002513static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002514imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002515{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002517 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002518 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002520 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002521 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002522}
2523
2524static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002525get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002526{
2527 FILE *fp;
2528 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002529 if (mode[0] == 'U')
2530 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531 fp = fopen(pathname, mode);
2532 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002533 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534 }
2535 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002536 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002537 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002538 PyErr_SetString(PyExc_ValueError,
2539 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002540 }
2541 return fp;
2542}
2543
Guido van Rossum79f25d91997-04-29 20:08:16 +00002544static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002545imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002546{
2547 char *name;
2548 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002549 PyObject *fob = NULL;
2550 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002551 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002552 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002553 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002554 return NULL;
2555 fp = get_file(pathname, fob, "rb");
2556 if (fp == NULL)
2557 return NULL;
2558 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002559 if (fob == NULL)
2560 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002561 return m;
2562}
2563
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002564#ifdef HAVE_DYNAMIC_LOADING
2565
Guido van Rossum79f25d91997-04-29 20:08:16 +00002566static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002567imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002568{
2569 char *name;
2570 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571 PyObject *fob = NULL;
2572 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002573 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002574 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002575 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002576 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002577 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002578 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002579 if (fp == NULL)
2580 return NULL;
2581 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002582 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002583 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002584}
2585
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002586#endif /* HAVE_DYNAMIC_LOADING */
2587
Guido van Rossum79f25d91997-04-29 20:08:16 +00002588static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002589imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002590{
2591 char *name;
2592 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002593 PyObject *fob = NULL;
2594 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002596 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002597 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002598 return NULL;
2599 fp = get_file(pathname, fob, "r");
2600 if (fp == NULL)
2601 return NULL;
2602 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002603 if (fob == NULL)
2604 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002605 return m;
2606}
2607
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002609imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002610{
2611 char *name;
2612 PyObject *fob;
2613 char *pathname;
2614 char *suffix; /* Unused */
2615 char *mode;
2616 int type;
2617 FILE *fp;
2618
Guido van Rossum43713e52000-02-29 13:59:29 +00002619 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002620 &name, &fob, &pathname,
2621 &suffix, &mode, &type))
2622 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002623 if (*mode) {
2624 /* Mode must start with 'r' or 'U' and must not contain '+'.
2625 Implicit in this test is the assumption that the mode
2626 may contain other modifiers like 'b' or 't'. */
2627
2628 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002629 PyErr_Format(PyExc_ValueError,
2630 "invalid file open mode %.200s", mode);
2631 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002632 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002633 }
2634 if (fob == Py_None)
2635 fp = NULL;
2636 else {
2637 if (!PyFile_Check(fob)) {
2638 PyErr_SetString(PyExc_ValueError,
2639 "load_module arg#2 should be a file or None");
2640 return NULL;
2641 }
2642 fp = get_file(pathname, fob, mode);
2643 if (fp == NULL)
2644 return NULL;
2645 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002646 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002647}
2648
2649static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002650imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002651{
2652 char *name;
2653 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002654 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002655 return NULL;
2656 return load_package(name, pathname);
2657}
2658
2659static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002660imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002661{
2662 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002663 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002664 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002665 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002666}
2667
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002668/* Doc strings */
2669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002670PyDoc_STRVAR(doc_imp,
2671"This module provides the components needed to build your own\n\
2672__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002674PyDoc_STRVAR(doc_find_module,
2675"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002676Search for a module. If path is omitted or None, search for a\n\
2677built-in, frozen or special module and continue search in sys.path.\n\
2678The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681PyDoc_STRVAR(doc_load_module,
2682"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002683Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002684The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002686PyDoc_STRVAR(doc_get_magic,
2687"get_magic() -> string\n\
2688Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002690PyDoc_STRVAR(doc_get_suffixes,
2691"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002692Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002693that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695PyDoc_STRVAR(doc_new_module,
2696"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002697Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002699
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700PyDoc_STRVAR(doc_lock_held,
2701"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002702Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002704
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002705PyDoc_STRVAR(doc_acquire_lock,
2706"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002707Acquires the interpreter's import lock for the current thread.\n\
2708This lock should be used by import hooks to ensure thread-safety\n\
2709when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002710On platforms without threads, this function does nothing.");
2711
2712PyDoc_STRVAR(doc_release_lock,
2713"release_lock() -> None\n\
2714Release the interpreter's import lock.\n\
2715On platforms without threads, this function does nothing.");
2716
Guido van Rossum79f25d91997-04-29 20:08:16 +00002717static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002718 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2719 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2720 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2721 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2722 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2723 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2724 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2725 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002726 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002727 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2728 {"init_builtin", imp_init_builtin, METH_VARARGS},
2729 {"init_frozen", imp_init_frozen, METH_VARARGS},
2730 {"is_builtin", imp_is_builtin, METH_VARARGS},
2731 {"is_frozen", imp_is_frozen, METH_VARARGS},
2732 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002733#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002734 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002735#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002736 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002737 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002738 {NULL, NULL} /* sentinel */
2739};
2740
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002741static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002742setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002743{
2744 PyObject *v;
2745 int err;
2746
2747 v = PyInt_FromLong((long)value);
2748 err = PyDict_SetItemString(d, name, v);
2749 Py_XDECREF(v);
2750 return err;
2751}
2752
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002753PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002754initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002755{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002756 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002757
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002758 m = Py_InitModule4("imp", imp_methods, doc_imp,
2759 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002760 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002761
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002762 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2763 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2764 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2765 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2766 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2767 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2768 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2769 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002770 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002771 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002772
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002773 failure:
2774 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002775}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002776
2777
Guido van Rossumb18618d2000-05-03 23:44:39 +00002778/* API for embedding applications that want to add their own entries
2779 to the table of built-in modules. This should normally be called
2780 *before* Py_Initialize(). When the table resize fails, -1 is
2781 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002782
2783 After a similar function by Just van Rossum. */
2784
2785int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002786PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002787{
2788 static struct _inittab *our_copy = NULL;
2789 struct _inittab *p;
2790 int i, n;
2791
2792 /* Count the number of entries in both tables */
2793 for (n = 0; newtab[n].name != NULL; n++)
2794 ;
2795 if (n == 0)
2796 return 0; /* Nothing to do */
2797 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2798 ;
2799
2800 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002801 p = our_copy;
2802 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002803 if (p == NULL)
2804 return -1;
2805
2806 /* Copy the tables into the new memory */
2807 if (our_copy != PyImport_Inittab)
2808 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2809 PyImport_Inittab = our_copy = p;
2810 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2811
2812 return 0;
2813}
2814
2815/* Shorthand to add a single entry given a name and a function */
2816
2817int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002818PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002819{
2820 struct _inittab newtab[2];
2821
2822 memset(newtab, '\0', sizeof newtab);
2823
2824 newtab[0].name = name;
2825 newtab[0].initfunc = initfunc;
2826
2827 return PyImport_ExtendInittab(newtab);
2828}