blob: 8f81d6ef8e1bc751579075cddf3382701507e668 [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
Tim Petersd1e87a82001-03-01 18:12:00 +00001337/* case_ok(char* buf, int len, int namelen, char* name)
1338 * The arguments here are tricky, best shown by example:
1339 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1340 * ^ ^ ^ ^
1341 * |--------------------- buf ---------------------|
1342 * |------------------- len ------------------|
1343 * |------ name -------|
1344 * |----- namelen -----|
1345 * buf is the full path, but len only counts up to (& exclusive of) the
1346 * extension. name is the module name, also exclusive of extension.
1347 *
1348 * We've already done a successful stat() or fopen() on buf, so know that
1349 * there's some match, possibly case-insensitive.
1350 *
Tim Peters50d8d372001-02-28 05:34:27 +00001351 * case_ok() is to return 1 if there's a case-sensitive match for
1352 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1353 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001354 *
Tim Peters50d8d372001-02-28 05:34:27 +00001355 * case_ok() is used to implement case-sensitive import semantics even
1356 * on platforms with case-insensitive filesystems. It's trivial to implement
1357 * for case-sensitive filesystems. It's pretty much a cross-platform
1358 * nightmare for systems with case-insensitive filesystems.
1359 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001360
Tim Peters50d8d372001-02-28 05:34:27 +00001361/* First we may need a pile of platform-specific header files; the sequence
1362 * of #if's here should match the sequence in the body of case_ok().
1363 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001364#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001365#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001366#ifdef __CYGWIN__
1367#include <sys/cygwin.h>
1368#endif
1369
Tim Peters50d8d372001-02-28 05:34:27 +00001370#elif defined(DJGPP)
1371#include <dir.h>
1372
Tim Petersd1e87a82001-03-01 18:12:00 +00001373#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001374#include <sys/types.h>
1375#include <dirent.h>
1376
Andrew MacIntyred9400542002-02-26 11:41:34 +00001377#elif defined(PYOS_OS2)
1378#define INCL_DOS
1379#define INCL_DOSERRORS
1380#define INCL_NOPMAPI
1381#include <os2.h>
1382
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001383#elif defined(RISCOS)
1384#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001385#endif
1386
Guido van Rossum0980bd91998-02-13 17:18:36 +00001387static int
Tim Peters50d8d372001-02-28 05:34:27 +00001388case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001389{
Tim Peters50d8d372001-02-28 05:34:27 +00001390/* Pick a platform-specific implementation; the sequence of #if's here should
1391 * match the sequence just above.
1392 */
1393
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001394/* MS_WINDOWS || __CYGWIN__ */
1395#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001396 WIN32_FIND_DATA data;
1397 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001398#ifdef __CYGWIN__
1399 char tempbuf[MAX_PATH];
1400#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001401
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001402 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001403 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001404
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001405#ifdef __CYGWIN__
1406 cygwin32_conv_to_win32_path(buf, tempbuf);
1407 h = FindFirstFile(tempbuf, &data);
1408#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001409 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001410#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001411 if (h == INVALID_HANDLE_VALUE) {
1412 PyErr_Format(PyExc_NameError,
1413 "Can't find file for module %.100s\n(filename %.300s)",
1414 name, buf);
1415 return 0;
1416 }
1417 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001418 return strncmp(data.cFileName, name, namelen) == 0;
1419
1420/* DJGPP */
1421#elif defined(DJGPP)
1422 struct ffblk ffblk;
1423 int done;
1424
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001425 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001426 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001427
1428 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1429 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001430 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001431 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001432 name, buf);
1433 return 0;
1434 }
Tim Peters50d8d372001-02-28 05:34:27 +00001435 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001436
Tim Peters677898a2001-03-02 03:28:03 +00001437/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001438#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001439 DIR *dirp;
1440 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001441 char dirname[MAXPATHLEN + 1];
1442 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001443
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001444 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001445 return 1;
1446
Tim Petersd1e87a82001-03-01 18:12:00 +00001447 /* Copy the dir component into dirname; substitute "." if empty */
1448 if (dirlen <= 0) {
1449 dirname[0] = '.';
1450 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001451 }
1452 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001453 assert(dirlen <= MAXPATHLEN);
1454 memcpy(dirname, buf, dirlen);
1455 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001456 }
1457 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001458 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001459 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001460 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001461 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001462 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001463#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001464 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001465#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001466 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001467#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001468 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001469 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001470 (void)closedir(dirp);
1471 return 1; /* Found */
1472 }
1473 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001474 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001475 }
Tim Peters430f5d42001-03-01 01:30:56 +00001476 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001477
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001478/* RISC OS */
1479#elif defined(RISCOS)
1480 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1481 char buf2[MAXPATHLEN+2];
1482 char *nameWithExt = buf+len-namelen;
1483 int canonlen;
1484 os_error *e;
1485
1486 if (Py_GETENV("PYTHONCASEOK") != NULL)
1487 return 1;
1488
1489 /* workaround:
1490 append wildcard, otherwise case of filename wouldn't be touched */
1491 strcpy(buf2, buf);
1492 strcat(buf2, "*");
1493
1494 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1495 canonlen = MAXPATHLEN+1-canonlen;
1496 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1497 return 0;
1498 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1499 return 1; /* match */
1500
1501 return 0;
1502
Andrew MacIntyred9400542002-02-26 11:41:34 +00001503/* OS/2 */
1504#elif defined(PYOS_OS2)
1505 HDIR hdir = 1;
1506 ULONG srchcnt = 1;
1507 FILEFINDBUF3 ffbuf;
1508 APIRET rc;
1509
1510 if (getenv("PYTHONCASEOK") != NULL)
1511 return 1;
1512
1513 rc = DosFindFirst(buf,
1514 &hdir,
1515 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1516 &ffbuf, sizeof(ffbuf),
1517 &srchcnt,
1518 FIL_STANDARD);
1519 if (rc != NO_ERROR)
1520 return 0;
1521 return strncmp(ffbuf.achName, name, namelen) == 0;
1522
Tim Peters50d8d372001-02-28 05:34:27 +00001523/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1524#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001525 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001526
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001527#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001528}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001529
Guido van Rossum0980bd91998-02-13 17:18:36 +00001530
Guido van Rossum197346f1997-10-31 18:38:52 +00001531#ifdef HAVE_STAT
1532/* Helper to look for __init__.py or __init__.py[co] in potential package */
1533static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001534find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001535{
Tim Peters0f9431f2001-07-05 03:47:53 +00001536 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001537 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001538 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001539 struct stat statbuf;
1540
Tim Peters0f9431f2001-07-05 03:47:53 +00001541/* For calling case_ok(buf, len, namelen, name):
1542 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1543 * ^ ^ ^ ^
1544 * |--------------------- buf ---------------------|
1545 * |------------------- len ------------------|
1546 * |------ name -------|
1547 * |----- namelen -----|
1548 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001549 if (save_len + 13 >= MAXPATHLEN)
1550 return 0;
1551 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001552 pname = buf + i;
1553 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001554 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001555 if (case_ok(buf,
1556 save_len + 9, /* len("/__init__") */
1557 8, /* len("__init__") */
1558 pname)) {
1559 buf[save_len] = '\0';
1560 return 1;
1561 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001562 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001563 i += strlen(pname);
1564 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001565 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001566 if (case_ok(buf,
1567 save_len + 9, /* len("/__init__") */
1568 8, /* len("__init__") */
1569 pname)) {
1570 buf[save_len] = '\0';
1571 return 1;
1572 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001573 }
1574 buf[save_len] = '\0';
1575 return 0;
1576}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001577
1578#else
1579
1580#ifdef RISCOS
1581static int
1582find_init_module(buf)
1583 char *buf;
1584{
1585 int save_len = strlen(buf);
1586 int i = save_len;
1587
1588 if (save_len + 13 >= MAXPATHLEN)
1589 return 0;
1590 buf[i++] = SEP;
1591 strcpy(buf+i, "__init__/py");
1592 if (isfile(buf)) {
1593 buf[save_len] = '\0';
1594 return 1;
1595 }
1596
1597 if (Py_OptimizeFlag)
1598 strcpy(buf+i, "o");
1599 else
1600 strcpy(buf+i, "c");
1601 if (isfile(buf)) {
1602 buf[save_len] = '\0';
1603 return 1;
1604 }
1605 buf[save_len] = '\0';
1606 return 0;
1607}
1608#endif /*RISCOS*/
1609
Guido van Rossum197346f1997-10-31 18:38:52 +00001610#endif /* HAVE_STAT */
1611
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001612
Tim Petersdbd9ba62000-07-09 03:09:57 +00001613static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001614
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001615/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001616 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001617
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001619load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001620{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001621 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001623 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001624
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001625 /* First check that there's an open file (if we need one) */
1626 switch (type) {
1627 case PY_SOURCE:
1628 case PY_COMPILED:
1629 if (fp == NULL) {
1630 PyErr_Format(PyExc_ValueError,
1631 "file object required for import (type code %d)",
1632 type);
1633 return NULL;
1634 }
1635 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001636
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001637 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001638
1639 case PY_SOURCE:
1640 m = load_source_module(name, buf, fp);
1641 break;
1642
1643 case PY_COMPILED:
1644 m = load_compiled_module(name, buf, fp);
1645 break;
1646
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001647#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001648 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001650 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001651#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001652
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001653 case PKG_DIRECTORY:
1654 m = load_package(name, buf);
1655 break;
1656
1657 case C_BUILTIN:
1658 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001659 if (buf != NULL && buf[0] != '\0')
1660 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001661 if (type == C_BUILTIN)
1662 err = init_builtin(name);
1663 else
1664 err = PyImport_ImportFrozenModule(name);
1665 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001666 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001667 if (err == 0) {
1668 PyErr_Format(PyExc_ImportError,
1669 "Purported %s module %.200s not found",
1670 type == C_BUILTIN ?
1671 "builtin" : "frozen",
1672 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001673 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001674 }
1675 modules = PyImport_GetModuleDict();
1676 m = PyDict_GetItemString(modules, name);
1677 if (m == NULL) {
1678 PyErr_Format(
1679 PyExc_ImportError,
1680 "%s module %.200s not properly initialized",
1681 type == C_BUILTIN ?
1682 "builtin" : "frozen",
1683 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001684 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001685 }
1686 Py_INCREF(m);
1687 break;
1688
Just van Rossum52e14d62002-12-30 22:08:05 +00001689 case IMP_HOOK: {
1690 if (loader == NULL) {
1691 PyErr_SetString(PyExc_ImportError,
1692 "import hook without loader");
1693 return NULL;
1694 }
1695 m = PyObject_CallMethod(loader, "load_module", "s", name);
1696 break;
1697 }
1698
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001699 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001700 PyErr_Format(PyExc_ImportError,
1701 "Don't know how to import %.200s (type code %d)",
1702 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001703 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704
1705 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001706
1707 return m;
1708}
1709
1710
1711/* Initialize a built-in module.
1712 Return 1 for succes, 0 if the module is not found, and -1 with
1713 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001714
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001715static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001716init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001717{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001718 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001719
Greg Ward201baee2001-10-04 14:52:06 +00001720 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001721 return 1;
1722
Guido van Rossum771c6c81997-10-31 18:37:24 +00001723 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001724 if (strcmp(name, p->name) == 0) {
1725 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001726 PyErr_Format(PyExc_ImportError,
1727 "Cannot re-init internal module %.200s",
1728 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001729 return -1;
1730 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001732 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001733 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001736 if (_PyImport_FixupExtension(name, name) == NULL)
1737 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001738 return 1;
1739 }
1740 }
1741 return 0;
1742}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001743
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001745/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001746
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001747static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001748find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001749{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001750 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001751
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001753 if (p->name == NULL)
1754 return NULL;
1755 if (strcmp(p->name, name) == 0)
1756 break;
1757 }
1758 return p;
1759}
1760
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001762get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001763{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001764 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001765 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001766
1767 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001768 PyErr_Format(PyExc_ImportError,
1769 "No such frozen object named %.200s",
1770 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001771 return NULL;
1772 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001773 if (p->code == NULL) {
1774 PyErr_Format(PyExc_ImportError,
1775 "Excluded frozen object named %.200s",
1776 name);
1777 return NULL;
1778 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001779 size = p->size;
1780 if (size < 0)
1781 size = -size;
1782 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001783}
1784
1785/* Initialize a frozen module.
1786 Return 1 for succes, 0 if the module is not found, and -1 with
1787 an exception set if the initialization failed.
1788 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001789
1790int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001791PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001792{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001793 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 PyObject *co;
1795 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001796 int ispackage;
1797 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001798
1799 if (p == NULL)
1800 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001801 if (p->code == NULL) {
1802 PyErr_Format(PyExc_ImportError,
1803 "Excluded frozen object named %.200s",
1804 name);
1805 return -1;
1806 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001807 size = p->size;
1808 ispackage = (size < 0);
1809 if (ispackage)
1810 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001812 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001813 name, ispackage ? " package" : "");
1814 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001815 if (co == NULL)
1816 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 if (!PyCode_Check(co)) {
1818 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001819 PyErr_Format(PyExc_TypeError,
1820 "frozen object %.200s is not a code object",
1821 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001822 return -1;
1823 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001824 if (ispackage) {
1825 /* Set __path__ to the package name */
1826 PyObject *d, *s;
1827 int err;
1828 m = PyImport_AddModule(name);
1829 if (m == NULL)
1830 return -1;
1831 d = PyModule_GetDict(m);
1832 s = PyString_InternFromString(name);
1833 if (s == NULL)
1834 return -1;
1835 err = PyDict_SetItemString(d, "__path__", s);
1836 Py_DECREF(s);
1837 if (err != 0)
1838 return err;
1839 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001840 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001842 if (m == NULL)
1843 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001844 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001845 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001846}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001847
1848
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001849/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001850 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001851
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001853PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001854{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001855 PyObject *pname;
1856 PyObject *result;
1857
1858 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001859 if (pname == NULL)
1860 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001861 result = PyImport_Import(pname);
1862 Py_DECREF(pname);
1863 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001864}
1865
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001866/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001867static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1868static PyObject *load_next(PyObject *mod, PyObject *altmod,
1869 char **p_name, char *buf, int *p_buflen);
1870static int mark_miss(char *name);
1871static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1872 char *buf, int buflen, int recursive);
1873static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001874
1875/* The Magnum Opus of dotted-name import :-) */
1876
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001877static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001878import_module_ex(char *name, PyObject *globals, PyObject *locals,
1879 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001880{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001881 char buf[MAXPATHLEN+1];
1882 int buflen = 0;
1883 PyObject *parent, *head, *next, *tail;
1884
1885 parent = get_parent(globals, buf, &buflen);
1886 if (parent == NULL)
1887 return NULL;
1888
1889 head = load_next(parent, Py_None, &name, buf, &buflen);
1890 if (head == NULL)
1891 return NULL;
1892
1893 tail = head;
1894 Py_INCREF(tail);
1895 while (name) {
1896 next = load_next(tail, tail, &name, buf, &buflen);
1897 Py_DECREF(tail);
1898 if (next == NULL) {
1899 Py_DECREF(head);
1900 return NULL;
1901 }
1902 tail = next;
1903 }
1904
1905 if (fromlist != NULL) {
1906 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1907 fromlist = NULL;
1908 }
1909
1910 if (fromlist == NULL) {
1911 Py_DECREF(tail);
1912 return head;
1913 }
1914
1915 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001916 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001917 Py_DECREF(tail);
1918 return NULL;
1919 }
1920
1921 return tail;
1922}
1923
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001924PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001925PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1926 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001927{
1928 PyObject *result;
1929 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001930 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001931 if (unlock_import() < 0) {
1932 Py_XDECREF(result);
1933 PyErr_SetString(PyExc_RuntimeError,
1934 "not holding the import lock");
1935 return NULL;
1936 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001937 return result;
1938}
1939
Fred Drake87590902004-05-28 20:21:36 +00001940/* Return the package that an import is being performed in. If globals comes
1941 from the module foo.bar.bat (not itself a package), this returns the
1942 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1943 the package's entry in sys.modules is returned.
1944
1945 The *name* of the returned package is returned in buf, with the length of
1946 the name in *p_buflen.
1947
1948 If globals doesn't come from a package or a module in a package, or a
1949 corresponding entry is not found in sys.modules, Py_None is returned.
1950*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001951static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001952get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001953{
1954 static PyObject *namestr = NULL;
1955 static PyObject *pathstr = NULL;
1956 PyObject *modname, *modpath, *modules, *parent;
1957
1958 if (globals == NULL || !PyDict_Check(globals))
1959 return Py_None;
1960
1961 if (namestr == NULL) {
1962 namestr = PyString_InternFromString("__name__");
1963 if (namestr == NULL)
1964 return NULL;
1965 }
1966 if (pathstr == NULL) {
1967 pathstr = PyString_InternFromString("__path__");
1968 if (pathstr == NULL)
1969 return NULL;
1970 }
1971
1972 *buf = '\0';
1973 *p_buflen = 0;
1974 modname = PyDict_GetItem(globals, namestr);
1975 if (modname == NULL || !PyString_Check(modname))
1976 return Py_None;
1977
1978 modpath = PyDict_GetItem(globals, pathstr);
1979 if (modpath != NULL) {
1980 int len = PyString_GET_SIZE(modname);
1981 if (len > MAXPATHLEN) {
1982 PyErr_SetString(PyExc_ValueError,
1983 "Module name too long");
1984 return NULL;
1985 }
1986 strcpy(buf, PyString_AS_STRING(modname));
1987 *p_buflen = len;
1988 }
1989 else {
1990 char *start = PyString_AS_STRING(modname);
1991 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001992 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001993 if (lastdot == NULL)
1994 return Py_None;
1995 len = lastdot - start;
1996 if (len >= MAXPATHLEN) {
1997 PyErr_SetString(PyExc_ValueError,
1998 "Module name too long");
1999 return NULL;
2000 }
2001 strncpy(buf, start, len);
2002 buf[len] = '\0';
2003 *p_buflen = len;
2004 }
2005
2006 modules = PyImport_GetModuleDict();
2007 parent = PyDict_GetItemString(modules, buf);
2008 if (parent == NULL)
2009 parent = Py_None;
2010 return parent;
2011 /* We expect, but can't guarantee, if parent != None, that:
2012 - parent.__name__ == buf
2013 - parent.__dict__ is globals
2014 If this is violated... Who cares? */
2015}
2016
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002017/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002018static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002019load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2020 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002021{
2022 char *name = *p_name;
2023 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002024 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002025 char *p;
2026 PyObject *result;
2027
2028 if (dot == NULL) {
2029 *p_name = NULL;
2030 len = strlen(name);
2031 }
2032 else {
2033 *p_name = dot+1;
2034 len = dot-name;
2035 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002036 if (len == 0) {
2037 PyErr_SetString(PyExc_ValueError,
2038 "Empty module name");
2039 return NULL;
2040 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002041
2042 p = buf + *p_buflen;
2043 if (p != buf)
2044 *p++ = '.';
2045 if (p+len-buf >= MAXPATHLEN) {
2046 PyErr_SetString(PyExc_ValueError,
2047 "Module name too long");
2048 return NULL;
2049 }
2050 strncpy(p, name, len);
2051 p[len] = '\0';
2052 *p_buflen = p+len-buf;
2053
2054 result = import_submodule(mod, p, buf);
2055 if (result == Py_None && altmod != mod) {
2056 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002057 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002058 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002059 if (result != NULL && result != Py_None) {
2060 if (mark_miss(buf) != 0) {
2061 Py_DECREF(result);
2062 return NULL;
2063 }
2064 strncpy(buf, name, len);
2065 buf[len] = '\0';
2066 *p_buflen = len;
2067 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002068 }
2069 if (result == NULL)
2070 return NULL;
2071
2072 if (result == Py_None) {
2073 Py_DECREF(result);
2074 PyErr_Format(PyExc_ImportError,
2075 "No module named %.200s", name);
2076 return NULL;
2077 }
2078
2079 return result;
2080}
2081
2082static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002083mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002084{
2085 PyObject *modules = PyImport_GetModuleDict();
2086 return PyDict_SetItemString(modules, name, Py_None);
2087}
2088
2089static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002090ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2091 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002092{
2093 int i;
2094
2095 if (!PyObject_HasAttrString(mod, "__path__"))
2096 return 1;
2097
2098 for (i = 0; ; i++) {
2099 PyObject *item = PySequence_GetItem(fromlist, i);
2100 int hasit;
2101 if (item == NULL) {
2102 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2103 PyErr_Clear();
2104 return 1;
2105 }
2106 return 0;
2107 }
2108 if (!PyString_Check(item)) {
2109 PyErr_SetString(PyExc_TypeError,
2110 "Item in ``from list'' not a string");
2111 Py_DECREF(item);
2112 return 0;
2113 }
2114 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002115 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002116 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002117 /* See if the package defines __all__ */
2118 if (recursive)
2119 continue; /* Avoid endless recursion */
2120 all = PyObject_GetAttrString(mod, "__all__");
2121 if (all == NULL)
2122 PyErr_Clear();
2123 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002124 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002125 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002126 if (!ret)
2127 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002128 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002129 continue;
2130 }
2131 hasit = PyObject_HasAttr(mod, item);
2132 if (!hasit) {
2133 char *subname = PyString_AS_STRING(item);
2134 PyObject *submod;
2135 char *p;
2136 if (buflen + strlen(subname) >= MAXPATHLEN) {
2137 PyErr_SetString(PyExc_ValueError,
2138 "Module name too long");
2139 Py_DECREF(item);
2140 return 0;
2141 }
2142 p = buf + buflen;
2143 *p++ = '.';
2144 strcpy(p, subname);
2145 submod = import_submodule(mod, subname, buf);
2146 Py_XDECREF(submod);
2147 if (submod == NULL) {
2148 Py_DECREF(item);
2149 return 0;
2150 }
2151 }
2152 Py_DECREF(item);
2153 }
2154
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002155 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002156}
2157
Neil Schemenauer00b09662003-06-16 21:03:07 +00002158static int
2159add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2160 PyObject *modules)
2161{
2162 if (mod == Py_None)
2163 return 1;
2164 /* Irrespective of the success of this load, make a
2165 reference to it in the parent package module. A copy gets
2166 saved in the modules dictionary under the full name, so get a
2167 reference from there, if need be. (The exception is when the
2168 load failed with a SyntaxError -- then there's no trace in
2169 sys.modules. In that case, of course, do nothing extra.) */
2170 if (submod == NULL) {
2171 submod = PyDict_GetItemString(modules, fullname);
2172 if (submod == NULL)
2173 return 1;
2174 }
2175 if (PyModule_Check(mod)) {
2176 /* We can't use setattr here since it can give a
2177 * spurious warning if the submodule name shadows a
2178 * builtin name */
2179 PyObject *dict = PyModule_GetDict(mod);
2180 if (!dict)
2181 return 0;
2182 if (PyDict_SetItemString(dict, subname, submod) < 0)
2183 return 0;
2184 }
2185 else {
2186 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2187 return 0;
2188 }
2189 return 1;
2190}
2191
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002192static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002193import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002194{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002195 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002196 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002197
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002198 /* Require:
2199 if mod == None: subname == fullname
2200 else: mod.__name__ + "." + subname == fullname
2201 */
2202
Tim Peters50d8d372001-02-28 05:34:27 +00002203 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002204 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002205 }
2206 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002207 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002208 char buf[MAXPATHLEN+1];
2209 struct filedescr *fdp;
2210 FILE *fp = NULL;
2211
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002212 if (mod == Py_None)
2213 path = NULL;
2214 else {
2215 path = PyObject_GetAttrString(mod, "__path__");
2216 if (path == NULL) {
2217 PyErr_Clear();
2218 Py_INCREF(Py_None);
2219 return Py_None;
2220 }
2221 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002222
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002223 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002224 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2225 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002226 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002227 if (fdp == NULL) {
2228 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2229 return NULL;
2230 PyErr_Clear();
2231 Py_INCREF(Py_None);
2232 return Py_None;
2233 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002234 m = load_module(fullname, fp, buf, fdp->type, loader);
2235 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002236 if (fp)
2237 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002238 if (!add_submodule(mod, m, fullname, subname, modules)) {
2239 Py_XDECREF(m);
2240 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002241 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002242 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002243
2244 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002245}
2246
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002247
2248/* Re-import a module of any kind and return its module object, WITH
2249 INCREMENTED REFERENCE COUNT */
2250
Guido van Rossum79f25d91997-04-29 20:08:16 +00002251PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002252PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002253{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002254 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002255 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002256 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002257 char buf[MAXPATHLEN+1];
2258 struct filedescr *fdp;
2259 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002260 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002261
Guido van Rossum79f25d91997-04-29 20:08:16 +00002262 if (m == NULL || !PyModule_Check(m)) {
2263 PyErr_SetString(PyExc_TypeError,
2264 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002265 return NULL;
2266 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002268 if (name == NULL)
2269 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002270 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002271 PyErr_Format(PyExc_ImportError,
2272 "reload(): module %.200s not in sys.modules",
2273 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002274 return NULL;
2275 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002276 subname = strrchr(name, '.');
2277 if (subname == NULL)
2278 subname = name;
2279 else {
2280 PyObject *parentname, *parent;
2281 parentname = PyString_FromStringAndSize(name, (subname-name));
2282 if (parentname == NULL)
2283 return NULL;
2284 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002285 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002286 if (parent == NULL) {
2287 PyErr_Format(PyExc_ImportError,
2288 "reload(): parent %.200s not in sys.modules",
2289 name);
2290 return NULL;
2291 }
2292 subname++;
2293 path = PyObject_GetAttrString(parent, "__path__");
2294 if (path == NULL)
2295 PyErr_Clear();
2296 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002297 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002298 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002299 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002300
2301 if (fdp == NULL) {
2302 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002303 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002304 }
2305
2306 newm = load_module(name, fp, buf, fdp->type, loader);
2307 Py_XDECREF(loader);
2308
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002309 if (fp)
2310 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002311 if (newm == NULL) {
2312 /* load_module probably removed name from modules because of
2313 * the error. Put back the original module object. We're
2314 * going to return NULL in this case regardless of whether
2315 * replacing name succeeds, so the return value is ignored.
2316 */
2317 PyDict_SetItemString(modules, name, m);
2318 }
2319 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002320}
2321
2322
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002323/* Higher-level import emulator which emulates the "import" statement
2324 more accurately -- it invokes the __import__() function from the
2325 builtins of the current globals. This means that the import is
2326 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002327 environment, e.g. by "rexec".
2328 A dummy list ["__doc__"] is passed as the 4th argument so that
2329 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2330 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002331
2332PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002333PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002334{
2335 static PyObject *silly_list = NULL;
2336 static PyObject *builtins_str = NULL;
2337 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002338 PyObject *globals = NULL;
2339 PyObject *import = NULL;
2340 PyObject *builtins = NULL;
2341 PyObject *r = NULL;
2342
2343 /* Initialize constant string objects */
2344 if (silly_list == NULL) {
2345 import_str = PyString_InternFromString("__import__");
2346 if (import_str == NULL)
2347 return NULL;
2348 builtins_str = PyString_InternFromString("__builtins__");
2349 if (builtins_str == NULL)
2350 return NULL;
2351 silly_list = Py_BuildValue("[s]", "__doc__");
2352 if (silly_list == NULL)
2353 return NULL;
2354 }
2355
2356 /* Get the builtins from current globals */
2357 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002358 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002359 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002360 builtins = PyObject_GetItem(globals, builtins_str);
2361 if (builtins == NULL)
2362 goto err;
2363 }
2364 else {
2365 /* No globals -- use standard builtins, and fake globals */
2366 PyErr_Clear();
2367
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002368 builtins = PyImport_ImportModuleEx("__builtin__",
2369 NULL, NULL, NULL);
2370 if (builtins == NULL)
2371 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002372 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2373 if (globals == NULL)
2374 goto err;
2375 }
2376
2377 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002378 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002379 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002380 if (import == NULL)
2381 PyErr_SetObject(PyExc_KeyError, import_str);
2382 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002383 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002384 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002385 if (import == NULL)
2386 goto err;
2387
2388 /* Call the _import__ function with the proper argument list */
2389 r = PyObject_CallFunction(import, "OOOO",
2390 module_name, globals, globals, silly_list);
2391
2392 err:
2393 Py_XDECREF(globals);
2394 Py_XDECREF(builtins);
2395 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002396
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002397 return r;
2398}
2399
2400
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002401/* Module 'imp' provides Python access to the primitives used for
2402 importing modules.
2403*/
2404
Guido van Rossum79f25d91997-04-29 20:08:16 +00002405static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002406imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407{
2408 char buf[4];
2409
Guido van Rossum96774c12000-05-01 20:19:08 +00002410 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2411 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2412 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2413 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002414
Guido van Rossum79f25d91997-04-29 20:08:16 +00002415 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002416}
2417
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002419imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002420{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002421 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002422 struct filedescr *fdp;
2423
Guido van Rossum79f25d91997-04-29 20:08:16 +00002424 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002425 if (list == NULL)
2426 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002427 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2428 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002429 fdp->suffix, fdp->mode, fdp->type);
2430 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002431 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002432 return NULL;
2433 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434 if (PyList_Append(list, item) < 0) {
2435 Py_DECREF(list);
2436 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002437 return NULL;
2438 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002440 }
2441 return list;
2442}
2443
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002445call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002446{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002447 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002448 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002449 struct filedescr *fdp;
2450 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002451 FILE *fp = NULL;
2452
2453 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002454 if (path == Py_None)
2455 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002456 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002457 if (fdp == NULL)
2458 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002459 if (fp != NULL) {
2460 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2461 if (fob == NULL) {
2462 fclose(fp);
2463 return NULL;
2464 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002466 else {
2467 fob = Py_None;
2468 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002469 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002470 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002471 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002472 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002473 return ret;
2474}
2475
Guido van Rossum79f25d91997-04-29 20:08:16 +00002476static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002477imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002478{
2479 char *name;
2480 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002481 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002482 return NULL;
2483 return call_find_module(name, path);
2484}
2485
2486static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002487imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002488{
2489 char *name;
2490 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002491 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002492 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002493 return NULL;
2494 ret = init_builtin(name);
2495 if (ret < 0)
2496 return NULL;
2497 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002498 Py_INCREF(Py_None);
2499 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002500 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002501 m = PyImport_AddModule(name);
2502 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002503 return m;
2504}
2505
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002507imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002508{
2509 char *name;
2510 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002511 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002512 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002513 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002514 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002515 if (ret < 0)
2516 return NULL;
2517 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002518 Py_INCREF(Py_None);
2519 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002520 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002521 m = PyImport_AddModule(name);
2522 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002523 return m;
2524}
2525
Guido van Rossum79f25d91997-04-29 20:08:16 +00002526static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002527imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002528{
2529 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002530
Guido van Rossum43713e52000-02-29 13:59:29 +00002531 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002532 return NULL;
2533 return get_frozen_object(name);
2534}
2535
Guido van Rossum79f25d91997-04-29 20:08:16 +00002536static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002537imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002538{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002540 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002541 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002542 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002543}
2544
Guido van Rossum79f25d91997-04-29 20:08:16 +00002545static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002546imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002547{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002548 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002549 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002550 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002551 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002552 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002553 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002554}
2555
2556static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002557get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558{
2559 FILE *fp;
2560 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002561 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002562 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563 fp = fopen(pathname, mode);
2564 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002565 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002566 }
2567 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002568 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002569 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570 PyErr_SetString(PyExc_ValueError,
2571 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572 }
2573 return fp;
2574}
2575
Guido van Rossum79f25d91997-04-29 20:08:16 +00002576static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002577imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002578{
2579 char *name;
2580 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002581 PyObject *fob = NULL;
2582 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002583 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002584 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002585 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002586 return NULL;
2587 fp = get_file(pathname, fob, "rb");
2588 if (fp == NULL)
2589 return NULL;
2590 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002591 if (fob == NULL)
2592 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002593 return m;
2594}
2595
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002596#ifdef HAVE_DYNAMIC_LOADING
2597
Guido van Rossum79f25d91997-04-29 20:08:16 +00002598static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002599imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002600{
2601 char *name;
2602 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002603 PyObject *fob = NULL;
2604 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002605 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002606 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002607 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002608 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002609 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002610 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002611 if (fp == NULL)
2612 return NULL;
2613 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002615 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002616}
2617
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002618#endif /* HAVE_DYNAMIC_LOADING */
2619
Guido van Rossum79f25d91997-04-29 20:08:16 +00002620static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002621imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002622{
2623 char *name;
2624 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002625 PyObject *fob = NULL;
2626 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002627 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002628 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002629 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002630 return NULL;
2631 fp = get_file(pathname, fob, "r");
2632 if (fp == NULL)
2633 return NULL;
2634 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002635 if (fob == NULL)
2636 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002637 return m;
2638}
2639
Guido van Rossum79f25d91997-04-29 20:08:16 +00002640static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002641imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002642{
2643 char *name;
2644 PyObject *fob;
2645 char *pathname;
2646 char *suffix; /* Unused */
2647 char *mode;
2648 int type;
2649 FILE *fp;
2650
Guido van Rossum43713e52000-02-29 13:59:29 +00002651 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002652 &name, &fob, &pathname,
2653 &suffix, &mode, &type))
2654 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002655 if (*mode) {
2656 /* Mode must start with 'r' or 'U' and must not contain '+'.
2657 Implicit in this test is the assumption that the mode
2658 may contain other modifiers like 'b' or 't'. */
2659
2660 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002661 PyErr_Format(PyExc_ValueError,
2662 "invalid file open mode %.200s", mode);
2663 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002664 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002665 }
2666 if (fob == Py_None)
2667 fp = NULL;
2668 else {
2669 if (!PyFile_Check(fob)) {
2670 PyErr_SetString(PyExc_ValueError,
2671 "load_module arg#2 should be a file or None");
2672 return NULL;
2673 }
2674 fp = get_file(pathname, fob, mode);
2675 if (fp == NULL)
2676 return NULL;
2677 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002678 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002679}
2680
2681static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002682imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002683{
2684 char *name;
2685 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002686 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002687 return NULL;
2688 return load_package(name, pathname);
2689}
2690
2691static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002692imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002693{
2694 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002695 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002696 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002697 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002698}
2699
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002700/* Doc strings */
2701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(doc_imp,
2703"This module provides the components needed to build your own\n\
2704__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002705
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002706PyDoc_STRVAR(doc_find_module,
2707"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002708Search for a module. If path is omitted or None, search for a\n\
2709built-in, frozen or special module and continue search in sys.path.\n\
2710The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002711package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713PyDoc_STRVAR(doc_load_module,
2714"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002715Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002716The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(doc_get_magic,
2719"get_magic() -> string\n\
2720Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722PyDoc_STRVAR(doc_get_suffixes,
2723"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002724Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727PyDoc_STRVAR(doc_new_module,
2728"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002729Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002730The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002733"lock_held() -> boolean\n\
2734Return True if the import lock is currently held, else False.\n\
2735On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002736
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002737PyDoc_STRVAR(doc_acquire_lock,
2738"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002739Acquires the interpreter's import lock for the current thread.\n\
2740This lock should be used by import hooks to ensure thread-safety\n\
2741when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002742On platforms without threads, this function does nothing.");
2743
2744PyDoc_STRVAR(doc_release_lock,
2745"release_lock() -> None\n\
2746Release the interpreter's import lock.\n\
2747On platforms without threads, this function does nothing.");
2748
Guido van Rossum79f25d91997-04-29 20:08:16 +00002749static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002750 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2751 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2752 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2753 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2754 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2755 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2756 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2757 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002758 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002759 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2760 {"init_builtin", imp_init_builtin, METH_VARARGS},
2761 {"init_frozen", imp_init_frozen, METH_VARARGS},
2762 {"is_builtin", imp_is_builtin, METH_VARARGS},
2763 {"is_frozen", imp_is_frozen, METH_VARARGS},
2764 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002765#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002766 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002767#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002768 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002769 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002770 {NULL, NULL} /* sentinel */
2771};
2772
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002773static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002774setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002775{
2776 PyObject *v;
2777 int err;
2778
2779 v = PyInt_FromLong((long)value);
2780 err = PyDict_SetItemString(d, name, v);
2781 Py_XDECREF(v);
2782 return err;
2783}
2784
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002785PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002786initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002787{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002788 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002789
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002790 m = Py_InitModule4("imp", imp_methods, doc_imp,
2791 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002792 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002793
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002794 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2795 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2796 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2797 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2798 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2799 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2800 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2801 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002802 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002803 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002804
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002805 failure:
2806 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002807}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002808
2809
Guido van Rossumb18618d2000-05-03 23:44:39 +00002810/* API for embedding applications that want to add their own entries
2811 to the table of built-in modules. This should normally be called
2812 *before* Py_Initialize(). When the table resize fails, -1 is
2813 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002814
2815 After a similar function by Just van Rossum. */
2816
2817int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002818PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002819{
2820 static struct _inittab *our_copy = NULL;
2821 struct _inittab *p;
2822 int i, n;
2823
2824 /* Count the number of entries in both tables */
2825 for (n = 0; newtab[n].name != NULL; n++)
2826 ;
2827 if (n == 0)
2828 return 0; /* Nothing to do */
2829 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2830 ;
2831
2832 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002833 p = our_copy;
2834 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002835 if (p == NULL)
2836 return -1;
2837
2838 /* Copy the tables into the new memory */
2839 if (our_copy != PyImport_Inittab)
2840 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2841 PyImport_Inittab = our_copy = p;
2842 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2843
2844 return 0;
2845}
2846
2847/* Shorthand to add a single entry given a name and a function */
2848
2849int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002850PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002851{
2852 struct _inittab newtab[2];
2853
2854 memset(newtab, '\0', sizeof newtab);
2855
2856 newtab[0].name = name;
2857 newtab[0].initfunc = initfunc;
2858
2859 return PyImport_ExtendInittab(newtab);
2860}