blob: 75c6d3e93ba56ea5c0770379c83574af42e2edc1 [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
Raymond Hettinger2c31a052004-09-22 18:44:21 +000052 Python 2.4b1: 62061
Tim Peters36515e22001-11-18 04:06:29 +000053*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +000054#define MAGIC (62061 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000055
Guido van Rossum96774c12000-05-01 20:19:08 +000056/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000057 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000058 compiler works which are enabled by command line switches. */
59static long pyc_magic = MAGIC;
60
Guido van Rossum25ce5661997-08-02 03:10:38 +000061/* See _PyImport_FixupExtension() below */
62static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000063
Guido van Rossum771c6c81997-10-31 18:37:24 +000064/* This table is defined in config.c: */
65extern struct _inittab _PyImport_Inittab[];
66
67struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000068
Guido van Rossumed1170e1999-12-20 21:23:41 +000069/* these tables define the module suffixes that Python recognizes */
70struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000071
72#ifdef RISCOS
73static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000074 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000075 {"/pyc", "rb", PY_COMPILED},
76 {0, 0}
77};
78#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000079static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000080 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000081#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000082 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000083#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000084 {".pyc", "rb", PY_COMPILED},
85 {0, 0}
86};
Guido van Rossum48a680c2001-03-02 06:34:14 +000087#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000088
Guido van Rossum1ae940a1995-01-02 19:04:15 +000089/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090
91void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Guido van Rossumed1170e1999-12-20 21:23:41 +000094 const struct filedescr *scan;
95 struct filedescr *filetab;
96 int countD = 0;
97 int countS = 0;
98
99 /* prepare _PyImport_Filetab: copy entries from
100 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
101 */
102 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
103 ++countD;
104 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
105 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000107 memcpy(filetab, _PyImport_DynLoadFiletab,
108 countD * sizeof(struct filedescr));
109 memcpy(filetab + countD, _PyImport_StandardFiletab,
110 countS * sizeof(struct filedescr));
111 filetab[countD + countS].suffix = NULL;
112
113 _PyImport_Filetab = filetab;
114
Guido van Rossum0824f631997-03-11 18:37:35 +0000115 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000116 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
117 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000118#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000119 if (strcmp(filetab->suffix, ".pyc") == 0)
120 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000121#else
122 if (strcmp(filetab->suffix, "/pyc") == 0)
123 filetab->suffix = "/pyo";
124#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000125 }
126 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000127
128 if (Py_UnicodeFlag) {
129 /* Fix the pyc_magic so that byte compiled code created
130 using the all-Unicode method doesn't interfere with
131 code created in normal operation mode. */
132 pyc_magic = MAGIC + 1;
133 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136void
Just van Rossum52e14d62002-12-30 22:08:05 +0000137_PyImportHooks_Init(void)
138{
139 PyObject *v, *path_hooks = NULL, *zimpimport;
140 int err = 0;
141
142 /* adding sys.path_hooks and sys.path_importer_cache, setting up
143 zipimport */
144
145 if (Py_VerboseFlag)
146 PySys_WriteStderr("# installing zipimport hook\n");
147
148 v = PyList_New(0);
149 if (v == NULL)
150 goto error;
151 err = PySys_SetObject("meta_path", v);
152 Py_DECREF(v);
153 if (err)
154 goto error;
155 v = PyDict_New();
156 if (v == NULL)
157 goto error;
158 err = PySys_SetObject("path_importer_cache", v);
159 Py_DECREF(v);
160 if (err)
161 goto error;
162 path_hooks = PyList_New(0);
163 if (path_hooks == NULL)
164 goto error;
165 err = PySys_SetObject("path_hooks", path_hooks);
166 if (err) {
167 error:
168 PyErr_Print();
169 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
170 "path_importer_cache failed");
171 }
172 zimpimport = PyImport_ImportModule("zipimport");
173 if (zimpimport == NULL) {
174 PyErr_Clear(); /* No zip import module -- okay */
175 if (Py_VerboseFlag)
176 PySys_WriteStderr("# can't import zipimport\n");
177 }
178 else {
179 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
180 "zipimporter");
181 Py_DECREF(zimpimport);
182 if (zipimporter == NULL) {
183 PyErr_Clear(); /* No zipimporter object -- okay */
184 if (Py_VerboseFlag)
185 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000186 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000187 }
188 else {
189 /* sys.path_hooks.append(zipimporter) */
190 err = PyList_Append(path_hooks, zipimporter);
191 Py_DECREF(zipimporter);
192 if (err)
193 goto error;
194 if (Py_VerboseFlag)
195 PySys_WriteStderr(
196 "# installed zipimport hook\n");
197 }
198 }
199 Py_DECREF(path_hooks);
200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
205 Py_XDECREF(extensions);
206 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000207 PyMem_DEL(_PyImport_Filetab);
208 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209}
210
211
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000212/* Locking primitives to prevent parallel imports of the same module
213 in different threads to return with a partially loaded module.
214 These calls are serialized by the global interpreter lock. */
215
216#ifdef WITH_THREAD
217
Guido van Rossum49b56061998-10-01 20:42:43 +0000218#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000219
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000221static long import_lock_thread = -1;
222static int import_lock_level = 0;
223
224static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000225lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000226{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000227 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000228 if (me == -1)
229 return; /* Too bad */
230 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000232 if (import_lock_thread == me) {
233 import_lock_level++;
234 return;
235 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000236 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
237 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000238 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000240 PyEval_RestoreThread(tstate);
241 }
242 import_lock_thread = me;
243 import_lock_level = 1;
244}
245
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000246static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000248{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000251 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000253 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254 import_lock_level--;
255 if (import_lock_level == 0) {
256 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000259 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000260}
261
262#else
263
264#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000265#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266
267#endif
268
Tim Peters69232342001-08-30 05:16:13 +0000269static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000270imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000271{
Tim Peters69232342001-08-30 05:16:13 +0000272#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000273 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000274#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000275 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000276#endif
277}
278
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000280imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000281{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000282#ifdef WITH_THREAD
283 lock_import();
284#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000285 Py_INCREF(Py_None);
286 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287}
288
289static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000290imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000291{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292#ifdef WITH_THREAD
293 if (unlock_import() < 0) {
294 PyErr_SetString(PyExc_RuntimeError,
295 "not holding the import lock");
296 return NULL;
297 }
298#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000299 Py_INCREF(Py_None);
300 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000301}
302
Guido van Rossum25ce5661997-08-02 03:10:38 +0000303/* Helper for sys */
304
305PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000308 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309 if (interp->modules == NULL)
310 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
311 return interp->modules;
312}
313
Guido van Rossum3f5da241990-12-20 15:06:42 +0000314
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000315/* List of names to clear in sys */
316static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000317 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000318 "exc_type", "exc_value", "exc_traceback",
319 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000320 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000321 NULL
322};
323
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000324static char* sys_files[] = {
325 "stdin", "__stdin__",
326 "stdout", "__stdout__",
327 "stderr", "__stderr__",
328 NULL
329};
330
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000332/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000333
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336{
Guido van Rossum758eec01998-01-19 21:58:26 +0000337 int pos, ndone;
338 char *name;
339 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000340 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000341 PyObject *modules = interp->modules;
342
343 if (modules == NULL)
344 return; /* Already done */
345
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000346 /* Delete some special variables first. These are common
347 places where user values hide and people complain when their
348 destructors fail. Since the modules containing them are
349 deleted *last* of all, they would come too late in the normal
350 destruction order. Sigh. */
351
352 value = PyDict_GetItemString(modules, "__builtin__");
353 if (value != NULL && PyModule_Check(value)) {
354 dict = PyModule_GetDict(value);
355 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000356 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000357 PyDict_SetItemString(dict, "_", Py_None);
358 }
359 value = PyDict_GetItemString(modules, "sys");
360 if (value != NULL && PyModule_Check(value)) {
361 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000362 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000363 dict = PyModule_GetDict(value);
364 for (p = sys_deletes; *p != NULL; p++) {
365 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000366 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000367 PyDict_SetItemString(dict, *p, Py_None);
368 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000369 for (p = sys_files; *p != NULL; p+=2) {
370 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000371 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000372 v = PyDict_GetItemString(dict, *(p+1));
373 if (v == NULL)
374 v = Py_None;
375 PyDict_SetItemString(dict, *p, v);
376 }
377 }
378
379 /* First, delete __main__ */
380 value = PyDict_GetItemString(modules, "__main__");
381 if (value != NULL && PyModule_Check(value)) {
382 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000383 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000384 _PyModule_Clear(value);
385 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000386 }
387
Guido van Rossum758eec01998-01-19 21:58:26 +0000388 /* The special treatment of __builtin__ here is because even
389 when it's not referenced as a module, its dictionary is
390 referenced by almost every module's __builtins__. Since
391 deleting a module clears its dictionary (even if there are
392 references left to it), we need to delete the __builtin__
393 module last. Likewise, we don't delete sys until the very
394 end because it is implicitly referenced (e.g. by print).
395
396 Also note that we 'delete' modules by replacing their entry
397 in the modules dict with None, rather than really deleting
398 them; this avoids a rehash of the modules dictionary and
399 also marks them as "non existent" so they won't be
400 re-imported. */
401
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000402 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000403 one (skipping __builtin__ and sys) and delete them */
404 do {
405 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000406 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000407 while (PyDict_Next(modules, &pos, &key, &value)) {
408 if (value->ob_refcnt != 1)
409 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000410 if (PyString_Check(key) && PyModule_Check(value)) {
411 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000412 if (strcmp(name, "__builtin__") == 0)
413 continue;
414 if (strcmp(name, "sys") == 0)
415 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000416 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000417 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000418 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000419 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000420 PyDict_SetItem(modules, key, Py_None);
421 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422 }
423 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000424 } while (ndone > 0);
425
Guido van Rossum758eec01998-01-19 21:58:26 +0000426 /* Next, delete all modules (still skipping __builtin__ and sys) */
427 pos = 0;
428 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000429 if (PyString_Check(key) && PyModule_Check(value)) {
430 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000431 if (strcmp(name, "__builtin__") == 0)
432 continue;
433 if (strcmp(name, "sys") == 0)
434 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000435 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000436 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000437 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000438 PyDict_SetItem(modules, key, Py_None);
439 }
440 }
441
442 /* Next, delete sys and __builtin__ (in that order) */
443 value = PyDict_GetItemString(modules, "sys");
444 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000445 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000446 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000447 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000448 PyDict_SetItemString(modules, "sys", Py_None);
449 }
450 value = PyDict_GetItemString(modules, "__builtin__");
451 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000452 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000453 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000454 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000455 PyDict_SetItemString(modules, "__builtin__", Py_None);
456 }
457
458 /* Finally, clear and delete the modules directory */
459 PyDict_Clear(modules);
460 interp->modules = NULL;
461 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000462}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000463
464
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465/* Helper for pythonrun.c -- return magic number */
466
467long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000468PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469{
Guido van Rossum96774c12000-05-01 20:19:08 +0000470 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000471}
472
473
Guido van Rossum25ce5661997-08-02 03:10:38 +0000474/* Magic for extension modules (built-in as well as dynamically
475 loaded). To prevent initializing an extension module more than
476 once, we keep a static dictionary 'extensions' keyed by module name
477 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000478 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000479 dictionary is stored by calling _PyImport_FixupExtension()
480 immediately after the module initialization function succeeds. A
481 copy can be retrieved from there by calling
482 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000485_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000487 PyObject *modules, *mod, *dict, *copy;
488 if (extensions == NULL) {
489 extensions = PyDict_New();
490 if (extensions == NULL)
491 return NULL;
492 }
493 modules = PyImport_GetModuleDict();
494 mod = PyDict_GetItemString(modules, name);
495 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000496 PyErr_Format(PyExc_SystemError,
497 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000498 return NULL;
499 }
500 dict = PyModule_GetDict(mod);
501 if (dict == NULL)
502 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000503 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000504 if (copy == NULL)
505 return NULL;
506 PyDict_SetItemString(extensions, filename, copy);
507 Py_DECREF(copy);
508 return copy;
509}
510
511PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000512_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000513{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000514 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000515 if (extensions == NULL)
516 return NULL;
517 dict = PyDict_GetItemString(extensions, filename);
518 if (dict == NULL)
519 return NULL;
520 mod = PyImport_AddModule(name);
521 if (mod == NULL)
522 return NULL;
523 mdict = PyModule_GetDict(mod);
524 if (mdict == NULL)
525 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000526 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000527 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000529 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000530 name, filename);
531 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532}
533
534
535/* Get the module object corresponding to a module name.
536 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000537 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000538 Because the former action is most common, THIS DOES NOT RETURN A
539 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000542PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 if (m == NULL)
552 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555 return NULL;
556 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558
559 return m;
560}
561
Tim Peters1cd70172004-08-02 03:52:12 +0000562/* Remove name from sys.modules, if it's there. */
563static void
564_RemoveModule(const char *name)
565{
566 PyObject *modules = PyImport_GetModuleDict();
567 if (PyDict_GetItemString(modules, name) == NULL)
568 return;
569 if (PyDict_DelItemString(modules, name) < 0)
570 Py_FatalError("import: deleting existing key in"
571 "sys.modules failed");
572}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000574/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000575 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
576 * removed from sys.modules, to avoid leaving damaged module objects
577 * in sys.modules. The caller may wish to restore the original
578 * module object (if any) in this case; PyImport_ReloadModule is an
579 * example.
580 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000582PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000584 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
585}
586
587PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000588PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000589{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594 if (m == NULL)
595 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000596 /* If the module is being reloaded, we get the old module back
597 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598 d = PyModule_GetDict(m);
599 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
600 if (PyDict_SetItemString(d, "__builtins__",
601 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000602 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000603 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000604 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000605 v = NULL;
606 if (pathname != NULL) {
607 v = PyString_FromString(pathname);
608 if (v == NULL)
609 PyErr_Clear();
610 }
611 if (v == NULL) {
612 v = ((PyCodeObject *)co)->co_filename;
613 Py_INCREF(v);
614 }
615 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000617 Py_DECREF(v);
618
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000619 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000621 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000623
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000625 PyErr_Format(PyExc_ImportError,
626 "Loaded module %.200s not found in sys.modules",
627 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000628 return NULL;
629 }
630
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632
633 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000634
635 error:
636 _RemoveModule(name);
637 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638}
639
640
641/* Given a pathname for a Python source file, fill a buffer with the
642 pathname for the corresponding compiled file. Return the pathname
643 for the compiled file, or NULL if there's no space in the buffer.
644 Doesn't set an exception. */
645
646static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000647make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648{
Tim Petersc1731372001-08-04 08:12:36 +0000649 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650 if (len+2 > buflen)
651 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000652
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000653#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000654 /* Treat .pyw as if it were .py. The case of ".pyw" must match
655 that used in _PyImport_StandardFiletab. */
656 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
657 --len; /* pretend 'w' isn't there */
658#endif
659 memcpy(buf, pathname, len);
660 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
661 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662
663 return buf;
664}
665
666
667/* Given a pathname for a Python source file, its time of last
668 modification, and a pathname for a compiled file, check whether the
669 compiled file represents the same version of the source. If so,
670 return a FILE pointer for the compiled file, positioned just after
671 the header; if not, return NULL.
672 Doesn't set an exception. */
673
674static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000675check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676{
677 FILE *fp;
678 long magic;
679 long pyc_mtime;
680
681 fp = fopen(cpathname, "rb");
682 if (fp == NULL)
683 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000685 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000687 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688 fclose(fp);
689 return NULL;
690 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000694 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 fclose(fp);
696 return NULL;
697 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000699 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700 return fp;
701}
702
703
704/* Read a code object from a file and check it for validity */
705
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000707read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000710
Tim Petersd9b9ac82001-01-28 00:27:39 +0000711 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000712 if (co == NULL)
713 return NULL;
714 if (!PyCode_Check(co)) {
715 PyErr_Format(PyExc_ImportError,
716 "Non-code object in %.200s", cpathname);
717 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718 return NULL;
719 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721}
722
723
724/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000725 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000728load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729{
730 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 PyCodeObject *co;
732 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000733
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000735 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000736 PyErr_Format(PyExc_ImportError,
737 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 return NULL;
739 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000741 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 if (co == NULL)
743 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000745 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000747 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749
750 return m;
751}
752
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000753/* Parse a source file and return the corresponding code object */
754
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 node *n;
760
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000761 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762 if (n == NULL)
763 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 co = PyNode_Compile(n, pathname);
765 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766
767 return co;
768}
769
770
Guido van Rossum55a83382000-09-20 20:31:38 +0000771/* Helper to open a bytecode file for writing in exclusive mode */
772
773static FILE *
774open_exclusive(char *filename)
775{
776#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
777 /* Use O_EXCL to avoid a race condition when another process tries to
778 write the same file. When that happens, our open() call fails,
779 which is just fine (since it's only a cache).
780 XXX If the file exists and is writable but the directory is not
781 writable, the file will never be written. Oh well.
782 */
783 int fd;
784 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000785 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
786#ifdef O_BINARY
787 |O_BINARY /* necessary for Windows */
788#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000789#ifdef __VMS
790 , 0666, "ctxt=bin", "shr=nil");
791#else
792 , 0666);
793#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000794 if (fd < 0)
795 return NULL;
796 return fdopen(fd, "wb");
797#else
798 /* Best we can do -- on Windows this can't happen anyway */
799 return fopen(filename, "wb");
800#endif
801}
802
803
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804/* Write a compiled module to a file, placing the time of last
805 modification of its source into the header.
806 Errors are ignored, if a write error occurs an attempt is made to
807 remove the file. */
808
809static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000810write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000811{
812 FILE *fp;
813
Guido van Rossum55a83382000-09-20 20:31:38 +0000814 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000817 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000818 "# can't create %s\n", cpathname);
819 return;
820 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000821 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000823 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
824 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000825 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000827 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828 /* Don't keep partial file */
829 fclose(fp);
830 (void) unlink(cpathname);
831 return;
832 }
833 /* Now write the true mtime */
834 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000835 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836 fflush(fp);
837 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000839 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840}
841
842
843/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000844 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
845 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000848load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849{
Fred Drake4c82b232000-06-30 16:18:57 +0000850 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851 FILE *fpc;
852 char buf[MAXPATHLEN+1];
853 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 PyCodeObject *co;
855 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000857 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000858 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000859 return NULL;
860#if SIZEOF_TIME_T > 4
861 /* Python's .pyc timestamp handling presumes that the timestamp fits
862 in 4 bytes. This will be fine until sometime in the year 2038,
863 when a 4-byte signed time_t will overflow.
864 */
865 if (mtime >> 32) {
866 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000867 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000868 return NULL;
869 }
870#endif
Tim Peters36515e22001-11-18 04:06:29 +0000871 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000872 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 if (cpathname != NULL &&
874 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000875 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876 fclose(fpc);
877 if (co == NULL)
878 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000880 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000882 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 }
884 else {
885 co = parse_source_module(pathname, fp);
886 if (co == NULL)
887 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000889 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890 name, pathname);
891 write_compiled_module(co, cpathname, mtime);
892 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000893 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000894 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000895
896 return m;
897}
898
899
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000900/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000901static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
902static struct filedescr *find_module(char *, char *, PyObject *,
903 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000904static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000905
906/* Load a package and return its module object WITH INCREMENTED
907 REFERENCE COUNT */
908
909static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000910load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000911{
Tim Peters1cd70172004-08-02 03:52:12 +0000912 PyObject *m, *d;
913 PyObject *file = NULL;
914 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000915 int err;
916 char buf[MAXPATHLEN+1];
917 FILE *fp = NULL;
918 struct filedescr *fdp;
919
920 m = PyImport_AddModule(name);
921 if (m == NULL)
922 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000923 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000924 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000925 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000926 d = PyModule_GetDict(m);
927 file = PyString_FromString(pathname);
928 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000929 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000930 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000931 if (path == NULL)
932 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000933 err = PyDict_SetItemString(d, "__file__", file);
934 if (err == 0)
935 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000936 if (err != 0)
937 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000938 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000939 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000940 if (fdp == NULL) {
941 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
942 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000943 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000944 }
945 else
946 m = NULL;
947 goto cleanup;
948 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000949 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000950 if (fp != NULL)
951 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000952 goto cleanup;
953
954 error:
955 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957 Py_XDECREF(path);
958 Py_XDECREF(file);
959 return m;
960}
961
962
963/* Helper to test for built-in module */
964
965static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000966is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000967{
968 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000969 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
970 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
971 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000972 return -1;
973 else
974 return 1;
975 }
976 }
977 return 0;
978}
979
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000980
Just van Rossum52e14d62002-12-30 22:08:05 +0000981/* Return an importer object for a sys.path/pkg.__path__ item 'p',
982 possibly by fetching it from the path_importer_cache dict. If it
983 wasn't yet cached, traverse path_hooks until it a hook is found
984 that can handle the path item. Return None if no hook could;
985 this tells our caller it should fall back to the builtin
986 import mechanism. Cache the result in path_importer_cache.
987 Returns a borrowed reference. */
988
989static PyObject *
990get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
991 PyObject *p)
992{
993 PyObject *importer;
994 int j, nhooks;
995
996 /* These conditions are the caller's responsibility: */
997 assert(PyList_Check(path_hooks));
998 assert(PyDict_Check(path_importer_cache));
999
1000 nhooks = PyList_Size(path_hooks);
1001 if (nhooks < 0)
1002 return NULL; /* Shouldn't happen */
1003
1004 importer = PyDict_GetItem(path_importer_cache, p);
1005 if (importer != NULL)
1006 return importer;
1007
1008 /* set path_importer_cache[p] to None to avoid recursion */
1009 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1010 return NULL;
1011
1012 for (j = 0; j < nhooks; j++) {
1013 PyObject *hook = PyList_GetItem(path_hooks, j);
1014 if (hook == NULL)
1015 return NULL;
1016 importer = PyObject_CallFunction(hook, "O", p);
1017 if (importer != NULL)
1018 break;
1019
1020 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1021 return NULL;
1022 }
1023 PyErr_Clear();
1024 }
1025 if (importer == NULL)
1026 importer = Py_None;
1027 else if (importer != Py_None) {
1028 int err = PyDict_SetItem(path_importer_cache, p, importer);
1029 Py_DECREF(importer);
1030 if (err != 0)
1031 return NULL;
1032 }
1033 return importer;
1034}
1035
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001036/* Search the path (default sys.path) for a module. Return the
1037 corresponding filedescr struct, and (via return arguments) the
1038 pathname and an open file. Return NULL if the module is not found. */
1039
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001040#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001041extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1042 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001043#endif
1044
Tim Peters50d8d372001-02-28 05:34:27 +00001045static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001046static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001047static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001048
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001050find_module(char *fullname, char *subname, PyObject *path, char *buf,
1051 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001052{
Fred Drake4c82b232000-06-30 16:18:57 +00001053 int i, npath;
1054 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001055 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001056 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001057 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001058 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001059#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001060 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001061#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001062 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1063 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1064 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001065 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001066#if defined(PYOS_OS2)
1067 size_t saved_len;
1068 size_t saved_namelen;
1069 char *saved_buf = NULL;
1070#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001071 if (p_loader != NULL)
1072 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001073
Just van Rossum52e14d62002-12-30 22:08:05 +00001074 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001075 PyErr_SetString(PyExc_OverflowError,
1076 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001077 return NULL;
1078 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001079 strcpy(name, subname);
1080
1081 /* sys.meta_path import hook */
1082 if (p_loader != NULL) {
1083 PyObject *meta_path;
1084
1085 meta_path = PySys_GetObject("meta_path");
1086 if (meta_path == NULL || !PyList_Check(meta_path)) {
1087 PyErr_SetString(PyExc_ImportError,
1088 "sys.meta_path must be a list of "
1089 "import hooks");
1090 return NULL;
1091 }
1092 Py_INCREF(meta_path); /* zap guard */
1093 npath = PyList_Size(meta_path);
1094 for (i = 0; i < npath; i++) {
1095 PyObject *loader;
1096 PyObject *hook = PyList_GetItem(meta_path, i);
1097 loader = PyObject_CallMethod(hook, "find_module",
1098 "sO", fullname,
1099 path != NULL ?
1100 path : Py_None);
1101 if (loader == NULL) {
1102 Py_DECREF(meta_path);
1103 return NULL; /* true error */
1104 }
1105 if (loader != Py_None) {
1106 /* a loader was found */
1107 *p_loader = loader;
1108 Py_DECREF(meta_path);
1109 return &importhookdescr;
1110 }
1111 Py_DECREF(loader);
1112 }
1113 Py_DECREF(meta_path);
1114 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001115
1116 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001117 /* The only type of submodule allowed inside a "frozen"
1118 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001119 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1120 PyErr_SetString(PyExc_ImportError,
1121 "full frozen module name too long");
1122 return NULL;
1123 }
1124 strcpy(buf, PyString_AsString(path));
1125 strcat(buf, ".");
1126 strcat(buf, name);
1127 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001128 if (find_frozen(name) != NULL) {
1129 strcpy(buf, name);
1130 return &fd_frozen;
1131 }
1132 PyErr_Format(PyExc_ImportError,
1133 "No frozen submodule named %.200s", name);
1134 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001135 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001136 if (path == NULL) {
1137 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001138 strcpy(buf, name);
1139 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001140 }
Greg Ward201baee2001-10-04 14:52:06 +00001141 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001142 strcpy(buf, name);
1143 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001144 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001145
Guido van Rossumac279101996-08-22 23:10:58 +00001146#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001147 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1148 if (fp != NULL) {
1149 *p_fp = fp;
1150 return fdp;
1151 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001152#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001153 path = PySys_GetObject("path");
1154 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155 if (path == NULL || !PyList_Check(path)) {
1156 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001157 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001158 return NULL;
1159 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001160
1161 path_hooks = PySys_GetObject("path_hooks");
1162 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1163 PyErr_SetString(PyExc_ImportError,
1164 "sys.path_hooks must be a list of "
1165 "import hooks");
1166 return NULL;
1167 }
1168 path_importer_cache = PySys_GetObject("path_importer_cache");
1169 if (path_importer_cache == NULL ||
1170 !PyDict_Check(path_importer_cache)) {
1171 PyErr_SetString(PyExc_ImportError,
1172 "sys.path_importer_cache must be a dict");
1173 return NULL;
1174 }
1175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001177 namelen = strlen(name);
1178 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001179 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001181#ifdef Py_USING_UNICODE
1182 if (PyUnicode_Check(v)) {
1183 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1184 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1185 if (copy == NULL)
1186 return NULL;
1187 v = copy;
1188 }
1189 else
1190#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001192 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001194 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1195 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001197 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001198 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001199 if (strlen(buf) != len) {
1200 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001201 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001202 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001203
1204 /* sys.path_hooks import hook */
1205 if (p_loader != NULL) {
1206 PyObject *importer;
1207
1208 importer = get_path_importer(path_importer_cache,
1209 path_hooks, v);
1210 if (importer == NULL)
1211 return NULL;
1212 /* Note: importer is a borrowed reference */
1213 if (importer != Py_None) {
1214 PyObject *loader;
1215 loader = PyObject_CallMethod(importer,
1216 "find_module",
1217 "s", fullname);
1218 if (loader == NULL)
1219 return NULL; /* error */
1220 if (loader != Py_None) {
1221 /* a loader was found */
1222 *p_loader = loader;
1223 return &importhookdescr;
1224 }
1225 Py_DECREF(loader);
1226 }
1227 /* no hook was successful, use builtin import */
1228 }
1229
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001230 if (len > 0 && buf[len-1] != SEP
1231#ifdef ALTSEP
1232 && buf[len-1] != ALTSEP
1233#endif
1234 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001236 strcpy(buf+len, name);
1237 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001238
1239 /* Check for package import (buf holds a directory name,
1240 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001241#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001242 if (stat(buf, &statbuf) == 0 && /* it exists */
1243 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1244 find_init_module(buf) && /* it has __init__.py */
1245 case_ok(buf, len, namelen, name)) { /* and case matches */
1246 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001247 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001248 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001249#else
1250 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001251#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001252 if (isdir(buf) &&
1253 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001254 case_ok(buf, len, namelen, name)) {
1255 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001256 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001257 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001258#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001259#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001260#if defined(PYOS_OS2)
1261 /* take a snapshot of the module spec for restoration
1262 * after the 8 character DLL hackery
1263 */
1264 saved_buf = strdup(buf);
1265 saved_len = len;
1266 saved_namelen = namelen;
1267#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001268 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001269#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001270 /* OS/2 limits DLLs to 8 character names (w/o
1271 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001272 * so if the name is longer than that and its a
1273 * dynamically loaded module we're going to try,
1274 * truncate the name before trying
1275 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001276 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001277 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001278 const struct filedescr *scan;
1279 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001280 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001281 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001282 break;
1283 else
1284 scan++;
1285 }
1286 if (scan->suffix != NULL) {
1287 /* yes, so truncate the name */
1288 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001289 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001290 buf[len] = '\0';
1291 }
1292 }
1293#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001294 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001295 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001296 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001297 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001298 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001299 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001300 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001301 if (fp != NULL) {
1302 if (case_ok(buf, len, namelen, name))
1303 break;
1304 else { /* continue search */
1305 fclose(fp);
1306 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001307 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001308 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001309#if defined(PYOS_OS2)
1310 /* restore the saved snapshot */
1311 strcpy(buf, saved_buf);
1312 len = saved_len;
1313 namelen = saved_namelen;
1314#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001315 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001316#if defined(PYOS_OS2)
1317 /* don't need/want the module name snapshot anymore */
1318 if (saved_buf)
1319 {
1320 free(saved_buf);
1321 saved_buf = NULL;
1322 }
1323#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001324 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325 if (fp != NULL)
1326 break;
1327 }
1328 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001329 PyErr_Format(PyExc_ImportError,
1330 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001331 return NULL;
1332 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001333 *p_fp = fp;
1334 return fdp;
1335}
1336
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001337/* Helpers for main.c
1338 * Find the source file corresponding to a named module
1339 */
1340struct filedescr *
1341_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1342 size_t buflen, FILE **p_fp, PyObject **p_loader)
1343{
1344 return find_module((char *) name, (char *) name, path,
1345 buf, buflen, p_fp, p_loader);
1346}
1347
1348PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1349{
1350 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1351}
1352
Tim Petersd1e87a82001-03-01 18:12:00 +00001353/* case_ok(char* buf, int len, int namelen, char* name)
1354 * The arguments here are tricky, best shown by example:
1355 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1356 * ^ ^ ^ ^
1357 * |--------------------- buf ---------------------|
1358 * |------------------- len ------------------|
1359 * |------ name -------|
1360 * |----- namelen -----|
1361 * buf is the full path, but len only counts up to (& exclusive of) the
1362 * extension. name is the module name, also exclusive of extension.
1363 *
1364 * We've already done a successful stat() or fopen() on buf, so know that
1365 * there's some match, possibly case-insensitive.
1366 *
Tim Peters50d8d372001-02-28 05:34:27 +00001367 * case_ok() is to return 1 if there's a case-sensitive match for
1368 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1369 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001370 *
Tim Peters50d8d372001-02-28 05:34:27 +00001371 * case_ok() is used to implement case-sensitive import semantics even
1372 * on platforms with case-insensitive filesystems. It's trivial to implement
1373 * for case-sensitive filesystems. It's pretty much a cross-platform
1374 * nightmare for systems with case-insensitive filesystems.
1375 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001376
Tim Peters50d8d372001-02-28 05:34:27 +00001377/* First we may need a pile of platform-specific header files; the sequence
1378 * of #if's here should match the sequence in the body of case_ok().
1379 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001380#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001381#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001382#ifdef __CYGWIN__
1383#include <sys/cygwin.h>
1384#endif
1385
Tim Peters50d8d372001-02-28 05:34:27 +00001386#elif defined(DJGPP)
1387#include <dir.h>
1388
Tim Petersd1e87a82001-03-01 18:12:00 +00001389#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001390#include <sys/types.h>
1391#include <dirent.h>
1392
Andrew MacIntyred9400542002-02-26 11:41:34 +00001393#elif defined(PYOS_OS2)
1394#define INCL_DOS
1395#define INCL_DOSERRORS
1396#define INCL_NOPMAPI
1397#include <os2.h>
1398
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001399#elif defined(RISCOS)
1400#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001401#endif
1402
Guido van Rossum0980bd91998-02-13 17:18:36 +00001403static int
Tim Peters50d8d372001-02-28 05:34:27 +00001404case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001405{
Tim Peters50d8d372001-02-28 05:34:27 +00001406/* Pick a platform-specific implementation; the sequence of #if's here should
1407 * match the sequence just above.
1408 */
1409
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001410/* MS_WINDOWS || __CYGWIN__ */
1411#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001412 WIN32_FIND_DATA data;
1413 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001414#ifdef __CYGWIN__
1415 char tempbuf[MAX_PATH];
1416#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001417
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001418 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001419 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001420
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001421#ifdef __CYGWIN__
1422 cygwin32_conv_to_win32_path(buf, tempbuf);
1423 h = FindFirstFile(tempbuf, &data);
1424#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001425 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001426#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001427 if (h == INVALID_HANDLE_VALUE) {
1428 PyErr_Format(PyExc_NameError,
1429 "Can't find file for module %.100s\n(filename %.300s)",
1430 name, buf);
1431 return 0;
1432 }
1433 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001434 return strncmp(data.cFileName, name, namelen) == 0;
1435
1436/* DJGPP */
1437#elif defined(DJGPP)
1438 struct ffblk ffblk;
1439 int done;
1440
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001441 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001442 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001443
1444 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1445 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001446 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001447 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001448 name, buf);
1449 return 0;
1450 }
Tim Peters50d8d372001-02-28 05:34:27 +00001451 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001452
Tim Peters677898a2001-03-02 03:28:03 +00001453/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001454#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001455 DIR *dirp;
1456 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001457 char dirname[MAXPATHLEN + 1];
1458 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001459
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001460 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001461 return 1;
1462
Tim Petersd1e87a82001-03-01 18:12:00 +00001463 /* Copy the dir component into dirname; substitute "." if empty */
1464 if (dirlen <= 0) {
1465 dirname[0] = '.';
1466 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001467 }
1468 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001469 assert(dirlen <= MAXPATHLEN);
1470 memcpy(dirname, buf, dirlen);
1471 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001472 }
1473 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001474 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001475 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001476 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001477 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001478 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001479#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001480 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001481#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001482 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001483#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001484 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001485 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001486 (void)closedir(dirp);
1487 return 1; /* Found */
1488 }
1489 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001490 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001491 }
Tim Peters430f5d42001-03-01 01:30:56 +00001492 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001493
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001494/* RISC OS */
1495#elif defined(RISCOS)
1496 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1497 char buf2[MAXPATHLEN+2];
1498 char *nameWithExt = buf+len-namelen;
1499 int canonlen;
1500 os_error *e;
1501
1502 if (Py_GETENV("PYTHONCASEOK") != NULL)
1503 return 1;
1504
1505 /* workaround:
1506 append wildcard, otherwise case of filename wouldn't be touched */
1507 strcpy(buf2, buf);
1508 strcat(buf2, "*");
1509
1510 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1511 canonlen = MAXPATHLEN+1-canonlen;
1512 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1513 return 0;
1514 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1515 return 1; /* match */
1516
1517 return 0;
1518
Andrew MacIntyred9400542002-02-26 11:41:34 +00001519/* OS/2 */
1520#elif defined(PYOS_OS2)
1521 HDIR hdir = 1;
1522 ULONG srchcnt = 1;
1523 FILEFINDBUF3 ffbuf;
1524 APIRET rc;
1525
1526 if (getenv("PYTHONCASEOK") != NULL)
1527 return 1;
1528
1529 rc = DosFindFirst(buf,
1530 &hdir,
1531 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1532 &ffbuf, sizeof(ffbuf),
1533 &srchcnt,
1534 FIL_STANDARD);
1535 if (rc != NO_ERROR)
1536 return 0;
1537 return strncmp(ffbuf.achName, name, namelen) == 0;
1538
Tim Peters50d8d372001-02-28 05:34:27 +00001539/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1540#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001541 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001542
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001543#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001544}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001545
Guido van Rossum0980bd91998-02-13 17:18:36 +00001546
Guido van Rossum197346f1997-10-31 18:38:52 +00001547#ifdef HAVE_STAT
1548/* Helper to look for __init__.py or __init__.py[co] in potential package */
1549static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001550find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001551{
Tim Peters0f9431f2001-07-05 03:47:53 +00001552 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001553 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001554 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001555 struct stat statbuf;
1556
Tim Peters0f9431f2001-07-05 03:47:53 +00001557/* For calling case_ok(buf, len, namelen, name):
1558 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1559 * ^ ^ ^ ^
1560 * |--------------------- buf ---------------------|
1561 * |------------------- len ------------------|
1562 * |------ name -------|
1563 * |----- namelen -----|
1564 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001565 if (save_len + 13 >= MAXPATHLEN)
1566 return 0;
1567 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001568 pname = buf + i;
1569 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001570 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001571 if (case_ok(buf,
1572 save_len + 9, /* len("/__init__") */
1573 8, /* len("__init__") */
1574 pname)) {
1575 buf[save_len] = '\0';
1576 return 1;
1577 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001578 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001579 i += strlen(pname);
1580 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001581 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001582 if (case_ok(buf,
1583 save_len + 9, /* len("/__init__") */
1584 8, /* len("__init__") */
1585 pname)) {
1586 buf[save_len] = '\0';
1587 return 1;
1588 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001589 }
1590 buf[save_len] = '\0';
1591 return 0;
1592}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001593
1594#else
1595
1596#ifdef RISCOS
1597static int
1598find_init_module(buf)
1599 char *buf;
1600{
1601 int save_len = strlen(buf);
1602 int i = save_len;
1603
1604 if (save_len + 13 >= MAXPATHLEN)
1605 return 0;
1606 buf[i++] = SEP;
1607 strcpy(buf+i, "__init__/py");
1608 if (isfile(buf)) {
1609 buf[save_len] = '\0';
1610 return 1;
1611 }
1612
1613 if (Py_OptimizeFlag)
1614 strcpy(buf+i, "o");
1615 else
1616 strcpy(buf+i, "c");
1617 if (isfile(buf)) {
1618 buf[save_len] = '\0';
1619 return 1;
1620 }
1621 buf[save_len] = '\0';
1622 return 0;
1623}
1624#endif /*RISCOS*/
1625
Guido van Rossum197346f1997-10-31 18:38:52 +00001626#endif /* HAVE_STAT */
1627
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001628
Tim Petersdbd9ba62000-07-09 03:09:57 +00001629static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001630
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001631/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001632 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001633
Guido van Rossum79f25d91997-04-29 20:08:16 +00001634static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001635load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001636{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001637 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001639 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001640
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001641 /* First check that there's an open file (if we need one) */
1642 switch (type) {
1643 case PY_SOURCE:
1644 case PY_COMPILED:
1645 if (fp == NULL) {
1646 PyErr_Format(PyExc_ValueError,
1647 "file object required for import (type code %d)",
1648 type);
1649 return NULL;
1650 }
1651 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001652
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001653 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001654
1655 case PY_SOURCE:
1656 m = load_source_module(name, buf, fp);
1657 break;
1658
1659 case PY_COMPILED:
1660 m = load_compiled_module(name, buf, fp);
1661 break;
1662
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001663#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001664 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001665 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001666 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001667#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001668
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001669 case PKG_DIRECTORY:
1670 m = load_package(name, buf);
1671 break;
1672
1673 case C_BUILTIN:
1674 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001675 if (buf != NULL && buf[0] != '\0')
1676 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001677 if (type == C_BUILTIN)
1678 err = init_builtin(name);
1679 else
1680 err = PyImport_ImportFrozenModule(name);
1681 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001682 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001683 if (err == 0) {
1684 PyErr_Format(PyExc_ImportError,
1685 "Purported %s module %.200s not found",
1686 type == C_BUILTIN ?
1687 "builtin" : "frozen",
1688 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001689 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001690 }
1691 modules = PyImport_GetModuleDict();
1692 m = PyDict_GetItemString(modules, name);
1693 if (m == NULL) {
1694 PyErr_Format(
1695 PyExc_ImportError,
1696 "%s module %.200s not properly initialized",
1697 type == C_BUILTIN ?
1698 "builtin" : "frozen",
1699 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001700 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001701 }
1702 Py_INCREF(m);
1703 break;
1704
Just van Rossum52e14d62002-12-30 22:08:05 +00001705 case IMP_HOOK: {
1706 if (loader == NULL) {
1707 PyErr_SetString(PyExc_ImportError,
1708 "import hook without loader");
1709 return NULL;
1710 }
1711 m = PyObject_CallMethod(loader, "load_module", "s", name);
1712 break;
1713 }
1714
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001716 PyErr_Format(PyExc_ImportError,
1717 "Don't know how to import %.200s (type code %d)",
1718 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001719 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720
1721 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722
1723 return m;
1724}
1725
1726
1727/* Initialize a built-in module.
1728 Return 1 for succes, 0 if the module is not found, and -1 with
1729 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001730
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001731static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001732init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001733{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001734 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001735
Greg Ward201baee2001-10-04 14:52:06 +00001736 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001737 return 1;
1738
Guido van Rossum771c6c81997-10-31 18:37:24 +00001739 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001740 if (strcmp(name, p->name) == 0) {
1741 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001742 PyErr_Format(PyExc_ImportError,
1743 "Cannot re-init internal module %.200s",
1744 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001745 return -1;
1746 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001748 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001749 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001751 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001752 if (_PyImport_FixupExtension(name, name) == NULL)
1753 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001754 return 1;
1755 }
1756 }
1757 return 0;
1758}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001759
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001760
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001761/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001762
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001763static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001764find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001765{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001766 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001767
Guido van Rossum79f25d91997-04-29 20:08:16 +00001768 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001769 if (p->name == NULL)
1770 return NULL;
1771 if (strcmp(p->name, name) == 0)
1772 break;
1773 }
1774 return p;
1775}
1776
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001778get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001779{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001780 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001781 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001782
1783 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001784 PyErr_Format(PyExc_ImportError,
1785 "No such frozen object named %.200s",
1786 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001787 return NULL;
1788 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001789 if (p->code == NULL) {
1790 PyErr_Format(PyExc_ImportError,
1791 "Excluded frozen object named %.200s",
1792 name);
1793 return NULL;
1794 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001795 size = p->size;
1796 if (size < 0)
1797 size = -size;
1798 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001799}
1800
1801/* Initialize a frozen module.
1802 Return 1 for succes, 0 if the module is not found, and -1 with
1803 an exception set if the initialization failed.
1804 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001805
1806int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001807PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001808{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001809 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 PyObject *co;
1811 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001812 int ispackage;
1813 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001814
1815 if (p == NULL)
1816 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001817 if (p->code == NULL) {
1818 PyErr_Format(PyExc_ImportError,
1819 "Excluded frozen object named %.200s",
1820 name);
1821 return -1;
1822 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001823 size = p->size;
1824 ispackage = (size < 0);
1825 if (ispackage)
1826 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001827 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001828 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001829 name, ispackage ? " package" : "");
1830 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001831 if (co == NULL)
1832 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if (!PyCode_Check(co)) {
1834 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001835 PyErr_Format(PyExc_TypeError,
1836 "frozen object %.200s is not a code object",
1837 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001838 return -1;
1839 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001840 if (ispackage) {
1841 /* Set __path__ to the package name */
1842 PyObject *d, *s;
1843 int err;
1844 m = PyImport_AddModule(name);
1845 if (m == NULL)
1846 return -1;
1847 d = PyModule_GetDict(m);
1848 s = PyString_InternFromString(name);
1849 if (s == NULL)
1850 return -1;
1851 err = PyDict_SetItemString(d, "__path__", s);
1852 Py_DECREF(s);
1853 if (err != 0)
1854 return err;
1855 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001856 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001858 if (m == NULL)
1859 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001861 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001862}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001863
1864
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001865/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001866 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001867
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001869PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001870{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001871 PyObject *pname;
1872 PyObject *result;
1873
1874 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001875 if (pname == NULL)
1876 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001877 result = PyImport_Import(pname);
1878 Py_DECREF(pname);
1879 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001880}
1881
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001882/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001883static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1884static PyObject *load_next(PyObject *mod, PyObject *altmod,
1885 char **p_name, char *buf, int *p_buflen);
1886static int mark_miss(char *name);
1887static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1888 char *buf, int buflen, int recursive);
1889static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001890
1891/* The Magnum Opus of dotted-name import :-) */
1892
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001893static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001894import_module_ex(char *name, PyObject *globals, PyObject *locals,
1895 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001896{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001897 char buf[MAXPATHLEN+1];
1898 int buflen = 0;
1899 PyObject *parent, *head, *next, *tail;
1900
1901 parent = get_parent(globals, buf, &buflen);
1902 if (parent == NULL)
1903 return NULL;
1904
1905 head = load_next(parent, Py_None, &name, buf, &buflen);
1906 if (head == NULL)
1907 return NULL;
1908
1909 tail = head;
1910 Py_INCREF(tail);
1911 while (name) {
1912 next = load_next(tail, tail, &name, buf, &buflen);
1913 Py_DECREF(tail);
1914 if (next == NULL) {
1915 Py_DECREF(head);
1916 return NULL;
1917 }
1918 tail = next;
1919 }
1920
1921 if (fromlist != NULL) {
1922 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1923 fromlist = NULL;
1924 }
1925
1926 if (fromlist == NULL) {
1927 Py_DECREF(tail);
1928 return head;
1929 }
1930
1931 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001932 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001933 Py_DECREF(tail);
1934 return NULL;
1935 }
1936
1937 return tail;
1938}
1939
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001940PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001941PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1942 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001943{
1944 PyObject *result;
1945 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001946 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001947 if (unlock_import() < 0) {
1948 Py_XDECREF(result);
1949 PyErr_SetString(PyExc_RuntimeError,
1950 "not holding the import lock");
1951 return NULL;
1952 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001953 return result;
1954}
1955
Fred Drake87590902004-05-28 20:21:36 +00001956/* Return the package that an import is being performed in. If globals comes
1957 from the module foo.bar.bat (not itself a package), this returns the
1958 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1959 the package's entry in sys.modules is returned.
1960
1961 The *name* of the returned package is returned in buf, with the length of
1962 the name in *p_buflen.
1963
1964 If globals doesn't come from a package or a module in a package, or a
1965 corresponding entry is not found in sys.modules, Py_None is returned.
1966*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001967static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001968get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001969{
1970 static PyObject *namestr = NULL;
1971 static PyObject *pathstr = NULL;
1972 PyObject *modname, *modpath, *modules, *parent;
1973
1974 if (globals == NULL || !PyDict_Check(globals))
1975 return Py_None;
1976
1977 if (namestr == NULL) {
1978 namestr = PyString_InternFromString("__name__");
1979 if (namestr == NULL)
1980 return NULL;
1981 }
1982 if (pathstr == NULL) {
1983 pathstr = PyString_InternFromString("__path__");
1984 if (pathstr == NULL)
1985 return NULL;
1986 }
1987
1988 *buf = '\0';
1989 *p_buflen = 0;
1990 modname = PyDict_GetItem(globals, namestr);
1991 if (modname == NULL || !PyString_Check(modname))
1992 return Py_None;
1993
1994 modpath = PyDict_GetItem(globals, pathstr);
1995 if (modpath != NULL) {
1996 int len = PyString_GET_SIZE(modname);
1997 if (len > MAXPATHLEN) {
1998 PyErr_SetString(PyExc_ValueError,
1999 "Module name too long");
2000 return NULL;
2001 }
2002 strcpy(buf, PyString_AS_STRING(modname));
2003 *p_buflen = len;
2004 }
2005 else {
2006 char *start = PyString_AS_STRING(modname);
2007 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002008 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002009 if (lastdot == NULL)
2010 return Py_None;
2011 len = lastdot - start;
2012 if (len >= MAXPATHLEN) {
2013 PyErr_SetString(PyExc_ValueError,
2014 "Module name too long");
2015 return NULL;
2016 }
2017 strncpy(buf, start, len);
2018 buf[len] = '\0';
2019 *p_buflen = len;
2020 }
2021
2022 modules = PyImport_GetModuleDict();
2023 parent = PyDict_GetItemString(modules, buf);
2024 if (parent == NULL)
2025 parent = Py_None;
2026 return parent;
2027 /* We expect, but can't guarantee, if parent != None, that:
2028 - parent.__name__ == buf
2029 - parent.__dict__ is globals
2030 If this is violated... Who cares? */
2031}
2032
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002033/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002034static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002035load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2036 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002037{
2038 char *name = *p_name;
2039 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002040 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002041 char *p;
2042 PyObject *result;
2043
2044 if (dot == NULL) {
2045 *p_name = NULL;
2046 len = strlen(name);
2047 }
2048 else {
2049 *p_name = dot+1;
2050 len = dot-name;
2051 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002052 if (len == 0) {
2053 PyErr_SetString(PyExc_ValueError,
2054 "Empty module name");
2055 return NULL;
2056 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002057
2058 p = buf + *p_buflen;
2059 if (p != buf)
2060 *p++ = '.';
2061 if (p+len-buf >= MAXPATHLEN) {
2062 PyErr_SetString(PyExc_ValueError,
2063 "Module name too long");
2064 return NULL;
2065 }
2066 strncpy(p, name, len);
2067 p[len] = '\0';
2068 *p_buflen = p+len-buf;
2069
2070 result = import_submodule(mod, p, buf);
2071 if (result == Py_None && altmod != mod) {
2072 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002073 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002074 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002075 if (result != NULL && result != Py_None) {
2076 if (mark_miss(buf) != 0) {
2077 Py_DECREF(result);
2078 return NULL;
2079 }
2080 strncpy(buf, name, len);
2081 buf[len] = '\0';
2082 *p_buflen = len;
2083 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002084 }
2085 if (result == NULL)
2086 return NULL;
2087
2088 if (result == Py_None) {
2089 Py_DECREF(result);
2090 PyErr_Format(PyExc_ImportError,
2091 "No module named %.200s", name);
2092 return NULL;
2093 }
2094
2095 return result;
2096}
2097
2098static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002099mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002100{
2101 PyObject *modules = PyImport_GetModuleDict();
2102 return PyDict_SetItemString(modules, name, Py_None);
2103}
2104
2105static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002106ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2107 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002108{
2109 int i;
2110
2111 if (!PyObject_HasAttrString(mod, "__path__"))
2112 return 1;
2113
2114 for (i = 0; ; i++) {
2115 PyObject *item = PySequence_GetItem(fromlist, i);
2116 int hasit;
2117 if (item == NULL) {
2118 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2119 PyErr_Clear();
2120 return 1;
2121 }
2122 return 0;
2123 }
2124 if (!PyString_Check(item)) {
2125 PyErr_SetString(PyExc_TypeError,
2126 "Item in ``from list'' not a string");
2127 Py_DECREF(item);
2128 return 0;
2129 }
2130 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002131 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002132 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002133 /* See if the package defines __all__ */
2134 if (recursive)
2135 continue; /* Avoid endless recursion */
2136 all = PyObject_GetAttrString(mod, "__all__");
2137 if (all == NULL)
2138 PyErr_Clear();
2139 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002140 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002141 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002142 if (!ret)
2143 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002144 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002145 continue;
2146 }
2147 hasit = PyObject_HasAttr(mod, item);
2148 if (!hasit) {
2149 char *subname = PyString_AS_STRING(item);
2150 PyObject *submod;
2151 char *p;
2152 if (buflen + strlen(subname) >= MAXPATHLEN) {
2153 PyErr_SetString(PyExc_ValueError,
2154 "Module name too long");
2155 Py_DECREF(item);
2156 return 0;
2157 }
2158 p = buf + buflen;
2159 *p++ = '.';
2160 strcpy(p, subname);
2161 submod = import_submodule(mod, subname, buf);
2162 Py_XDECREF(submod);
2163 if (submod == NULL) {
2164 Py_DECREF(item);
2165 return 0;
2166 }
2167 }
2168 Py_DECREF(item);
2169 }
2170
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002171 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002172}
2173
Neil Schemenauer00b09662003-06-16 21:03:07 +00002174static int
2175add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2176 PyObject *modules)
2177{
2178 if (mod == Py_None)
2179 return 1;
2180 /* Irrespective of the success of this load, make a
2181 reference to it in the parent package module. A copy gets
2182 saved in the modules dictionary under the full name, so get a
2183 reference from there, if need be. (The exception is when the
2184 load failed with a SyntaxError -- then there's no trace in
2185 sys.modules. In that case, of course, do nothing extra.) */
2186 if (submod == NULL) {
2187 submod = PyDict_GetItemString(modules, fullname);
2188 if (submod == NULL)
2189 return 1;
2190 }
2191 if (PyModule_Check(mod)) {
2192 /* We can't use setattr here since it can give a
2193 * spurious warning if the submodule name shadows a
2194 * builtin name */
2195 PyObject *dict = PyModule_GetDict(mod);
2196 if (!dict)
2197 return 0;
2198 if (PyDict_SetItemString(dict, subname, submod) < 0)
2199 return 0;
2200 }
2201 else {
2202 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2203 return 0;
2204 }
2205 return 1;
2206}
2207
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002208static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002209import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002210{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002211 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002212 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002213
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002214 /* Require:
2215 if mod == None: subname == fullname
2216 else: mod.__name__ + "." + subname == fullname
2217 */
2218
Tim Peters50d8d372001-02-28 05:34:27 +00002219 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002220 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002221 }
2222 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002223 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002224 char buf[MAXPATHLEN+1];
2225 struct filedescr *fdp;
2226 FILE *fp = NULL;
2227
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002228 if (mod == Py_None)
2229 path = NULL;
2230 else {
2231 path = PyObject_GetAttrString(mod, "__path__");
2232 if (path == NULL) {
2233 PyErr_Clear();
2234 Py_INCREF(Py_None);
2235 return Py_None;
2236 }
2237 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002238
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002239 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002240 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2241 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002242 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002243 if (fdp == NULL) {
2244 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2245 return NULL;
2246 PyErr_Clear();
2247 Py_INCREF(Py_None);
2248 return Py_None;
2249 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002250 m = load_module(fullname, fp, buf, fdp->type, loader);
2251 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002252 if (fp)
2253 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002254 if (!add_submodule(mod, m, fullname, subname, modules)) {
2255 Py_XDECREF(m);
2256 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002257 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002258 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002259
2260 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002261}
2262
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002263
2264/* Re-import a module of any kind and return its module object, WITH
2265 INCREMENTED REFERENCE COUNT */
2266
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002268PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002269{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002270 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002271 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002272 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002273 char buf[MAXPATHLEN+1];
2274 struct filedescr *fdp;
2275 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002276 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002277
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 if (m == NULL || !PyModule_Check(m)) {
2279 PyErr_SetString(PyExc_TypeError,
2280 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002281 return NULL;
2282 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002284 if (name == NULL)
2285 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002286 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002287 PyErr_Format(PyExc_ImportError,
2288 "reload(): module %.200s not in sys.modules",
2289 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002290 return NULL;
2291 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002292 subname = strrchr(name, '.');
2293 if (subname == NULL)
2294 subname = name;
2295 else {
2296 PyObject *parentname, *parent;
2297 parentname = PyString_FromStringAndSize(name, (subname-name));
2298 if (parentname == NULL)
2299 return NULL;
2300 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002301 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002302 if (parent == NULL) {
2303 PyErr_Format(PyExc_ImportError,
2304 "reload(): parent %.200s not in sys.modules",
2305 name);
2306 return NULL;
2307 }
2308 subname++;
2309 path = PyObject_GetAttrString(parent, "__path__");
2310 if (path == NULL)
2311 PyErr_Clear();
2312 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002313 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002314 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002315 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002316
2317 if (fdp == NULL) {
2318 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002319 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002320 }
2321
2322 newm = load_module(name, fp, buf, fdp->type, loader);
2323 Py_XDECREF(loader);
2324
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002325 if (fp)
2326 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002327 if (newm == NULL) {
2328 /* load_module probably removed name from modules because of
2329 * the error. Put back the original module object. We're
2330 * going to return NULL in this case regardless of whether
2331 * replacing name succeeds, so the return value is ignored.
2332 */
2333 PyDict_SetItemString(modules, name, m);
2334 }
2335 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002336}
2337
2338
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002339/* Higher-level import emulator which emulates the "import" statement
2340 more accurately -- it invokes the __import__() function from the
2341 builtins of the current globals. This means that the import is
2342 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002343 environment, e.g. by "rexec".
2344 A dummy list ["__doc__"] is passed as the 4th argument so that
2345 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2346 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002347
2348PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002349PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002350{
2351 static PyObject *silly_list = NULL;
2352 static PyObject *builtins_str = NULL;
2353 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002354 PyObject *globals = NULL;
2355 PyObject *import = NULL;
2356 PyObject *builtins = NULL;
2357 PyObject *r = NULL;
2358
2359 /* Initialize constant string objects */
2360 if (silly_list == NULL) {
2361 import_str = PyString_InternFromString("__import__");
2362 if (import_str == NULL)
2363 return NULL;
2364 builtins_str = PyString_InternFromString("__builtins__");
2365 if (builtins_str == NULL)
2366 return NULL;
2367 silly_list = Py_BuildValue("[s]", "__doc__");
2368 if (silly_list == NULL)
2369 return NULL;
2370 }
2371
2372 /* Get the builtins from current globals */
2373 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002374 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002375 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002376 builtins = PyObject_GetItem(globals, builtins_str);
2377 if (builtins == NULL)
2378 goto err;
2379 }
2380 else {
2381 /* No globals -- use standard builtins, and fake globals */
2382 PyErr_Clear();
2383
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002384 builtins = PyImport_ImportModuleEx("__builtin__",
2385 NULL, NULL, NULL);
2386 if (builtins == NULL)
2387 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002388 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2389 if (globals == NULL)
2390 goto err;
2391 }
2392
2393 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002394 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002395 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002396 if (import == NULL)
2397 PyErr_SetObject(PyExc_KeyError, import_str);
2398 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002399 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002400 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002401 if (import == NULL)
2402 goto err;
2403
2404 /* Call the _import__ function with the proper argument list */
2405 r = PyObject_CallFunction(import, "OOOO",
2406 module_name, globals, globals, silly_list);
2407
2408 err:
2409 Py_XDECREF(globals);
2410 Py_XDECREF(builtins);
2411 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002412
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002413 return r;
2414}
2415
2416
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417/* Module 'imp' provides Python access to the primitives used for
2418 importing modules.
2419*/
2420
Guido van Rossum79f25d91997-04-29 20:08:16 +00002421static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002422imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002423{
2424 char buf[4];
2425
Guido van Rossum96774c12000-05-01 20:19:08 +00002426 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2427 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2428 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2429 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002430
Guido van Rossum79f25d91997-04-29 20:08:16 +00002431 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002432}
2433
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002435imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002436{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438 struct filedescr *fdp;
2439
Guido van Rossum79f25d91997-04-29 20:08:16 +00002440 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441 if (list == NULL)
2442 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002443 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2444 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002445 fdp->suffix, fdp->mode, fdp->type);
2446 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002447 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002448 return NULL;
2449 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002450 if (PyList_Append(list, item) < 0) {
2451 Py_DECREF(list);
2452 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002453 return NULL;
2454 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002455 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002456 }
2457 return list;
2458}
2459
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002461call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002462{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002463 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 struct filedescr *fdp;
2466 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002467 FILE *fp = NULL;
2468
2469 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002470 if (path == Py_None)
2471 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002472 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002473 if (fdp == NULL)
2474 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002475 if (fp != NULL) {
2476 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2477 if (fob == NULL) {
2478 fclose(fp);
2479 return NULL;
2480 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002481 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002482 else {
2483 fob = Py_None;
2484 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002485 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002486 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002487 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002488 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002489 return ret;
2490}
2491
Guido van Rossum79f25d91997-04-29 20:08:16 +00002492static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002493imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002494{
2495 char *name;
2496 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002497 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002498 return NULL;
2499 return call_find_module(name, path);
2500}
2501
2502static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002503imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002504{
2505 char *name;
2506 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002507 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002508 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002509 return NULL;
2510 ret = init_builtin(name);
2511 if (ret < 0)
2512 return NULL;
2513 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002514 Py_INCREF(Py_None);
2515 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002517 m = PyImport_AddModule(name);
2518 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519 return m;
2520}
2521
Guido van Rossum79f25d91997-04-29 20:08:16 +00002522static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002523imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002524{
2525 char *name;
2526 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002528 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002529 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531 if (ret < 0)
2532 return NULL;
2533 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002534 Py_INCREF(Py_None);
2535 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002536 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002537 m = PyImport_AddModule(name);
2538 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539 return m;
2540}
2541
Guido van Rossum79f25d91997-04-29 20:08:16 +00002542static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002543imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002544{
2545 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002546
Guido van Rossum43713e52000-02-29 13:59:29 +00002547 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002548 return NULL;
2549 return get_frozen_object(name);
2550}
2551
Guido van Rossum79f25d91997-04-29 20:08:16 +00002552static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002553imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002554{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002555 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002556 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002557 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002558 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002559}
2560
Guido van Rossum79f25d91997-04-29 20:08:16 +00002561static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002562imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002564 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002565 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002566 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002567 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002568 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002569 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570}
2571
2572static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002573get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002574{
2575 FILE *fp;
2576 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002577 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002578 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002579 fp = fopen(pathname, mode);
2580 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002581 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002582 }
2583 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002585 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002586 PyErr_SetString(PyExc_ValueError,
2587 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002588 }
2589 return fp;
2590}
2591
Guido van Rossum79f25d91997-04-29 20:08:16 +00002592static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002593imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002594{
2595 char *name;
2596 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002597 PyObject *fob = NULL;
2598 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002599 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002600 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002601 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602 return NULL;
2603 fp = get_file(pathname, fob, "rb");
2604 if (fp == NULL)
2605 return NULL;
2606 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002607 if (fob == NULL)
2608 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002609 return m;
2610}
2611
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002612#ifdef HAVE_DYNAMIC_LOADING
2613
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002615imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002616{
2617 char *name;
2618 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002619 PyObject *fob = NULL;
2620 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002621 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002622 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002623 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002624 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002625 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002626 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002627 if (fp == NULL)
2628 return NULL;
2629 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002630 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002631 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002632}
2633
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002634#endif /* HAVE_DYNAMIC_LOADING */
2635
Guido van Rossum79f25d91997-04-29 20:08:16 +00002636static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002637imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002638{
2639 char *name;
2640 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002641 PyObject *fob = NULL;
2642 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002643 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002644 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002646 return NULL;
2647 fp = get_file(pathname, fob, "r");
2648 if (fp == NULL)
2649 return NULL;
2650 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002651 if (fob == NULL)
2652 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002653 return m;
2654}
2655
Guido van Rossum79f25d91997-04-29 20:08:16 +00002656static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002657imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002658{
2659 char *name;
2660 PyObject *fob;
2661 char *pathname;
2662 char *suffix; /* Unused */
2663 char *mode;
2664 int type;
2665 FILE *fp;
2666
Guido van Rossum43713e52000-02-29 13:59:29 +00002667 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002668 &name, &fob, &pathname,
2669 &suffix, &mode, &type))
2670 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002671 if (*mode) {
2672 /* Mode must start with 'r' or 'U' and must not contain '+'.
2673 Implicit in this test is the assumption that the mode
2674 may contain other modifiers like 'b' or 't'. */
2675
2676 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002677 PyErr_Format(PyExc_ValueError,
2678 "invalid file open mode %.200s", mode);
2679 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002680 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002681 }
2682 if (fob == Py_None)
2683 fp = NULL;
2684 else {
2685 if (!PyFile_Check(fob)) {
2686 PyErr_SetString(PyExc_ValueError,
2687 "load_module arg#2 should be a file or None");
2688 return NULL;
2689 }
2690 fp = get_file(pathname, fob, mode);
2691 if (fp == NULL)
2692 return NULL;
2693 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002694 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002695}
2696
2697static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002698imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002699{
2700 char *name;
2701 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002702 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002703 return NULL;
2704 return load_package(name, pathname);
2705}
2706
2707static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002708imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002709{
2710 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002711 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002712 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002713 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002714}
2715
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002716/* Doc strings */
2717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(doc_imp,
2719"This module provides the components needed to build your own\n\
2720__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722PyDoc_STRVAR(doc_find_module,
2723"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002724Search for a module. If path is omitted or None, search for a\n\
2725built-in, frozen or special module and continue search in sys.path.\n\
2726The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729PyDoc_STRVAR(doc_load_module,
2730"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002731Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734PyDoc_STRVAR(doc_get_magic,
2735"get_magic() -> string\n\
2736Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002738PyDoc_STRVAR(doc_get_suffixes,
2739"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002740Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002743PyDoc_STRVAR(doc_new_module,
2744"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002745Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002748PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002749"lock_held() -> boolean\n\
2750Return True if the import lock is currently held, else False.\n\
2751On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002752
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002753PyDoc_STRVAR(doc_acquire_lock,
2754"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002755Acquires the interpreter's import lock for the current thread.\n\
2756This lock should be used by import hooks to ensure thread-safety\n\
2757when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002758On platforms without threads, this function does nothing.");
2759
2760PyDoc_STRVAR(doc_release_lock,
2761"release_lock() -> None\n\
2762Release the interpreter's import lock.\n\
2763On platforms without threads, this function does nothing.");
2764
Guido van Rossum79f25d91997-04-29 20:08:16 +00002765static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002766 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2767 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2768 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2769 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2770 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2771 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2772 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2773 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002774 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002775 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2776 {"init_builtin", imp_init_builtin, METH_VARARGS},
2777 {"init_frozen", imp_init_frozen, METH_VARARGS},
2778 {"is_builtin", imp_is_builtin, METH_VARARGS},
2779 {"is_frozen", imp_is_frozen, METH_VARARGS},
2780 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002781#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002782 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002783#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002784 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002785 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002786 {NULL, NULL} /* sentinel */
2787};
2788
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002789static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002790setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002791{
2792 PyObject *v;
2793 int err;
2794
2795 v = PyInt_FromLong((long)value);
2796 err = PyDict_SetItemString(d, name, v);
2797 Py_XDECREF(v);
2798 return err;
2799}
2800
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002801PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002802initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002803{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002804 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002805
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002806 m = Py_InitModule4("imp", imp_methods, doc_imp,
2807 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002808 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002809
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002810 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2811 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2812 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2813 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2814 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2815 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2816 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2817 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002818 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002819 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002820
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002821 failure:
2822 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002823}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002824
2825
Guido van Rossumb18618d2000-05-03 23:44:39 +00002826/* API for embedding applications that want to add their own entries
2827 to the table of built-in modules. This should normally be called
2828 *before* Py_Initialize(). When the table resize fails, -1 is
2829 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002830
2831 After a similar function by Just van Rossum. */
2832
2833int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002834PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002835{
2836 static struct _inittab *our_copy = NULL;
2837 struct _inittab *p;
2838 int i, n;
2839
2840 /* Count the number of entries in both tables */
2841 for (n = 0; newtab[n].name != NULL; n++)
2842 ;
2843 if (n == 0)
2844 return 0; /* Nothing to do */
2845 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2846 ;
2847
2848 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002849 p = our_copy;
2850 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002851 if (p == NULL)
2852 return -1;
2853
2854 /* Copy the tables into the new memory */
2855 if (our_copy != PyImport_Inittab)
2856 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2857 PyImport_Inittab = our_copy = p;
2858 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2859
2860 return 0;
2861}
2862
2863/* Shorthand to add a single entry given a name and a function */
2864
2865int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002866PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002867{
2868 struct _inittab newtab[2];
2869
2870 memset(newtab, '\0', sizeof newtab);
2871
2872 newtab[0].name = name;
2873 newtab[0].initfunc = initfunc;
2874
2875 return PyImport_ExtendInittab(newtab);
2876}