blob: 80427f8e1c8e54e850919fa56c753e30038d3d50 [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
Tim Peters1cd70172004-08-02 03:52:12 +0000560/* Remove name from sys.modules, if it's there. */
561static void
562_RemoveModule(const char *name)
563{
564 PyObject *modules = PyImport_GetModuleDict();
565 if (PyDict_GetItemString(modules, name) == NULL)
566 return;
567 if (PyDict_DelItemString(modules, name) < 0)
568 Py_FatalError("import: deleting existing key in"
569 "sys.modules failed");
570}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000572/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000573 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
574 * removed from sys.modules, to avoid leaving damaged module objects
575 * in sys.modules. The caller may wish to restore the original
576 * module object (if any) in this case; PyImport_ReloadModule is an
577 * example.
578 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000580PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000582 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
583}
584
585PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000586PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000587{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592 if (m == NULL)
593 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000594 /* If the module is being reloaded, we get the old module back
595 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000596 d = PyModule_GetDict(m);
597 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
598 if (PyDict_SetItemString(d, "__builtins__",
599 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000600 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000601 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000602 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000603 v = NULL;
604 if (pathname != NULL) {
605 v = PyString_FromString(pathname);
606 if (v == NULL)
607 PyErr_Clear();
608 }
609 if (v == NULL) {
610 v = ((PyCodeObject *)co)->co_filename;
611 Py_INCREF(v);
612 }
613 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000615 Py_DECREF(v);
616
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000617 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000619 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000621
Guido van Rossum25ce5661997-08-02 03:10:38 +0000622 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000623 PyErr_Format(PyExc_ImportError,
624 "Loaded module %.200s not found in sys.modules",
625 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000626 return NULL;
627 }
628
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630
631 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000632
633 error:
634 _RemoveModule(name);
635 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636}
637
638
639/* Given a pathname for a Python source file, fill a buffer with the
640 pathname for the corresponding compiled file. Return the pathname
641 for the compiled file, or NULL if there's no space in the buffer.
642 Doesn't set an exception. */
643
644static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646{
Tim Petersc1731372001-08-04 08:12:36 +0000647 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 if (len+2 > buflen)
649 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000650
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000651#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000652 /* Treat .pyw as if it were .py. The case of ".pyw" must match
653 that used in _PyImport_StandardFiletab. */
654 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
655 --len; /* pretend 'w' isn't there */
656#endif
657 memcpy(buf, pathname, len);
658 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
659 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660
661 return buf;
662}
663
664
665/* Given a pathname for a Python source file, its time of last
666 modification, and a pathname for a compiled file, check whether the
667 compiled file represents the same version of the source. If so,
668 return a FILE pointer for the compiled file, positioned just after
669 the header; if not, return NULL.
670 Doesn't set an exception. */
671
672static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000673check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674{
675 FILE *fp;
676 long magic;
677 long pyc_mtime;
678
679 fp = fopen(cpathname, "rb");
680 if (fp == NULL)
681 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000683 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000685 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686 fclose(fp);
687 return NULL;
688 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000690 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000692 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693 fclose(fp);
694 return NULL;
695 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000697 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 return fp;
699}
700
701
702/* Read a code object from a file and check it for validity */
703
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000705read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708
Tim Petersd9b9ac82001-01-28 00:27:39 +0000709 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000710 if (co == NULL)
711 return NULL;
712 if (!PyCode_Check(co)) {
713 PyErr_Format(PyExc_ImportError,
714 "Non-code object in %.200s", cpathname);
715 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716 return NULL;
717 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719}
720
721
722/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000723 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000724
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000726load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000727{
728 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 PyCodeObject *co;
730 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000733 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000734 PyErr_Format(PyExc_ImportError,
735 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736 return NULL;
737 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000739 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740 if (co == NULL)
741 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000743 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000745 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747
748 return m;
749}
750
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751/* Parse a source file and return the corresponding code object */
752
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000754parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757 node *n;
758
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000759 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760 if (n == NULL)
761 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 co = PyNode_Compile(n, pathname);
763 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000764
765 return co;
766}
767
768
Guido van Rossum55a83382000-09-20 20:31:38 +0000769/* Helper to open a bytecode file for writing in exclusive mode */
770
771static FILE *
772open_exclusive(char *filename)
773{
774#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
775 /* Use O_EXCL to avoid a race condition when another process tries to
776 write the same file. When that happens, our open() call fails,
777 which is just fine (since it's only a cache).
778 XXX If the file exists and is writable but the directory is not
779 writable, the file will never be written. Oh well.
780 */
781 int fd;
782 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000783 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
784#ifdef O_BINARY
785 |O_BINARY /* necessary for Windows */
786#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000787#ifdef __VMS
788 , 0666, "ctxt=bin", "shr=nil");
789#else
790 , 0666);
791#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000792 if (fd < 0)
793 return NULL;
794 return fdopen(fd, "wb");
795#else
796 /* Best we can do -- on Windows this can't happen anyway */
797 return fopen(filename, "wb");
798#endif
799}
800
801
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802/* Write a compiled module to a file, placing the time of last
803 modification of its source into the header.
804 Errors are ignored, if a write error occurs an attempt is made to
805 remove the file. */
806
807static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000808write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000809{
810 FILE *fp;
811
Guido van Rossum55a83382000-09-20 20:31:38 +0000812 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000813 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000815 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816 "# can't create %s\n", cpathname);
817 return;
818 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000819 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000821 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
822 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000823 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000824 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000825 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826 /* Don't keep partial file */
827 fclose(fp);
828 (void) unlink(cpathname);
829 return;
830 }
831 /* Now write the true mtime */
832 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000833 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000834 fflush(fp);
835 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000837 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838}
839
840
841/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000842 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
843 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000846load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847{
Fred Drake4c82b232000-06-30 16:18:57 +0000848 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849 FILE *fpc;
850 char buf[MAXPATHLEN+1];
851 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 PyCodeObject *co;
853 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000855 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000856 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000857 return NULL;
858#if SIZEOF_TIME_T > 4
859 /* Python's .pyc timestamp handling presumes that the timestamp fits
860 in 4 bytes. This will be fine until sometime in the year 2038,
861 when a 4-byte signed time_t will overflow.
862 */
863 if (mtime >> 32) {
864 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000865 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000866 return NULL;
867 }
868#endif
Tim Peters36515e22001-11-18 04:06:29 +0000869 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000870 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871 if (cpathname != NULL &&
872 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000873 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000874 fclose(fpc);
875 if (co == NULL)
876 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000878 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000879 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000880 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881 }
882 else {
883 co = parse_source_module(pathname, fp);
884 if (co == NULL)
885 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000887 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000888 name, pathname);
889 write_compiled_module(co, cpathname, mtime);
890 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000891 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893
894 return m;
895}
896
897
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000898/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000899static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
900static struct filedescr *find_module(char *, char *, PyObject *,
901 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000902static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000903
904/* Load a package and return its module object WITH INCREMENTED
905 REFERENCE COUNT */
906
907static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000908load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000909{
Tim Peters1cd70172004-08-02 03:52:12 +0000910 PyObject *m, *d;
911 PyObject *file = NULL;
912 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000913 int err;
914 char buf[MAXPATHLEN+1];
915 FILE *fp = NULL;
916 struct filedescr *fdp;
917
918 m = PyImport_AddModule(name);
919 if (m == NULL)
920 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000921 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000922 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000923 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000924 d = PyModule_GetDict(m);
925 file = PyString_FromString(pathname);
926 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000927 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000928 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000929 if (path == NULL)
930 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931 err = PyDict_SetItemString(d, "__file__", file);
932 if (err == 0)
933 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000934 if (err != 0)
935 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000936 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000937 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000938 if (fdp == NULL) {
939 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
940 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000941 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000942 }
943 else
944 m = NULL;
945 goto cleanup;
946 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000947 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000948 if (fp != NULL)
949 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000950 goto cleanup;
951
952 error:
953 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000954 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000955 Py_XDECREF(path);
956 Py_XDECREF(file);
957 return m;
958}
959
960
961/* Helper to test for built-in module */
962
963static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000964is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000965{
966 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000967 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
968 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
969 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000970 return -1;
971 else
972 return 1;
973 }
974 }
975 return 0;
976}
977
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000978
Just van Rossum52e14d62002-12-30 22:08:05 +0000979/* Return an importer object for a sys.path/pkg.__path__ item 'p',
980 possibly by fetching it from the path_importer_cache dict. If it
981 wasn't yet cached, traverse path_hooks until it a hook is found
982 that can handle the path item. Return None if no hook could;
983 this tells our caller it should fall back to the builtin
984 import mechanism. Cache the result in path_importer_cache.
985 Returns a borrowed reference. */
986
987static PyObject *
988get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
989 PyObject *p)
990{
991 PyObject *importer;
992 int j, nhooks;
993
994 /* These conditions are the caller's responsibility: */
995 assert(PyList_Check(path_hooks));
996 assert(PyDict_Check(path_importer_cache));
997
998 nhooks = PyList_Size(path_hooks);
999 if (nhooks < 0)
1000 return NULL; /* Shouldn't happen */
1001
1002 importer = PyDict_GetItem(path_importer_cache, p);
1003 if (importer != NULL)
1004 return importer;
1005
1006 /* set path_importer_cache[p] to None to avoid recursion */
1007 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1008 return NULL;
1009
1010 for (j = 0; j < nhooks; j++) {
1011 PyObject *hook = PyList_GetItem(path_hooks, j);
1012 if (hook == NULL)
1013 return NULL;
1014 importer = PyObject_CallFunction(hook, "O", p);
1015 if (importer != NULL)
1016 break;
1017
1018 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1019 return NULL;
1020 }
1021 PyErr_Clear();
1022 }
1023 if (importer == NULL)
1024 importer = Py_None;
1025 else if (importer != Py_None) {
1026 int err = PyDict_SetItem(path_importer_cache, p, importer);
1027 Py_DECREF(importer);
1028 if (err != 0)
1029 return NULL;
1030 }
1031 return importer;
1032}
1033
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001034/* Search the path (default sys.path) for a module. Return the
1035 corresponding filedescr struct, and (via return arguments) the
1036 pathname and an open file. Return NULL if the module is not found. */
1037
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001038#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001039extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1040 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001041#endif
1042
Tim Peters50d8d372001-02-28 05:34:27 +00001043static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001044static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001045static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001046
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001047static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001048find_module(char *fullname, char *subname, PyObject *path, char *buf,
1049 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001050{
Fred Drake4c82b232000-06-30 16:18:57 +00001051 int i, npath;
1052 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001053 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001054 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001055 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001056 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001057#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001058 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001059#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001060 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1061 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1062 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001063 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001064#if defined(PYOS_OS2)
1065 size_t saved_len;
1066 size_t saved_namelen;
1067 char *saved_buf = NULL;
1068#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001069 if (p_loader != NULL)
1070 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001071
Just van Rossum52e14d62002-12-30 22:08:05 +00001072 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001073 PyErr_SetString(PyExc_OverflowError,
1074 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001075 return NULL;
1076 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001077 strcpy(name, subname);
1078
1079 /* sys.meta_path import hook */
1080 if (p_loader != NULL) {
1081 PyObject *meta_path;
1082
1083 meta_path = PySys_GetObject("meta_path");
1084 if (meta_path == NULL || !PyList_Check(meta_path)) {
1085 PyErr_SetString(PyExc_ImportError,
1086 "sys.meta_path must be a list of "
1087 "import hooks");
1088 return NULL;
1089 }
1090 Py_INCREF(meta_path); /* zap guard */
1091 npath = PyList_Size(meta_path);
1092 for (i = 0; i < npath; i++) {
1093 PyObject *loader;
1094 PyObject *hook = PyList_GetItem(meta_path, i);
1095 loader = PyObject_CallMethod(hook, "find_module",
1096 "sO", fullname,
1097 path != NULL ?
1098 path : Py_None);
1099 if (loader == NULL) {
1100 Py_DECREF(meta_path);
1101 return NULL; /* true error */
1102 }
1103 if (loader != Py_None) {
1104 /* a loader was found */
1105 *p_loader = loader;
1106 Py_DECREF(meta_path);
1107 return &importhookdescr;
1108 }
1109 Py_DECREF(loader);
1110 }
1111 Py_DECREF(meta_path);
1112 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001113
1114 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001115 /* The only type of submodule allowed inside a "frozen"
1116 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001117 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1118 PyErr_SetString(PyExc_ImportError,
1119 "full frozen module name too long");
1120 return NULL;
1121 }
1122 strcpy(buf, PyString_AsString(path));
1123 strcat(buf, ".");
1124 strcat(buf, name);
1125 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001126 if (find_frozen(name) != NULL) {
1127 strcpy(buf, name);
1128 return &fd_frozen;
1129 }
1130 PyErr_Format(PyExc_ImportError,
1131 "No frozen submodule named %.200s", name);
1132 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001133 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001134 if (path == NULL) {
1135 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001136 strcpy(buf, name);
1137 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001138 }
Greg Ward201baee2001-10-04 14:52:06 +00001139 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001140 strcpy(buf, name);
1141 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001142 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001143
Guido van Rossumac279101996-08-22 23:10:58 +00001144#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001145 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1146 if (fp != NULL) {
1147 *p_fp = fp;
1148 return fdp;
1149 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001150#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001151 path = PySys_GetObject("path");
1152 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153 if (path == NULL || !PyList_Check(path)) {
1154 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001155 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001156 return NULL;
1157 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001158
1159 path_hooks = PySys_GetObject("path_hooks");
1160 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1161 PyErr_SetString(PyExc_ImportError,
1162 "sys.path_hooks must be a list of "
1163 "import hooks");
1164 return NULL;
1165 }
1166 path_importer_cache = PySys_GetObject("path_importer_cache");
1167 if (path_importer_cache == NULL ||
1168 !PyDict_Check(path_importer_cache)) {
1169 PyErr_SetString(PyExc_ImportError,
1170 "sys.path_importer_cache must be a dict");
1171 return NULL;
1172 }
1173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001175 namelen = strlen(name);
1176 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001177 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001179#ifdef Py_USING_UNICODE
1180 if (PyUnicode_Check(v)) {
1181 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1182 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1183 if (copy == NULL)
1184 return NULL;
1185 v = copy;
1186 }
1187 else
1188#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001190 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001192 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1193 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001194 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001195 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001197 if (strlen(buf) != len) {
1198 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001199 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001200 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001201
1202 /* sys.path_hooks import hook */
1203 if (p_loader != NULL) {
1204 PyObject *importer;
1205
1206 importer = get_path_importer(path_importer_cache,
1207 path_hooks, v);
1208 if (importer == NULL)
1209 return NULL;
1210 /* Note: importer is a borrowed reference */
1211 if (importer != Py_None) {
1212 PyObject *loader;
1213 loader = PyObject_CallMethod(importer,
1214 "find_module",
1215 "s", fullname);
1216 if (loader == NULL)
1217 return NULL; /* error */
1218 if (loader != Py_None) {
1219 /* a loader was found */
1220 *p_loader = loader;
1221 return &importhookdescr;
1222 }
1223 Py_DECREF(loader);
1224 }
1225 /* no hook was successful, use builtin import */
1226 }
1227
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001228 if (len > 0 && buf[len-1] != SEP
1229#ifdef ALTSEP
1230 && buf[len-1] != ALTSEP
1231#endif
1232 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001234 strcpy(buf+len, name);
1235 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001236
1237 /* Check for package import (buf holds a directory name,
1238 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001239#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001240 if (stat(buf, &statbuf) == 0 && /* it exists */
1241 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1242 find_init_module(buf) && /* it has __init__.py */
1243 case_ok(buf, len, namelen, name)) { /* and case matches */
1244 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001245 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001246 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001247#else
1248 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001249#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001250 if (isdir(buf) &&
1251 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001252 case_ok(buf, len, namelen, name)) {
1253 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001254 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001255 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001256#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001257#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001258#if defined(PYOS_OS2)
1259 /* take a snapshot of the module spec for restoration
1260 * after the 8 character DLL hackery
1261 */
1262 saved_buf = strdup(buf);
1263 saved_len = len;
1264 saved_namelen = namelen;
1265#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001267#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001268 /* OS/2 limits DLLs to 8 character names (w/o
1269 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001270 * so if the name is longer than that and its a
1271 * dynamically loaded module we're going to try,
1272 * truncate the name before trying
1273 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001274 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001275 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001276 const struct filedescr *scan;
1277 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001278 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001279 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001280 break;
1281 else
1282 scan++;
1283 }
1284 if (scan->suffix != NULL) {
1285 /* yes, so truncate the name */
1286 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001287 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001288 buf[len] = '\0';
1289 }
1290 }
1291#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001292 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001293 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001294 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001295 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001296 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001297 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001298 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001299 if (fp != NULL) {
1300 if (case_ok(buf, len, namelen, name))
1301 break;
1302 else { /* continue search */
1303 fclose(fp);
1304 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001305 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001306 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001307#if defined(PYOS_OS2)
1308 /* restore the saved snapshot */
1309 strcpy(buf, saved_buf);
1310 len = saved_len;
1311 namelen = saved_namelen;
1312#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001313 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001314#if defined(PYOS_OS2)
1315 /* don't need/want the module name snapshot anymore */
1316 if (saved_buf)
1317 {
1318 free(saved_buf);
1319 saved_buf = NULL;
1320 }
1321#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001322 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001323 if (fp != NULL)
1324 break;
1325 }
1326 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001327 PyErr_Format(PyExc_ImportError,
1328 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001329 return NULL;
1330 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001331 *p_fp = fp;
1332 return fdp;
1333}
1334
Tim Petersd1e87a82001-03-01 18:12:00 +00001335/* case_ok(char* buf, int len, int namelen, char* name)
1336 * The arguments here are tricky, best shown by example:
1337 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1338 * ^ ^ ^ ^
1339 * |--------------------- buf ---------------------|
1340 * |------------------- len ------------------|
1341 * |------ name -------|
1342 * |----- namelen -----|
1343 * buf is the full path, but len only counts up to (& exclusive of) the
1344 * extension. name is the module name, also exclusive of extension.
1345 *
1346 * We've already done a successful stat() or fopen() on buf, so know that
1347 * there's some match, possibly case-insensitive.
1348 *
Tim Peters50d8d372001-02-28 05:34:27 +00001349 * case_ok() is to return 1 if there's a case-sensitive match for
1350 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1351 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001352 *
Tim Peters50d8d372001-02-28 05:34:27 +00001353 * case_ok() is used to implement case-sensitive import semantics even
1354 * on platforms with case-insensitive filesystems. It's trivial to implement
1355 * for case-sensitive filesystems. It's pretty much a cross-platform
1356 * nightmare for systems with case-insensitive filesystems.
1357 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001358
Tim Peters50d8d372001-02-28 05:34:27 +00001359/* First we may need a pile of platform-specific header files; the sequence
1360 * of #if's here should match the sequence in the body of case_ok().
1361 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001362#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001363#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001364#ifdef __CYGWIN__
1365#include <sys/cygwin.h>
1366#endif
1367
Tim Peters50d8d372001-02-28 05:34:27 +00001368#elif defined(DJGPP)
1369#include <dir.h>
1370
Tim Petersd1e87a82001-03-01 18:12:00 +00001371#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001372#include <sys/types.h>
1373#include <dirent.h>
1374
Andrew MacIntyred9400542002-02-26 11:41:34 +00001375#elif defined(PYOS_OS2)
1376#define INCL_DOS
1377#define INCL_DOSERRORS
1378#define INCL_NOPMAPI
1379#include <os2.h>
1380
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001381#elif defined(RISCOS)
1382#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001383#endif
1384
Guido van Rossum0980bd91998-02-13 17:18:36 +00001385static int
Tim Peters50d8d372001-02-28 05:34:27 +00001386case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001387{
Tim Peters50d8d372001-02-28 05:34:27 +00001388/* Pick a platform-specific implementation; the sequence of #if's here should
1389 * match the sequence just above.
1390 */
1391
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001392/* MS_WINDOWS || __CYGWIN__ */
1393#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001394 WIN32_FIND_DATA data;
1395 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001396#ifdef __CYGWIN__
1397 char tempbuf[MAX_PATH];
1398#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001399
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001400 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001401 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001402
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001403#ifdef __CYGWIN__
1404 cygwin32_conv_to_win32_path(buf, tempbuf);
1405 h = FindFirstFile(tempbuf, &data);
1406#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001407 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001408#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001409 if (h == INVALID_HANDLE_VALUE) {
1410 PyErr_Format(PyExc_NameError,
1411 "Can't find file for module %.100s\n(filename %.300s)",
1412 name, buf);
1413 return 0;
1414 }
1415 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001416 return strncmp(data.cFileName, name, namelen) == 0;
1417
1418/* DJGPP */
1419#elif defined(DJGPP)
1420 struct ffblk ffblk;
1421 int done;
1422
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001423 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001424 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001425
1426 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1427 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001428 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001429 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001430 name, buf);
1431 return 0;
1432 }
Tim Peters50d8d372001-02-28 05:34:27 +00001433 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001434
Tim Peters677898a2001-03-02 03:28:03 +00001435/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001436#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001437 DIR *dirp;
1438 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001439 char dirname[MAXPATHLEN + 1];
1440 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001441
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001442 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001443 return 1;
1444
Tim Petersd1e87a82001-03-01 18:12:00 +00001445 /* Copy the dir component into dirname; substitute "." if empty */
1446 if (dirlen <= 0) {
1447 dirname[0] = '.';
1448 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001449 }
1450 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001451 assert(dirlen <= MAXPATHLEN);
1452 memcpy(dirname, buf, dirlen);
1453 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001454 }
1455 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001456 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001457 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001458 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001459 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001460 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001461#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001462 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001463#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001464 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001465#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001466 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001467 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001468 (void)closedir(dirp);
1469 return 1; /* Found */
1470 }
1471 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001472 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001473 }
Tim Peters430f5d42001-03-01 01:30:56 +00001474 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001475
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001476/* RISC OS */
1477#elif defined(RISCOS)
1478 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1479 char buf2[MAXPATHLEN+2];
1480 char *nameWithExt = buf+len-namelen;
1481 int canonlen;
1482 os_error *e;
1483
1484 if (Py_GETENV("PYTHONCASEOK") != NULL)
1485 return 1;
1486
1487 /* workaround:
1488 append wildcard, otherwise case of filename wouldn't be touched */
1489 strcpy(buf2, buf);
1490 strcat(buf2, "*");
1491
1492 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1493 canonlen = MAXPATHLEN+1-canonlen;
1494 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1495 return 0;
1496 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1497 return 1; /* match */
1498
1499 return 0;
1500
Andrew MacIntyred9400542002-02-26 11:41:34 +00001501/* OS/2 */
1502#elif defined(PYOS_OS2)
1503 HDIR hdir = 1;
1504 ULONG srchcnt = 1;
1505 FILEFINDBUF3 ffbuf;
1506 APIRET rc;
1507
1508 if (getenv("PYTHONCASEOK") != NULL)
1509 return 1;
1510
1511 rc = DosFindFirst(buf,
1512 &hdir,
1513 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1514 &ffbuf, sizeof(ffbuf),
1515 &srchcnt,
1516 FIL_STANDARD);
1517 if (rc != NO_ERROR)
1518 return 0;
1519 return strncmp(ffbuf.achName, name, namelen) == 0;
1520
Tim Peters50d8d372001-02-28 05:34:27 +00001521/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1522#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001523 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001524
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001525#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001526}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001527
Guido van Rossum0980bd91998-02-13 17:18:36 +00001528
Guido van Rossum197346f1997-10-31 18:38:52 +00001529#ifdef HAVE_STAT
1530/* Helper to look for __init__.py or __init__.py[co] in potential package */
1531static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001532find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001533{
Tim Peters0f9431f2001-07-05 03:47:53 +00001534 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001535 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001536 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001537 struct stat statbuf;
1538
Tim Peters0f9431f2001-07-05 03:47:53 +00001539/* For calling case_ok(buf, len, namelen, name):
1540 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1541 * ^ ^ ^ ^
1542 * |--------------------- buf ---------------------|
1543 * |------------------- len ------------------|
1544 * |------ name -------|
1545 * |----- namelen -----|
1546 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001547 if (save_len + 13 >= MAXPATHLEN)
1548 return 0;
1549 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001550 pname = buf + i;
1551 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001552 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001553 if (case_ok(buf,
1554 save_len + 9, /* len("/__init__") */
1555 8, /* len("__init__") */
1556 pname)) {
1557 buf[save_len] = '\0';
1558 return 1;
1559 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001560 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001561 i += strlen(pname);
1562 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001563 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001564 if (case_ok(buf,
1565 save_len + 9, /* len("/__init__") */
1566 8, /* len("__init__") */
1567 pname)) {
1568 buf[save_len] = '\0';
1569 return 1;
1570 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001571 }
1572 buf[save_len] = '\0';
1573 return 0;
1574}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001575
1576#else
1577
1578#ifdef RISCOS
1579static int
1580find_init_module(buf)
1581 char *buf;
1582{
1583 int save_len = strlen(buf);
1584 int i = save_len;
1585
1586 if (save_len + 13 >= MAXPATHLEN)
1587 return 0;
1588 buf[i++] = SEP;
1589 strcpy(buf+i, "__init__/py");
1590 if (isfile(buf)) {
1591 buf[save_len] = '\0';
1592 return 1;
1593 }
1594
1595 if (Py_OptimizeFlag)
1596 strcpy(buf+i, "o");
1597 else
1598 strcpy(buf+i, "c");
1599 if (isfile(buf)) {
1600 buf[save_len] = '\0';
1601 return 1;
1602 }
1603 buf[save_len] = '\0';
1604 return 0;
1605}
1606#endif /*RISCOS*/
1607
Guido van Rossum197346f1997-10-31 18:38:52 +00001608#endif /* HAVE_STAT */
1609
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001610
Tim Petersdbd9ba62000-07-09 03:09:57 +00001611static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001612
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001613/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001614 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001615
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001617load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001618{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001619 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001621 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001622
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001623 /* First check that there's an open file (if we need one) */
1624 switch (type) {
1625 case PY_SOURCE:
1626 case PY_COMPILED:
1627 if (fp == NULL) {
1628 PyErr_Format(PyExc_ValueError,
1629 "file object required for import (type code %d)",
1630 type);
1631 return NULL;
1632 }
1633 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001634
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001635 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001636
1637 case PY_SOURCE:
1638 m = load_source_module(name, buf, fp);
1639 break;
1640
1641 case PY_COMPILED:
1642 m = load_compiled_module(name, buf, fp);
1643 break;
1644
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001645#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001648 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001649#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001650
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001651 case PKG_DIRECTORY:
1652 m = load_package(name, buf);
1653 break;
1654
1655 case C_BUILTIN:
1656 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001657 if (buf != NULL && buf[0] != '\0')
1658 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001659 if (type == C_BUILTIN)
1660 err = init_builtin(name);
1661 else
1662 err = PyImport_ImportFrozenModule(name);
1663 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001664 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001665 if (err == 0) {
1666 PyErr_Format(PyExc_ImportError,
1667 "Purported %s module %.200s not found",
1668 type == C_BUILTIN ?
1669 "builtin" : "frozen",
1670 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001671 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001672 }
1673 modules = PyImport_GetModuleDict();
1674 m = PyDict_GetItemString(modules, name);
1675 if (m == NULL) {
1676 PyErr_Format(
1677 PyExc_ImportError,
1678 "%s module %.200s not properly initialized",
1679 type == C_BUILTIN ?
1680 "builtin" : "frozen",
1681 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001682 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001683 }
1684 Py_INCREF(m);
1685 break;
1686
Just van Rossum52e14d62002-12-30 22:08:05 +00001687 case IMP_HOOK: {
1688 if (loader == NULL) {
1689 PyErr_SetString(PyExc_ImportError,
1690 "import hook without loader");
1691 return NULL;
1692 }
1693 m = PyObject_CallMethod(loader, "load_module", "s", name);
1694 break;
1695 }
1696
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001697 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001698 PyErr_Format(PyExc_ImportError,
1699 "Don't know how to import %.200s (type code %d)",
1700 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001701 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001702
1703 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704
1705 return m;
1706}
1707
1708
1709/* Initialize a built-in module.
1710 Return 1 for succes, 0 if the module is not found, and -1 with
1711 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001712
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001713static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001714init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001715{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001716 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001717
Greg Ward201baee2001-10-04 14:52:06 +00001718 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001719 return 1;
1720
Guido van Rossum771c6c81997-10-31 18:37:24 +00001721 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001722 if (strcmp(name, p->name) == 0) {
1723 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001724 PyErr_Format(PyExc_ImportError,
1725 "Cannot re-init internal module %.200s",
1726 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001727 return -1;
1728 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001730 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001731 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001733 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001734 if (_PyImport_FixupExtension(name, name) == NULL)
1735 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001736 return 1;
1737 }
1738 }
1739 return 0;
1740}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001741
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001743/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001745static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001746find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001747{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001748 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001749
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001751 if (p->name == NULL)
1752 return NULL;
1753 if (strcmp(p->name, name) == 0)
1754 break;
1755 }
1756 return p;
1757}
1758
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001760get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001761{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001762 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001763 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001764
1765 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001766 PyErr_Format(PyExc_ImportError,
1767 "No such frozen object named %.200s",
1768 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001769 return NULL;
1770 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001771 if (p->code == NULL) {
1772 PyErr_Format(PyExc_ImportError,
1773 "Excluded frozen object named %.200s",
1774 name);
1775 return NULL;
1776 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001777 size = p->size;
1778 if (size < 0)
1779 size = -size;
1780 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001781}
1782
1783/* Initialize a frozen module.
1784 Return 1 for succes, 0 if the module is not found, and -1 with
1785 an exception set if the initialization failed.
1786 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001787
1788int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001789PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001790{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001791 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792 PyObject *co;
1793 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001794 int ispackage;
1795 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001796
1797 if (p == NULL)
1798 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001799 if (p->code == NULL) {
1800 PyErr_Format(PyExc_ImportError,
1801 "Excluded frozen object named %.200s",
1802 name);
1803 return -1;
1804 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001805 size = p->size;
1806 ispackage = (size < 0);
1807 if (ispackage)
1808 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001810 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001811 name, ispackage ? " package" : "");
1812 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001813 if (co == NULL)
1814 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001815 if (!PyCode_Check(co)) {
1816 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001817 PyErr_Format(PyExc_TypeError,
1818 "frozen object %.200s is not a code object",
1819 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001820 return -1;
1821 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001822 if (ispackage) {
1823 /* Set __path__ to the package name */
1824 PyObject *d, *s;
1825 int err;
1826 m = PyImport_AddModule(name);
1827 if (m == NULL)
1828 return -1;
1829 d = PyModule_GetDict(m);
1830 s = PyString_InternFromString(name);
1831 if (s == NULL)
1832 return -1;
1833 err = PyDict_SetItemString(d, "__path__", s);
1834 Py_DECREF(s);
1835 if (err != 0)
1836 return err;
1837 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001838 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001840 if (m == NULL)
1841 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001843 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001844}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001845
1846
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001847/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001848 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001849
Guido van Rossum79f25d91997-04-29 20:08:16 +00001850PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001851PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001852{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001853 PyObject *pname;
1854 PyObject *result;
1855
1856 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001857 if (pname == NULL)
1858 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001859 result = PyImport_Import(pname);
1860 Py_DECREF(pname);
1861 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001862}
1863
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001864/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001865static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1866static PyObject *load_next(PyObject *mod, PyObject *altmod,
1867 char **p_name, char *buf, int *p_buflen);
1868static int mark_miss(char *name);
1869static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1870 char *buf, int buflen, int recursive);
1871static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001872
1873/* The Magnum Opus of dotted-name import :-) */
1874
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001875static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001876import_module_ex(char *name, PyObject *globals, PyObject *locals,
1877 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001878{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001879 char buf[MAXPATHLEN+1];
1880 int buflen = 0;
1881 PyObject *parent, *head, *next, *tail;
1882
1883 parent = get_parent(globals, buf, &buflen);
1884 if (parent == NULL)
1885 return NULL;
1886
1887 head = load_next(parent, Py_None, &name, buf, &buflen);
1888 if (head == NULL)
1889 return NULL;
1890
1891 tail = head;
1892 Py_INCREF(tail);
1893 while (name) {
1894 next = load_next(tail, tail, &name, buf, &buflen);
1895 Py_DECREF(tail);
1896 if (next == NULL) {
1897 Py_DECREF(head);
1898 return NULL;
1899 }
1900 tail = next;
1901 }
1902
1903 if (fromlist != NULL) {
1904 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1905 fromlist = NULL;
1906 }
1907
1908 if (fromlist == NULL) {
1909 Py_DECREF(tail);
1910 return head;
1911 }
1912
1913 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001914 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001915 Py_DECREF(tail);
1916 return NULL;
1917 }
1918
1919 return tail;
1920}
1921
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001922PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001923PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1924 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001925{
1926 PyObject *result;
1927 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001928 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001929 if (unlock_import() < 0) {
1930 Py_XDECREF(result);
1931 PyErr_SetString(PyExc_RuntimeError,
1932 "not holding the import lock");
1933 return NULL;
1934 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001935 return result;
1936}
1937
Fred Drake87590902004-05-28 20:21:36 +00001938/* Return the package that an import is being performed in. If globals comes
1939 from the module foo.bar.bat (not itself a package), this returns the
1940 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1941 the package's entry in sys.modules is returned.
1942
1943 The *name* of the returned package is returned in buf, with the length of
1944 the name in *p_buflen.
1945
1946 If globals doesn't come from a package or a module in a package, or a
1947 corresponding entry is not found in sys.modules, Py_None is returned.
1948*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001949static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001950get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001951{
1952 static PyObject *namestr = NULL;
1953 static PyObject *pathstr = NULL;
1954 PyObject *modname, *modpath, *modules, *parent;
1955
1956 if (globals == NULL || !PyDict_Check(globals))
1957 return Py_None;
1958
1959 if (namestr == NULL) {
1960 namestr = PyString_InternFromString("__name__");
1961 if (namestr == NULL)
1962 return NULL;
1963 }
1964 if (pathstr == NULL) {
1965 pathstr = PyString_InternFromString("__path__");
1966 if (pathstr == NULL)
1967 return NULL;
1968 }
1969
1970 *buf = '\0';
1971 *p_buflen = 0;
1972 modname = PyDict_GetItem(globals, namestr);
1973 if (modname == NULL || !PyString_Check(modname))
1974 return Py_None;
1975
1976 modpath = PyDict_GetItem(globals, pathstr);
1977 if (modpath != NULL) {
1978 int len = PyString_GET_SIZE(modname);
1979 if (len > MAXPATHLEN) {
1980 PyErr_SetString(PyExc_ValueError,
1981 "Module name too long");
1982 return NULL;
1983 }
1984 strcpy(buf, PyString_AS_STRING(modname));
1985 *p_buflen = len;
1986 }
1987 else {
1988 char *start = PyString_AS_STRING(modname);
1989 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001990 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001991 if (lastdot == NULL)
1992 return Py_None;
1993 len = lastdot - start;
1994 if (len >= MAXPATHLEN) {
1995 PyErr_SetString(PyExc_ValueError,
1996 "Module name too long");
1997 return NULL;
1998 }
1999 strncpy(buf, start, len);
2000 buf[len] = '\0';
2001 *p_buflen = len;
2002 }
2003
2004 modules = PyImport_GetModuleDict();
2005 parent = PyDict_GetItemString(modules, buf);
2006 if (parent == NULL)
2007 parent = Py_None;
2008 return parent;
2009 /* We expect, but can't guarantee, if parent != None, that:
2010 - parent.__name__ == buf
2011 - parent.__dict__ is globals
2012 If this is violated... Who cares? */
2013}
2014
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002015/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002016static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002017load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2018 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002019{
2020 char *name = *p_name;
2021 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002022 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002023 char *p;
2024 PyObject *result;
2025
2026 if (dot == NULL) {
2027 *p_name = NULL;
2028 len = strlen(name);
2029 }
2030 else {
2031 *p_name = dot+1;
2032 len = dot-name;
2033 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002034 if (len == 0) {
2035 PyErr_SetString(PyExc_ValueError,
2036 "Empty module name");
2037 return NULL;
2038 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002039
2040 p = buf + *p_buflen;
2041 if (p != buf)
2042 *p++ = '.';
2043 if (p+len-buf >= MAXPATHLEN) {
2044 PyErr_SetString(PyExc_ValueError,
2045 "Module name too long");
2046 return NULL;
2047 }
2048 strncpy(p, name, len);
2049 p[len] = '\0';
2050 *p_buflen = p+len-buf;
2051
2052 result = import_submodule(mod, p, buf);
2053 if (result == Py_None && altmod != mod) {
2054 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002055 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002056 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002057 if (result != NULL && result != Py_None) {
2058 if (mark_miss(buf) != 0) {
2059 Py_DECREF(result);
2060 return NULL;
2061 }
2062 strncpy(buf, name, len);
2063 buf[len] = '\0';
2064 *p_buflen = len;
2065 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002066 }
2067 if (result == NULL)
2068 return NULL;
2069
2070 if (result == Py_None) {
2071 Py_DECREF(result);
2072 PyErr_Format(PyExc_ImportError,
2073 "No module named %.200s", name);
2074 return NULL;
2075 }
2076
2077 return result;
2078}
2079
2080static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002081mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002082{
2083 PyObject *modules = PyImport_GetModuleDict();
2084 return PyDict_SetItemString(modules, name, Py_None);
2085}
2086
2087static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002088ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2089 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002090{
2091 int i;
2092
2093 if (!PyObject_HasAttrString(mod, "__path__"))
2094 return 1;
2095
2096 for (i = 0; ; i++) {
2097 PyObject *item = PySequence_GetItem(fromlist, i);
2098 int hasit;
2099 if (item == NULL) {
2100 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2101 PyErr_Clear();
2102 return 1;
2103 }
2104 return 0;
2105 }
2106 if (!PyString_Check(item)) {
2107 PyErr_SetString(PyExc_TypeError,
2108 "Item in ``from list'' not a string");
2109 Py_DECREF(item);
2110 return 0;
2111 }
2112 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002113 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002114 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002115 /* See if the package defines __all__ */
2116 if (recursive)
2117 continue; /* Avoid endless recursion */
2118 all = PyObject_GetAttrString(mod, "__all__");
2119 if (all == NULL)
2120 PyErr_Clear();
2121 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002122 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002123 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002124 if (!ret)
2125 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002126 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002127 continue;
2128 }
2129 hasit = PyObject_HasAttr(mod, item);
2130 if (!hasit) {
2131 char *subname = PyString_AS_STRING(item);
2132 PyObject *submod;
2133 char *p;
2134 if (buflen + strlen(subname) >= MAXPATHLEN) {
2135 PyErr_SetString(PyExc_ValueError,
2136 "Module name too long");
2137 Py_DECREF(item);
2138 return 0;
2139 }
2140 p = buf + buflen;
2141 *p++ = '.';
2142 strcpy(p, subname);
2143 submod = import_submodule(mod, subname, buf);
2144 Py_XDECREF(submod);
2145 if (submod == NULL) {
2146 Py_DECREF(item);
2147 return 0;
2148 }
2149 }
2150 Py_DECREF(item);
2151 }
2152
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002153 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002154}
2155
Neil Schemenauer00b09662003-06-16 21:03:07 +00002156static int
2157add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2158 PyObject *modules)
2159{
2160 if (mod == Py_None)
2161 return 1;
2162 /* Irrespective of the success of this load, make a
2163 reference to it in the parent package module. A copy gets
2164 saved in the modules dictionary under the full name, so get a
2165 reference from there, if need be. (The exception is when the
2166 load failed with a SyntaxError -- then there's no trace in
2167 sys.modules. In that case, of course, do nothing extra.) */
2168 if (submod == NULL) {
2169 submod = PyDict_GetItemString(modules, fullname);
2170 if (submod == NULL)
2171 return 1;
2172 }
2173 if (PyModule_Check(mod)) {
2174 /* We can't use setattr here since it can give a
2175 * spurious warning if the submodule name shadows a
2176 * builtin name */
2177 PyObject *dict = PyModule_GetDict(mod);
2178 if (!dict)
2179 return 0;
2180 if (PyDict_SetItemString(dict, subname, submod) < 0)
2181 return 0;
2182 }
2183 else {
2184 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2185 return 0;
2186 }
2187 return 1;
2188}
2189
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002190static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002191import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002192{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002193 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002194 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002195
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002196 /* Require:
2197 if mod == None: subname == fullname
2198 else: mod.__name__ + "." + subname == fullname
2199 */
2200
Tim Peters50d8d372001-02-28 05:34:27 +00002201 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002202 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002203 }
2204 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002205 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002206 char buf[MAXPATHLEN+1];
2207 struct filedescr *fdp;
2208 FILE *fp = NULL;
2209
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002210 if (mod == Py_None)
2211 path = NULL;
2212 else {
2213 path = PyObject_GetAttrString(mod, "__path__");
2214 if (path == NULL) {
2215 PyErr_Clear();
2216 Py_INCREF(Py_None);
2217 return Py_None;
2218 }
2219 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002220
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002221 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002222 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2223 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002224 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002225 if (fdp == NULL) {
2226 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2227 return NULL;
2228 PyErr_Clear();
2229 Py_INCREF(Py_None);
2230 return Py_None;
2231 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002232 m = load_module(fullname, fp, buf, fdp->type, loader);
2233 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002234 if (fp)
2235 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002236 if (!add_submodule(mod, m, fullname, subname, modules)) {
2237 Py_XDECREF(m);
2238 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002239 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002240 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002241
2242 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002243}
2244
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002245
2246/* Re-import a module of any kind and return its module object, WITH
2247 INCREMENTED REFERENCE COUNT */
2248
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002250PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002251{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002252 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002253 PyObject *path = NULL;
2254 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002255 char buf[MAXPATHLEN+1];
2256 struct filedescr *fdp;
2257 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002258 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002259
Guido van Rossum79f25d91997-04-29 20:08:16 +00002260 if (m == NULL || !PyModule_Check(m)) {
2261 PyErr_SetString(PyExc_TypeError,
2262 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002263 return NULL;
2264 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002265 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002266 if (name == NULL)
2267 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002268 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002269 PyErr_Format(PyExc_ImportError,
2270 "reload(): module %.200s not in sys.modules",
2271 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002272 return NULL;
2273 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002274 subname = strrchr(name, '.');
2275 if (subname == NULL)
2276 subname = name;
2277 else {
2278 PyObject *parentname, *parent;
2279 parentname = PyString_FromStringAndSize(name, (subname-name));
2280 if (parentname == NULL)
2281 return NULL;
2282 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002283 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002284 if (parent == NULL) {
2285 PyErr_Format(PyExc_ImportError,
2286 "reload(): parent %.200s not in sys.modules",
2287 name);
2288 return NULL;
2289 }
2290 subname++;
2291 path = PyObject_GetAttrString(parent, "__path__");
2292 if (path == NULL)
2293 PyErr_Clear();
2294 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002295 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002296 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002297 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002298 if (fdp == NULL)
2299 return NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002300 newm = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002301 if (fp)
2302 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002303 if (newm == NULL) {
2304 /* load_module probably removed name from modules because of
2305 * the error. Put back the original module object. We're
2306 * going to return NULL in this case regardless of whether
2307 * replacing name succeeds, so the return value is ignored.
2308 */
2309 PyDict_SetItemString(modules, name, m);
2310 }
2311 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002312}
2313
2314
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002315/* Higher-level import emulator which emulates the "import" statement
2316 more accurately -- it invokes the __import__() function from the
2317 builtins of the current globals. This means that the import is
2318 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002319 environment, e.g. by "rexec".
2320 A dummy list ["__doc__"] is passed as the 4th argument so that
2321 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2322 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002323
2324PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002325PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002326{
2327 static PyObject *silly_list = NULL;
2328 static PyObject *builtins_str = NULL;
2329 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002330 PyObject *globals = NULL;
2331 PyObject *import = NULL;
2332 PyObject *builtins = NULL;
2333 PyObject *r = NULL;
2334
2335 /* Initialize constant string objects */
2336 if (silly_list == NULL) {
2337 import_str = PyString_InternFromString("__import__");
2338 if (import_str == NULL)
2339 return NULL;
2340 builtins_str = PyString_InternFromString("__builtins__");
2341 if (builtins_str == NULL)
2342 return NULL;
2343 silly_list = Py_BuildValue("[s]", "__doc__");
2344 if (silly_list == NULL)
2345 return NULL;
2346 }
2347
2348 /* Get the builtins from current globals */
2349 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002350 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002351 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002352 builtins = PyObject_GetItem(globals, builtins_str);
2353 if (builtins == NULL)
2354 goto err;
2355 }
2356 else {
2357 /* No globals -- use standard builtins, and fake globals */
2358 PyErr_Clear();
2359
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002360 builtins = PyImport_ImportModuleEx("__builtin__",
2361 NULL, NULL, NULL);
2362 if (builtins == NULL)
2363 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002364 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2365 if (globals == NULL)
2366 goto err;
2367 }
2368
2369 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002370 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002371 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002372 if (import == NULL)
2373 PyErr_SetObject(PyExc_KeyError, import_str);
2374 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002375 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002376 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002377 if (import == NULL)
2378 goto err;
2379
2380 /* Call the _import__ function with the proper argument list */
2381 r = PyObject_CallFunction(import, "OOOO",
2382 module_name, globals, globals, silly_list);
2383
2384 err:
2385 Py_XDECREF(globals);
2386 Py_XDECREF(builtins);
2387 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002388
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002389 return r;
2390}
2391
2392
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002393/* Module 'imp' provides Python access to the primitives used for
2394 importing modules.
2395*/
2396
Guido van Rossum79f25d91997-04-29 20:08:16 +00002397static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002398imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002399{
2400 char buf[4];
2401
Guido van Rossum96774c12000-05-01 20:19:08 +00002402 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2403 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2404 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2405 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002406
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002408}
2409
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002411imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002412{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002413 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002414 struct filedescr *fdp;
2415
Guido van Rossum79f25d91997-04-29 20:08:16 +00002416 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417 if (list == NULL)
2418 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002419 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2420 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002421 fdp->suffix, fdp->mode, fdp->type);
2422 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002423 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002424 return NULL;
2425 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002426 if (PyList_Append(list, item) < 0) {
2427 Py_DECREF(list);
2428 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002429 return NULL;
2430 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002431 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002432 }
2433 return list;
2434}
2435
Guido van Rossum79f25d91997-04-29 20:08:16 +00002436static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002437call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002439 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002440 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441 struct filedescr *fdp;
2442 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002443 FILE *fp = NULL;
2444
2445 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002446 if (path == Py_None)
2447 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002448 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002449 if (fdp == NULL)
2450 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002451 if (fp != NULL) {
2452 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2453 if (fob == NULL) {
2454 fclose(fp);
2455 return NULL;
2456 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002457 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002458 else {
2459 fob = Py_None;
2460 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002461 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002462 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002463 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 return ret;
2466}
2467
Guido van Rossum79f25d91997-04-29 20:08:16 +00002468static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002469imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002470{
2471 char *name;
2472 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002473 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002474 return NULL;
2475 return call_find_module(name, path);
2476}
2477
2478static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002479imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002480{
2481 char *name;
2482 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002483 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002484 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002485 return NULL;
2486 ret = init_builtin(name);
2487 if (ret < 0)
2488 return NULL;
2489 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002490 Py_INCREF(Py_None);
2491 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002492 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002493 m = PyImport_AddModule(name);
2494 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002495 return m;
2496}
2497
Guido van Rossum79f25d91997-04-29 20:08:16 +00002498static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002499imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002500{
2501 char *name;
2502 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002503 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002504 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002505 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002507 if (ret < 0)
2508 return NULL;
2509 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002510 Py_INCREF(Py_None);
2511 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002512 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002513 m = PyImport_AddModule(name);
2514 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002515 return m;
2516}
2517
Guido van Rossum79f25d91997-04-29 20:08:16 +00002518static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002519imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002520{
2521 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002522
Guido van Rossum43713e52000-02-29 13:59:29 +00002523 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002524 return NULL;
2525 return get_frozen_object(name);
2526}
2527
Guido van Rossum79f25d91997-04-29 20:08:16 +00002528static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002529imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002530{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002532 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002533 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002534 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002535}
2536
Guido van Rossum79f25d91997-04-29 20:08:16 +00002537static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002538imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002540 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002541 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002542 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002543 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002544 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002545 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002546}
2547
2548static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002549get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002550{
2551 FILE *fp;
2552 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002553 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002554 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002555 fp = fopen(pathname, mode);
2556 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002557 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558 }
2559 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002560 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002561 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002562 PyErr_SetString(PyExc_ValueError,
2563 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002564 }
2565 return fp;
2566}
2567
Guido van Rossum79f25d91997-04-29 20:08:16 +00002568static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002569imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570{
2571 char *name;
2572 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002573 PyObject *fob = NULL;
2574 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002575 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002576 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002577 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002578 return NULL;
2579 fp = get_file(pathname, fob, "rb");
2580 if (fp == NULL)
2581 return NULL;
2582 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002583 if (fob == NULL)
2584 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002585 return m;
2586}
2587
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002588#ifdef HAVE_DYNAMIC_LOADING
2589
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002591imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592{
2593 char *name;
2594 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002595 PyObject *fob = NULL;
2596 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002597 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002598 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002599 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002600 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002601 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002602 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002603 if (fp == NULL)
2604 return NULL;
2605 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002606 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002607 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002608}
2609
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002610#endif /* HAVE_DYNAMIC_LOADING */
2611
Guido van Rossum79f25d91997-04-29 20:08:16 +00002612static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002613imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002614{
2615 char *name;
2616 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002617 PyObject *fob = NULL;
2618 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002619 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002620 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002621 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002622 return NULL;
2623 fp = get_file(pathname, fob, "r");
2624 if (fp == NULL)
2625 return NULL;
2626 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002627 if (fob == NULL)
2628 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002629 return m;
2630}
2631
Guido van Rossum79f25d91997-04-29 20:08:16 +00002632static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002633imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002634{
2635 char *name;
2636 PyObject *fob;
2637 char *pathname;
2638 char *suffix; /* Unused */
2639 char *mode;
2640 int type;
2641 FILE *fp;
2642
Guido van Rossum43713e52000-02-29 13:59:29 +00002643 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002644 &name, &fob, &pathname,
2645 &suffix, &mode, &type))
2646 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002647 if (*mode) {
2648 /* Mode must start with 'r' or 'U' and must not contain '+'.
2649 Implicit in this test is the assumption that the mode
2650 may contain other modifiers like 'b' or 't'. */
2651
2652 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002653 PyErr_Format(PyExc_ValueError,
2654 "invalid file open mode %.200s", mode);
2655 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002656 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002657 }
2658 if (fob == Py_None)
2659 fp = NULL;
2660 else {
2661 if (!PyFile_Check(fob)) {
2662 PyErr_SetString(PyExc_ValueError,
2663 "load_module arg#2 should be a file or None");
2664 return NULL;
2665 }
2666 fp = get_file(pathname, fob, mode);
2667 if (fp == NULL)
2668 return NULL;
2669 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002670 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002671}
2672
2673static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002674imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002675{
2676 char *name;
2677 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002678 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002679 return NULL;
2680 return load_package(name, pathname);
2681}
2682
2683static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002684imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002685{
2686 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002687 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002688 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002689 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002690}
2691
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002692/* Doc strings */
2693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002694PyDoc_STRVAR(doc_imp,
2695"This module provides the components needed to build your own\n\
2696__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(doc_find_module,
2699"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002700Search for a module. If path is omitted or None, search for a\n\
2701built-in, frozen or special module and continue search in sys.path.\n\
2702The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002705PyDoc_STRVAR(doc_load_module,
2706"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002707Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002708The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002710PyDoc_STRVAR(doc_get_magic,
2711"get_magic() -> string\n\
2712Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002714PyDoc_STRVAR(doc_get_suffixes,
2715"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002716Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719PyDoc_STRVAR(doc_new_module,
2720"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002721Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002724PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002725"lock_held() -> boolean\n\
2726Return True if the import lock is currently held, else False.\n\
2727On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002728
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002729PyDoc_STRVAR(doc_acquire_lock,
2730"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002731Acquires the interpreter's import lock for the current thread.\n\
2732This lock should be used by import hooks to ensure thread-safety\n\
2733when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002734On platforms without threads, this function does nothing.");
2735
2736PyDoc_STRVAR(doc_release_lock,
2737"release_lock() -> None\n\
2738Release the interpreter's import lock.\n\
2739On platforms without threads, this function does nothing.");
2740
Guido van Rossum79f25d91997-04-29 20:08:16 +00002741static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002742 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2743 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2744 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2745 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2746 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2747 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2748 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2749 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002750 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002751 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2752 {"init_builtin", imp_init_builtin, METH_VARARGS},
2753 {"init_frozen", imp_init_frozen, METH_VARARGS},
2754 {"is_builtin", imp_is_builtin, METH_VARARGS},
2755 {"is_frozen", imp_is_frozen, METH_VARARGS},
2756 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002757#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002758 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002759#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002760 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002761 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002762 {NULL, NULL} /* sentinel */
2763};
2764
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002765static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002766setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002767{
2768 PyObject *v;
2769 int err;
2770
2771 v = PyInt_FromLong((long)value);
2772 err = PyDict_SetItemString(d, name, v);
2773 Py_XDECREF(v);
2774 return err;
2775}
2776
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002777PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002778initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002779{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002780 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002781
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002782 m = Py_InitModule4("imp", imp_methods, doc_imp,
2783 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002784 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002785
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002786 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2787 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2788 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2789 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2790 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2791 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2792 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2793 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002794 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002795 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002796
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002797 failure:
2798 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002799}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002800
2801
Guido van Rossumb18618d2000-05-03 23:44:39 +00002802/* API for embedding applications that want to add their own entries
2803 to the table of built-in modules. This should normally be called
2804 *before* Py_Initialize(). When the table resize fails, -1 is
2805 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002806
2807 After a similar function by Just van Rossum. */
2808
2809int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002810PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002811{
2812 static struct _inittab *our_copy = NULL;
2813 struct _inittab *p;
2814 int i, n;
2815
2816 /* Count the number of entries in both tables */
2817 for (n = 0; newtab[n].name != NULL; n++)
2818 ;
2819 if (n == 0)
2820 return 0; /* Nothing to do */
2821 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2822 ;
2823
2824 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002825 p = our_copy;
2826 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002827 if (p == NULL)
2828 return -1;
2829
2830 /* Copy the tables into the new memory */
2831 if (our_copy != PyImport_Inittab)
2832 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2833 PyImport_Inittab = our_copy = p;
2834 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2835
2836 return 0;
2837}
2838
2839/* Shorthand to add a single entry given a name and a function */
2840
2841int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002842PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002843{
2844 struct _inittab newtab[2];
2845
2846 memset(newtab, '\0', sizeof newtab);
2847
2848 newtab[0].name = name;
2849 newtab[0].initfunc = initfunc;
2850
2851 return PyImport_ExtendInittab(newtab);
2852}