blob: 5143a600eb74aacfbfbb88f77af4a286055493f0 [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
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000022/* Magic word to reject .pyc files generated by other Python versions.
23 It should change for each incompatible change to the bytecode.
24
25 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000026 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000027 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000028
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000029 Apparently, there was a distinction made between even and odd
30 bytecodes that is related to Unicode. The details aren't clear,
31 but the magic number has been odd for a long time.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000032
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000033 There were a variety of old schemes for setting the magic number.
34 The current working scheme is to increment the previous value by
35 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000036
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037 Known values:
38 Python 1.5: 20121
39 Python 1.5.1: 20121
40 Python 1.5.2: 20121
41 Python 2.0: 50823
42 Python 2.0.1: 50823
43 Python 2.1: 60202
44 Python 2.1.1: 60202
45 Python 2.1.2: 60202
46 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000047 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000048 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000049 Python 2.3a0: 62011 (!)
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000050 Python 2.4a0: 62031
Tim Peters36515e22001-11-18 04:06:29 +000051*/
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000052#define MAGIC (62031 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000053
Guido van Rossum96774c12000-05-01 20:19:08 +000054/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000055 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000056 compiler works which are enabled by command line switches. */
57static long pyc_magic = MAGIC;
58
Guido van Rossum25ce5661997-08-02 03:10:38 +000059/* See _PyImport_FixupExtension() below */
60static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
Guido van Rossum771c6c81997-10-31 18:37:24 +000062/* This table is defined in config.c: */
63extern struct _inittab _PyImport_Inittab[];
64
65struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000066
Guido van Rossumed1170e1999-12-20 21:23:41 +000067/* these tables define the module suffixes that Python recognizes */
68struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000069
70#ifdef RISCOS
71static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000072 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000073 {"/pyc", "rb", PY_COMPILED},
74 {0, 0}
75};
76#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000077static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000078 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000079#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000080 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000081#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000082 {".pyc", "rb", PY_COMPILED},
83 {0, 0}
84};
Guido van Rossum48a680c2001-03-02 06:34:14 +000085#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000086
Guido van Rossum1ae940a1995-01-02 19:04:15 +000087/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088
89void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000090_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091{
Guido van Rossumed1170e1999-12-20 21:23:41 +000092 const struct filedescr *scan;
93 struct filedescr *filetab;
94 int countD = 0;
95 int countS = 0;
96
97 /* prepare _PyImport_Filetab: copy entries from
98 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
99 */
100 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
101 ++countD;
102 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
103 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000104 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000105 memcpy(filetab, _PyImport_DynLoadFiletab,
106 countD * sizeof(struct filedescr));
107 memcpy(filetab + countD, _PyImport_StandardFiletab,
108 countS * sizeof(struct filedescr));
109 filetab[countD + countS].suffix = NULL;
110
111 _PyImport_Filetab = filetab;
112
Guido van Rossum0824f631997-03-11 18:37:35 +0000113 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
115 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000116#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000117 if (strcmp(filetab->suffix, ".pyc") == 0)
118 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000119#else
120 if (strcmp(filetab->suffix, "/pyc") == 0)
121 filetab->suffix = "/pyo";
122#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000123 }
124 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000125
126 if (Py_UnicodeFlag) {
127 /* Fix the pyc_magic so that byte compiled code created
128 using the all-Unicode method doesn't interfere with
129 code created in normal operation mode. */
130 pyc_magic = MAGIC + 1;
131 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Guido van Rossum25ce5661997-08-02 03:10:38 +0000134void
Just van Rossum52e14d62002-12-30 22:08:05 +0000135_PyImportHooks_Init(void)
136{
137 PyObject *v, *path_hooks = NULL, *zimpimport;
138 int err = 0;
139
140 /* adding sys.path_hooks and sys.path_importer_cache, setting up
141 zipimport */
142
143 if (Py_VerboseFlag)
144 PySys_WriteStderr("# installing zipimport hook\n");
145
146 v = PyList_New(0);
147 if (v == NULL)
148 goto error;
149 err = PySys_SetObject("meta_path", v);
150 Py_DECREF(v);
151 if (err)
152 goto error;
153 v = PyDict_New();
154 if (v == NULL)
155 goto error;
156 err = PySys_SetObject("path_importer_cache", v);
157 Py_DECREF(v);
158 if (err)
159 goto error;
160 path_hooks = PyList_New(0);
161 if (path_hooks == NULL)
162 goto error;
163 err = PySys_SetObject("path_hooks", path_hooks);
164 if (err) {
165 error:
166 PyErr_Print();
167 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
168 "path_importer_cache failed");
169 }
170 zimpimport = PyImport_ImportModule("zipimport");
171 if (zimpimport == NULL) {
172 PyErr_Clear(); /* No zip import module -- okay */
173 if (Py_VerboseFlag)
174 PySys_WriteStderr("# can't import zipimport\n");
175 }
176 else {
177 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
178 "zipimporter");
179 Py_DECREF(zimpimport);
180 if (zipimporter == NULL) {
181 PyErr_Clear(); /* No zipimporter object -- okay */
182 if (Py_VerboseFlag)
183 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000184 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000185 }
186 else {
187 /* sys.path_hooks.append(zipimporter) */
188 err = PyList_Append(path_hooks, zipimporter);
189 Py_DECREF(zipimporter);
190 if (err)
191 goto error;
192 if (Py_VerboseFlag)
193 PySys_WriteStderr(
194 "# installed zipimport hook\n");
195 }
196 }
197 Py_DECREF(path_hooks);
198}
199
200void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202{
203 Py_XDECREF(extensions);
204 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000205 PyMem_DEL(_PyImport_Filetab);
206 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000207}
208
209
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000210/* Locking primitives to prevent parallel imports of the same module
211 in different threads to return with a partially loaded module.
212 These calls are serialized by the global interpreter lock. */
213
214#ifdef WITH_THREAD
215
Guido van Rossum49b56061998-10-01 20:42:43 +0000216#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000217
Guido van Rossum65d5b571998-12-21 19:32:43 +0000218static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000219static long import_lock_thread = -1;
220static int import_lock_level = 0;
221
222static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000224{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000225 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000226 if (me == -1)
227 return; /* Too bad */
228 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000230 if (import_lock_thread == me) {
231 import_lock_level++;
232 return;
233 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000234 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
235 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000236 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000238 PyEval_RestoreThread(tstate);
239 }
240 import_lock_thread = me;
241 import_lock_level = 1;
242}
243
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000244static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000246{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000248 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000249 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000251 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252 import_lock_level--;
253 if (import_lock_level == 0) {
254 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000257 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258}
259
260#else
261
262#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000263#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000264
265#endif
266
Tim Peters69232342001-08-30 05:16:13 +0000267static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000268imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000269{
Tim Peters69232342001-08-30 05:16:13 +0000270#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000271 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000272#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000273 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000274#endif
275}
276
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000277static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000278imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000280#ifdef WITH_THREAD
281 lock_import();
282#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000283 Py_INCREF(Py_None);
284 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000285}
286
287static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000288imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000289{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000290#ifdef WITH_THREAD
291 if (unlock_import() < 0) {
292 PyErr_SetString(PyExc_RuntimeError,
293 "not holding the import lock");
294 return NULL;
295 }
296#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000297 Py_INCREF(Py_None);
298 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000299}
300
Guido van Rossum25ce5661997-08-02 03:10:38 +0000301/* Helper for sys */
302
303PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000306 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307 if (interp->modules == NULL)
308 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
309 return interp->modules;
310}
311
Guido van Rossum3f5da241990-12-20 15:06:42 +0000312
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000313/* List of names to clear in sys */
314static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000315 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000316 "exc_type", "exc_value", "exc_traceback",
317 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000318 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000319 NULL
320};
321
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000322static char* sys_files[] = {
323 "stdin", "__stdin__",
324 "stdout", "__stdout__",
325 "stderr", "__stderr__",
326 NULL
327};
328
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000329
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000330/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334{
Guido van Rossum758eec01998-01-19 21:58:26 +0000335 int pos, ndone;
336 char *name;
337 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000338 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000339 PyObject *modules = interp->modules;
340
341 if (modules == NULL)
342 return; /* Already done */
343
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000344 /* Delete some special variables first. These are common
345 places where user values hide and people complain when their
346 destructors fail. Since the modules containing them are
347 deleted *last* of all, they would come too late in the normal
348 destruction order. Sigh. */
349
350 value = PyDict_GetItemString(modules, "__builtin__");
351 if (value != NULL && PyModule_Check(value)) {
352 dict = PyModule_GetDict(value);
353 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000354 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000355 PyDict_SetItemString(dict, "_", Py_None);
356 }
357 value = PyDict_GetItemString(modules, "sys");
358 if (value != NULL && PyModule_Check(value)) {
359 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000360 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000361 dict = PyModule_GetDict(value);
362 for (p = sys_deletes; *p != NULL; p++) {
363 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000364 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000365 PyDict_SetItemString(dict, *p, Py_None);
366 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000367 for (p = sys_files; *p != NULL; p+=2) {
368 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000369 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000370 v = PyDict_GetItemString(dict, *(p+1));
371 if (v == NULL)
372 v = Py_None;
373 PyDict_SetItemString(dict, *p, v);
374 }
375 }
376
377 /* First, delete __main__ */
378 value = PyDict_GetItemString(modules, "__main__");
379 if (value != NULL && PyModule_Check(value)) {
380 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000381 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000382 _PyModule_Clear(value);
383 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000384 }
385
Guido van Rossum758eec01998-01-19 21:58:26 +0000386 /* The special treatment of __builtin__ here is because even
387 when it's not referenced as a module, its dictionary is
388 referenced by almost every module's __builtins__. Since
389 deleting a module clears its dictionary (even if there are
390 references left to it), we need to delete the __builtin__
391 module last. Likewise, we don't delete sys until the very
392 end because it is implicitly referenced (e.g. by print).
393
394 Also note that we 'delete' modules by replacing their entry
395 in the modules dict with None, rather than really deleting
396 them; this avoids a rehash of the modules dictionary and
397 also marks them as "non existent" so they won't be
398 re-imported. */
399
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000400 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000401 one (skipping __builtin__ and sys) and delete them */
402 do {
403 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000405 while (PyDict_Next(modules, &pos, &key, &value)) {
406 if (value->ob_refcnt != 1)
407 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000408 if (PyString_Check(key) && PyModule_Check(value)) {
409 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000410 if (strcmp(name, "__builtin__") == 0)
411 continue;
412 if (strcmp(name, "sys") == 0)
413 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000414 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000415 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000416 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000417 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000418 PyDict_SetItem(modules, key, Py_None);
419 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000420 }
421 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000422 } while (ndone > 0);
423
Guido van Rossum758eec01998-01-19 21:58:26 +0000424 /* Next, delete all modules (still skipping __builtin__ and sys) */
425 pos = 0;
426 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000427 if (PyString_Check(key) && PyModule_Check(value)) {
428 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000429 if (strcmp(name, "__builtin__") == 0)
430 continue;
431 if (strcmp(name, "sys") == 0)
432 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000433 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000434 PySys_WriteStderr("# cleanup[2] %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 }
438 }
439
440 /* Next, delete sys and __builtin__ (in that order) */
441 value = PyDict_GetItemString(modules, "sys");
442 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000443 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000444 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000445 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000446 PyDict_SetItemString(modules, "sys", Py_None);
447 }
448 value = PyDict_GetItemString(modules, "__builtin__");
449 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000450 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000451 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000452 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000453 PyDict_SetItemString(modules, "__builtin__", Py_None);
454 }
455
456 /* Finally, clear and delete the modules directory */
457 PyDict_Clear(modules);
458 interp->modules = NULL;
459 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000460}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000461
462
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463/* Helper for pythonrun.c -- return magic number */
464
465long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000466PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000467{
Guido van Rossum96774c12000-05-01 20:19:08 +0000468 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469}
470
471
Guido van Rossum25ce5661997-08-02 03:10:38 +0000472/* Magic for extension modules (built-in as well as dynamically
473 loaded). To prevent initializing an extension module more than
474 once, we keep a static dictionary 'extensions' keyed by module name
475 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000476 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000477 dictionary is stored by calling _PyImport_FixupExtension()
478 immediately after the module initialization function succeeds. A
479 copy can be retrieved from there by calling
480 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481
Guido van Rossum79f25d91997-04-29 20:08:16 +0000482PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000483_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000485 PyObject *modules, *mod, *dict, *copy;
486 if (extensions == NULL) {
487 extensions = PyDict_New();
488 if (extensions == NULL)
489 return NULL;
490 }
491 modules = PyImport_GetModuleDict();
492 mod = PyDict_GetItemString(modules, name);
493 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000494 PyErr_Format(PyExc_SystemError,
495 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000496 return NULL;
497 }
498 dict = PyModule_GetDict(mod);
499 if (dict == NULL)
500 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000501 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000502 if (copy == NULL)
503 return NULL;
504 PyDict_SetItemString(extensions, filename, copy);
505 Py_DECREF(copy);
506 return copy;
507}
508
509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000511{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000512 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000513 if (extensions == NULL)
514 return NULL;
515 dict = PyDict_GetItemString(extensions, filename);
516 if (dict == NULL)
517 return NULL;
518 mod = PyImport_AddModule(name);
519 if (mod == NULL)
520 return NULL;
521 mdict = PyModule_GetDict(mod);
522 if (mdict == NULL)
523 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000524 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000525 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000527 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 name, filename);
529 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000530}
531
532
533/* Get the module object corresponding to a module name.
534 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000535 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000536 Because the former action is most common, THIS DOES NOT RETURN A
537 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538
Guido van Rossum79f25d91997-04-29 20:08:16 +0000539PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000540PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544
Guido van Rossum25ce5661997-08-02 03:10:38 +0000545 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 if (m == NULL)
550 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000551 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553 return NULL;
554 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556
557 return m;
558}
559
560
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000561/* Execute a code object in a module and return the module object
562 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000567 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
568}
569
570PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000571PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000572{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000573 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000574 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000575
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577 if (m == NULL)
578 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000579 /* If the module is being reloaded, we get the old module back
580 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 d = PyModule_GetDict(m);
582 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
583 if (PyDict_SetItemString(d, "__builtins__",
584 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000585 return NULL;
586 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000587 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000588 v = NULL;
589 if (pathname != NULL) {
590 v = PyString_FromString(pathname);
591 if (v == NULL)
592 PyErr_Clear();
593 }
594 if (v == NULL) {
595 v = ((PyCodeObject *)co)->co_filename;
596 Py_INCREF(v);
597 }
598 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000600 Py_DECREF(v);
601
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000602 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603 if (v == NULL)
604 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000606
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000608 PyErr_Format(PyExc_ImportError,
609 "Loaded module %.200s not found in sys.modules",
610 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000611 return NULL;
612 }
613
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615
616 return m;
617}
618
619
620/* Given a pathname for a Python source file, fill a buffer with the
621 pathname for the corresponding compiled file. Return the pathname
622 for the compiled file, or NULL if there's no space in the buffer.
623 Doesn't set an exception. */
624
625static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000626make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627{
Tim Petersc1731372001-08-04 08:12:36 +0000628 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000629 if (len+2 > buflen)
630 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000631
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000632#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000633 /* Treat .pyw as if it were .py. The case of ".pyw" must match
634 that used in _PyImport_StandardFiletab. */
635 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
636 --len; /* pretend 'w' isn't there */
637#endif
638 memcpy(buf, pathname, len);
639 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
640 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641
642 return buf;
643}
644
645
646/* Given a pathname for a Python source file, its time of last
647 modification, and a pathname for a compiled file, check whether the
648 compiled file represents the same version of the source. If so,
649 return a FILE pointer for the compiled file, positioned just after
650 the header; if not, return NULL.
651 Doesn't set an exception. */
652
653static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655{
656 FILE *fp;
657 long magic;
658 long pyc_mtime;
659
660 fp = fopen(cpathname, "rb");
661 if (fp == NULL)
662 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000663 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000664 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000666 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667 fclose(fp);
668 return NULL;
669 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000671 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000673 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 fclose(fp);
675 return NULL;
676 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000678 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000679 return fp;
680}
681
682
683/* Read a code object from a file and check it for validity */
684
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000686read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000688 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689
Tim Petersd9b9ac82001-01-28 00:27:39 +0000690 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000691 if (co == NULL)
692 return NULL;
693 if (!PyCode_Check(co)) {
694 PyErr_Format(PyExc_ImportError,
695 "Non-code object in %.200s", cpathname);
696 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000697 return NULL;
698 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700}
701
702
703/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000704 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000707load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708{
709 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 PyCodeObject *co;
711 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000714 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000715 PyErr_Format(PyExc_ImportError,
716 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717 return NULL;
718 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000720 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721 if (co == NULL)
722 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000723 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000724 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000726 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728
729 return m;
730}
731
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000732/* Parse a source file and return the corresponding code object */
733
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000735parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 node *n;
739
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000740 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 if (n == NULL)
742 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 co = PyNode_Compile(n, pathname);
744 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745
746 return co;
747}
748
749
Guido van Rossum55a83382000-09-20 20:31:38 +0000750/* Helper to open a bytecode file for writing in exclusive mode */
751
752static FILE *
753open_exclusive(char *filename)
754{
755#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
756 /* Use O_EXCL to avoid a race condition when another process tries to
757 write the same file. When that happens, our open() call fails,
758 which is just fine (since it's only a cache).
759 XXX If the file exists and is writable but the directory is not
760 writable, the file will never be written. Oh well.
761 */
762 int fd;
763 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000764 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
765#ifdef O_BINARY
766 |O_BINARY /* necessary for Windows */
767#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000768#ifdef __VMS
769 , 0666, "ctxt=bin", "shr=nil");
770#else
771 , 0666);
772#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000773 if (fd < 0)
774 return NULL;
775 return fdopen(fd, "wb");
776#else
777 /* Best we can do -- on Windows this can't happen anyway */
778 return fopen(filename, "wb");
779#endif
780}
781
782
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783/* Write a compiled module to a file, placing the time of last
784 modification of its source into the header.
785 Errors are ignored, if a write error occurs an attempt is made to
786 remove the file. */
787
788static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000789write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000790{
791 FILE *fp;
792
Guido van Rossum55a83382000-09-20 20:31:38 +0000793 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000794 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000796 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 "# can't create %s\n", cpathname);
798 return;
799 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000800 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 PyMarshal_WriteLongToFile(0L, fp);
803 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000804 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000806 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000807 /* Don't keep partial file */
808 fclose(fp);
809 (void) unlink(cpathname);
810 return;
811 }
812 /* Now write the true mtime */
813 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815 fflush(fp);
816 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000818 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819}
820
821
822/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000823 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
824 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000825
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000827load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828{
Fred Drake4c82b232000-06-30 16:18:57 +0000829 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000830 FILE *fpc;
831 char buf[MAXPATHLEN+1];
832 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 PyCodeObject *co;
834 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000836 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000837 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000838 return NULL;
839#if SIZEOF_TIME_T > 4
840 /* Python's .pyc timestamp handling presumes that the timestamp fits
841 in 4 bytes. This will be fine until sometime in the year 2038,
842 when a 4-byte signed time_t will overflow.
843 */
844 if (mtime >> 32) {
845 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000846 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000847 return NULL;
848 }
849#endif
Tim Peters36515e22001-11-18 04:06:29 +0000850 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000851 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852 if (cpathname != NULL &&
853 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000854 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855 fclose(fpc);
856 if (co == NULL)
857 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000859 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000861 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862 }
863 else {
864 co = parse_source_module(pathname, fp);
865 if (co == NULL)
866 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000868 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869 name, pathname);
870 write_compiled_module(co, cpathname, mtime);
871 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000872 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000873 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000874
875 return m;
876}
877
878
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000879/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000880static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
881static struct filedescr *find_module(char *, char *, PyObject *,
882 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000883static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000884
885/* Load a package and return its module object WITH INCREMENTED
886 REFERENCE COUNT */
887
888static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000889load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000890{
891 PyObject *m, *d, *file, *path;
892 int err;
893 char buf[MAXPATHLEN+1];
894 FILE *fp = NULL;
895 struct filedescr *fdp;
896
897 m = PyImport_AddModule(name);
898 if (m == NULL)
899 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000900 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000901 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000902 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000903 d = PyModule_GetDict(m);
904 file = PyString_FromString(pathname);
905 if (file == NULL)
906 return NULL;
907 path = Py_BuildValue("[O]", file);
908 if (path == NULL) {
909 Py_DECREF(file);
910 return NULL;
911 }
912 err = PyDict_SetItemString(d, "__file__", file);
913 if (err == 0)
914 err = PyDict_SetItemString(d, "__path__", path);
915 if (err != 0) {
916 m = NULL;
917 goto cleanup;
918 }
919 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000920 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000921 if (fdp == NULL) {
922 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
923 PyErr_Clear();
924 }
925 else
926 m = NULL;
927 goto cleanup;
928 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000929 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000930 if (fp != NULL)
931 fclose(fp);
932 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000933 Py_XDECREF(path);
934 Py_XDECREF(file);
935 return m;
936}
937
938
939/* Helper to test for built-in module */
940
941static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000942is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000943{
944 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000945 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
946 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
947 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000948 return -1;
949 else
950 return 1;
951 }
952 }
953 return 0;
954}
955
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956
Just van Rossum52e14d62002-12-30 22:08:05 +0000957/* Return an importer object for a sys.path/pkg.__path__ item 'p',
958 possibly by fetching it from the path_importer_cache dict. If it
959 wasn't yet cached, traverse path_hooks until it a hook is found
960 that can handle the path item. Return None if no hook could;
961 this tells our caller it should fall back to the builtin
962 import mechanism. Cache the result in path_importer_cache.
963 Returns a borrowed reference. */
964
965static PyObject *
966get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
967 PyObject *p)
968{
969 PyObject *importer;
970 int j, nhooks;
971
972 /* These conditions are the caller's responsibility: */
973 assert(PyList_Check(path_hooks));
974 assert(PyDict_Check(path_importer_cache));
975
976 nhooks = PyList_Size(path_hooks);
977 if (nhooks < 0)
978 return NULL; /* Shouldn't happen */
979
980 importer = PyDict_GetItem(path_importer_cache, p);
981 if (importer != NULL)
982 return importer;
983
984 /* set path_importer_cache[p] to None to avoid recursion */
985 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
986 return NULL;
987
988 for (j = 0; j < nhooks; j++) {
989 PyObject *hook = PyList_GetItem(path_hooks, j);
990 if (hook == NULL)
991 return NULL;
992 importer = PyObject_CallFunction(hook, "O", p);
993 if (importer != NULL)
994 break;
995
996 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
997 return NULL;
998 }
999 PyErr_Clear();
1000 }
1001 if (importer == NULL)
1002 importer = Py_None;
1003 else if (importer != Py_None) {
1004 int err = PyDict_SetItem(path_importer_cache, p, importer);
1005 Py_DECREF(importer);
1006 if (err != 0)
1007 return NULL;
1008 }
1009 return importer;
1010}
1011
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012/* Search the path (default sys.path) for a module. Return the
1013 corresponding filedescr struct, and (via return arguments) the
1014 pathname and an open file. Return NULL if the module is not found. */
1015
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001016#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001017extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1018 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001019#endif
1020
Tim Peters50d8d372001-02-28 05:34:27 +00001021static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001022static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001023static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001024
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001025static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001026find_module(char *fullname, char *subname, PyObject *path, char *buf,
1027 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001028{
Fred Drake4c82b232000-06-30 16:18:57 +00001029 int i, npath;
1030 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001031 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001032 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001033 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001034 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001035#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001036 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001037#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001038 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1039 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1040 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001041 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001042#if defined(PYOS_OS2)
1043 size_t saved_len;
1044 size_t saved_namelen;
1045 char *saved_buf = NULL;
1046#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001047 if (p_loader != NULL)
1048 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001049
Just van Rossum52e14d62002-12-30 22:08:05 +00001050 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001051 PyErr_SetString(PyExc_OverflowError,
1052 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001053 return NULL;
1054 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001055 strcpy(name, subname);
1056
1057 /* sys.meta_path import hook */
1058 if (p_loader != NULL) {
1059 PyObject *meta_path;
1060
1061 meta_path = PySys_GetObject("meta_path");
1062 if (meta_path == NULL || !PyList_Check(meta_path)) {
1063 PyErr_SetString(PyExc_ImportError,
1064 "sys.meta_path must be a list of "
1065 "import hooks");
1066 return NULL;
1067 }
1068 Py_INCREF(meta_path); /* zap guard */
1069 npath = PyList_Size(meta_path);
1070 for (i = 0; i < npath; i++) {
1071 PyObject *loader;
1072 PyObject *hook = PyList_GetItem(meta_path, i);
1073 loader = PyObject_CallMethod(hook, "find_module",
1074 "sO", fullname,
1075 path != NULL ?
1076 path : Py_None);
1077 if (loader == NULL) {
1078 Py_DECREF(meta_path);
1079 return NULL; /* true error */
1080 }
1081 if (loader != Py_None) {
1082 /* a loader was found */
1083 *p_loader = loader;
1084 Py_DECREF(meta_path);
1085 return &importhookdescr;
1086 }
1087 Py_DECREF(loader);
1088 }
1089 Py_DECREF(meta_path);
1090 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001091
1092 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001093 /* The only type of submodule allowed inside a "frozen"
1094 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001095 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1096 PyErr_SetString(PyExc_ImportError,
1097 "full frozen module name too long");
1098 return NULL;
1099 }
1100 strcpy(buf, PyString_AsString(path));
1101 strcat(buf, ".");
1102 strcat(buf, name);
1103 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001104 if (find_frozen(name) != NULL) {
1105 strcpy(buf, name);
1106 return &fd_frozen;
1107 }
1108 PyErr_Format(PyExc_ImportError,
1109 "No frozen submodule named %.200s", name);
1110 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001111 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001112 if (path == NULL) {
1113 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001114 strcpy(buf, name);
1115 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001116 }
Greg Ward201baee2001-10-04 14:52:06 +00001117 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001118 strcpy(buf, name);
1119 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001120 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001121
Guido van Rossumac279101996-08-22 23:10:58 +00001122#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001123 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1124 if (fp != NULL) {
1125 *p_fp = fp;
1126 return fdp;
1127 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001128#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001129 path = PySys_GetObject("path");
1130 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 if (path == NULL || !PyList_Check(path)) {
1132 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001133 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001134 return NULL;
1135 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001136
1137 path_hooks = PySys_GetObject("path_hooks");
1138 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1139 PyErr_SetString(PyExc_ImportError,
1140 "sys.path_hooks must be a list of "
1141 "import hooks");
1142 return NULL;
1143 }
1144 path_importer_cache = PySys_GetObject("path_importer_cache");
1145 if (path_importer_cache == NULL ||
1146 !PyDict_Check(path_importer_cache)) {
1147 PyErr_SetString(PyExc_ImportError,
1148 "sys.path_importer_cache must be a dict");
1149 return NULL;
1150 }
1151
Guido van Rossum79f25d91997-04-29 20:08:16 +00001152 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001153 namelen = strlen(name);
1154 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001155 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001157#ifdef Py_USING_UNICODE
1158 if (PyUnicode_Check(v)) {
1159 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1160 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1161 if (copy == NULL)
1162 return NULL;
1163 v = copy;
1164 }
1165 else
1166#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001168 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001169 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001170 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1171 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001172 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001173 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001175 if (strlen(buf) != len) {
1176 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001177 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001178 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001179
1180 /* sys.path_hooks import hook */
1181 if (p_loader != NULL) {
1182 PyObject *importer;
1183
1184 importer = get_path_importer(path_importer_cache,
1185 path_hooks, v);
1186 if (importer == NULL)
1187 return NULL;
1188 /* Note: importer is a borrowed reference */
1189 if (importer != Py_None) {
1190 PyObject *loader;
1191 loader = PyObject_CallMethod(importer,
1192 "find_module",
1193 "s", fullname);
1194 if (loader == NULL)
1195 return NULL; /* error */
1196 if (loader != Py_None) {
1197 /* a loader was found */
1198 *p_loader = loader;
1199 return &importhookdescr;
1200 }
1201 Py_DECREF(loader);
1202 }
1203 /* no hook was successful, use builtin import */
1204 }
1205
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001206 if (len > 0 && buf[len-1] != SEP
1207#ifdef ALTSEP
1208 && buf[len-1] != ALTSEP
1209#endif
1210 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001211 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001212 strcpy(buf+len, name);
1213 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001214
1215 /* Check for package import (buf holds a directory name,
1216 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001217#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001218 if (stat(buf, &statbuf) == 0 && /* it exists */
1219 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1220 find_init_module(buf) && /* it has __init__.py */
1221 case_ok(buf, len, namelen, name)) { /* and case matches */
1222 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001223 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001224 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001225#else
1226 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001227#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001228 if (isdir(buf) &&
1229 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001230 case_ok(buf, len, namelen, name)) {
1231 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001232 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001233 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001234#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001235#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001236#if defined(PYOS_OS2)
1237 /* take a snapshot of the module spec for restoration
1238 * after the 8 character DLL hackery
1239 */
1240 saved_buf = strdup(buf);
1241 saved_len = len;
1242 saved_namelen = namelen;
1243#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001244 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001245#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001246 /* OS/2 limits DLLs to 8 character names (w/o
1247 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001248 * so if the name is longer than that and its a
1249 * dynamically loaded module we're going to try,
1250 * truncate the name before trying
1251 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001252 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001253 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001254 const struct filedescr *scan;
1255 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001256 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001257 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001258 break;
1259 else
1260 scan++;
1261 }
1262 if (scan->suffix != NULL) {
1263 /* yes, so truncate the name */
1264 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001265 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001266 buf[len] = '\0';
1267 }
1268 }
1269#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001270 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001272 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001273 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001274 if (filemode[0] == 'U')
1275 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001276 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001277 if (fp != NULL) {
1278 if (case_ok(buf, len, namelen, name))
1279 break;
1280 else { /* continue search */
1281 fclose(fp);
1282 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001283 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001284 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001285#if defined(PYOS_OS2)
1286 /* restore the saved snapshot */
1287 strcpy(buf, saved_buf);
1288 len = saved_len;
1289 namelen = saved_namelen;
1290#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001291 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001292#if defined(PYOS_OS2)
1293 /* don't need/want the module name snapshot anymore */
1294 if (saved_buf)
1295 {
1296 free(saved_buf);
1297 saved_buf = NULL;
1298 }
1299#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001300 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001301 if (fp != NULL)
1302 break;
1303 }
1304 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001305 PyErr_Format(PyExc_ImportError,
1306 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307 return NULL;
1308 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001309 *p_fp = fp;
1310 return fdp;
1311}
1312
Tim Petersd1e87a82001-03-01 18:12:00 +00001313/* case_ok(char* buf, int len, int namelen, char* name)
1314 * The arguments here are tricky, best shown by example:
1315 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1316 * ^ ^ ^ ^
1317 * |--------------------- buf ---------------------|
1318 * |------------------- len ------------------|
1319 * |------ name -------|
1320 * |----- namelen -----|
1321 * buf is the full path, but len only counts up to (& exclusive of) the
1322 * extension. name is the module name, also exclusive of extension.
1323 *
1324 * We've already done a successful stat() or fopen() on buf, so know that
1325 * there's some match, possibly case-insensitive.
1326 *
Tim Peters50d8d372001-02-28 05:34:27 +00001327 * case_ok() is to return 1 if there's a case-sensitive match for
1328 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1329 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001330 *
Tim Peters50d8d372001-02-28 05:34:27 +00001331 * case_ok() is used to implement case-sensitive import semantics even
1332 * on platforms with case-insensitive filesystems. It's trivial to implement
1333 * for case-sensitive filesystems. It's pretty much a cross-platform
1334 * nightmare for systems with case-insensitive filesystems.
1335 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001336
Tim Peters50d8d372001-02-28 05:34:27 +00001337/* First we may need a pile of platform-specific header files; the sequence
1338 * of #if's here should match the sequence in the body of case_ok().
1339 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001340#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001341#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001342#ifdef __CYGWIN__
1343#include <sys/cygwin.h>
1344#endif
1345
Tim Peters50d8d372001-02-28 05:34:27 +00001346#elif defined(DJGPP)
1347#include <dir.h>
1348
Tim Petersd1e87a82001-03-01 18:12:00 +00001349#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001350#include <sys/types.h>
1351#include <dirent.h>
1352
Andrew MacIntyred9400542002-02-26 11:41:34 +00001353#elif defined(PYOS_OS2)
1354#define INCL_DOS
1355#define INCL_DOSERRORS
1356#define INCL_NOPMAPI
1357#include <os2.h>
1358
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001359#elif defined(RISCOS)
1360#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001361#endif
1362
Guido van Rossum0980bd91998-02-13 17:18:36 +00001363static int
Tim Peters50d8d372001-02-28 05:34:27 +00001364case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001365{
Tim Peters50d8d372001-02-28 05:34:27 +00001366/* Pick a platform-specific implementation; the sequence of #if's here should
1367 * match the sequence just above.
1368 */
1369
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001370/* MS_WINDOWS || __CYGWIN__ */
1371#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001372 WIN32_FIND_DATA data;
1373 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001374#ifdef __CYGWIN__
1375 char tempbuf[MAX_PATH];
1376#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001377
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001378 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001379 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001380
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001381#ifdef __CYGWIN__
1382 cygwin32_conv_to_win32_path(buf, tempbuf);
1383 h = FindFirstFile(tempbuf, &data);
1384#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001385 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001386#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001387 if (h == INVALID_HANDLE_VALUE) {
1388 PyErr_Format(PyExc_NameError,
1389 "Can't find file for module %.100s\n(filename %.300s)",
1390 name, buf);
1391 return 0;
1392 }
1393 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001394 return strncmp(data.cFileName, name, namelen) == 0;
1395
1396/* DJGPP */
1397#elif defined(DJGPP)
1398 struct ffblk ffblk;
1399 int done;
1400
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001401 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001402 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001403
1404 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1405 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001406 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001407 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001408 name, buf);
1409 return 0;
1410 }
Tim Peters50d8d372001-02-28 05:34:27 +00001411 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001412
Tim Peters677898a2001-03-02 03:28:03 +00001413/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001414#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001415 DIR *dirp;
1416 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001417 char dirname[MAXPATHLEN + 1];
1418 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001419
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001420 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001421 return 1;
1422
Tim Petersd1e87a82001-03-01 18:12:00 +00001423 /* Copy the dir component into dirname; substitute "." if empty */
1424 if (dirlen <= 0) {
1425 dirname[0] = '.';
1426 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001427 }
1428 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001429 assert(dirlen <= MAXPATHLEN);
1430 memcpy(dirname, buf, dirlen);
1431 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001432 }
1433 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001434 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001435 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001436 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001437 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001438 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001439#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001440 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001441#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001442 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001443#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001444 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001445 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001446 (void)closedir(dirp);
1447 return 1; /* Found */
1448 }
1449 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001450 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001451 }
Tim Peters430f5d42001-03-01 01:30:56 +00001452 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001453
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001454/* RISC OS */
1455#elif defined(RISCOS)
1456 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1457 char buf2[MAXPATHLEN+2];
1458 char *nameWithExt = buf+len-namelen;
1459 int canonlen;
1460 os_error *e;
1461
1462 if (Py_GETENV("PYTHONCASEOK") != NULL)
1463 return 1;
1464
1465 /* workaround:
1466 append wildcard, otherwise case of filename wouldn't be touched */
1467 strcpy(buf2, buf);
1468 strcat(buf2, "*");
1469
1470 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1471 canonlen = MAXPATHLEN+1-canonlen;
1472 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1473 return 0;
1474 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1475 return 1; /* match */
1476
1477 return 0;
1478
Andrew MacIntyred9400542002-02-26 11:41:34 +00001479/* OS/2 */
1480#elif defined(PYOS_OS2)
1481 HDIR hdir = 1;
1482 ULONG srchcnt = 1;
1483 FILEFINDBUF3 ffbuf;
1484 APIRET rc;
1485
1486 if (getenv("PYTHONCASEOK") != NULL)
1487 return 1;
1488
1489 rc = DosFindFirst(buf,
1490 &hdir,
1491 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1492 &ffbuf, sizeof(ffbuf),
1493 &srchcnt,
1494 FIL_STANDARD);
1495 if (rc != NO_ERROR)
1496 return 0;
1497 return strncmp(ffbuf.achName, name, namelen) == 0;
1498
Tim Peters50d8d372001-02-28 05:34:27 +00001499/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1500#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001501 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001502
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001503#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001504}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001505
Guido van Rossum0980bd91998-02-13 17:18:36 +00001506
Guido van Rossum197346f1997-10-31 18:38:52 +00001507#ifdef HAVE_STAT
1508/* Helper to look for __init__.py or __init__.py[co] in potential package */
1509static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001510find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001511{
Tim Peters0f9431f2001-07-05 03:47:53 +00001512 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001513 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001514 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001515 struct stat statbuf;
1516
Tim Peters0f9431f2001-07-05 03:47:53 +00001517/* For calling case_ok(buf, len, namelen, name):
1518 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1519 * ^ ^ ^ ^
1520 * |--------------------- buf ---------------------|
1521 * |------------------- len ------------------|
1522 * |------ name -------|
1523 * |----- namelen -----|
1524 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001525 if (save_len + 13 >= MAXPATHLEN)
1526 return 0;
1527 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001528 pname = buf + i;
1529 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001530 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001531 if (case_ok(buf,
1532 save_len + 9, /* len("/__init__") */
1533 8, /* len("__init__") */
1534 pname)) {
1535 buf[save_len] = '\0';
1536 return 1;
1537 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001538 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001539 i += strlen(pname);
1540 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001541 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001542 if (case_ok(buf,
1543 save_len + 9, /* len("/__init__") */
1544 8, /* len("__init__") */
1545 pname)) {
1546 buf[save_len] = '\0';
1547 return 1;
1548 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001549 }
1550 buf[save_len] = '\0';
1551 return 0;
1552}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001553
1554#else
1555
1556#ifdef RISCOS
1557static int
1558find_init_module(buf)
1559 char *buf;
1560{
1561 int save_len = strlen(buf);
1562 int i = save_len;
1563
1564 if (save_len + 13 >= MAXPATHLEN)
1565 return 0;
1566 buf[i++] = SEP;
1567 strcpy(buf+i, "__init__/py");
1568 if (isfile(buf)) {
1569 buf[save_len] = '\0';
1570 return 1;
1571 }
1572
1573 if (Py_OptimizeFlag)
1574 strcpy(buf+i, "o");
1575 else
1576 strcpy(buf+i, "c");
1577 if (isfile(buf)) {
1578 buf[save_len] = '\0';
1579 return 1;
1580 }
1581 buf[save_len] = '\0';
1582 return 0;
1583}
1584#endif /*RISCOS*/
1585
Guido van Rossum197346f1997-10-31 18:38:52 +00001586#endif /* HAVE_STAT */
1587
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001588
Tim Petersdbd9ba62000-07-09 03:09:57 +00001589static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001590
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001591/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001592 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001593
Guido van Rossum79f25d91997-04-29 20:08:16 +00001594static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001595load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001596{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001597 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001598 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001599 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001600
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001601 /* First check that there's an open file (if we need one) */
1602 switch (type) {
1603 case PY_SOURCE:
1604 case PY_COMPILED:
1605 if (fp == NULL) {
1606 PyErr_Format(PyExc_ValueError,
1607 "file object required for import (type code %d)",
1608 type);
1609 return NULL;
1610 }
1611 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001612
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001613 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001614
1615 case PY_SOURCE:
1616 m = load_source_module(name, buf, fp);
1617 break;
1618
1619 case PY_COMPILED:
1620 m = load_compiled_module(name, buf, fp);
1621 break;
1622
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001623#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001624 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001626 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001627#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001628
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001629 case PKG_DIRECTORY:
1630 m = load_package(name, buf);
1631 break;
1632
1633 case C_BUILTIN:
1634 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001635 if (buf != NULL && buf[0] != '\0')
1636 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001637 if (type == C_BUILTIN)
1638 err = init_builtin(name);
1639 else
1640 err = PyImport_ImportFrozenModule(name);
1641 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001642 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001643 if (err == 0) {
1644 PyErr_Format(PyExc_ImportError,
1645 "Purported %s module %.200s not found",
1646 type == C_BUILTIN ?
1647 "builtin" : "frozen",
1648 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001649 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001650 }
1651 modules = PyImport_GetModuleDict();
1652 m = PyDict_GetItemString(modules, name);
1653 if (m == NULL) {
1654 PyErr_Format(
1655 PyExc_ImportError,
1656 "%s module %.200s not properly initialized",
1657 type == C_BUILTIN ?
1658 "builtin" : "frozen",
1659 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001660 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001661 }
1662 Py_INCREF(m);
1663 break;
1664
Just van Rossum52e14d62002-12-30 22:08:05 +00001665 case IMP_HOOK: {
1666 if (loader == NULL) {
1667 PyErr_SetString(PyExc_ImportError,
1668 "import hook without loader");
1669 return NULL;
1670 }
1671 m = PyObject_CallMethod(loader, "load_module", "s", name);
1672 break;
1673 }
1674
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001675 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001676 PyErr_Format(PyExc_ImportError,
1677 "Don't know how to import %.200s (type code %d)",
1678 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001679 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680
1681 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001682
1683 return m;
1684}
1685
1686
1687/* Initialize a built-in module.
1688 Return 1 for succes, 0 if the module is not found, and -1 with
1689 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001690
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001691static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001692init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001693{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001694 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001695
Greg Ward201baee2001-10-04 14:52:06 +00001696 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001697 return 1;
1698
Guido van Rossum771c6c81997-10-31 18:37:24 +00001699 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001700 if (strcmp(name, p->name) == 0) {
1701 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001702 PyErr_Format(PyExc_ImportError,
1703 "Cannot re-init internal module %.200s",
1704 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001705 return -1;
1706 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001708 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001709 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001711 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001712 if (_PyImport_FixupExtension(name, name) == NULL)
1713 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001714 return 1;
1715 }
1716 }
1717 return 0;
1718}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001719
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001721/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001723static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001724find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001725{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001726 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001727
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001729 if (p->name == NULL)
1730 return NULL;
1731 if (strcmp(p->name, name) == 0)
1732 break;
1733 }
1734 return p;
1735}
1736
Guido van Rossum79f25d91997-04-29 20:08:16 +00001737static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001738get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001739{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001740 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001741 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001742
1743 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001744 PyErr_Format(PyExc_ImportError,
1745 "No such frozen object named %.200s",
1746 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001747 return NULL;
1748 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001749 if (p->code == NULL) {
1750 PyErr_Format(PyExc_ImportError,
1751 "Excluded frozen object named %.200s",
1752 name);
1753 return NULL;
1754 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001755 size = p->size;
1756 if (size < 0)
1757 size = -size;
1758 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001759}
1760
1761/* Initialize a frozen module.
1762 Return 1 for succes, 0 if the module is not found, and -1 with
1763 an exception set if the initialization failed.
1764 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001765
1766int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001767PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001768{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001769 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 PyObject *co;
1771 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001772 int ispackage;
1773 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001774
1775 if (p == NULL)
1776 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001777 if (p->code == NULL) {
1778 PyErr_Format(PyExc_ImportError,
1779 "Excluded frozen object named %.200s",
1780 name);
1781 return -1;
1782 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001783 size = p->size;
1784 ispackage = (size < 0);
1785 if (ispackage)
1786 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001788 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001789 name, ispackage ? " package" : "");
1790 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001791 if (co == NULL)
1792 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 if (!PyCode_Check(co)) {
1794 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001795 PyErr_Format(PyExc_TypeError,
1796 "frozen object %.200s is not a code object",
1797 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001798 return -1;
1799 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001800 if (ispackage) {
1801 /* Set __path__ to the package name */
1802 PyObject *d, *s;
1803 int err;
1804 m = PyImport_AddModule(name);
1805 if (m == NULL)
1806 return -1;
1807 d = PyModule_GetDict(m);
1808 s = PyString_InternFromString(name);
1809 if (s == NULL)
1810 return -1;
1811 err = PyDict_SetItemString(d, "__path__", s);
1812 Py_DECREF(s);
1813 if (err != 0)
1814 return err;
1815 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001816 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001818 if (m == NULL)
1819 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001821 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001822}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001823
1824
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001825/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001826 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001827
Guido van Rossum79f25d91997-04-29 20:08:16 +00001828PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001829PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001830{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001831 PyObject *pname;
1832 PyObject *result;
1833
1834 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001835 if (pname == NULL)
1836 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001837 result = PyImport_Import(pname);
1838 Py_DECREF(pname);
1839 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001840}
1841
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001842/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001843static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1844static PyObject *load_next(PyObject *mod, PyObject *altmod,
1845 char **p_name, char *buf, int *p_buflen);
1846static int mark_miss(char *name);
1847static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1848 char *buf, int buflen, int recursive);
1849static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001850
1851/* The Magnum Opus of dotted-name import :-) */
1852
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001853static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001854import_module_ex(char *name, PyObject *globals, PyObject *locals,
1855 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001856{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001857 char buf[MAXPATHLEN+1];
1858 int buflen = 0;
1859 PyObject *parent, *head, *next, *tail;
1860
1861 parent = get_parent(globals, buf, &buflen);
1862 if (parent == NULL)
1863 return NULL;
1864
1865 head = load_next(parent, Py_None, &name, buf, &buflen);
1866 if (head == NULL)
1867 return NULL;
1868
1869 tail = head;
1870 Py_INCREF(tail);
1871 while (name) {
1872 next = load_next(tail, tail, &name, buf, &buflen);
1873 Py_DECREF(tail);
1874 if (next == NULL) {
1875 Py_DECREF(head);
1876 return NULL;
1877 }
1878 tail = next;
1879 }
1880
1881 if (fromlist != NULL) {
1882 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1883 fromlist = NULL;
1884 }
1885
1886 if (fromlist == NULL) {
1887 Py_DECREF(tail);
1888 return head;
1889 }
1890
1891 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001892 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001893 Py_DECREF(tail);
1894 return NULL;
1895 }
1896
1897 return tail;
1898}
1899
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001900PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001901PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1902 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001903{
1904 PyObject *result;
1905 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001906 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001907 if (unlock_import() < 0) {
1908 Py_XDECREF(result);
1909 PyErr_SetString(PyExc_RuntimeError,
1910 "not holding the import lock");
1911 return NULL;
1912 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001913 return result;
1914}
1915
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001917get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001918{
1919 static PyObject *namestr = NULL;
1920 static PyObject *pathstr = NULL;
1921 PyObject *modname, *modpath, *modules, *parent;
1922
1923 if (globals == NULL || !PyDict_Check(globals))
1924 return Py_None;
1925
1926 if (namestr == NULL) {
1927 namestr = PyString_InternFromString("__name__");
1928 if (namestr == NULL)
1929 return NULL;
1930 }
1931 if (pathstr == NULL) {
1932 pathstr = PyString_InternFromString("__path__");
1933 if (pathstr == NULL)
1934 return NULL;
1935 }
1936
1937 *buf = '\0';
1938 *p_buflen = 0;
1939 modname = PyDict_GetItem(globals, namestr);
1940 if (modname == NULL || !PyString_Check(modname))
1941 return Py_None;
1942
1943 modpath = PyDict_GetItem(globals, pathstr);
1944 if (modpath != NULL) {
1945 int len = PyString_GET_SIZE(modname);
1946 if (len > MAXPATHLEN) {
1947 PyErr_SetString(PyExc_ValueError,
1948 "Module name too long");
1949 return NULL;
1950 }
1951 strcpy(buf, PyString_AS_STRING(modname));
1952 *p_buflen = len;
1953 }
1954 else {
1955 char *start = PyString_AS_STRING(modname);
1956 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001957 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001958 if (lastdot == NULL)
1959 return Py_None;
1960 len = lastdot - start;
1961 if (len >= MAXPATHLEN) {
1962 PyErr_SetString(PyExc_ValueError,
1963 "Module name too long");
1964 return NULL;
1965 }
1966 strncpy(buf, start, len);
1967 buf[len] = '\0';
1968 *p_buflen = len;
1969 }
1970
1971 modules = PyImport_GetModuleDict();
1972 parent = PyDict_GetItemString(modules, buf);
1973 if (parent == NULL)
1974 parent = Py_None;
1975 return parent;
1976 /* We expect, but can't guarantee, if parent != None, that:
1977 - parent.__name__ == buf
1978 - parent.__dict__ is globals
1979 If this is violated... Who cares? */
1980}
1981
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001982/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001983static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001984load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1985 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001986{
1987 char *name = *p_name;
1988 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001989 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001990 char *p;
1991 PyObject *result;
1992
1993 if (dot == NULL) {
1994 *p_name = NULL;
1995 len = strlen(name);
1996 }
1997 else {
1998 *p_name = dot+1;
1999 len = dot-name;
2000 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002001 if (len == 0) {
2002 PyErr_SetString(PyExc_ValueError,
2003 "Empty module name");
2004 return NULL;
2005 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002006
2007 p = buf + *p_buflen;
2008 if (p != buf)
2009 *p++ = '.';
2010 if (p+len-buf >= MAXPATHLEN) {
2011 PyErr_SetString(PyExc_ValueError,
2012 "Module name too long");
2013 return NULL;
2014 }
2015 strncpy(p, name, len);
2016 p[len] = '\0';
2017 *p_buflen = p+len-buf;
2018
2019 result = import_submodule(mod, p, buf);
2020 if (result == Py_None && altmod != mod) {
2021 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002022 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002023 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002024 if (result != NULL && result != Py_None) {
2025 if (mark_miss(buf) != 0) {
2026 Py_DECREF(result);
2027 return NULL;
2028 }
2029 strncpy(buf, name, len);
2030 buf[len] = '\0';
2031 *p_buflen = len;
2032 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002033 }
2034 if (result == NULL)
2035 return NULL;
2036
2037 if (result == Py_None) {
2038 Py_DECREF(result);
2039 PyErr_Format(PyExc_ImportError,
2040 "No module named %.200s", name);
2041 return NULL;
2042 }
2043
2044 return result;
2045}
2046
2047static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002048mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002049{
2050 PyObject *modules = PyImport_GetModuleDict();
2051 return PyDict_SetItemString(modules, name, Py_None);
2052}
2053
2054static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002055ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2056 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002057{
2058 int i;
2059
2060 if (!PyObject_HasAttrString(mod, "__path__"))
2061 return 1;
2062
2063 for (i = 0; ; i++) {
2064 PyObject *item = PySequence_GetItem(fromlist, i);
2065 int hasit;
2066 if (item == NULL) {
2067 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2068 PyErr_Clear();
2069 return 1;
2070 }
2071 return 0;
2072 }
2073 if (!PyString_Check(item)) {
2074 PyErr_SetString(PyExc_TypeError,
2075 "Item in ``from list'' not a string");
2076 Py_DECREF(item);
2077 return 0;
2078 }
2079 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002080 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002081 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002082 /* See if the package defines __all__ */
2083 if (recursive)
2084 continue; /* Avoid endless recursion */
2085 all = PyObject_GetAttrString(mod, "__all__");
2086 if (all == NULL)
2087 PyErr_Clear();
2088 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002089 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002090 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002091 if (!ret)
2092 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002093 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002094 continue;
2095 }
2096 hasit = PyObject_HasAttr(mod, item);
2097 if (!hasit) {
2098 char *subname = PyString_AS_STRING(item);
2099 PyObject *submod;
2100 char *p;
2101 if (buflen + strlen(subname) >= MAXPATHLEN) {
2102 PyErr_SetString(PyExc_ValueError,
2103 "Module name too long");
2104 Py_DECREF(item);
2105 return 0;
2106 }
2107 p = buf + buflen;
2108 *p++ = '.';
2109 strcpy(p, subname);
2110 submod = import_submodule(mod, subname, buf);
2111 Py_XDECREF(submod);
2112 if (submod == NULL) {
2113 Py_DECREF(item);
2114 return 0;
2115 }
2116 }
2117 Py_DECREF(item);
2118 }
2119
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002120 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002121}
2122
Neil Schemenauer00b09662003-06-16 21:03:07 +00002123static int
2124add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2125 PyObject *modules)
2126{
2127 if (mod == Py_None)
2128 return 1;
2129 /* Irrespective of the success of this load, make a
2130 reference to it in the parent package module. A copy gets
2131 saved in the modules dictionary under the full name, so get a
2132 reference from there, if need be. (The exception is when the
2133 load failed with a SyntaxError -- then there's no trace in
2134 sys.modules. In that case, of course, do nothing extra.) */
2135 if (submod == NULL) {
2136 submod = PyDict_GetItemString(modules, fullname);
2137 if (submod == NULL)
2138 return 1;
2139 }
2140 if (PyModule_Check(mod)) {
2141 /* We can't use setattr here since it can give a
2142 * spurious warning if the submodule name shadows a
2143 * builtin name */
2144 PyObject *dict = PyModule_GetDict(mod);
2145 if (!dict)
2146 return 0;
2147 if (PyDict_SetItemString(dict, subname, submod) < 0)
2148 return 0;
2149 }
2150 else {
2151 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2152 return 0;
2153 }
2154 return 1;
2155}
2156
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002157static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002158import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002159{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002160 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002161 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002162
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002163 /* Require:
2164 if mod == None: subname == fullname
2165 else: mod.__name__ + "." + subname == fullname
2166 */
2167
Tim Peters50d8d372001-02-28 05:34:27 +00002168 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002169 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002170 }
2171 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002172 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002173 char buf[MAXPATHLEN+1];
2174 struct filedescr *fdp;
2175 FILE *fp = NULL;
2176
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002177 if (mod == Py_None)
2178 path = NULL;
2179 else {
2180 path = PyObject_GetAttrString(mod, "__path__");
2181 if (path == NULL) {
2182 PyErr_Clear();
2183 Py_INCREF(Py_None);
2184 return Py_None;
2185 }
2186 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002187
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002188 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002189 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2190 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002191 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002192 if (fdp == NULL) {
2193 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2194 return NULL;
2195 PyErr_Clear();
2196 Py_INCREF(Py_None);
2197 return Py_None;
2198 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002199 m = load_module(fullname, fp, buf, fdp->type, loader);
2200 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002201 if (fp)
2202 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002203 if (!add_submodule(mod, m, fullname, subname, modules)) {
2204 Py_XDECREF(m);
2205 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002206 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002207 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002208
2209 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002210}
2211
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002212
2213/* Re-import a module of any kind and return its module object, WITH
2214 INCREMENTED REFERENCE COUNT */
2215
Guido van Rossum79f25d91997-04-29 20:08:16 +00002216PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002217PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002218{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002219 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002220 PyObject *path = NULL;
2221 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002222 char buf[MAXPATHLEN+1];
2223 struct filedescr *fdp;
2224 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002225
Guido van Rossum79f25d91997-04-29 20:08:16 +00002226 if (m == NULL || !PyModule_Check(m)) {
2227 PyErr_SetString(PyExc_TypeError,
2228 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002229 return NULL;
2230 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002231 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002232 if (name == NULL)
2233 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002234 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002235 PyErr_Format(PyExc_ImportError,
2236 "reload(): module %.200s not in sys.modules",
2237 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002238 return NULL;
2239 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002240 subname = strrchr(name, '.');
2241 if (subname == NULL)
2242 subname = name;
2243 else {
2244 PyObject *parentname, *parent;
2245 parentname = PyString_FromStringAndSize(name, (subname-name));
2246 if (parentname == NULL)
2247 return NULL;
2248 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002249 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002250 if (parent == NULL) {
2251 PyErr_Format(PyExc_ImportError,
2252 "reload(): parent %.200s not in sys.modules",
2253 name);
2254 return NULL;
2255 }
2256 subname++;
2257 path = PyObject_GetAttrString(parent, "__path__");
2258 if (path == NULL)
2259 PyErr_Clear();
2260 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002261 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002262 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002263 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002264 if (fdp == NULL)
2265 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002266 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002267 if (fp)
2268 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002269 return m;
2270}
2271
2272
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002273/* Higher-level import emulator which emulates the "import" statement
2274 more accurately -- it invokes the __import__() function from the
2275 builtins of the current globals. This means that the import is
2276 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002277 environment, e.g. by "rexec".
2278 A dummy list ["__doc__"] is passed as the 4th argument so that
2279 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2280 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002281
2282PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002283PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002284{
2285 static PyObject *silly_list = NULL;
2286 static PyObject *builtins_str = NULL;
2287 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002288 PyObject *globals = NULL;
2289 PyObject *import = NULL;
2290 PyObject *builtins = NULL;
2291 PyObject *r = NULL;
2292
2293 /* Initialize constant string objects */
2294 if (silly_list == NULL) {
2295 import_str = PyString_InternFromString("__import__");
2296 if (import_str == NULL)
2297 return NULL;
2298 builtins_str = PyString_InternFromString("__builtins__");
2299 if (builtins_str == NULL)
2300 return NULL;
2301 silly_list = Py_BuildValue("[s]", "__doc__");
2302 if (silly_list == NULL)
2303 return NULL;
2304 }
2305
2306 /* Get the builtins from current globals */
2307 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002308 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002309 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002310 builtins = PyObject_GetItem(globals, builtins_str);
2311 if (builtins == NULL)
2312 goto err;
2313 }
2314 else {
2315 /* No globals -- use standard builtins, and fake globals */
2316 PyErr_Clear();
2317
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002318 builtins = PyImport_ImportModuleEx("__builtin__",
2319 NULL, NULL, NULL);
2320 if (builtins == NULL)
2321 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002322 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2323 if (globals == NULL)
2324 goto err;
2325 }
2326
2327 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002328 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002329 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002330 if (import == NULL)
2331 PyErr_SetObject(PyExc_KeyError, import_str);
2332 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002333 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002334 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002335 if (import == NULL)
2336 goto err;
2337
2338 /* Call the _import__ function with the proper argument list */
2339 r = PyObject_CallFunction(import, "OOOO",
2340 module_name, globals, globals, silly_list);
2341
2342 err:
2343 Py_XDECREF(globals);
2344 Py_XDECREF(builtins);
2345 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002346
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002347 return r;
2348}
2349
2350
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002351/* Module 'imp' provides Python access to the primitives used for
2352 importing modules.
2353*/
2354
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002356imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002357{
2358 char buf[4];
2359
Guido van Rossum96774c12000-05-01 20:19:08 +00002360 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2361 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2362 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2363 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002364
Guido van Rossum79f25d91997-04-29 20:08:16 +00002365 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002366}
2367
Guido van Rossum79f25d91997-04-29 20:08:16 +00002368static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002369imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002370{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002371 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002372 struct filedescr *fdp;
2373
Guido van Rossum79f25d91997-04-29 20:08:16 +00002374 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002375 if (list == NULL)
2376 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002377 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2378 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002379 fdp->suffix, fdp->mode, fdp->type);
2380 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002381 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002382 return NULL;
2383 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002384 if (PyList_Append(list, item) < 0) {
2385 Py_DECREF(list);
2386 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002387 return NULL;
2388 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002389 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002390 }
2391 return list;
2392}
2393
Guido van Rossum79f25d91997-04-29 20:08:16 +00002394static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002395call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002396{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002397 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002398 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002399 struct filedescr *fdp;
2400 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002401 FILE *fp = NULL;
2402
2403 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002404 if (path == Py_None)
2405 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002406 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407 if (fdp == NULL)
2408 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002409 if (fp != NULL) {
2410 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2411 if (fob == NULL) {
2412 fclose(fp);
2413 return NULL;
2414 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002415 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002416 else {
2417 fob = Py_None;
2418 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002419 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002421 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002422 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002423 return ret;
2424}
2425
Guido van Rossum79f25d91997-04-29 20:08:16 +00002426static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002427imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002428{
2429 char *name;
2430 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002431 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002432 return NULL;
2433 return call_find_module(name, path);
2434}
2435
2436static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002437imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438{
2439 char *name;
2440 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002441 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002442 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002443 return NULL;
2444 ret = init_builtin(name);
2445 if (ret < 0)
2446 return NULL;
2447 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002448 Py_INCREF(Py_None);
2449 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002450 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002451 m = PyImport_AddModule(name);
2452 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002453 return m;
2454}
2455
Guido van Rossum79f25d91997-04-29 20:08:16 +00002456static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002457imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002458{
2459 char *name;
2460 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002461 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002462 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002463 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 if (ret < 0)
2466 return NULL;
2467 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002468 Py_INCREF(Py_None);
2469 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002470 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002471 m = PyImport_AddModule(name);
2472 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002473 return m;
2474}
2475
Guido van Rossum79f25d91997-04-29 20:08:16 +00002476static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002477imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002478{
2479 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002480
Guido van Rossum43713e52000-02-29 13:59:29 +00002481 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002482 return NULL;
2483 return get_frozen_object(name);
2484}
2485
Guido van Rossum79f25d91997-04-29 20:08:16 +00002486static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002487imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002488{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002489 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002490 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002491 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002492 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002493}
2494
Guido van Rossum79f25d91997-04-29 20:08:16 +00002495static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002496imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002497{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002498 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002499 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002500 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002501 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002502 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002503 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002504}
2505
2506static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002507get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002508{
2509 FILE *fp;
2510 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002511 if (mode[0] == 'U')
2512 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002513 fp = fopen(pathname, mode);
2514 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002515 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516 }
2517 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002518 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002520 PyErr_SetString(PyExc_ValueError,
2521 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002522 }
2523 return fp;
2524}
2525
Guido van Rossum79f25d91997-04-29 20:08:16 +00002526static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002527imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002528{
2529 char *name;
2530 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002531 PyObject *fob = NULL;
2532 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002533 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002534 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002535 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002536 return NULL;
2537 fp = get_file(pathname, fob, "rb");
2538 if (fp == NULL)
2539 return NULL;
2540 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002541 if (fob == NULL)
2542 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002543 return m;
2544}
2545
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002546#ifdef HAVE_DYNAMIC_LOADING
2547
Guido van Rossum79f25d91997-04-29 20:08:16 +00002548static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002549imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002550{
2551 char *name;
2552 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002553 PyObject *fob = NULL;
2554 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002555 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002556 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002557 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002559 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002560 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002561 if (fp == NULL)
2562 return NULL;
2563 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002564 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002565 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002566}
2567
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002568#endif /* HAVE_DYNAMIC_LOADING */
2569
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002571imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572{
2573 char *name;
2574 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002575 PyObject *fob = NULL;
2576 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002578 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002579 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002580 return NULL;
2581 fp = get_file(pathname, fob, "r");
2582 if (fp == NULL)
2583 return NULL;
2584 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002585 if (fob == NULL)
2586 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002587 return m;
2588}
2589
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002591imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002592{
2593 char *name;
2594 PyObject *fob;
2595 char *pathname;
2596 char *suffix; /* Unused */
2597 char *mode;
2598 int type;
2599 FILE *fp;
2600
Guido van Rossum43713e52000-02-29 13:59:29 +00002601 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002602 &name, &fob, &pathname,
2603 &suffix, &mode, &type))
2604 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002605 if (*mode) {
2606 /* Mode must start with 'r' or 'U' and must not contain '+'.
2607 Implicit in this test is the assumption that the mode
2608 may contain other modifiers like 'b' or 't'. */
2609
2610 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002611 PyErr_Format(PyExc_ValueError,
2612 "invalid file open mode %.200s", mode);
2613 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002614 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002615 }
2616 if (fob == Py_None)
2617 fp = NULL;
2618 else {
2619 if (!PyFile_Check(fob)) {
2620 PyErr_SetString(PyExc_ValueError,
2621 "load_module arg#2 should be a file or None");
2622 return NULL;
2623 }
2624 fp = get_file(pathname, fob, mode);
2625 if (fp == NULL)
2626 return NULL;
2627 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002628 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002629}
2630
2631static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002632imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002633{
2634 char *name;
2635 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002636 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002637 return NULL;
2638 return load_package(name, pathname);
2639}
2640
2641static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002642imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002643{
2644 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002645 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002646 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002647 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002648}
2649
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002650/* Doc strings */
2651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002652PyDoc_STRVAR(doc_imp,
2653"This module provides the components needed to build your own\n\
2654__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(doc_find_module,
2657"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002658Search for a module. If path is omitted or None, search for a\n\
2659built-in, frozen or special module and continue search in sys.path.\n\
2660The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663PyDoc_STRVAR(doc_load_module,
2664"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002665Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002666The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002668PyDoc_STRVAR(doc_get_magic,
2669"get_magic() -> string\n\
2670Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002672PyDoc_STRVAR(doc_get_suffixes,
2673"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002674Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002675that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002677PyDoc_STRVAR(doc_new_module,
2678"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002679Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682PyDoc_STRVAR(doc_lock_held,
2683"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002684Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002685On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002686
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002687PyDoc_STRVAR(doc_acquire_lock,
2688"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002689Acquires the interpreter's import lock for the current thread.\n\
2690This lock should be used by import hooks to ensure thread-safety\n\
2691when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002692On platforms without threads, this function does nothing.");
2693
2694PyDoc_STRVAR(doc_release_lock,
2695"release_lock() -> None\n\
2696Release the interpreter's import lock.\n\
2697On platforms without threads, this function does nothing.");
2698
Guido van Rossum79f25d91997-04-29 20:08:16 +00002699static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002700 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2701 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2702 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2703 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2704 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2705 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2706 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2707 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002708 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002709 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2710 {"init_builtin", imp_init_builtin, METH_VARARGS},
2711 {"init_frozen", imp_init_frozen, METH_VARARGS},
2712 {"is_builtin", imp_is_builtin, METH_VARARGS},
2713 {"is_frozen", imp_is_frozen, METH_VARARGS},
2714 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002715#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002716 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002717#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002718 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002719 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002720 {NULL, NULL} /* sentinel */
2721};
2722
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002723static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002724setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002725{
2726 PyObject *v;
2727 int err;
2728
2729 v = PyInt_FromLong((long)value);
2730 err = PyDict_SetItemString(d, name, v);
2731 Py_XDECREF(v);
2732 return err;
2733}
2734
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002735PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002736initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002737{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002738 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002739
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002740 m = Py_InitModule4("imp", imp_methods, doc_imp,
2741 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002742 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002743
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002744 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2745 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2746 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2747 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2748 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2749 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2750 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2751 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002752 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002753 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002754
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002755 failure:
2756 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002757}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002758
2759
Guido van Rossumb18618d2000-05-03 23:44:39 +00002760/* API for embedding applications that want to add their own entries
2761 to the table of built-in modules. This should normally be called
2762 *before* Py_Initialize(). When the table resize fails, -1 is
2763 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002764
2765 After a similar function by Just van Rossum. */
2766
2767int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002768PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002769{
2770 static struct _inittab *our_copy = NULL;
2771 struct _inittab *p;
2772 int i, n;
2773
2774 /* Count the number of entries in both tables */
2775 for (n = 0; newtab[n].name != NULL; n++)
2776 ;
2777 if (n == 0)
2778 return 0; /* Nothing to do */
2779 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2780 ;
2781
2782 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002783 p = our_copy;
2784 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002785 if (p == NULL)
2786 return -1;
2787
2788 /* Copy the tables into the new memory */
2789 if (our_copy != PyImport_Inittab)
2790 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2791 PyImport_Inittab = our_copy = p;
2792 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2793
2794 return 0;
2795}
2796
2797/* Shorthand to add a single entry given a name and a function */
2798
2799int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002800PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002801{
2802 struct _inittab newtab[2];
2803
2804 memset(newtab, '\0', sizeof newtab);
2805
2806 newtab[0].name = name;
2807 newtab[0].initfunc = initfunc;
2808
2809 return PyImport_ExtendInittab(newtab);
2810}