blob: e76ada9783862da2b9034c5934f3f7dad3456c0c [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
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000029 The magic numbers must be spaced apart atleast 2 values, as the
30 -U interpeter flag will cause MAGIC+1 being used. They have been
31 odd numbers for some time now.
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 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000050 Python 2.4a0: 62041
Tim Peters36515e22001-11-18 04:06:29 +000051*/
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000052#define MAGIC (62041 | ((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 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000800 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000802 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
803 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
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);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000814 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
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();
Thomas Heller25653242004-06-07 15:04:10 +0000924 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000925 }
926 else
927 m = NULL;
928 goto cleanup;
929 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000930 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931 if (fp != NULL)
932 fclose(fp);
933 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000934 Py_XDECREF(path);
935 Py_XDECREF(file);
936 return m;
937}
938
939
940/* Helper to test for built-in module */
941
942static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000943is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000944{
945 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000946 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
947 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
948 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949 return -1;
950 else
951 return 1;
952 }
953 }
954 return 0;
955}
956
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957
Just van Rossum52e14d62002-12-30 22:08:05 +0000958/* Return an importer object for a sys.path/pkg.__path__ item 'p',
959 possibly by fetching it from the path_importer_cache dict. If it
960 wasn't yet cached, traverse path_hooks until it a hook is found
961 that can handle the path item. Return None if no hook could;
962 this tells our caller it should fall back to the builtin
963 import mechanism. Cache the result in path_importer_cache.
964 Returns a borrowed reference. */
965
966static PyObject *
967get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
968 PyObject *p)
969{
970 PyObject *importer;
971 int j, nhooks;
972
973 /* These conditions are the caller's responsibility: */
974 assert(PyList_Check(path_hooks));
975 assert(PyDict_Check(path_importer_cache));
976
977 nhooks = PyList_Size(path_hooks);
978 if (nhooks < 0)
979 return NULL; /* Shouldn't happen */
980
981 importer = PyDict_GetItem(path_importer_cache, p);
982 if (importer != NULL)
983 return importer;
984
985 /* set path_importer_cache[p] to None to avoid recursion */
986 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
987 return NULL;
988
989 for (j = 0; j < nhooks; j++) {
990 PyObject *hook = PyList_GetItem(path_hooks, j);
991 if (hook == NULL)
992 return NULL;
993 importer = PyObject_CallFunction(hook, "O", p);
994 if (importer != NULL)
995 break;
996
997 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
998 return NULL;
999 }
1000 PyErr_Clear();
1001 }
1002 if (importer == NULL)
1003 importer = Py_None;
1004 else if (importer != Py_None) {
1005 int err = PyDict_SetItem(path_importer_cache, p, importer);
1006 Py_DECREF(importer);
1007 if (err != 0)
1008 return NULL;
1009 }
1010 return importer;
1011}
1012
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001013/* Search the path (default sys.path) for a module. Return the
1014 corresponding filedescr struct, and (via return arguments) the
1015 pathname and an open file. Return NULL if the module is not found. */
1016
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001017#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001018extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1019 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001020#endif
1021
Tim Peters50d8d372001-02-28 05:34:27 +00001022static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001023static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001024static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001025
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001026static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001027find_module(char *fullname, char *subname, PyObject *path, char *buf,
1028 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001029{
Fred Drake4c82b232000-06-30 16:18:57 +00001030 int i, npath;
1031 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001032 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001033 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001034 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001035 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001036#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001037 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001038#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001039 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1040 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1041 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001042 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001043#if defined(PYOS_OS2)
1044 size_t saved_len;
1045 size_t saved_namelen;
1046 char *saved_buf = NULL;
1047#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001048 if (p_loader != NULL)
1049 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001050
Just van Rossum52e14d62002-12-30 22:08:05 +00001051 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001052 PyErr_SetString(PyExc_OverflowError,
1053 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001054 return NULL;
1055 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001056 strcpy(name, subname);
1057
1058 /* sys.meta_path import hook */
1059 if (p_loader != NULL) {
1060 PyObject *meta_path;
1061
1062 meta_path = PySys_GetObject("meta_path");
1063 if (meta_path == NULL || !PyList_Check(meta_path)) {
1064 PyErr_SetString(PyExc_ImportError,
1065 "sys.meta_path must be a list of "
1066 "import hooks");
1067 return NULL;
1068 }
1069 Py_INCREF(meta_path); /* zap guard */
1070 npath = PyList_Size(meta_path);
1071 for (i = 0; i < npath; i++) {
1072 PyObject *loader;
1073 PyObject *hook = PyList_GetItem(meta_path, i);
1074 loader = PyObject_CallMethod(hook, "find_module",
1075 "sO", fullname,
1076 path != NULL ?
1077 path : Py_None);
1078 if (loader == NULL) {
1079 Py_DECREF(meta_path);
1080 return NULL; /* true error */
1081 }
1082 if (loader != Py_None) {
1083 /* a loader was found */
1084 *p_loader = loader;
1085 Py_DECREF(meta_path);
1086 return &importhookdescr;
1087 }
1088 Py_DECREF(loader);
1089 }
1090 Py_DECREF(meta_path);
1091 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001092
1093 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001094 /* The only type of submodule allowed inside a "frozen"
1095 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001096 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1097 PyErr_SetString(PyExc_ImportError,
1098 "full frozen module name too long");
1099 return NULL;
1100 }
1101 strcpy(buf, PyString_AsString(path));
1102 strcat(buf, ".");
1103 strcat(buf, name);
1104 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001105 if (find_frozen(name) != NULL) {
1106 strcpy(buf, name);
1107 return &fd_frozen;
1108 }
1109 PyErr_Format(PyExc_ImportError,
1110 "No frozen submodule named %.200s", name);
1111 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001112 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001113 if (path == NULL) {
1114 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001115 strcpy(buf, name);
1116 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001117 }
Greg Ward201baee2001-10-04 14:52:06 +00001118 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001119 strcpy(buf, name);
1120 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001121 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001122
Guido van Rossumac279101996-08-22 23:10:58 +00001123#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001124 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1125 if (fp != NULL) {
1126 *p_fp = fp;
1127 return fdp;
1128 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001129#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001130 path = PySys_GetObject("path");
1131 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132 if (path == NULL || !PyList_Check(path)) {
1133 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001134 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001135 return NULL;
1136 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001137
1138 path_hooks = PySys_GetObject("path_hooks");
1139 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1140 PyErr_SetString(PyExc_ImportError,
1141 "sys.path_hooks must be a list of "
1142 "import hooks");
1143 return NULL;
1144 }
1145 path_importer_cache = PySys_GetObject("path_importer_cache");
1146 if (path_importer_cache == NULL ||
1147 !PyDict_Check(path_importer_cache)) {
1148 PyErr_SetString(PyExc_ImportError,
1149 "sys.path_importer_cache must be a dict");
1150 return NULL;
1151 }
1152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001154 namelen = strlen(name);
1155 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001156 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001158#ifdef Py_USING_UNICODE
1159 if (PyUnicode_Check(v)) {
1160 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1161 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1162 if (copy == NULL)
1163 return NULL;
1164 v = copy;
1165 }
1166 else
1167#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001169 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001171 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1172 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001173 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001174 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001176 if (strlen(buf) != len) {
1177 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001179 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001180
1181 /* sys.path_hooks import hook */
1182 if (p_loader != NULL) {
1183 PyObject *importer;
1184
1185 importer = get_path_importer(path_importer_cache,
1186 path_hooks, v);
1187 if (importer == NULL)
1188 return NULL;
1189 /* Note: importer is a borrowed reference */
1190 if (importer != Py_None) {
1191 PyObject *loader;
1192 loader = PyObject_CallMethod(importer,
1193 "find_module",
1194 "s", fullname);
1195 if (loader == NULL)
1196 return NULL; /* error */
1197 if (loader != Py_None) {
1198 /* a loader was found */
1199 *p_loader = loader;
1200 return &importhookdescr;
1201 }
1202 Py_DECREF(loader);
1203 }
1204 /* no hook was successful, use builtin import */
1205 }
1206
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001207 if (len > 0 && buf[len-1] != SEP
1208#ifdef ALTSEP
1209 && buf[len-1] != ALTSEP
1210#endif
1211 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001212 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001213 strcpy(buf+len, name);
1214 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001215
1216 /* Check for package import (buf holds a directory name,
1217 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001218#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001219 if (stat(buf, &statbuf) == 0 && /* it exists */
1220 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1221 find_init_module(buf) && /* it has __init__.py */
1222 case_ok(buf, len, namelen, name)) { /* and case matches */
1223 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001224 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001225 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001226#else
1227 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001228#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001229 if (isdir(buf) &&
1230 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001231 case_ok(buf, len, namelen, name)) {
1232 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001233 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001234 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001235#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001236#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001237#if defined(PYOS_OS2)
1238 /* take a snapshot of the module spec for restoration
1239 * after the 8 character DLL hackery
1240 */
1241 saved_buf = strdup(buf);
1242 saved_len = len;
1243 saved_namelen = namelen;
1244#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001246#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001247 /* OS/2 limits DLLs to 8 character names (w/o
1248 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001249 * so if the name is longer than that and its a
1250 * dynamically loaded module we're going to try,
1251 * truncate the name before trying
1252 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001253 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001254 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001255 const struct filedescr *scan;
1256 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001257 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001258 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001259 break;
1260 else
1261 scan++;
1262 }
1263 if (scan->suffix != NULL) {
1264 /* yes, so truncate the name */
1265 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001266 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001267 buf[len] = '\0';
1268 }
1269 }
1270#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001271 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001272 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001273 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001274 filemode = fdp->mode;
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001275 if (filemode[0] == 'U')
1276 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001277 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001278 if (fp != NULL) {
1279 if (case_ok(buf, len, namelen, name))
1280 break;
1281 else { /* continue search */
1282 fclose(fp);
1283 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001284 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001285 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001286#if defined(PYOS_OS2)
1287 /* restore the saved snapshot */
1288 strcpy(buf, saved_buf);
1289 len = saved_len;
1290 namelen = saved_namelen;
1291#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001292 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001293#if defined(PYOS_OS2)
1294 /* don't need/want the module name snapshot anymore */
1295 if (saved_buf)
1296 {
1297 free(saved_buf);
1298 saved_buf = NULL;
1299 }
1300#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001301 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001302 if (fp != NULL)
1303 break;
1304 }
1305 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001306 PyErr_Format(PyExc_ImportError,
1307 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001308 return NULL;
1309 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001310 *p_fp = fp;
1311 return fdp;
1312}
1313
Tim Petersd1e87a82001-03-01 18:12:00 +00001314/* case_ok(char* buf, int len, int namelen, char* name)
1315 * The arguments here are tricky, best shown by example:
1316 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1317 * ^ ^ ^ ^
1318 * |--------------------- buf ---------------------|
1319 * |------------------- len ------------------|
1320 * |------ name -------|
1321 * |----- namelen -----|
1322 * buf is the full path, but len only counts up to (& exclusive of) the
1323 * extension. name is the module name, also exclusive of extension.
1324 *
1325 * We've already done a successful stat() or fopen() on buf, so know that
1326 * there's some match, possibly case-insensitive.
1327 *
Tim Peters50d8d372001-02-28 05:34:27 +00001328 * case_ok() is to return 1 if there's a case-sensitive match for
1329 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1330 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001331 *
Tim Peters50d8d372001-02-28 05:34:27 +00001332 * case_ok() is used to implement case-sensitive import semantics even
1333 * on platforms with case-insensitive filesystems. It's trivial to implement
1334 * for case-sensitive filesystems. It's pretty much a cross-platform
1335 * nightmare for systems with case-insensitive filesystems.
1336 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001337
Tim Peters50d8d372001-02-28 05:34:27 +00001338/* First we may need a pile of platform-specific header files; the sequence
1339 * of #if's here should match the sequence in the body of case_ok().
1340 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001341#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001342#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001343#ifdef __CYGWIN__
1344#include <sys/cygwin.h>
1345#endif
1346
Tim Peters50d8d372001-02-28 05:34:27 +00001347#elif defined(DJGPP)
1348#include <dir.h>
1349
Tim Petersd1e87a82001-03-01 18:12:00 +00001350#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001351#include <sys/types.h>
1352#include <dirent.h>
1353
Andrew MacIntyred9400542002-02-26 11:41:34 +00001354#elif defined(PYOS_OS2)
1355#define INCL_DOS
1356#define INCL_DOSERRORS
1357#define INCL_NOPMAPI
1358#include <os2.h>
1359
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001360#elif defined(RISCOS)
1361#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001362#endif
1363
Guido van Rossum0980bd91998-02-13 17:18:36 +00001364static int
Tim Peters50d8d372001-02-28 05:34:27 +00001365case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001366{
Tim Peters50d8d372001-02-28 05:34:27 +00001367/* Pick a platform-specific implementation; the sequence of #if's here should
1368 * match the sequence just above.
1369 */
1370
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001371/* MS_WINDOWS || __CYGWIN__ */
1372#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001373 WIN32_FIND_DATA data;
1374 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001375#ifdef __CYGWIN__
1376 char tempbuf[MAX_PATH];
1377#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001378
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001379 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001380 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001381
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001382#ifdef __CYGWIN__
1383 cygwin32_conv_to_win32_path(buf, tempbuf);
1384 h = FindFirstFile(tempbuf, &data);
1385#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001386 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001387#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001388 if (h == INVALID_HANDLE_VALUE) {
1389 PyErr_Format(PyExc_NameError,
1390 "Can't find file for module %.100s\n(filename %.300s)",
1391 name, buf);
1392 return 0;
1393 }
1394 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001395 return strncmp(data.cFileName, name, namelen) == 0;
1396
1397/* DJGPP */
1398#elif defined(DJGPP)
1399 struct ffblk ffblk;
1400 int done;
1401
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001402 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001403 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001404
1405 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1406 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001407 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001408 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001409 name, buf);
1410 return 0;
1411 }
Tim Peters50d8d372001-02-28 05:34:27 +00001412 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001413
Tim Peters677898a2001-03-02 03:28:03 +00001414/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001415#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001416 DIR *dirp;
1417 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001418 char dirname[MAXPATHLEN + 1];
1419 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001420
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001421 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001422 return 1;
1423
Tim Petersd1e87a82001-03-01 18:12:00 +00001424 /* Copy the dir component into dirname; substitute "." if empty */
1425 if (dirlen <= 0) {
1426 dirname[0] = '.';
1427 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001428 }
1429 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001430 assert(dirlen <= MAXPATHLEN);
1431 memcpy(dirname, buf, dirlen);
1432 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001433 }
1434 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001435 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001436 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001437 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001438 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001439 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001440#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001441 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001442#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001443 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001444#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001445 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001446 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001447 (void)closedir(dirp);
1448 return 1; /* Found */
1449 }
1450 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001451 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001452 }
Tim Peters430f5d42001-03-01 01:30:56 +00001453 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001454
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001455/* RISC OS */
1456#elif defined(RISCOS)
1457 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1458 char buf2[MAXPATHLEN+2];
1459 char *nameWithExt = buf+len-namelen;
1460 int canonlen;
1461 os_error *e;
1462
1463 if (Py_GETENV("PYTHONCASEOK") != NULL)
1464 return 1;
1465
1466 /* workaround:
1467 append wildcard, otherwise case of filename wouldn't be touched */
1468 strcpy(buf2, buf);
1469 strcat(buf2, "*");
1470
1471 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1472 canonlen = MAXPATHLEN+1-canonlen;
1473 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1474 return 0;
1475 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1476 return 1; /* match */
1477
1478 return 0;
1479
Andrew MacIntyred9400542002-02-26 11:41:34 +00001480/* OS/2 */
1481#elif defined(PYOS_OS2)
1482 HDIR hdir = 1;
1483 ULONG srchcnt = 1;
1484 FILEFINDBUF3 ffbuf;
1485 APIRET rc;
1486
1487 if (getenv("PYTHONCASEOK") != NULL)
1488 return 1;
1489
1490 rc = DosFindFirst(buf,
1491 &hdir,
1492 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1493 &ffbuf, sizeof(ffbuf),
1494 &srchcnt,
1495 FIL_STANDARD);
1496 if (rc != NO_ERROR)
1497 return 0;
1498 return strncmp(ffbuf.achName, name, namelen) == 0;
1499
Tim Peters50d8d372001-02-28 05:34:27 +00001500/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1501#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001502 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001503
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001504#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001505}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001506
Guido van Rossum0980bd91998-02-13 17:18:36 +00001507
Guido van Rossum197346f1997-10-31 18:38:52 +00001508#ifdef HAVE_STAT
1509/* Helper to look for __init__.py or __init__.py[co] in potential package */
1510static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001511find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001512{
Tim Peters0f9431f2001-07-05 03:47:53 +00001513 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001514 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001515 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001516 struct stat statbuf;
1517
Tim Peters0f9431f2001-07-05 03:47:53 +00001518/* For calling case_ok(buf, len, namelen, name):
1519 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1520 * ^ ^ ^ ^
1521 * |--------------------- buf ---------------------|
1522 * |------------------- len ------------------|
1523 * |------ name -------|
1524 * |----- namelen -----|
1525 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001526 if (save_len + 13 >= MAXPATHLEN)
1527 return 0;
1528 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001529 pname = buf + i;
1530 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001531 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001532 if (case_ok(buf,
1533 save_len + 9, /* len("/__init__") */
1534 8, /* len("__init__") */
1535 pname)) {
1536 buf[save_len] = '\0';
1537 return 1;
1538 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001539 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001540 i += strlen(pname);
1541 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001542 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001543 if (case_ok(buf,
1544 save_len + 9, /* len("/__init__") */
1545 8, /* len("__init__") */
1546 pname)) {
1547 buf[save_len] = '\0';
1548 return 1;
1549 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001550 }
1551 buf[save_len] = '\0';
1552 return 0;
1553}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001554
1555#else
1556
1557#ifdef RISCOS
1558static int
1559find_init_module(buf)
1560 char *buf;
1561{
1562 int save_len = strlen(buf);
1563 int i = save_len;
1564
1565 if (save_len + 13 >= MAXPATHLEN)
1566 return 0;
1567 buf[i++] = SEP;
1568 strcpy(buf+i, "__init__/py");
1569 if (isfile(buf)) {
1570 buf[save_len] = '\0';
1571 return 1;
1572 }
1573
1574 if (Py_OptimizeFlag)
1575 strcpy(buf+i, "o");
1576 else
1577 strcpy(buf+i, "c");
1578 if (isfile(buf)) {
1579 buf[save_len] = '\0';
1580 return 1;
1581 }
1582 buf[save_len] = '\0';
1583 return 0;
1584}
1585#endif /*RISCOS*/
1586
Guido van Rossum197346f1997-10-31 18:38:52 +00001587#endif /* HAVE_STAT */
1588
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001589
Tim Petersdbd9ba62000-07-09 03:09:57 +00001590static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001591
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001593 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001594
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001596load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001597{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001598 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001600 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001601
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001602 /* First check that there's an open file (if we need one) */
1603 switch (type) {
1604 case PY_SOURCE:
1605 case PY_COMPILED:
1606 if (fp == NULL) {
1607 PyErr_Format(PyExc_ValueError,
1608 "file object required for import (type code %d)",
1609 type);
1610 return NULL;
1611 }
1612 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001613
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001614 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001615
1616 case PY_SOURCE:
1617 m = load_source_module(name, buf, fp);
1618 break;
1619
1620 case PY_COMPILED:
1621 m = load_compiled_module(name, buf, fp);
1622 break;
1623
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001624#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001625 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001626 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001627 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001628#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001629
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001630 case PKG_DIRECTORY:
1631 m = load_package(name, buf);
1632 break;
1633
1634 case C_BUILTIN:
1635 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001636 if (buf != NULL && buf[0] != '\0')
1637 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001638 if (type == C_BUILTIN)
1639 err = init_builtin(name);
1640 else
1641 err = PyImport_ImportFrozenModule(name);
1642 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001643 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001644 if (err == 0) {
1645 PyErr_Format(PyExc_ImportError,
1646 "Purported %s module %.200s not found",
1647 type == C_BUILTIN ?
1648 "builtin" : "frozen",
1649 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001650 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001651 }
1652 modules = PyImport_GetModuleDict();
1653 m = PyDict_GetItemString(modules, name);
1654 if (m == NULL) {
1655 PyErr_Format(
1656 PyExc_ImportError,
1657 "%s module %.200s not properly initialized",
1658 type == C_BUILTIN ?
1659 "builtin" : "frozen",
1660 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001661 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001662 }
1663 Py_INCREF(m);
1664 break;
1665
Just van Rossum52e14d62002-12-30 22:08:05 +00001666 case IMP_HOOK: {
1667 if (loader == NULL) {
1668 PyErr_SetString(PyExc_ImportError,
1669 "import hook without loader");
1670 return NULL;
1671 }
1672 m = PyObject_CallMethod(loader, "load_module", "s", name);
1673 break;
1674 }
1675
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001676 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001677 PyErr_Format(PyExc_ImportError,
1678 "Don't know how to import %.200s (type code %d)",
1679 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001680 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001681
1682 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001683
1684 return m;
1685}
1686
1687
1688/* Initialize a built-in module.
1689 Return 1 for succes, 0 if the module is not found, and -1 with
1690 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001691
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001692static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001693init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001694{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001695 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001696
Greg Ward201baee2001-10-04 14:52:06 +00001697 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001698 return 1;
1699
Guido van Rossum771c6c81997-10-31 18:37:24 +00001700 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001701 if (strcmp(name, p->name) == 0) {
1702 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001703 PyErr_Format(PyExc_ImportError,
1704 "Cannot re-init internal module %.200s",
1705 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001706 return -1;
1707 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001708 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001709 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001710 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001712 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001713 if (_PyImport_FixupExtension(name, name) == NULL)
1714 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001715 return 1;
1716 }
1717 }
1718 return 0;
1719}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001720
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001722/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001723
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001724static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001725find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001726{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001727 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001728
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001730 if (p->name == NULL)
1731 return NULL;
1732 if (strcmp(p->name, name) == 0)
1733 break;
1734 }
1735 return p;
1736}
1737
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001739get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001740{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001741 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001742 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001743
1744 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001745 PyErr_Format(PyExc_ImportError,
1746 "No such frozen object named %.200s",
1747 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001748 return NULL;
1749 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001750 if (p->code == NULL) {
1751 PyErr_Format(PyExc_ImportError,
1752 "Excluded frozen object named %.200s",
1753 name);
1754 return NULL;
1755 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001756 size = p->size;
1757 if (size < 0)
1758 size = -size;
1759 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001760}
1761
1762/* Initialize a frozen module.
1763 Return 1 for succes, 0 if the module is not found, and -1 with
1764 an exception set if the initialization failed.
1765 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001766
1767int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001768PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001769{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001770 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771 PyObject *co;
1772 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001773 int ispackage;
1774 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001775
1776 if (p == NULL)
1777 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001778 if (p->code == NULL) {
1779 PyErr_Format(PyExc_ImportError,
1780 "Excluded frozen object named %.200s",
1781 name);
1782 return -1;
1783 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001784 size = p->size;
1785 ispackage = (size < 0);
1786 if (ispackage)
1787 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001789 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001790 name, ispackage ? " package" : "");
1791 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001792 if (co == NULL)
1793 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 if (!PyCode_Check(co)) {
1795 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001796 PyErr_Format(PyExc_TypeError,
1797 "frozen object %.200s is not a code object",
1798 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001799 return -1;
1800 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001801 if (ispackage) {
1802 /* Set __path__ to the package name */
1803 PyObject *d, *s;
1804 int err;
1805 m = PyImport_AddModule(name);
1806 if (m == NULL)
1807 return -1;
1808 d = PyModule_GetDict(m);
1809 s = PyString_InternFromString(name);
1810 if (s == NULL)
1811 return -1;
1812 err = PyDict_SetItemString(d, "__path__", s);
1813 Py_DECREF(s);
1814 if (err != 0)
1815 return err;
1816 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001817 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001818 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001819 if (m == NULL)
1820 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001821 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001822 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001823}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001824
1825
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001827 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001828
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001830PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001831{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001832 PyObject *pname;
1833 PyObject *result;
1834
1835 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001836 if (pname == NULL)
1837 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001838 result = PyImport_Import(pname);
1839 Py_DECREF(pname);
1840 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001841}
1842
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001843/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001844static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1845static PyObject *load_next(PyObject *mod, PyObject *altmod,
1846 char **p_name, char *buf, int *p_buflen);
1847static int mark_miss(char *name);
1848static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1849 char *buf, int buflen, int recursive);
1850static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001851
1852/* The Magnum Opus of dotted-name import :-) */
1853
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001854static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001855import_module_ex(char *name, PyObject *globals, PyObject *locals,
1856 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001857{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001858 char buf[MAXPATHLEN+1];
1859 int buflen = 0;
1860 PyObject *parent, *head, *next, *tail;
1861
1862 parent = get_parent(globals, buf, &buflen);
1863 if (parent == NULL)
1864 return NULL;
1865
1866 head = load_next(parent, Py_None, &name, buf, &buflen);
1867 if (head == NULL)
1868 return NULL;
1869
1870 tail = head;
1871 Py_INCREF(tail);
1872 while (name) {
1873 next = load_next(tail, tail, &name, buf, &buflen);
1874 Py_DECREF(tail);
1875 if (next == NULL) {
1876 Py_DECREF(head);
1877 return NULL;
1878 }
1879 tail = next;
1880 }
1881
1882 if (fromlist != NULL) {
1883 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1884 fromlist = NULL;
1885 }
1886
1887 if (fromlist == NULL) {
1888 Py_DECREF(tail);
1889 return head;
1890 }
1891
1892 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001893 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001894 Py_DECREF(tail);
1895 return NULL;
1896 }
1897
1898 return tail;
1899}
1900
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001901PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001902PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1903 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001904{
1905 PyObject *result;
1906 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001907 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001908 if (unlock_import() < 0) {
1909 Py_XDECREF(result);
1910 PyErr_SetString(PyExc_RuntimeError,
1911 "not holding the import lock");
1912 return NULL;
1913 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001914 return result;
1915}
1916
Fred Drake87590902004-05-28 20:21:36 +00001917/* Return the package that an import is being performed in. If globals comes
1918 from the module foo.bar.bat (not itself a package), this returns the
1919 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1920 the package's entry in sys.modules is returned.
1921
1922 The *name* of the returned package is returned in buf, with the length of
1923 the name in *p_buflen.
1924
1925 If globals doesn't come from a package or a module in a package, or a
1926 corresponding entry is not found in sys.modules, Py_None is returned.
1927*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001928static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001929get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001930{
1931 static PyObject *namestr = NULL;
1932 static PyObject *pathstr = NULL;
1933 PyObject *modname, *modpath, *modules, *parent;
1934
1935 if (globals == NULL || !PyDict_Check(globals))
1936 return Py_None;
1937
1938 if (namestr == NULL) {
1939 namestr = PyString_InternFromString("__name__");
1940 if (namestr == NULL)
1941 return NULL;
1942 }
1943 if (pathstr == NULL) {
1944 pathstr = PyString_InternFromString("__path__");
1945 if (pathstr == NULL)
1946 return NULL;
1947 }
1948
1949 *buf = '\0';
1950 *p_buflen = 0;
1951 modname = PyDict_GetItem(globals, namestr);
1952 if (modname == NULL || !PyString_Check(modname))
1953 return Py_None;
1954
1955 modpath = PyDict_GetItem(globals, pathstr);
1956 if (modpath != NULL) {
1957 int len = PyString_GET_SIZE(modname);
1958 if (len > MAXPATHLEN) {
1959 PyErr_SetString(PyExc_ValueError,
1960 "Module name too long");
1961 return NULL;
1962 }
1963 strcpy(buf, PyString_AS_STRING(modname));
1964 *p_buflen = len;
1965 }
1966 else {
1967 char *start = PyString_AS_STRING(modname);
1968 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001969 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001970 if (lastdot == NULL)
1971 return Py_None;
1972 len = lastdot - start;
1973 if (len >= MAXPATHLEN) {
1974 PyErr_SetString(PyExc_ValueError,
1975 "Module name too long");
1976 return NULL;
1977 }
1978 strncpy(buf, start, len);
1979 buf[len] = '\0';
1980 *p_buflen = len;
1981 }
1982
1983 modules = PyImport_GetModuleDict();
1984 parent = PyDict_GetItemString(modules, buf);
1985 if (parent == NULL)
1986 parent = Py_None;
1987 return parent;
1988 /* We expect, but can't guarantee, if parent != None, that:
1989 - parent.__name__ == buf
1990 - parent.__dict__ is globals
1991 If this is violated... Who cares? */
1992}
1993
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001994/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001995static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001996load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1997 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001998{
1999 char *name = *p_name;
2000 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002001 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002002 char *p;
2003 PyObject *result;
2004
2005 if (dot == NULL) {
2006 *p_name = NULL;
2007 len = strlen(name);
2008 }
2009 else {
2010 *p_name = dot+1;
2011 len = dot-name;
2012 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002013 if (len == 0) {
2014 PyErr_SetString(PyExc_ValueError,
2015 "Empty module name");
2016 return NULL;
2017 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002018
2019 p = buf + *p_buflen;
2020 if (p != buf)
2021 *p++ = '.';
2022 if (p+len-buf >= MAXPATHLEN) {
2023 PyErr_SetString(PyExc_ValueError,
2024 "Module name too long");
2025 return NULL;
2026 }
2027 strncpy(p, name, len);
2028 p[len] = '\0';
2029 *p_buflen = p+len-buf;
2030
2031 result = import_submodule(mod, p, buf);
2032 if (result == Py_None && altmod != mod) {
2033 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002034 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002035 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002036 if (result != NULL && result != Py_None) {
2037 if (mark_miss(buf) != 0) {
2038 Py_DECREF(result);
2039 return NULL;
2040 }
2041 strncpy(buf, name, len);
2042 buf[len] = '\0';
2043 *p_buflen = len;
2044 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002045 }
2046 if (result == NULL)
2047 return NULL;
2048
2049 if (result == Py_None) {
2050 Py_DECREF(result);
2051 PyErr_Format(PyExc_ImportError,
2052 "No module named %.200s", name);
2053 return NULL;
2054 }
2055
2056 return result;
2057}
2058
2059static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002060mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002061{
2062 PyObject *modules = PyImport_GetModuleDict();
2063 return PyDict_SetItemString(modules, name, Py_None);
2064}
2065
2066static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002067ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2068 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002069{
2070 int i;
2071
2072 if (!PyObject_HasAttrString(mod, "__path__"))
2073 return 1;
2074
2075 for (i = 0; ; i++) {
2076 PyObject *item = PySequence_GetItem(fromlist, i);
2077 int hasit;
2078 if (item == NULL) {
2079 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2080 PyErr_Clear();
2081 return 1;
2082 }
2083 return 0;
2084 }
2085 if (!PyString_Check(item)) {
2086 PyErr_SetString(PyExc_TypeError,
2087 "Item in ``from list'' not a string");
2088 Py_DECREF(item);
2089 return 0;
2090 }
2091 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002092 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002093 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002094 /* See if the package defines __all__ */
2095 if (recursive)
2096 continue; /* Avoid endless recursion */
2097 all = PyObject_GetAttrString(mod, "__all__");
2098 if (all == NULL)
2099 PyErr_Clear();
2100 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002101 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002102 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002103 if (!ret)
2104 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002105 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002106 continue;
2107 }
2108 hasit = PyObject_HasAttr(mod, item);
2109 if (!hasit) {
2110 char *subname = PyString_AS_STRING(item);
2111 PyObject *submod;
2112 char *p;
2113 if (buflen + strlen(subname) >= MAXPATHLEN) {
2114 PyErr_SetString(PyExc_ValueError,
2115 "Module name too long");
2116 Py_DECREF(item);
2117 return 0;
2118 }
2119 p = buf + buflen;
2120 *p++ = '.';
2121 strcpy(p, subname);
2122 submod = import_submodule(mod, subname, buf);
2123 Py_XDECREF(submod);
2124 if (submod == NULL) {
2125 Py_DECREF(item);
2126 return 0;
2127 }
2128 }
2129 Py_DECREF(item);
2130 }
2131
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002132 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002133}
2134
Neil Schemenauer00b09662003-06-16 21:03:07 +00002135static int
2136add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2137 PyObject *modules)
2138{
2139 if (mod == Py_None)
2140 return 1;
2141 /* Irrespective of the success of this load, make a
2142 reference to it in the parent package module. A copy gets
2143 saved in the modules dictionary under the full name, so get a
2144 reference from there, if need be. (The exception is when the
2145 load failed with a SyntaxError -- then there's no trace in
2146 sys.modules. In that case, of course, do nothing extra.) */
2147 if (submod == NULL) {
2148 submod = PyDict_GetItemString(modules, fullname);
2149 if (submod == NULL)
2150 return 1;
2151 }
2152 if (PyModule_Check(mod)) {
2153 /* We can't use setattr here since it can give a
2154 * spurious warning if the submodule name shadows a
2155 * builtin name */
2156 PyObject *dict = PyModule_GetDict(mod);
2157 if (!dict)
2158 return 0;
2159 if (PyDict_SetItemString(dict, subname, submod) < 0)
2160 return 0;
2161 }
2162 else {
2163 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2164 return 0;
2165 }
2166 return 1;
2167}
2168
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002169static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002170import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002171{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002172 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002173 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002174
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002175 /* Require:
2176 if mod == None: subname == fullname
2177 else: mod.__name__ + "." + subname == fullname
2178 */
2179
Tim Peters50d8d372001-02-28 05:34:27 +00002180 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002181 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002182 }
2183 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002184 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002185 char buf[MAXPATHLEN+1];
2186 struct filedescr *fdp;
2187 FILE *fp = NULL;
2188
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002189 if (mod == Py_None)
2190 path = NULL;
2191 else {
2192 path = PyObject_GetAttrString(mod, "__path__");
2193 if (path == NULL) {
2194 PyErr_Clear();
2195 Py_INCREF(Py_None);
2196 return Py_None;
2197 }
2198 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002199
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002200 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002201 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2202 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002203 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002204 if (fdp == NULL) {
2205 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2206 return NULL;
2207 PyErr_Clear();
2208 Py_INCREF(Py_None);
2209 return Py_None;
2210 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002211 m = load_module(fullname, fp, buf, fdp->type, loader);
2212 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002213 if (fp)
2214 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002215 if (!add_submodule(mod, m, fullname, subname, modules)) {
2216 Py_XDECREF(m);
2217 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002218 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002219 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002220
2221 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002222}
2223
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002224
2225/* Re-import a module of any kind and return its module object, WITH
2226 INCREMENTED REFERENCE COUNT */
2227
Guido van Rossum79f25d91997-04-29 20:08:16 +00002228PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002229PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002230{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002231 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002232 PyObject *path = NULL;
2233 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002234 char buf[MAXPATHLEN+1];
2235 struct filedescr *fdp;
2236 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002237
Guido van Rossum79f25d91997-04-29 20:08:16 +00002238 if (m == NULL || !PyModule_Check(m)) {
2239 PyErr_SetString(PyExc_TypeError,
2240 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002241 return NULL;
2242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002243 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002244 if (name == NULL)
2245 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002246 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002247 PyErr_Format(PyExc_ImportError,
2248 "reload(): module %.200s not in sys.modules",
2249 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002250 return NULL;
2251 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002252 subname = strrchr(name, '.');
2253 if (subname == NULL)
2254 subname = name;
2255 else {
2256 PyObject *parentname, *parent;
2257 parentname = PyString_FromStringAndSize(name, (subname-name));
2258 if (parentname == NULL)
2259 return NULL;
2260 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002261 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002262 if (parent == NULL) {
2263 PyErr_Format(PyExc_ImportError,
2264 "reload(): parent %.200s not in sys.modules",
2265 name);
2266 return NULL;
2267 }
2268 subname++;
2269 path = PyObject_GetAttrString(parent, "__path__");
2270 if (path == NULL)
2271 PyErr_Clear();
2272 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002273 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002274 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002275 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002276 if (fdp == NULL)
2277 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002278 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002279 if (fp)
2280 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002281 return m;
2282}
2283
2284
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002285/* Higher-level import emulator which emulates the "import" statement
2286 more accurately -- it invokes the __import__() function from the
2287 builtins of the current globals. This means that the import is
2288 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002289 environment, e.g. by "rexec".
2290 A dummy list ["__doc__"] is passed as the 4th argument so that
2291 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2292 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002293
2294PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002295PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002296{
2297 static PyObject *silly_list = NULL;
2298 static PyObject *builtins_str = NULL;
2299 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002300 PyObject *globals = NULL;
2301 PyObject *import = NULL;
2302 PyObject *builtins = NULL;
2303 PyObject *r = NULL;
2304
2305 /* Initialize constant string objects */
2306 if (silly_list == NULL) {
2307 import_str = PyString_InternFromString("__import__");
2308 if (import_str == NULL)
2309 return NULL;
2310 builtins_str = PyString_InternFromString("__builtins__");
2311 if (builtins_str == NULL)
2312 return NULL;
2313 silly_list = Py_BuildValue("[s]", "__doc__");
2314 if (silly_list == NULL)
2315 return NULL;
2316 }
2317
2318 /* Get the builtins from current globals */
2319 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002320 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002321 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002322 builtins = PyObject_GetItem(globals, builtins_str);
2323 if (builtins == NULL)
2324 goto err;
2325 }
2326 else {
2327 /* No globals -- use standard builtins, and fake globals */
2328 PyErr_Clear();
2329
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002330 builtins = PyImport_ImportModuleEx("__builtin__",
2331 NULL, NULL, NULL);
2332 if (builtins == NULL)
2333 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002334 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2335 if (globals == NULL)
2336 goto err;
2337 }
2338
2339 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002340 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002341 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002342 if (import == NULL)
2343 PyErr_SetObject(PyExc_KeyError, import_str);
2344 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002345 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002346 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002347 if (import == NULL)
2348 goto err;
2349
2350 /* Call the _import__ function with the proper argument list */
2351 r = PyObject_CallFunction(import, "OOOO",
2352 module_name, globals, globals, silly_list);
2353
2354 err:
2355 Py_XDECREF(globals);
2356 Py_XDECREF(builtins);
2357 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002358
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002359 return r;
2360}
2361
2362
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002363/* Module 'imp' provides Python access to the primitives used for
2364 importing modules.
2365*/
2366
Guido van Rossum79f25d91997-04-29 20:08:16 +00002367static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002368imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002369{
2370 char buf[4];
2371
Guido van Rossum96774c12000-05-01 20:19:08 +00002372 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2373 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2374 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2375 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002376
Guido van Rossum79f25d91997-04-29 20:08:16 +00002377 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002378}
2379
Guido van Rossum79f25d91997-04-29 20:08:16 +00002380static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002381imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002382{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002383 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002384 struct filedescr *fdp;
2385
Guido van Rossum79f25d91997-04-29 20:08:16 +00002386 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002387 if (list == NULL)
2388 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002389 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2390 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002391 fdp->suffix, fdp->mode, fdp->type);
2392 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002393 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002394 return NULL;
2395 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002396 if (PyList_Append(list, item) < 0) {
2397 Py_DECREF(list);
2398 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002399 return NULL;
2400 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002401 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002402 }
2403 return list;
2404}
2405
Guido van Rossum79f25d91997-04-29 20:08:16 +00002406static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002407call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002408{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002409 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002411 struct filedescr *fdp;
2412 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002413 FILE *fp = NULL;
2414
2415 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002416 if (path == Py_None)
2417 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002418 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002419 if (fdp == NULL)
2420 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002421 if (fp != NULL) {
2422 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2423 if (fob == NULL) {
2424 fclose(fp);
2425 return NULL;
2426 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002427 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002428 else {
2429 fob = Py_None;
2430 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002431 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002432 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002435 return ret;
2436}
2437
Guido van Rossum79f25d91997-04-29 20:08:16 +00002438static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002439imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002440{
2441 char *name;
2442 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002443 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002444 return NULL;
2445 return call_find_module(name, path);
2446}
2447
2448static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002449imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002450{
2451 char *name;
2452 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002454 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002455 return NULL;
2456 ret = init_builtin(name);
2457 if (ret < 0)
2458 return NULL;
2459 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460 Py_INCREF(Py_None);
2461 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002462 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 m = PyImport_AddModule(name);
2464 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 return m;
2466}
2467
Guido van Rossum79f25d91997-04-29 20:08:16 +00002468static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002469imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002470{
2471 char *name;
2472 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002473 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002474 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002475 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002476 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002477 if (ret < 0)
2478 return NULL;
2479 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002480 Py_INCREF(Py_None);
2481 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002482 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002483 m = PyImport_AddModule(name);
2484 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002485 return m;
2486}
2487
Guido van Rossum79f25d91997-04-29 20:08:16 +00002488static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002489imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002490{
2491 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002492
Guido van Rossum43713e52000-02-29 13:59:29 +00002493 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002494 return NULL;
2495 return get_frozen_object(name);
2496}
2497
Guido van Rossum79f25d91997-04-29 20:08:16 +00002498static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002499imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002500{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002501 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002502 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002503 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002504 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002505}
2506
Guido van Rossum79f25d91997-04-29 20:08:16 +00002507static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002508imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002509{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002510 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002511 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002512 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002513 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002514 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002515 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516}
2517
2518static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002519get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002520{
2521 FILE *fp;
2522 if (fob == NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002523 if (mode[0] == 'U')
2524 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002525 fp = fopen(pathname, mode);
2526 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002528 }
2529 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002532 PyErr_SetString(PyExc_ValueError,
2533 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534 }
2535 return fp;
2536}
2537
Guido van Rossum79f25d91997-04-29 20:08:16 +00002538static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002539imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002540{
2541 char *name;
2542 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002543 PyObject *fob = NULL;
2544 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002545 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002546 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002547 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002548 return NULL;
2549 fp = get_file(pathname, fob, "rb");
2550 if (fp == NULL)
2551 return NULL;
2552 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002553 if (fob == NULL)
2554 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002555 return m;
2556}
2557
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002558#ifdef HAVE_DYNAMIC_LOADING
2559
Guido van Rossum79f25d91997-04-29 20:08:16 +00002560static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002561imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002562{
2563 char *name;
2564 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002565 PyObject *fob = NULL;
2566 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002567 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002568 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002569 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002571 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002572 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002573 if (fp == NULL)
2574 return NULL;
2575 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002576 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002577 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002578}
2579
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002580#endif /* HAVE_DYNAMIC_LOADING */
2581
Guido van Rossum79f25d91997-04-29 20:08:16 +00002582static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002583imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002584{
2585 char *name;
2586 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 PyObject *fob = NULL;
2588 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002589 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002590 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592 return NULL;
2593 fp = get_file(pathname, fob, "r");
2594 if (fp == NULL)
2595 return NULL;
2596 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002597 if (fob == NULL)
2598 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002599 return m;
2600}
2601
Guido van Rossum79f25d91997-04-29 20:08:16 +00002602static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002603imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002604{
2605 char *name;
2606 PyObject *fob;
2607 char *pathname;
2608 char *suffix; /* Unused */
2609 char *mode;
2610 int type;
2611 FILE *fp;
2612
Guido van Rossum43713e52000-02-29 13:59:29 +00002613 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002614 &name, &fob, &pathname,
2615 &suffix, &mode, &type))
2616 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002617 if (*mode) {
2618 /* Mode must start with 'r' or 'U' and must not contain '+'.
2619 Implicit in this test is the assumption that the mode
2620 may contain other modifiers like 'b' or 't'. */
2621
2622 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002623 PyErr_Format(PyExc_ValueError,
2624 "invalid file open mode %.200s", mode);
2625 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002626 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002627 }
2628 if (fob == Py_None)
2629 fp = NULL;
2630 else {
2631 if (!PyFile_Check(fob)) {
2632 PyErr_SetString(PyExc_ValueError,
2633 "load_module arg#2 should be a file or None");
2634 return NULL;
2635 }
2636 fp = get_file(pathname, fob, mode);
2637 if (fp == NULL)
2638 return NULL;
2639 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002640 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002641}
2642
2643static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002644imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002645{
2646 char *name;
2647 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002648 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002649 return NULL;
2650 return load_package(name, pathname);
2651}
2652
2653static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002654imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002655{
2656 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002657 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002658 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002659 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002660}
2661
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002662/* Doc strings */
2663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002664PyDoc_STRVAR(doc_imp,
2665"This module provides the components needed to build your own\n\
2666__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002668PyDoc_STRVAR(doc_find_module,
2669"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002670Search for a module. If path is omitted or None, search for a\n\
2671built-in, frozen or special module and continue search in sys.path.\n\
2672The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002673package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002675PyDoc_STRVAR(doc_load_module,
2676"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002677Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680PyDoc_STRVAR(doc_get_magic,
2681"get_magic() -> string\n\
2682Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002684PyDoc_STRVAR(doc_get_suffixes,
2685"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002686Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002687that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002689PyDoc_STRVAR(doc_new_module,
2690"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002691Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002692The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002694PyDoc_STRVAR(doc_lock_held,
2695"lock_held() -> 0 or 1\n\
Tim Peters69232342001-08-30 05:16:13 +00002696Return 1 if the import lock is currently held.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002697On platforms without threads, return 0.");
Tim Peters69232342001-08-30 05:16:13 +00002698
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002699PyDoc_STRVAR(doc_acquire_lock,
2700"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002701Acquires the interpreter's import lock for the current thread.\n\
2702This lock should be used by import hooks to ensure thread-safety\n\
2703when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002704On platforms without threads, this function does nothing.");
2705
2706PyDoc_STRVAR(doc_release_lock,
2707"release_lock() -> None\n\
2708Release the interpreter's import lock.\n\
2709On platforms without threads, this function does nothing.");
2710
Guido van Rossum79f25d91997-04-29 20:08:16 +00002711static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002712 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2713 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2714 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2715 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2716 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2717 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2718 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2719 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002720 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002721 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2722 {"init_builtin", imp_init_builtin, METH_VARARGS},
2723 {"init_frozen", imp_init_frozen, METH_VARARGS},
2724 {"is_builtin", imp_is_builtin, METH_VARARGS},
2725 {"is_frozen", imp_is_frozen, METH_VARARGS},
2726 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002727#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002728 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002729#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002730 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002731 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002732 {NULL, NULL} /* sentinel */
2733};
2734
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002735static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002736setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002737{
2738 PyObject *v;
2739 int err;
2740
2741 v = PyInt_FromLong((long)value);
2742 err = PyDict_SetItemString(d, name, v);
2743 Py_XDECREF(v);
2744 return err;
2745}
2746
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002747PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002748initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002749{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002750 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002751
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002752 m = Py_InitModule4("imp", imp_methods, doc_imp,
2753 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002754 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002755
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002756 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2757 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2758 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2759 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2760 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2761 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2762 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2763 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002764 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002765 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002766
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002767 failure:
2768 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002769}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002770
2771
Guido van Rossumb18618d2000-05-03 23:44:39 +00002772/* API for embedding applications that want to add their own entries
2773 to the table of built-in modules. This should normally be called
2774 *before* Py_Initialize(). When the table resize fails, -1 is
2775 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002776
2777 After a similar function by Just van Rossum. */
2778
2779int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002780PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002781{
2782 static struct _inittab *our_copy = NULL;
2783 struct _inittab *p;
2784 int i, n;
2785
2786 /* Count the number of entries in both tables */
2787 for (n = 0; newtab[n].name != NULL; n++)
2788 ;
2789 if (n == 0)
2790 return 0; /* Nothing to do */
2791 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2792 ;
2793
2794 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002795 p = our_copy;
2796 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002797 if (p == NULL)
2798 return -1;
2799
2800 /* Copy the tables into the new memory */
2801 if (our_copy != PyImport_Inittab)
2802 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2803 PyImport_Inittab = our_copy = p;
2804 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2805
2806 return 0;
2807}
2808
2809/* Shorthand to add a single entry given a name and a function */
2810
2811int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002812PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002813{
2814 struct _inittab newtab[2];
2815
2816 memset(newtab, '\0', sizeof newtab);
2817
2818 newtab[0].name = name;
2819 newtab[0].initfunc = initfunc;
2820
2821 return PyImport_ExtendInittab(newtab);
2822}