blob: 8bbd31b5cc31605aa7246deae269fc83e01e42a6 [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
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000051 Python 2.4a3: 62051
Tim Peters36515e22001-11-18 04:06:29 +000052*/
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000053#define MAGIC (62051 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000054
Guido van Rossum96774c12000-05-01 20:19:08 +000055/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000056 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000057 compiler works which are enabled by command line switches. */
58static long pyc_magic = MAGIC;
59
Guido van Rossum25ce5661997-08-02 03:10:38 +000060/* See _PyImport_FixupExtension() below */
61static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000062
Guido van Rossum771c6c81997-10-31 18:37:24 +000063/* This table is defined in config.c: */
64extern struct _inittab _PyImport_Inittab[];
65
66struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000067
Guido van Rossumed1170e1999-12-20 21:23:41 +000068/* these tables define the module suffixes that Python recognizes */
69struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000070
71#ifdef RISCOS
72static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000073 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000074 {"/pyc", "rb", PY_COMPILED},
75 {0, 0}
76};
77#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000078static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000079 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000080#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000081 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000082#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000083 {".pyc", "rb", PY_COMPILED},
84 {0, 0}
85};
Guido van Rossum48a680c2001-03-02 06:34:14 +000086#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000087
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089
90void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000091_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Guido van Rossumed1170e1999-12-20 21:23:41 +000093 const struct filedescr *scan;
94 struct filedescr *filetab;
95 int countD = 0;
96 int countS = 0;
97
98 /* prepare _PyImport_Filetab: copy entries from
99 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
100 */
101 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
102 ++countD;
103 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
104 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000105 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000106 memcpy(filetab, _PyImport_DynLoadFiletab,
107 countD * sizeof(struct filedescr));
108 memcpy(filetab + countD, _PyImport_StandardFiletab,
109 countS * sizeof(struct filedescr));
110 filetab[countD + countS].suffix = NULL;
111
112 _PyImport_Filetab = filetab;
113
Guido van Rossum0824f631997-03-11 18:37:35 +0000114 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000115 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
116 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000117#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000118 if (strcmp(filetab->suffix, ".pyc") == 0)
119 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000120#else
121 if (strcmp(filetab->suffix, "/pyc") == 0)
122 filetab->suffix = "/pyo";
123#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000124 }
125 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000126
127 if (Py_UnicodeFlag) {
128 /* Fix the pyc_magic so that byte compiled code created
129 using the all-Unicode method doesn't interfere with
130 code created in normal operation mode. */
131 pyc_magic = MAGIC + 1;
132 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossum25ce5661997-08-02 03:10:38 +0000135void
Just van Rossum52e14d62002-12-30 22:08:05 +0000136_PyImportHooks_Init(void)
137{
138 PyObject *v, *path_hooks = NULL, *zimpimport;
139 int err = 0;
140
141 /* adding sys.path_hooks and sys.path_importer_cache, setting up
142 zipimport */
143
144 if (Py_VerboseFlag)
145 PySys_WriteStderr("# installing zipimport hook\n");
146
147 v = PyList_New(0);
148 if (v == NULL)
149 goto error;
150 err = PySys_SetObject("meta_path", v);
151 Py_DECREF(v);
152 if (err)
153 goto error;
154 v = PyDict_New();
155 if (v == NULL)
156 goto error;
157 err = PySys_SetObject("path_importer_cache", v);
158 Py_DECREF(v);
159 if (err)
160 goto error;
161 path_hooks = PyList_New(0);
162 if (path_hooks == NULL)
163 goto error;
164 err = PySys_SetObject("path_hooks", path_hooks);
165 if (err) {
166 error:
167 PyErr_Print();
168 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
169 "path_importer_cache failed");
170 }
171 zimpimport = PyImport_ImportModule("zipimport");
172 if (zimpimport == NULL) {
173 PyErr_Clear(); /* No zip import module -- okay */
174 if (Py_VerboseFlag)
175 PySys_WriteStderr("# can't import zipimport\n");
176 }
177 else {
178 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
179 "zipimporter");
180 Py_DECREF(zimpimport);
181 if (zipimporter == NULL) {
182 PyErr_Clear(); /* No zipimporter object -- okay */
183 if (Py_VerboseFlag)
184 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000185 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000186 }
187 else {
188 /* sys.path_hooks.append(zipimporter) */
189 err = PyList_Append(path_hooks, zipimporter);
190 Py_DECREF(zipimporter);
191 if (err)
192 goto error;
193 if (Py_VerboseFlag)
194 PySys_WriteStderr(
195 "# installed zipimport hook\n");
196 }
197 }
198 Py_DECREF(path_hooks);
199}
200
201void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203{
204 Py_XDECREF(extensions);
205 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000206 PyMem_DEL(_PyImport_Filetab);
207 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208}
209
210
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000211/* Locking primitives to prevent parallel imports of the same module
212 in different threads to return with a partially loaded module.
213 These calls are serialized by the global interpreter lock. */
214
215#ifdef WITH_THREAD
216
Guido van Rossum49b56061998-10-01 20:42:43 +0000217#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000218
Guido van Rossum65d5b571998-12-21 19:32:43 +0000219static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000220static long import_lock_thread = -1;
221static int import_lock_level = 0;
222
223static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000225{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000226 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000227 if (me == -1)
228 return; /* Too bad */
229 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000231 if (import_lock_thread == me) {
232 import_lock_level++;
233 return;
234 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000235 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
236 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000237 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000239 PyEval_RestoreThread(tstate);
240 }
241 import_lock_thread = me;
242 import_lock_level = 1;
243}
244
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000245static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000247{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000248 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000250 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000252 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000253 import_lock_level--;
254 if (import_lock_level == 0) {
255 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000257 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000258 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259}
260
261#else
262
263#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000264#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000265
266#endif
267
Tim Peters69232342001-08-30 05:16:13 +0000268static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000269imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000270{
Tim Peters69232342001-08-30 05:16:13 +0000271#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000272 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000273#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000274 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000275#endif
276}
277
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000279imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000280{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000281#ifdef WITH_THREAD
282 lock_import();
283#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000284 Py_INCREF(Py_None);
285 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286}
287
288static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000289imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000290{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000291#ifdef WITH_THREAD
292 if (unlock_import() < 0) {
293 PyErr_SetString(PyExc_RuntimeError,
294 "not holding the import lock");
295 return NULL;
296 }
297#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000298 Py_INCREF(Py_None);
299 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000300}
301
Guido van Rossum25ce5661997-08-02 03:10:38 +0000302/* Helper for sys */
303
304PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000307 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000308 if (interp->modules == NULL)
309 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
310 return interp->modules;
311}
312
Guido van Rossum3f5da241990-12-20 15:06:42 +0000313
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000314/* List of names to clear in sys */
315static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000316 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000317 "exc_type", "exc_value", "exc_traceback",
318 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000319 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000320 NULL
321};
322
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000323static char* sys_files[] = {
324 "stdin", "__stdin__",
325 "stdout", "__stdout__",
326 "stderr", "__stderr__",
327 NULL
328};
329
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000330
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000331/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332
Guido van Rossum3f5da241990-12-20 15:06:42 +0000333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335{
Guido van Rossum758eec01998-01-19 21:58:26 +0000336 int pos, ndone;
337 char *name;
338 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000339 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000340 PyObject *modules = interp->modules;
341
342 if (modules == NULL)
343 return; /* Already done */
344
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000345 /* Delete some special variables first. These are common
346 places where user values hide and people complain when their
347 destructors fail. Since the modules containing them are
348 deleted *last* of all, they would come too late in the normal
349 destruction order. Sigh. */
350
351 value = PyDict_GetItemString(modules, "__builtin__");
352 if (value != NULL && PyModule_Check(value)) {
353 dict = PyModule_GetDict(value);
354 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000355 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000356 PyDict_SetItemString(dict, "_", Py_None);
357 }
358 value = PyDict_GetItemString(modules, "sys");
359 if (value != NULL && PyModule_Check(value)) {
360 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000361 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000362 dict = PyModule_GetDict(value);
363 for (p = sys_deletes; *p != NULL; p++) {
364 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000365 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000366 PyDict_SetItemString(dict, *p, Py_None);
367 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000368 for (p = sys_files; *p != NULL; p+=2) {
369 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000370 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000371 v = PyDict_GetItemString(dict, *(p+1));
372 if (v == NULL)
373 v = Py_None;
374 PyDict_SetItemString(dict, *p, v);
375 }
376 }
377
378 /* First, delete __main__ */
379 value = PyDict_GetItemString(modules, "__main__");
380 if (value != NULL && PyModule_Check(value)) {
381 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000382 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000383 _PyModule_Clear(value);
384 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000385 }
386
Guido van Rossum758eec01998-01-19 21:58:26 +0000387 /* The special treatment of __builtin__ here is because even
388 when it's not referenced as a module, its dictionary is
389 referenced by almost every module's __builtins__. Since
390 deleting a module clears its dictionary (even if there are
391 references left to it), we need to delete the __builtin__
392 module last. Likewise, we don't delete sys until the very
393 end because it is implicitly referenced (e.g. by print).
394
395 Also note that we 'delete' modules by replacing their entry
396 in the modules dict with None, rather than really deleting
397 them; this avoids a rehash of the modules dictionary and
398 also marks them as "non existent" so they won't be
399 re-imported. */
400
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000401 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000402 one (skipping __builtin__ and sys) and delete them */
403 do {
404 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000405 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000406 while (PyDict_Next(modules, &pos, &key, &value)) {
407 if (value->ob_refcnt != 1)
408 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000409 if (PyString_Check(key) && PyModule_Check(value)) {
410 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000411 if (strcmp(name, "__builtin__") == 0)
412 continue;
413 if (strcmp(name, "sys") == 0)
414 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000415 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000416 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000417 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000418 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000419 PyDict_SetItem(modules, key, Py_None);
420 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000421 }
422 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000423 } while (ndone > 0);
424
Guido van Rossum758eec01998-01-19 21:58:26 +0000425 /* Next, delete all modules (still skipping __builtin__ and sys) */
426 pos = 0;
427 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000428 if (PyString_Check(key) && PyModule_Check(value)) {
429 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000430 if (strcmp(name, "__builtin__") == 0)
431 continue;
432 if (strcmp(name, "sys") == 0)
433 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000434 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000435 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000436 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000437 PyDict_SetItem(modules, key, Py_None);
438 }
439 }
440
441 /* Next, delete sys and __builtin__ (in that order) */
442 value = PyDict_GetItemString(modules, "sys");
443 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000444 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000445 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000446 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000447 PyDict_SetItemString(modules, "sys", Py_None);
448 }
449 value = PyDict_GetItemString(modules, "__builtin__");
450 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000451 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000452 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000453 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000454 PyDict_SetItemString(modules, "__builtin__", Py_None);
455 }
456
457 /* Finally, clear and delete the modules directory */
458 PyDict_Clear(modules);
459 interp->modules = NULL;
460 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000461}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000462
463
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464/* Helper for pythonrun.c -- return magic number */
465
466long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000467PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000468{
Guido van Rossum96774c12000-05-01 20:19:08 +0000469 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470}
471
472
Guido van Rossum25ce5661997-08-02 03:10:38 +0000473/* Magic for extension modules (built-in as well as dynamically
474 loaded). To prevent initializing an extension module more than
475 once, we keep a static dictionary 'extensions' keyed by module name
476 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000477 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000478 dictionary is stored by calling _PyImport_FixupExtension()
479 immediately after the module initialization function succeeds. A
480 copy can be retrieved from there by calling
481 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482
Guido van Rossum79f25d91997-04-29 20:08:16 +0000483PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000484_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000485{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000486 PyObject *modules, *mod, *dict, *copy;
487 if (extensions == NULL) {
488 extensions = PyDict_New();
489 if (extensions == NULL)
490 return NULL;
491 }
492 modules = PyImport_GetModuleDict();
493 mod = PyDict_GetItemString(modules, name);
494 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000495 PyErr_Format(PyExc_SystemError,
496 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000497 return NULL;
498 }
499 dict = PyModule_GetDict(mod);
500 if (dict == NULL)
501 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000502 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000503 if (copy == NULL)
504 return NULL;
505 PyDict_SetItemString(extensions, filename, copy);
506 Py_DECREF(copy);
507 return copy;
508}
509
510PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000511_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000512{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000513 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000514 if (extensions == NULL)
515 return NULL;
516 dict = PyDict_GetItemString(extensions, filename);
517 if (dict == NULL)
518 return NULL;
519 mod = PyImport_AddModule(name);
520 if (mod == NULL)
521 return NULL;
522 mdict = PyModule_GetDict(mod);
523 if (mdict == NULL)
524 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000525 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000527 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000528 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529 name, filename);
530 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000531}
532
533
534/* Get the module object corresponding to a module name.
535 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000536 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000537 Because the former action is most common, THIS DOES NOT RETURN A
538 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000541PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000547 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 if (m == NULL)
551 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000552 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000553 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 return NULL;
555 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557
558 return m;
559}
560
Tim Peters1cd70172004-08-02 03:52:12 +0000561/* Remove name from sys.modules, if it's there. */
562static void
563_RemoveModule(const char *name)
564{
565 PyObject *modules = PyImport_GetModuleDict();
566 if (PyDict_GetItemString(modules, name) == NULL)
567 return;
568 if (PyDict_DelItemString(modules, name) < 0)
569 Py_FatalError("import: deleting existing key in"
570 "sys.modules failed");
571}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000573/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000574 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
575 * removed from sys.modules, to avoid leaving damaged module objects
576 * in sys.modules. The caller may wish to restore the original
577 * module object (if any) in this case; PyImport_ReloadModule is an
578 * example.
579 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000580PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000581PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000583 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
584}
585
586PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000588{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000591
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593 if (m == NULL)
594 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000595 /* If the module is being reloaded, we get the old module back
596 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 d = PyModule_GetDict(m);
598 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
599 if (PyDict_SetItemString(d, "__builtins__",
600 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000601 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000602 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000603 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000604 v = NULL;
605 if (pathname != NULL) {
606 v = PyString_FromString(pathname);
607 if (v == NULL)
608 PyErr_Clear();
609 }
610 if (v == NULL) {
611 v = ((PyCodeObject *)co)->co_filename;
612 Py_INCREF(v);
613 }
614 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000616 Py_DECREF(v);
617
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000618 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000620 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000622
Guido van Rossum25ce5661997-08-02 03:10:38 +0000623 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000624 PyErr_Format(PyExc_ImportError,
625 "Loaded module %.200s not found in sys.modules",
626 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000627 return NULL;
628 }
629
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631
632 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000633
634 error:
635 _RemoveModule(name);
636 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000637}
638
639
640/* Given a pathname for a Python source file, fill a buffer with the
641 pathname for the corresponding compiled file. Return the pathname
642 for the compiled file, or NULL if there's no space in the buffer.
643 Doesn't set an exception. */
644
645static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000646make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647{
Tim Petersc1731372001-08-04 08:12:36 +0000648 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649 if (len+2 > buflen)
650 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000651
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000652#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000653 /* Treat .pyw as if it were .py. The case of ".pyw" must match
654 that used in _PyImport_StandardFiletab. */
655 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
656 --len; /* pretend 'w' isn't there */
657#endif
658 memcpy(buf, pathname, len);
659 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
660 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661
662 return buf;
663}
664
665
666/* Given a pathname for a Python source file, its time of last
667 modification, and a pathname for a compiled file, check whether the
668 compiled file represents the same version of the source. If so,
669 return a FILE pointer for the compiled file, positioned just after
670 the header; if not, return NULL.
671 Doesn't set an exception. */
672
673static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000674check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000675{
676 FILE *fp;
677 long magic;
678 long pyc_mtime;
679
680 fp = fopen(cpathname, "rb");
681 if (fp == NULL)
682 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000684 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000686 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687 fclose(fp);
688 return NULL;
689 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000693 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694 fclose(fp);
695 return NULL;
696 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000698 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 return fp;
700}
701
702
703/* Read a code object from a file and check it for validity */
704
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000706read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000709
Tim Petersd9b9ac82001-01-28 00:27:39 +0000710 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000711 if (co == NULL)
712 return NULL;
713 if (!PyCode_Check(co)) {
714 PyErr_Format(PyExc_ImportError,
715 "Non-code object in %.200s", cpathname);
716 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717 return NULL;
718 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000720}
721
722
723/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000724 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000727load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728{
729 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 PyCodeObject *co;
731 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000734 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000735 PyErr_Format(PyExc_ImportError,
736 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737 return NULL;
738 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000740 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 if (co == NULL)
742 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000744 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000746 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748
749 return m;
750}
751
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000752/* Parse a source file and return the corresponding code object */
753
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000755parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758 node *n;
759
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000760 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761 if (n == NULL)
762 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763 co = PyNode_Compile(n, pathname);
764 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000765
766 return co;
767}
768
769
Guido van Rossum55a83382000-09-20 20:31:38 +0000770/* Helper to open a bytecode file for writing in exclusive mode */
771
772static FILE *
773open_exclusive(char *filename)
774{
775#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
776 /* Use O_EXCL to avoid a race condition when another process tries to
777 write the same file. When that happens, our open() call fails,
778 which is just fine (since it's only a cache).
779 XXX If the file exists and is writable but the directory is not
780 writable, the file will never be written. Oh well.
781 */
782 int fd;
783 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000784 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
785#ifdef O_BINARY
786 |O_BINARY /* necessary for Windows */
787#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000788#ifdef __VMS
789 , 0666, "ctxt=bin", "shr=nil");
790#else
791 , 0666);
792#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000793 if (fd < 0)
794 return NULL;
795 return fdopen(fd, "wb");
796#else
797 /* Best we can do -- on Windows this can't happen anyway */
798 return fopen(filename, "wb");
799#endif
800}
801
802
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000803/* Write a compiled module to a file, placing the time of last
804 modification of its source into the header.
805 Errors are ignored, if a write error occurs an attempt is made to
806 remove the file. */
807
808static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000809write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810{
811 FILE *fp;
812
Guido van Rossum55a83382000-09-20 20:31:38 +0000813 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000814 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000816 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817 "# can't create %s\n", cpathname);
818 return;
819 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000820 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000821 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000822 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
823 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000824 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000826 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827 /* Don't keep partial file */
828 fclose(fp);
829 (void) unlink(cpathname);
830 return;
831 }
832 /* Now write the true mtime */
833 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000834 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835 fflush(fp);
836 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000838 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839}
840
841
842/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000843 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
844 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000845
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000847load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848{
Fred Drake4c82b232000-06-30 16:18:57 +0000849 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000850 FILE *fpc;
851 char buf[MAXPATHLEN+1];
852 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 PyCodeObject *co;
854 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000856 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000857 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000858 return NULL;
859#if SIZEOF_TIME_T > 4
860 /* Python's .pyc timestamp handling presumes that the timestamp fits
861 in 4 bytes. This will be fine until sometime in the year 2038,
862 when a 4-byte signed time_t will overflow.
863 */
864 if (mtime >> 32) {
865 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000866 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000867 return NULL;
868 }
869#endif
Tim Peters36515e22001-11-18 04:06:29 +0000870 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000871 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872 if (cpathname != NULL &&
873 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000874 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000875 fclose(fpc);
876 if (co == NULL)
877 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000879 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000880 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000881 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882 }
883 else {
884 co = parse_source_module(pathname, fp);
885 if (co == NULL)
886 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000887 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000888 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000889 name, pathname);
890 write_compiled_module(co, cpathname, mtime);
891 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000892 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000893 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894
895 return m;
896}
897
898
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000899/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000900static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
901static struct filedescr *find_module(char *, char *, PyObject *,
902 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000903static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000904
905/* Load a package and return its module object WITH INCREMENTED
906 REFERENCE COUNT */
907
908static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000909load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000910{
Tim Peters1cd70172004-08-02 03:52:12 +0000911 PyObject *m, *d;
912 PyObject *file = NULL;
913 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000914 int err;
915 char buf[MAXPATHLEN+1];
916 FILE *fp = NULL;
917 struct filedescr *fdp;
918
919 m = PyImport_AddModule(name);
920 if (m == NULL)
921 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000922 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000923 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000924 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000925 d = PyModule_GetDict(m);
926 file = PyString_FromString(pathname);
927 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000928 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000929 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000930 if (path == NULL)
931 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000932 err = PyDict_SetItemString(d, "__file__", file);
933 if (err == 0)
934 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000935 if (err != 0)
936 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000937 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000938 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000939 if (fdp == NULL) {
940 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
941 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000942 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000943 }
944 else
945 m = NULL;
946 goto cleanup;
947 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000948 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949 if (fp != NULL)
950 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000951 goto cleanup;
952
953 error:
954 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000955 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956 Py_XDECREF(path);
957 Py_XDECREF(file);
958 return m;
959}
960
961
962/* Helper to test for built-in module */
963
964static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000965is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000966{
967 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000968 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
969 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
970 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000971 return -1;
972 else
973 return 1;
974 }
975 }
976 return 0;
977}
978
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000979
Just van Rossum52e14d62002-12-30 22:08:05 +0000980/* Return an importer object for a sys.path/pkg.__path__ item 'p',
981 possibly by fetching it from the path_importer_cache dict. If it
982 wasn't yet cached, traverse path_hooks until it a hook is found
983 that can handle the path item. Return None if no hook could;
984 this tells our caller it should fall back to the builtin
985 import mechanism. Cache the result in path_importer_cache.
986 Returns a borrowed reference. */
987
988static PyObject *
989get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
990 PyObject *p)
991{
992 PyObject *importer;
993 int j, nhooks;
994
995 /* These conditions are the caller's responsibility: */
996 assert(PyList_Check(path_hooks));
997 assert(PyDict_Check(path_importer_cache));
998
999 nhooks = PyList_Size(path_hooks);
1000 if (nhooks < 0)
1001 return NULL; /* Shouldn't happen */
1002
1003 importer = PyDict_GetItem(path_importer_cache, p);
1004 if (importer != NULL)
1005 return importer;
1006
1007 /* set path_importer_cache[p] to None to avoid recursion */
1008 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1009 return NULL;
1010
1011 for (j = 0; j < nhooks; j++) {
1012 PyObject *hook = PyList_GetItem(path_hooks, j);
1013 if (hook == NULL)
1014 return NULL;
1015 importer = PyObject_CallFunction(hook, "O", p);
1016 if (importer != NULL)
1017 break;
1018
1019 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1020 return NULL;
1021 }
1022 PyErr_Clear();
1023 }
1024 if (importer == NULL)
1025 importer = Py_None;
1026 else if (importer != Py_None) {
1027 int err = PyDict_SetItem(path_importer_cache, p, importer);
1028 Py_DECREF(importer);
1029 if (err != 0)
1030 return NULL;
1031 }
1032 return importer;
1033}
1034
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001035/* Search the path (default sys.path) for a module. Return the
1036 corresponding filedescr struct, and (via return arguments) the
1037 pathname and an open file. Return NULL if the module is not found. */
1038
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001039#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001040extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1041 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001042#endif
1043
Tim Peters50d8d372001-02-28 05:34:27 +00001044static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001045static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001046static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001047
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001049find_module(char *fullname, char *subname, PyObject *path, char *buf,
1050 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001051{
Fred Drake4c82b232000-06-30 16:18:57 +00001052 int i, npath;
1053 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001054 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001055 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001056 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001057 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001058#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001059 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001060#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001061 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1062 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1063 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001064 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001065#if defined(PYOS_OS2)
1066 size_t saved_len;
1067 size_t saved_namelen;
1068 char *saved_buf = NULL;
1069#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001070 if (p_loader != NULL)
1071 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001072
Just van Rossum52e14d62002-12-30 22:08:05 +00001073 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001074 PyErr_SetString(PyExc_OverflowError,
1075 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001076 return NULL;
1077 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001078 strcpy(name, subname);
1079
1080 /* sys.meta_path import hook */
1081 if (p_loader != NULL) {
1082 PyObject *meta_path;
1083
1084 meta_path = PySys_GetObject("meta_path");
1085 if (meta_path == NULL || !PyList_Check(meta_path)) {
1086 PyErr_SetString(PyExc_ImportError,
1087 "sys.meta_path must be a list of "
1088 "import hooks");
1089 return NULL;
1090 }
1091 Py_INCREF(meta_path); /* zap guard */
1092 npath = PyList_Size(meta_path);
1093 for (i = 0; i < npath; i++) {
1094 PyObject *loader;
1095 PyObject *hook = PyList_GetItem(meta_path, i);
1096 loader = PyObject_CallMethod(hook, "find_module",
1097 "sO", fullname,
1098 path != NULL ?
1099 path : Py_None);
1100 if (loader == NULL) {
1101 Py_DECREF(meta_path);
1102 return NULL; /* true error */
1103 }
1104 if (loader != Py_None) {
1105 /* a loader was found */
1106 *p_loader = loader;
1107 Py_DECREF(meta_path);
1108 return &importhookdescr;
1109 }
1110 Py_DECREF(loader);
1111 }
1112 Py_DECREF(meta_path);
1113 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001114
1115 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001116 /* The only type of submodule allowed inside a "frozen"
1117 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001118 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1119 PyErr_SetString(PyExc_ImportError,
1120 "full frozen module name too long");
1121 return NULL;
1122 }
1123 strcpy(buf, PyString_AsString(path));
1124 strcat(buf, ".");
1125 strcat(buf, name);
1126 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001127 if (find_frozen(name) != NULL) {
1128 strcpy(buf, name);
1129 return &fd_frozen;
1130 }
1131 PyErr_Format(PyExc_ImportError,
1132 "No frozen submodule named %.200s", name);
1133 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001134 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001135 if (path == NULL) {
1136 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001137 strcpy(buf, name);
1138 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001139 }
Greg Ward201baee2001-10-04 14:52:06 +00001140 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001141 strcpy(buf, name);
1142 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001143 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001144
Guido van Rossumac279101996-08-22 23:10:58 +00001145#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001146 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1147 if (fp != NULL) {
1148 *p_fp = fp;
1149 return fdp;
1150 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001151#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001152 path = PySys_GetObject("path");
1153 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 if (path == NULL || !PyList_Check(path)) {
1155 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001156 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157 return NULL;
1158 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001159
1160 path_hooks = PySys_GetObject("path_hooks");
1161 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1162 PyErr_SetString(PyExc_ImportError,
1163 "sys.path_hooks must be a list of "
1164 "import hooks");
1165 return NULL;
1166 }
1167 path_importer_cache = PySys_GetObject("path_importer_cache");
1168 if (path_importer_cache == NULL ||
1169 !PyDict_Check(path_importer_cache)) {
1170 PyErr_SetString(PyExc_ImportError,
1171 "sys.path_importer_cache must be a dict");
1172 return NULL;
1173 }
1174
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001176 namelen = strlen(name);
1177 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001178 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001180#ifdef Py_USING_UNICODE
1181 if (PyUnicode_Check(v)) {
1182 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1183 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1184 if (copy == NULL)
1185 return NULL;
1186 v = copy;
1187 }
1188 else
1189#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001191 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001193 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1194 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001195 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001196 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001198 if (strlen(buf) != len) {
1199 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001201 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001202
1203 /* sys.path_hooks import hook */
1204 if (p_loader != NULL) {
1205 PyObject *importer;
1206
1207 importer = get_path_importer(path_importer_cache,
1208 path_hooks, v);
1209 if (importer == NULL)
1210 return NULL;
1211 /* Note: importer is a borrowed reference */
1212 if (importer != Py_None) {
1213 PyObject *loader;
1214 loader = PyObject_CallMethod(importer,
1215 "find_module",
1216 "s", fullname);
1217 if (loader == NULL)
1218 return NULL; /* error */
1219 if (loader != Py_None) {
1220 /* a loader was found */
1221 *p_loader = loader;
1222 return &importhookdescr;
1223 }
1224 Py_DECREF(loader);
1225 }
1226 /* no hook was successful, use builtin import */
1227 }
1228
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001229 if (len > 0 && buf[len-1] != SEP
1230#ifdef ALTSEP
1231 && buf[len-1] != ALTSEP
1232#endif
1233 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001234 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001235 strcpy(buf+len, name);
1236 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001237
1238 /* Check for package import (buf holds a directory name,
1239 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001240#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001241 if (stat(buf, &statbuf) == 0 && /* it exists */
1242 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1243 find_init_module(buf) && /* it has __init__.py */
1244 case_ok(buf, len, namelen, name)) { /* and case matches */
1245 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001246 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001247 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001248#else
1249 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001250#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001251 if (isdir(buf) &&
1252 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001253 case_ok(buf, len, namelen, name)) {
1254 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001255 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001256 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001257#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001258#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001259#if defined(PYOS_OS2)
1260 /* take a snapshot of the module spec for restoration
1261 * after the 8 character DLL hackery
1262 */
1263 saved_buf = strdup(buf);
1264 saved_len = len;
1265 saved_namelen = namelen;
1266#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001267 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001268#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001269 /* OS/2 limits DLLs to 8 character names (w/o
1270 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001271 * so if the name is longer than that and its a
1272 * dynamically loaded module we're going to try,
1273 * truncate the name before trying
1274 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001275 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001276 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001277 const struct filedescr *scan;
1278 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001279 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001280 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001281 break;
1282 else
1283 scan++;
1284 }
1285 if (scan->suffix != NULL) {
1286 /* yes, so truncate the name */
1287 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001288 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001289 buf[len] = '\0';
1290 }
1291 }
1292#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001293 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001295 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001296 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001297 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001298 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001299 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001300 if (fp != NULL) {
1301 if (case_ok(buf, len, namelen, name))
1302 break;
1303 else { /* continue search */
1304 fclose(fp);
1305 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001306 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001307 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001308#if defined(PYOS_OS2)
1309 /* restore the saved snapshot */
1310 strcpy(buf, saved_buf);
1311 len = saved_len;
1312 namelen = saved_namelen;
1313#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001314 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001315#if defined(PYOS_OS2)
1316 /* don't need/want the module name snapshot anymore */
1317 if (saved_buf)
1318 {
1319 free(saved_buf);
1320 saved_buf = NULL;
1321 }
1322#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001323 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001324 if (fp != NULL)
1325 break;
1326 }
1327 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001328 PyErr_Format(PyExc_ImportError,
1329 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001330 return NULL;
1331 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001332 *p_fp = fp;
1333 return fdp;
1334}
1335
Tim Petersd1e87a82001-03-01 18:12:00 +00001336/* case_ok(char* buf, int len, int namelen, char* name)
1337 * The arguments here are tricky, best shown by example:
1338 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1339 * ^ ^ ^ ^
1340 * |--------------------- buf ---------------------|
1341 * |------------------- len ------------------|
1342 * |------ name -------|
1343 * |----- namelen -----|
1344 * buf is the full path, but len only counts up to (& exclusive of) the
1345 * extension. name is the module name, also exclusive of extension.
1346 *
1347 * We've already done a successful stat() or fopen() on buf, so know that
1348 * there's some match, possibly case-insensitive.
1349 *
Tim Peters50d8d372001-02-28 05:34:27 +00001350 * case_ok() is to return 1 if there's a case-sensitive match for
1351 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1352 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001353 *
Tim Peters50d8d372001-02-28 05:34:27 +00001354 * case_ok() is used to implement case-sensitive import semantics even
1355 * on platforms with case-insensitive filesystems. It's trivial to implement
1356 * for case-sensitive filesystems. It's pretty much a cross-platform
1357 * nightmare for systems with case-insensitive filesystems.
1358 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001359
Tim Peters50d8d372001-02-28 05:34:27 +00001360/* First we may need a pile of platform-specific header files; the sequence
1361 * of #if's here should match the sequence in the body of case_ok().
1362 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001363#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001364#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001365#ifdef __CYGWIN__
1366#include <sys/cygwin.h>
1367#endif
1368
Tim Peters50d8d372001-02-28 05:34:27 +00001369#elif defined(DJGPP)
1370#include <dir.h>
1371
Tim Petersd1e87a82001-03-01 18:12:00 +00001372#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001373#include <sys/types.h>
1374#include <dirent.h>
1375
Andrew MacIntyred9400542002-02-26 11:41:34 +00001376#elif defined(PYOS_OS2)
1377#define INCL_DOS
1378#define INCL_DOSERRORS
1379#define INCL_NOPMAPI
1380#include <os2.h>
1381
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001382#elif defined(RISCOS)
1383#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001384#endif
1385
Guido van Rossum0980bd91998-02-13 17:18:36 +00001386static int
Tim Peters50d8d372001-02-28 05:34:27 +00001387case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001388{
Tim Peters50d8d372001-02-28 05:34:27 +00001389/* Pick a platform-specific implementation; the sequence of #if's here should
1390 * match the sequence just above.
1391 */
1392
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001393/* MS_WINDOWS || __CYGWIN__ */
1394#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001395 WIN32_FIND_DATA data;
1396 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001397#ifdef __CYGWIN__
1398 char tempbuf[MAX_PATH];
1399#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001400
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001401 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001402 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001403
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001404#ifdef __CYGWIN__
1405 cygwin32_conv_to_win32_path(buf, tempbuf);
1406 h = FindFirstFile(tempbuf, &data);
1407#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001408 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001409#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001410 if (h == INVALID_HANDLE_VALUE) {
1411 PyErr_Format(PyExc_NameError,
1412 "Can't find file for module %.100s\n(filename %.300s)",
1413 name, buf);
1414 return 0;
1415 }
1416 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001417 return strncmp(data.cFileName, name, namelen) == 0;
1418
1419/* DJGPP */
1420#elif defined(DJGPP)
1421 struct ffblk ffblk;
1422 int done;
1423
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001424 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001425 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001426
1427 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1428 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001429 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001430 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001431 name, buf);
1432 return 0;
1433 }
Tim Peters50d8d372001-02-28 05:34:27 +00001434 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001435
Tim Peters677898a2001-03-02 03:28:03 +00001436/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001437#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001438 DIR *dirp;
1439 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001440 char dirname[MAXPATHLEN + 1];
1441 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001442
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001443 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001444 return 1;
1445
Tim Petersd1e87a82001-03-01 18:12:00 +00001446 /* Copy the dir component into dirname; substitute "." if empty */
1447 if (dirlen <= 0) {
1448 dirname[0] = '.';
1449 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001450 }
1451 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001452 assert(dirlen <= MAXPATHLEN);
1453 memcpy(dirname, buf, dirlen);
1454 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001455 }
1456 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001457 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001458 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001459 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001460 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001461 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001462#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001463 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001464#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001465 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001466#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001467 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001468 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001469 (void)closedir(dirp);
1470 return 1; /* Found */
1471 }
1472 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001473 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001474 }
Tim Peters430f5d42001-03-01 01:30:56 +00001475 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001476
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001477/* RISC OS */
1478#elif defined(RISCOS)
1479 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1480 char buf2[MAXPATHLEN+2];
1481 char *nameWithExt = buf+len-namelen;
1482 int canonlen;
1483 os_error *e;
1484
1485 if (Py_GETENV("PYTHONCASEOK") != NULL)
1486 return 1;
1487
1488 /* workaround:
1489 append wildcard, otherwise case of filename wouldn't be touched */
1490 strcpy(buf2, buf);
1491 strcat(buf2, "*");
1492
1493 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1494 canonlen = MAXPATHLEN+1-canonlen;
1495 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1496 return 0;
1497 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1498 return 1; /* match */
1499
1500 return 0;
1501
Andrew MacIntyred9400542002-02-26 11:41:34 +00001502/* OS/2 */
1503#elif defined(PYOS_OS2)
1504 HDIR hdir = 1;
1505 ULONG srchcnt = 1;
1506 FILEFINDBUF3 ffbuf;
1507 APIRET rc;
1508
1509 if (getenv("PYTHONCASEOK") != NULL)
1510 return 1;
1511
1512 rc = DosFindFirst(buf,
1513 &hdir,
1514 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1515 &ffbuf, sizeof(ffbuf),
1516 &srchcnt,
1517 FIL_STANDARD);
1518 if (rc != NO_ERROR)
1519 return 0;
1520 return strncmp(ffbuf.achName, name, namelen) == 0;
1521
Tim Peters50d8d372001-02-28 05:34:27 +00001522/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1523#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001524 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001525
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001526#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001527}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001528
Guido van Rossum0980bd91998-02-13 17:18:36 +00001529
Guido van Rossum197346f1997-10-31 18:38:52 +00001530#ifdef HAVE_STAT
1531/* Helper to look for __init__.py or __init__.py[co] in potential package */
1532static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001533find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001534{
Tim Peters0f9431f2001-07-05 03:47:53 +00001535 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001536 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001537 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001538 struct stat statbuf;
1539
Tim Peters0f9431f2001-07-05 03:47:53 +00001540/* For calling case_ok(buf, len, namelen, name):
1541 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1542 * ^ ^ ^ ^
1543 * |--------------------- buf ---------------------|
1544 * |------------------- len ------------------|
1545 * |------ name -------|
1546 * |----- namelen -----|
1547 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001548 if (save_len + 13 >= MAXPATHLEN)
1549 return 0;
1550 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001551 pname = buf + i;
1552 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001553 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001554 if (case_ok(buf,
1555 save_len + 9, /* len("/__init__") */
1556 8, /* len("__init__") */
1557 pname)) {
1558 buf[save_len] = '\0';
1559 return 1;
1560 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001561 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001562 i += strlen(pname);
1563 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001564 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001565 if (case_ok(buf,
1566 save_len + 9, /* len("/__init__") */
1567 8, /* len("__init__") */
1568 pname)) {
1569 buf[save_len] = '\0';
1570 return 1;
1571 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001572 }
1573 buf[save_len] = '\0';
1574 return 0;
1575}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001576
1577#else
1578
1579#ifdef RISCOS
1580static int
1581find_init_module(buf)
1582 char *buf;
1583{
1584 int save_len = strlen(buf);
1585 int i = save_len;
1586
1587 if (save_len + 13 >= MAXPATHLEN)
1588 return 0;
1589 buf[i++] = SEP;
1590 strcpy(buf+i, "__init__/py");
1591 if (isfile(buf)) {
1592 buf[save_len] = '\0';
1593 return 1;
1594 }
1595
1596 if (Py_OptimizeFlag)
1597 strcpy(buf+i, "o");
1598 else
1599 strcpy(buf+i, "c");
1600 if (isfile(buf)) {
1601 buf[save_len] = '\0';
1602 return 1;
1603 }
1604 buf[save_len] = '\0';
1605 return 0;
1606}
1607#endif /*RISCOS*/
1608
Guido van Rossum197346f1997-10-31 18:38:52 +00001609#endif /* HAVE_STAT */
1610
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001611
Tim Petersdbd9ba62000-07-09 03:09:57 +00001612static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001613
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001614/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001615 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001616
Guido van Rossum79f25d91997-04-29 20:08:16 +00001617static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001618load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001619{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001620 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001621 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001622 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001623
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001624 /* First check that there's an open file (if we need one) */
1625 switch (type) {
1626 case PY_SOURCE:
1627 case PY_COMPILED:
1628 if (fp == NULL) {
1629 PyErr_Format(PyExc_ValueError,
1630 "file object required for import (type code %d)",
1631 type);
1632 return NULL;
1633 }
1634 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001635
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001636 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001637
1638 case PY_SOURCE:
1639 m = load_source_module(name, buf, fp);
1640 break;
1641
1642 case PY_COMPILED:
1643 m = load_compiled_module(name, buf, fp);
1644 break;
1645
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001646#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001647 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001648 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001649 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001650#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001651
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001652 case PKG_DIRECTORY:
1653 m = load_package(name, buf);
1654 break;
1655
1656 case C_BUILTIN:
1657 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001658 if (buf != NULL && buf[0] != '\0')
1659 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001660 if (type == C_BUILTIN)
1661 err = init_builtin(name);
1662 else
1663 err = PyImport_ImportFrozenModule(name);
1664 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001665 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001666 if (err == 0) {
1667 PyErr_Format(PyExc_ImportError,
1668 "Purported %s module %.200s not found",
1669 type == C_BUILTIN ?
1670 "builtin" : "frozen",
1671 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001672 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001673 }
1674 modules = PyImport_GetModuleDict();
1675 m = PyDict_GetItemString(modules, name);
1676 if (m == NULL) {
1677 PyErr_Format(
1678 PyExc_ImportError,
1679 "%s module %.200s not properly initialized",
1680 type == C_BUILTIN ?
1681 "builtin" : "frozen",
1682 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001683 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001684 }
1685 Py_INCREF(m);
1686 break;
1687
Just van Rossum52e14d62002-12-30 22:08:05 +00001688 case IMP_HOOK: {
1689 if (loader == NULL) {
1690 PyErr_SetString(PyExc_ImportError,
1691 "import hook without loader");
1692 return NULL;
1693 }
1694 m = PyObject_CallMethod(loader, "load_module", "s", name);
1695 break;
1696 }
1697
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001698 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001699 PyErr_Format(PyExc_ImportError,
1700 "Don't know how to import %.200s (type code %d)",
1701 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001702 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001703
1704 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001705
1706 return m;
1707}
1708
1709
1710/* Initialize a built-in module.
1711 Return 1 for succes, 0 if the module is not found, and -1 with
1712 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001713
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001714static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001715init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001716{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001717 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001718
Greg Ward201baee2001-10-04 14:52:06 +00001719 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001720 return 1;
1721
Guido van Rossum771c6c81997-10-31 18:37:24 +00001722 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001723 if (strcmp(name, p->name) == 0) {
1724 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001725 PyErr_Format(PyExc_ImportError,
1726 "Cannot re-init internal module %.200s",
1727 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001728 return -1;
1729 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001731 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001732 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001733 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001734 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001735 if (_PyImport_FixupExtension(name, name) == NULL)
1736 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001737 return 1;
1738 }
1739 }
1740 return 0;
1741}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001742
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001743
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001744/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001745
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001746static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001747find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001748{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001749 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001750
Guido van Rossum79f25d91997-04-29 20:08:16 +00001751 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001752 if (p->name == NULL)
1753 return NULL;
1754 if (strcmp(p->name, name) == 0)
1755 break;
1756 }
1757 return p;
1758}
1759
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001761get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001762{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001763 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001764 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001765
1766 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001767 PyErr_Format(PyExc_ImportError,
1768 "No such frozen object named %.200s",
1769 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001770 return NULL;
1771 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001772 if (p->code == NULL) {
1773 PyErr_Format(PyExc_ImportError,
1774 "Excluded frozen object named %.200s",
1775 name);
1776 return NULL;
1777 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001778 size = p->size;
1779 if (size < 0)
1780 size = -size;
1781 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001782}
1783
1784/* Initialize a frozen module.
1785 Return 1 for succes, 0 if the module is not found, and -1 with
1786 an exception set if the initialization failed.
1787 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001788
1789int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001790PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001791{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001792 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 PyObject *co;
1794 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001795 int ispackage;
1796 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001797
1798 if (p == NULL)
1799 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001800 if (p->code == NULL) {
1801 PyErr_Format(PyExc_ImportError,
1802 "Excluded frozen object named %.200s",
1803 name);
1804 return -1;
1805 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001806 size = p->size;
1807 ispackage = (size < 0);
1808 if (ispackage)
1809 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001811 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001812 name, ispackage ? " package" : "");
1813 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001814 if (co == NULL)
1815 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816 if (!PyCode_Check(co)) {
1817 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001818 PyErr_Format(PyExc_TypeError,
1819 "frozen object %.200s is not a code object",
1820 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001821 return -1;
1822 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001823 if (ispackage) {
1824 /* Set __path__ to the package name */
1825 PyObject *d, *s;
1826 int err;
1827 m = PyImport_AddModule(name);
1828 if (m == NULL)
1829 return -1;
1830 d = PyModule_GetDict(m);
1831 s = PyString_InternFromString(name);
1832 if (s == NULL)
1833 return -1;
1834 err = PyDict_SetItemString(d, "__path__", s);
1835 Py_DECREF(s);
1836 if (err != 0)
1837 return err;
1838 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001839 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001841 if (m == NULL)
1842 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001843 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001844 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001845}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001846
1847
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001848/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001849 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001850
Guido van Rossum79f25d91997-04-29 20:08:16 +00001851PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001852PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001853{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001854 PyObject *pname;
1855 PyObject *result;
1856
1857 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001858 if (pname == NULL)
1859 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001860 result = PyImport_Import(pname);
1861 Py_DECREF(pname);
1862 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001863}
1864
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001865/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001866static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1867static PyObject *load_next(PyObject *mod, PyObject *altmod,
1868 char **p_name, char *buf, int *p_buflen);
1869static int mark_miss(char *name);
1870static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1871 char *buf, int buflen, int recursive);
1872static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001873
1874/* The Magnum Opus of dotted-name import :-) */
1875
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001876static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001877import_module_ex(char *name, PyObject *globals, PyObject *locals,
1878 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001879{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001880 char buf[MAXPATHLEN+1];
1881 int buflen = 0;
1882 PyObject *parent, *head, *next, *tail;
1883
1884 parent = get_parent(globals, buf, &buflen);
1885 if (parent == NULL)
1886 return NULL;
1887
1888 head = load_next(parent, Py_None, &name, buf, &buflen);
1889 if (head == NULL)
1890 return NULL;
1891
1892 tail = head;
1893 Py_INCREF(tail);
1894 while (name) {
1895 next = load_next(tail, tail, &name, buf, &buflen);
1896 Py_DECREF(tail);
1897 if (next == NULL) {
1898 Py_DECREF(head);
1899 return NULL;
1900 }
1901 tail = next;
1902 }
1903
1904 if (fromlist != NULL) {
1905 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1906 fromlist = NULL;
1907 }
1908
1909 if (fromlist == NULL) {
1910 Py_DECREF(tail);
1911 return head;
1912 }
1913
1914 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001915 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001916 Py_DECREF(tail);
1917 return NULL;
1918 }
1919
1920 return tail;
1921}
1922
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001923PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001924PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1925 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001926{
1927 PyObject *result;
1928 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001929 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001930 if (unlock_import() < 0) {
1931 Py_XDECREF(result);
1932 PyErr_SetString(PyExc_RuntimeError,
1933 "not holding the import lock");
1934 return NULL;
1935 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001936 return result;
1937}
1938
Fred Drake87590902004-05-28 20:21:36 +00001939/* Return the package that an import is being performed in. If globals comes
1940 from the module foo.bar.bat (not itself a package), this returns the
1941 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1942 the package's entry in sys.modules is returned.
1943
1944 The *name* of the returned package is returned in buf, with the length of
1945 the name in *p_buflen.
1946
1947 If globals doesn't come from a package or a module in a package, or a
1948 corresponding entry is not found in sys.modules, Py_None is returned.
1949*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001950static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001951get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001952{
1953 static PyObject *namestr = NULL;
1954 static PyObject *pathstr = NULL;
1955 PyObject *modname, *modpath, *modules, *parent;
1956
1957 if (globals == NULL || !PyDict_Check(globals))
1958 return Py_None;
1959
1960 if (namestr == NULL) {
1961 namestr = PyString_InternFromString("__name__");
1962 if (namestr == NULL)
1963 return NULL;
1964 }
1965 if (pathstr == NULL) {
1966 pathstr = PyString_InternFromString("__path__");
1967 if (pathstr == NULL)
1968 return NULL;
1969 }
1970
1971 *buf = '\0';
1972 *p_buflen = 0;
1973 modname = PyDict_GetItem(globals, namestr);
1974 if (modname == NULL || !PyString_Check(modname))
1975 return Py_None;
1976
1977 modpath = PyDict_GetItem(globals, pathstr);
1978 if (modpath != NULL) {
1979 int len = PyString_GET_SIZE(modname);
1980 if (len > MAXPATHLEN) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "Module name too long");
1983 return NULL;
1984 }
1985 strcpy(buf, PyString_AS_STRING(modname));
1986 *p_buflen = len;
1987 }
1988 else {
1989 char *start = PyString_AS_STRING(modname);
1990 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001991 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001992 if (lastdot == NULL)
1993 return Py_None;
1994 len = lastdot - start;
1995 if (len >= MAXPATHLEN) {
1996 PyErr_SetString(PyExc_ValueError,
1997 "Module name too long");
1998 return NULL;
1999 }
2000 strncpy(buf, start, len);
2001 buf[len] = '\0';
2002 *p_buflen = len;
2003 }
2004
2005 modules = PyImport_GetModuleDict();
2006 parent = PyDict_GetItemString(modules, buf);
2007 if (parent == NULL)
2008 parent = Py_None;
2009 return parent;
2010 /* We expect, but can't guarantee, if parent != None, that:
2011 - parent.__name__ == buf
2012 - parent.__dict__ is globals
2013 If this is violated... Who cares? */
2014}
2015
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002016/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002017static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002018load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2019 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002020{
2021 char *name = *p_name;
2022 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002023 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002024 char *p;
2025 PyObject *result;
2026
2027 if (dot == NULL) {
2028 *p_name = NULL;
2029 len = strlen(name);
2030 }
2031 else {
2032 *p_name = dot+1;
2033 len = dot-name;
2034 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002035 if (len == 0) {
2036 PyErr_SetString(PyExc_ValueError,
2037 "Empty module name");
2038 return NULL;
2039 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002040
2041 p = buf + *p_buflen;
2042 if (p != buf)
2043 *p++ = '.';
2044 if (p+len-buf >= MAXPATHLEN) {
2045 PyErr_SetString(PyExc_ValueError,
2046 "Module name too long");
2047 return NULL;
2048 }
2049 strncpy(p, name, len);
2050 p[len] = '\0';
2051 *p_buflen = p+len-buf;
2052
2053 result = import_submodule(mod, p, buf);
2054 if (result == Py_None && altmod != mod) {
2055 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002056 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002057 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002058 if (result != NULL && result != Py_None) {
2059 if (mark_miss(buf) != 0) {
2060 Py_DECREF(result);
2061 return NULL;
2062 }
2063 strncpy(buf, name, len);
2064 buf[len] = '\0';
2065 *p_buflen = len;
2066 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002067 }
2068 if (result == NULL)
2069 return NULL;
2070
2071 if (result == Py_None) {
2072 Py_DECREF(result);
2073 PyErr_Format(PyExc_ImportError,
2074 "No module named %.200s", name);
2075 return NULL;
2076 }
2077
2078 return result;
2079}
2080
2081static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002082mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002083{
2084 PyObject *modules = PyImport_GetModuleDict();
2085 return PyDict_SetItemString(modules, name, Py_None);
2086}
2087
2088static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002089ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2090 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002091{
2092 int i;
2093
2094 if (!PyObject_HasAttrString(mod, "__path__"))
2095 return 1;
2096
2097 for (i = 0; ; i++) {
2098 PyObject *item = PySequence_GetItem(fromlist, i);
2099 int hasit;
2100 if (item == NULL) {
2101 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2102 PyErr_Clear();
2103 return 1;
2104 }
2105 return 0;
2106 }
2107 if (!PyString_Check(item)) {
2108 PyErr_SetString(PyExc_TypeError,
2109 "Item in ``from list'' not a string");
2110 Py_DECREF(item);
2111 return 0;
2112 }
2113 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002114 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002115 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002116 /* See if the package defines __all__ */
2117 if (recursive)
2118 continue; /* Avoid endless recursion */
2119 all = PyObject_GetAttrString(mod, "__all__");
2120 if (all == NULL)
2121 PyErr_Clear();
2122 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002123 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002124 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002125 if (!ret)
2126 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002127 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002128 continue;
2129 }
2130 hasit = PyObject_HasAttr(mod, item);
2131 if (!hasit) {
2132 char *subname = PyString_AS_STRING(item);
2133 PyObject *submod;
2134 char *p;
2135 if (buflen + strlen(subname) >= MAXPATHLEN) {
2136 PyErr_SetString(PyExc_ValueError,
2137 "Module name too long");
2138 Py_DECREF(item);
2139 return 0;
2140 }
2141 p = buf + buflen;
2142 *p++ = '.';
2143 strcpy(p, subname);
2144 submod = import_submodule(mod, subname, buf);
2145 Py_XDECREF(submod);
2146 if (submod == NULL) {
2147 Py_DECREF(item);
2148 return 0;
2149 }
2150 }
2151 Py_DECREF(item);
2152 }
2153
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002154 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002155}
2156
Neil Schemenauer00b09662003-06-16 21:03:07 +00002157static int
2158add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2159 PyObject *modules)
2160{
2161 if (mod == Py_None)
2162 return 1;
2163 /* Irrespective of the success of this load, make a
2164 reference to it in the parent package module. A copy gets
2165 saved in the modules dictionary under the full name, so get a
2166 reference from there, if need be. (The exception is when the
2167 load failed with a SyntaxError -- then there's no trace in
2168 sys.modules. In that case, of course, do nothing extra.) */
2169 if (submod == NULL) {
2170 submod = PyDict_GetItemString(modules, fullname);
2171 if (submod == NULL)
2172 return 1;
2173 }
2174 if (PyModule_Check(mod)) {
2175 /* We can't use setattr here since it can give a
2176 * spurious warning if the submodule name shadows a
2177 * builtin name */
2178 PyObject *dict = PyModule_GetDict(mod);
2179 if (!dict)
2180 return 0;
2181 if (PyDict_SetItemString(dict, subname, submod) < 0)
2182 return 0;
2183 }
2184 else {
2185 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2186 return 0;
2187 }
2188 return 1;
2189}
2190
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002191static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002192import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002193{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002194 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002195 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002196
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002197 /* Require:
2198 if mod == None: subname == fullname
2199 else: mod.__name__ + "." + subname == fullname
2200 */
2201
Tim Peters50d8d372001-02-28 05:34:27 +00002202 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002203 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002204 }
2205 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002206 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002207 char buf[MAXPATHLEN+1];
2208 struct filedescr *fdp;
2209 FILE *fp = NULL;
2210
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002211 if (mod == Py_None)
2212 path = NULL;
2213 else {
2214 path = PyObject_GetAttrString(mod, "__path__");
2215 if (path == NULL) {
2216 PyErr_Clear();
2217 Py_INCREF(Py_None);
2218 return Py_None;
2219 }
2220 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002221
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002222 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002223 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2224 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002225 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002226 if (fdp == NULL) {
2227 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2228 return NULL;
2229 PyErr_Clear();
2230 Py_INCREF(Py_None);
2231 return Py_None;
2232 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002233 m = load_module(fullname, fp, buf, fdp->type, loader);
2234 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002235 if (fp)
2236 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002237 if (!add_submodule(mod, m, fullname, subname, modules)) {
2238 Py_XDECREF(m);
2239 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002240 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002241 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002242
2243 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002244}
2245
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002246
2247/* Re-import a module of any kind and return its module object, WITH
2248 INCREMENTED REFERENCE COUNT */
2249
Guido van Rossum79f25d91997-04-29 20:08:16 +00002250PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002251PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002252{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002253 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002254 PyObject *path = NULL;
2255 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002256 char buf[MAXPATHLEN+1];
2257 struct filedescr *fdp;
2258 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002259 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002260
Guido van Rossum79f25d91997-04-29 20:08:16 +00002261 if (m == NULL || !PyModule_Check(m)) {
2262 PyErr_SetString(PyExc_TypeError,
2263 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002264 return NULL;
2265 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002266 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002267 if (name == NULL)
2268 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002269 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002270 PyErr_Format(PyExc_ImportError,
2271 "reload(): module %.200s not in sys.modules",
2272 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002273 return NULL;
2274 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002275 subname = strrchr(name, '.');
2276 if (subname == NULL)
2277 subname = name;
2278 else {
2279 PyObject *parentname, *parent;
2280 parentname = PyString_FromStringAndSize(name, (subname-name));
2281 if (parentname == NULL)
2282 return NULL;
2283 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002284 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002285 if (parent == NULL) {
2286 PyErr_Format(PyExc_ImportError,
2287 "reload(): parent %.200s not in sys.modules",
2288 name);
2289 return NULL;
2290 }
2291 subname++;
2292 path = PyObject_GetAttrString(parent, "__path__");
2293 if (path == NULL)
2294 PyErr_Clear();
2295 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002296 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002297 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum222ef561997-09-06 19:41:09 +00002298 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002299 if (fdp == NULL)
2300 return NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002301 newm = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002302 if (fp)
2303 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002304 if (newm == NULL) {
2305 /* load_module probably removed name from modules because of
2306 * the error. Put back the original module object. We're
2307 * going to return NULL in this case regardless of whether
2308 * replacing name succeeds, so the return value is ignored.
2309 */
2310 PyDict_SetItemString(modules, name, m);
2311 }
2312 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002313}
2314
2315
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002316/* Higher-level import emulator which emulates the "import" statement
2317 more accurately -- it invokes the __import__() function from the
2318 builtins of the current globals. This means that the import is
2319 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002320 environment, e.g. by "rexec".
2321 A dummy list ["__doc__"] is passed as the 4th argument so that
2322 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2323 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002324
2325PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002326PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002327{
2328 static PyObject *silly_list = NULL;
2329 static PyObject *builtins_str = NULL;
2330 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002331 PyObject *globals = NULL;
2332 PyObject *import = NULL;
2333 PyObject *builtins = NULL;
2334 PyObject *r = NULL;
2335
2336 /* Initialize constant string objects */
2337 if (silly_list == NULL) {
2338 import_str = PyString_InternFromString("__import__");
2339 if (import_str == NULL)
2340 return NULL;
2341 builtins_str = PyString_InternFromString("__builtins__");
2342 if (builtins_str == NULL)
2343 return NULL;
2344 silly_list = Py_BuildValue("[s]", "__doc__");
2345 if (silly_list == NULL)
2346 return NULL;
2347 }
2348
2349 /* Get the builtins from current globals */
2350 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002351 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002352 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002353 builtins = PyObject_GetItem(globals, builtins_str);
2354 if (builtins == NULL)
2355 goto err;
2356 }
2357 else {
2358 /* No globals -- use standard builtins, and fake globals */
2359 PyErr_Clear();
2360
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002361 builtins = PyImport_ImportModuleEx("__builtin__",
2362 NULL, NULL, NULL);
2363 if (builtins == NULL)
2364 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002365 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2366 if (globals == NULL)
2367 goto err;
2368 }
2369
2370 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002371 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002372 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002373 if (import == NULL)
2374 PyErr_SetObject(PyExc_KeyError, import_str);
2375 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002376 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002377 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002378 if (import == NULL)
2379 goto err;
2380
2381 /* Call the _import__ function with the proper argument list */
2382 r = PyObject_CallFunction(import, "OOOO",
2383 module_name, globals, globals, silly_list);
2384
2385 err:
2386 Py_XDECREF(globals);
2387 Py_XDECREF(builtins);
2388 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002389
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002390 return r;
2391}
2392
2393
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002394/* Module 'imp' provides Python access to the primitives used for
2395 importing modules.
2396*/
2397
Guido van Rossum79f25d91997-04-29 20:08:16 +00002398static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002399imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002400{
2401 char buf[4];
2402
Guido van Rossum96774c12000-05-01 20:19:08 +00002403 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2404 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2405 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2406 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407
Guido van Rossum79f25d91997-04-29 20:08:16 +00002408 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002409}
2410
Guido van Rossum79f25d91997-04-29 20:08:16 +00002411static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002412imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002413{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002414 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002415 struct filedescr *fdp;
2416
Guido van Rossum79f25d91997-04-29 20:08:16 +00002417 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002418 if (list == NULL)
2419 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2421 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002422 fdp->suffix, fdp->mode, fdp->type);
2423 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002424 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002425 return NULL;
2426 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002427 if (PyList_Append(list, item) < 0) {
2428 Py_DECREF(list);
2429 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002430 return NULL;
2431 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002432 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433 }
2434 return list;
2435}
2436
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002438call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002439{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002440 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002441 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002442 struct filedescr *fdp;
2443 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002444 FILE *fp = NULL;
2445
2446 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002447 if (path == Py_None)
2448 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002449 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002450 if (fdp == NULL)
2451 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002452 if (fp != NULL) {
2453 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2454 if (fob == NULL) {
2455 fclose(fp);
2456 return NULL;
2457 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002458 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002459 else {
2460 fob = Py_None;
2461 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002462 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002464 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002465 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002466 return ret;
2467}
2468
Guido van Rossum79f25d91997-04-29 20:08:16 +00002469static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002470imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002471{
2472 char *name;
2473 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002474 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002475 return NULL;
2476 return call_find_module(name, path);
2477}
2478
2479static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002480imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002481{
2482 char *name;
2483 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002484 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002485 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002486 return NULL;
2487 ret = init_builtin(name);
2488 if (ret < 0)
2489 return NULL;
2490 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002491 Py_INCREF(Py_None);
2492 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002493 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002494 m = PyImport_AddModule(name);
2495 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002496 return m;
2497}
2498
Guido van Rossum79f25d91997-04-29 20:08:16 +00002499static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002500imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002501{
2502 char *name;
2503 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002504 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002505 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002506 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002507 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002508 if (ret < 0)
2509 return NULL;
2510 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002511 Py_INCREF(Py_None);
2512 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002513 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002514 m = PyImport_AddModule(name);
2515 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516 return m;
2517}
2518
Guido van Rossum79f25d91997-04-29 20:08:16 +00002519static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002520imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002521{
2522 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002523
Guido van Rossum43713e52000-02-29 13:59:29 +00002524 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002525 return NULL;
2526 return get_frozen_object(name);
2527}
2528
Guido van Rossum79f25d91997-04-29 20:08:16 +00002529static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002530imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002532 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002533 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002535 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002536}
2537
Guido van Rossum79f25d91997-04-29 20:08:16 +00002538static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002539imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002540{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002541 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002542 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002543 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002544 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002545 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002546 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002547}
2548
2549static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002550get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002551{
2552 FILE *fp;
2553 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002554 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002555 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002556 fp = fopen(pathname, mode);
2557 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002558 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002559 }
2560 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002561 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002562 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002563 PyErr_SetString(PyExc_ValueError,
2564 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002565 }
2566 return fp;
2567}
2568
Guido van Rossum79f25d91997-04-29 20:08:16 +00002569static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002570imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002571{
2572 char *name;
2573 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574 PyObject *fob = NULL;
2575 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002576 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002577 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002578 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002579 return NULL;
2580 fp = get_file(pathname, fob, "rb");
2581 if (fp == NULL)
2582 return NULL;
2583 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002584 if (fob == NULL)
2585 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002586 return m;
2587}
2588
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002589#ifdef HAVE_DYNAMIC_LOADING
2590
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002592imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002593{
2594 char *name;
2595 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002596 PyObject *fob = NULL;
2597 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002598 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002599 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002601 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002602 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002603 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002604 if (fp == NULL)
2605 return NULL;
2606 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002607 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002608 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002609}
2610
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002611#endif /* HAVE_DYNAMIC_LOADING */
2612
Guido van Rossum79f25d91997-04-29 20:08:16 +00002613static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002614imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002615{
2616 char *name;
2617 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002618 PyObject *fob = NULL;
2619 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002620 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002621 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002622 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002623 return NULL;
2624 fp = get_file(pathname, fob, "r");
2625 if (fp == NULL)
2626 return NULL;
2627 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002628 if (fob == NULL)
2629 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002630 return m;
2631}
2632
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002634imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002635{
2636 char *name;
2637 PyObject *fob;
2638 char *pathname;
2639 char *suffix; /* Unused */
2640 char *mode;
2641 int type;
2642 FILE *fp;
2643
Guido van Rossum43713e52000-02-29 13:59:29 +00002644 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002645 &name, &fob, &pathname,
2646 &suffix, &mode, &type))
2647 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002648 if (*mode) {
2649 /* Mode must start with 'r' or 'U' and must not contain '+'.
2650 Implicit in this test is the assumption that the mode
2651 may contain other modifiers like 'b' or 't'. */
2652
2653 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002654 PyErr_Format(PyExc_ValueError,
2655 "invalid file open mode %.200s", mode);
2656 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002657 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002658 }
2659 if (fob == Py_None)
2660 fp = NULL;
2661 else {
2662 if (!PyFile_Check(fob)) {
2663 PyErr_SetString(PyExc_ValueError,
2664 "load_module arg#2 should be a file or None");
2665 return NULL;
2666 }
2667 fp = get_file(pathname, fob, mode);
2668 if (fp == NULL)
2669 return NULL;
2670 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002671 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002672}
2673
2674static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002675imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002676{
2677 char *name;
2678 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002679 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002680 return NULL;
2681 return load_package(name, pathname);
2682}
2683
2684static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002685imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002686{
2687 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002688 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002689 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002690 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002691}
2692
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002693/* Doc strings */
2694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695PyDoc_STRVAR(doc_imp,
2696"This module provides the components needed to build your own\n\
2697__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002699PyDoc_STRVAR(doc_find_module,
2700"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002701Search for a module. If path is omitted or None, search for a\n\
2702built-in, frozen or special module and continue search in sys.path.\n\
2703The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002705
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002706PyDoc_STRVAR(doc_load_module,
2707"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002708Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002709The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002711PyDoc_STRVAR(doc_get_magic,
2712"get_magic() -> string\n\
2713Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002714
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002715PyDoc_STRVAR(doc_get_suffixes,
2716"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002717Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002720PyDoc_STRVAR(doc_new_module,
2721"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002722Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002723The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002726"lock_held() -> boolean\n\
2727Return True if the import lock is currently held, else False.\n\
2728On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002729
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002730PyDoc_STRVAR(doc_acquire_lock,
2731"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002732Acquires the interpreter's import lock for the current thread.\n\
2733This lock should be used by import hooks to ensure thread-safety\n\
2734when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002735On platforms without threads, this function does nothing.");
2736
2737PyDoc_STRVAR(doc_release_lock,
2738"release_lock() -> None\n\
2739Release the interpreter's import lock.\n\
2740On platforms without threads, this function does nothing.");
2741
Guido van Rossum79f25d91997-04-29 20:08:16 +00002742static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002743 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2744 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2745 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2746 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2747 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2748 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2749 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2750 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002751 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002752 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2753 {"init_builtin", imp_init_builtin, METH_VARARGS},
2754 {"init_frozen", imp_init_frozen, METH_VARARGS},
2755 {"is_builtin", imp_is_builtin, METH_VARARGS},
2756 {"is_frozen", imp_is_frozen, METH_VARARGS},
2757 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002758#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002759 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002760#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002761 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002762 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002763 {NULL, NULL} /* sentinel */
2764};
2765
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002766static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002767setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002768{
2769 PyObject *v;
2770 int err;
2771
2772 v = PyInt_FromLong((long)value);
2773 err = PyDict_SetItemString(d, name, v);
2774 Py_XDECREF(v);
2775 return err;
2776}
2777
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002778PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002779initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002780{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002781 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002782
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002783 m = Py_InitModule4("imp", imp_methods, doc_imp,
2784 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002785 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002786
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002787 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2788 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2789 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2790 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2791 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2792 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2793 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2794 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002795 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002796 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002797
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002798 failure:
2799 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002800}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002801
2802
Guido van Rossumb18618d2000-05-03 23:44:39 +00002803/* API for embedding applications that want to add their own entries
2804 to the table of built-in modules. This should normally be called
2805 *before* Py_Initialize(). When the table resize fails, -1 is
2806 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002807
2808 After a similar function by Just van Rossum. */
2809
2810int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002811PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002812{
2813 static struct _inittab *our_copy = NULL;
2814 struct _inittab *p;
2815 int i, n;
2816
2817 /* Count the number of entries in both tables */
2818 for (n = 0; newtab[n].name != NULL; n++)
2819 ;
2820 if (n == 0)
2821 return 0; /* Nothing to do */
2822 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2823 ;
2824
2825 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002826 p = our_copy;
2827 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002828 if (p == NULL)
2829 return -1;
2830
2831 /* Copy the tables into the new memory */
2832 if (our_copy != PyImport_Inittab)
2833 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2834 PyImport_Inittab = our_copy = p;
2835 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2836
2837 return 0;
2838}
2839
2840/* Shorthand to add a single entry given a name and a function */
2841
2842int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002843PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002844{
2845 struct _inittab newtab[2];
2846
2847 memset(newtab, '\0', sizeof newtab);
2848
2849 newtab[0].name = name;
2850 newtab[0].initfunc = initfunc;
2851
2852 return PyImport_ExtendInittab(newtab);
2853}