blob: 0362dbd47a2a29f0cb8c51a62c9d22763b91e3e4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
10#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000011#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000014
Guido van Rossum55a83382000-09-20 20:31:38 +000015#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
18
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000019extern time_t PyOS_GetLastModificationTime(char *, FILE *);
20 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000021
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000022/* Magic word to reject .pyc files generated by other Python versions.
23 It should change for each incompatible change to the bytecode.
24
25 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000026 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000027 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000028
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000029 The magic numbers must be spaced apart atleast 2 values, as the
30 -U interpeter flag will cause MAGIC+1 being used. They have been
31 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000032
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000033 There were a variety of old schemes for setting the magic number.
34 The current working scheme is to increment the previous value by
35 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000036
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037 Known values:
38 Python 1.5: 20121
39 Python 1.5.1: 20121
40 Python 1.5.2: 20121
41 Python 2.0: 50823
42 Python 2.0.1: 50823
43 Python 2.1: 60202
44 Python 2.1.1: 60202
45 Python 2.1.2: 60202
46 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000047 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000048 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000049 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000050 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000051 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000052 Python 2.4b1: 62061
Tim Peters36515e22001-11-18 04:06:29 +000053*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +000054#define MAGIC (62061 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000055
Guido van Rossum96774c12000-05-01 20:19:08 +000056/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000057 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000058 compiler works which are enabled by command line switches. */
59static long pyc_magic = MAGIC;
60
Guido van Rossum25ce5661997-08-02 03:10:38 +000061/* See _PyImport_FixupExtension() below */
62static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000063
Guido van Rossum771c6c81997-10-31 18:37:24 +000064/* This table is defined in config.c: */
65extern struct _inittab _PyImport_Inittab[];
66
67struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000068
Guido van Rossumed1170e1999-12-20 21:23:41 +000069/* these tables define the module suffixes that Python recognizes */
70struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000071
72#ifdef RISCOS
73static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000074 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000075 {"/pyc", "rb", PY_COMPILED},
76 {0, 0}
77};
78#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000079static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000080 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000081#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000082 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000083#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000084 {".pyc", "rb", PY_COMPILED},
85 {0, 0}
86};
Guido van Rossum48a680c2001-03-02 06:34:14 +000087#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000088
Guido van Rossum1ae940a1995-01-02 19:04:15 +000089/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090
91void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Guido van Rossumed1170e1999-12-20 21:23:41 +000094 const struct filedescr *scan;
95 struct filedescr *filetab;
96 int countD = 0;
97 int countS = 0;
98
99 /* prepare _PyImport_Filetab: copy entries from
100 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
101 */
102 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
103 ++countD;
104 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
105 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000107 memcpy(filetab, _PyImport_DynLoadFiletab,
108 countD * sizeof(struct filedescr));
109 memcpy(filetab + countD, _PyImport_StandardFiletab,
110 countS * sizeof(struct filedescr));
111 filetab[countD + countS].suffix = NULL;
112
113 _PyImport_Filetab = filetab;
114
Guido van Rossum0824f631997-03-11 18:37:35 +0000115 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000116 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
117 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000118#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000119 if (strcmp(filetab->suffix, ".pyc") == 0)
120 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000121#else
122 if (strcmp(filetab->suffix, "/pyc") == 0)
123 filetab->suffix = "/pyo";
124#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000125 }
126 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000127
128 if (Py_UnicodeFlag) {
129 /* Fix the pyc_magic so that byte compiled code created
130 using the all-Unicode method doesn't interfere with
131 code created in normal operation mode. */
132 pyc_magic = MAGIC + 1;
133 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136void
Just van Rossum52e14d62002-12-30 22:08:05 +0000137_PyImportHooks_Init(void)
138{
139 PyObject *v, *path_hooks = NULL, *zimpimport;
140 int err = 0;
141
142 /* adding sys.path_hooks and sys.path_importer_cache, setting up
143 zipimport */
144
145 if (Py_VerboseFlag)
146 PySys_WriteStderr("# installing zipimport hook\n");
147
148 v = PyList_New(0);
149 if (v == NULL)
150 goto error;
151 err = PySys_SetObject("meta_path", v);
152 Py_DECREF(v);
153 if (err)
154 goto error;
155 v = PyDict_New();
156 if (v == NULL)
157 goto error;
158 err = PySys_SetObject("path_importer_cache", v);
159 Py_DECREF(v);
160 if (err)
161 goto error;
162 path_hooks = PyList_New(0);
163 if (path_hooks == NULL)
164 goto error;
165 err = PySys_SetObject("path_hooks", path_hooks);
166 if (err) {
167 error:
168 PyErr_Print();
169 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
170 "path_importer_cache failed");
171 }
172 zimpimport = PyImport_ImportModule("zipimport");
173 if (zimpimport == NULL) {
174 PyErr_Clear(); /* No zip import module -- okay */
175 if (Py_VerboseFlag)
176 PySys_WriteStderr("# can't import zipimport\n");
177 }
178 else {
179 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
180 "zipimporter");
181 Py_DECREF(zimpimport);
182 if (zipimporter == NULL) {
183 PyErr_Clear(); /* No zipimporter object -- okay */
184 if (Py_VerboseFlag)
185 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000186 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000187 }
188 else {
189 /* sys.path_hooks.append(zipimporter) */
190 err = PyList_Append(path_hooks, zipimporter);
191 Py_DECREF(zipimporter);
192 if (err)
193 goto error;
194 if (Py_VerboseFlag)
195 PySys_WriteStderr(
196 "# installed zipimport hook\n");
197 }
198 }
199 Py_DECREF(path_hooks);
200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
205 Py_XDECREF(extensions);
206 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000207 PyMem_DEL(_PyImport_Filetab);
208 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209}
210
211
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000212/* Locking primitives to prevent parallel imports of the same module
213 in different threads to return with a partially loaded module.
214 These calls are serialized by the global interpreter lock. */
215
216#ifdef WITH_THREAD
217
Guido van Rossum49b56061998-10-01 20:42:43 +0000218#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000219
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000221static long import_lock_thread = -1;
222static int import_lock_level = 0;
223
224static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000225lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000226{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000227 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000228 if (me == -1)
229 return; /* Too bad */
230 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000232 if (import_lock_thread == me) {
233 import_lock_level++;
234 return;
235 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000236 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
237 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000238 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000240 PyEval_RestoreThread(tstate);
241 }
242 import_lock_thread = me;
243 import_lock_level = 1;
244}
245
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000246static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000248{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000251 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000253 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254 import_lock_level--;
255 if (import_lock_level == 0) {
256 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000259 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000260}
261
262#else
263
264#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000265#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266
267#endif
268
Tim Peters69232342001-08-30 05:16:13 +0000269static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000270imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000271{
Tim Peters69232342001-08-30 05:16:13 +0000272#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000273 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000274#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000275 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000276#endif
277}
278
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000280imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000281{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000282#ifdef WITH_THREAD
283 lock_import();
284#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000285 Py_INCREF(Py_None);
286 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287}
288
289static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000290imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000291{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292#ifdef WITH_THREAD
293 if (unlock_import() < 0) {
294 PyErr_SetString(PyExc_RuntimeError,
295 "not holding the import lock");
296 return NULL;
297 }
298#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000299 Py_INCREF(Py_None);
300 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000301}
302
Guido van Rossum25ce5661997-08-02 03:10:38 +0000303/* Helper for sys */
304
305PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000308 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309 if (interp->modules == NULL)
310 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
311 return interp->modules;
312}
313
Guido van Rossum3f5da241990-12-20 15:06:42 +0000314
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000315/* List of names to clear in sys */
316static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000317 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000318 "exc_type", "exc_value", "exc_traceback",
319 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000320 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000321 NULL
322};
323
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000324static char* sys_files[] = {
325 "stdin", "__stdin__",
326 "stdout", "__stdout__",
327 "stderr", "__stderr__",
328 NULL
329};
330
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000332/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000333
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336{
Guido van Rossum758eec01998-01-19 21:58:26 +0000337 int pos, ndone;
338 char *name;
339 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000340 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000341 PyObject *modules = interp->modules;
342
343 if (modules == NULL)
344 return; /* Already done */
345
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000346 /* Delete some special variables first. These are common
347 places where user values hide and people complain when their
348 destructors fail. Since the modules containing them are
349 deleted *last* of all, they would come too late in the normal
350 destruction order. Sigh. */
351
352 value = PyDict_GetItemString(modules, "__builtin__");
353 if (value != NULL && PyModule_Check(value)) {
354 dict = PyModule_GetDict(value);
355 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000356 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000357 PyDict_SetItemString(dict, "_", Py_None);
358 }
359 value = PyDict_GetItemString(modules, "sys");
360 if (value != NULL && PyModule_Check(value)) {
361 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000362 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000363 dict = PyModule_GetDict(value);
364 for (p = sys_deletes; *p != NULL; p++) {
365 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000366 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000367 PyDict_SetItemString(dict, *p, Py_None);
368 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000369 for (p = sys_files; *p != NULL; p+=2) {
370 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000371 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000372 v = PyDict_GetItemString(dict, *(p+1));
373 if (v == NULL)
374 v = Py_None;
375 PyDict_SetItemString(dict, *p, v);
376 }
377 }
378
379 /* First, delete __main__ */
380 value = PyDict_GetItemString(modules, "__main__");
381 if (value != NULL && PyModule_Check(value)) {
382 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000383 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000384 _PyModule_Clear(value);
385 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000386 }
387
Guido van Rossum758eec01998-01-19 21:58:26 +0000388 /* The special treatment of __builtin__ here is because even
389 when it's not referenced as a module, its dictionary is
390 referenced by almost every module's __builtins__. Since
391 deleting a module clears its dictionary (even if there are
392 references left to it), we need to delete the __builtin__
393 module last. Likewise, we don't delete sys until the very
394 end because it is implicitly referenced (e.g. by print).
395
396 Also note that we 'delete' modules by replacing their entry
397 in the modules dict with None, rather than really deleting
398 them; this avoids a rehash of the modules dictionary and
399 also marks them as "non existent" so they won't be
400 re-imported. */
401
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000402 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000403 one (skipping __builtin__ and sys) and delete them */
404 do {
405 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000406 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000407 while (PyDict_Next(modules, &pos, &key, &value)) {
408 if (value->ob_refcnt != 1)
409 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000410 if (PyString_Check(key) && PyModule_Check(value)) {
411 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000412 if (strcmp(name, "__builtin__") == 0)
413 continue;
414 if (strcmp(name, "sys") == 0)
415 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000416 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000417 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000418 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000419 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000420 PyDict_SetItem(modules, key, Py_None);
421 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422 }
423 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000424 } while (ndone > 0);
425
Guido van Rossum758eec01998-01-19 21:58:26 +0000426 /* Next, delete all modules (still skipping __builtin__ and sys) */
427 pos = 0;
428 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000429 if (PyString_Check(key) && PyModule_Check(value)) {
430 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000431 if (strcmp(name, "__builtin__") == 0)
432 continue;
433 if (strcmp(name, "sys") == 0)
434 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000435 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000436 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000437 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000438 PyDict_SetItem(modules, key, Py_None);
439 }
440 }
441
442 /* Next, delete sys and __builtin__ (in that order) */
443 value = PyDict_GetItemString(modules, "sys");
444 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000445 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000446 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000447 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000448 PyDict_SetItemString(modules, "sys", Py_None);
449 }
450 value = PyDict_GetItemString(modules, "__builtin__");
451 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000452 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000453 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000454 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000455 PyDict_SetItemString(modules, "__builtin__", Py_None);
456 }
457
458 /* Finally, clear and delete the modules directory */
459 PyDict_Clear(modules);
460 interp->modules = NULL;
461 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000462}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000463
464
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465/* Helper for pythonrun.c -- return magic number */
466
467long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000468PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469{
Guido van Rossum96774c12000-05-01 20:19:08 +0000470 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000471}
472
473
Guido van Rossum25ce5661997-08-02 03:10:38 +0000474/* Magic for extension modules (built-in as well as dynamically
475 loaded). To prevent initializing an extension module more than
476 once, we keep a static dictionary 'extensions' keyed by module name
477 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000478 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000479 dictionary is stored by calling _PyImport_FixupExtension()
480 immediately after the module initialization function succeeds. A
481 copy can be retrieved from there by calling
482 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000485_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000487 PyObject *modules, *mod, *dict, *copy;
488 if (extensions == NULL) {
489 extensions = PyDict_New();
490 if (extensions == NULL)
491 return NULL;
492 }
493 modules = PyImport_GetModuleDict();
494 mod = PyDict_GetItemString(modules, name);
495 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000496 PyErr_Format(PyExc_SystemError,
497 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000498 return NULL;
499 }
500 dict = PyModule_GetDict(mod);
501 if (dict == NULL)
502 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000503 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000504 if (copy == NULL)
505 return NULL;
506 PyDict_SetItemString(extensions, filename, copy);
507 Py_DECREF(copy);
508 return copy;
509}
510
511PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000512_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000513{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000514 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000515 if (extensions == NULL)
516 return NULL;
517 dict = PyDict_GetItemString(extensions, filename);
518 if (dict == NULL)
519 return NULL;
520 mod = PyImport_AddModule(name);
521 if (mod == NULL)
522 return NULL;
523 mdict = PyModule_GetDict(mod);
524 if (mdict == NULL)
525 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000526 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000527 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000529 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000530 name, filename);
531 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532}
533
534
535/* Get the module object corresponding to a module name.
536 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000537 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000538 Because the former action is most common, THIS DOES NOT RETURN A
539 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000542PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 if (m == NULL)
552 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555 return NULL;
556 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558
559 return m;
560}
561
Tim Peters1cd70172004-08-02 03:52:12 +0000562/* Remove name from sys.modules, if it's there. */
563static void
564_RemoveModule(const char *name)
565{
566 PyObject *modules = PyImport_GetModuleDict();
567 if (PyDict_GetItemString(modules, name) == NULL)
568 return;
569 if (PyDict_DelItemString(modules, name) < 0)
570 Py_FatalError("import: deleting existing key in"
571 "sys.modules failed");
572}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000574/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000575 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
576 * removed from sys.modules, to avoid leaving damaged module objects
577 * in sys.modules. The caller may wish to restore the original
578 * module object (if any) in this case; PyImport_ReloadModule is an
579 * example.
580 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000582PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000584 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
585}
586
587PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000588PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000589{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594 if (m == NULL)
595 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000596 /* If the module is being reloaded, we get the old module back
597 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598 d = PyModule_GetDict(m);
599 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
600 if (PyDict_SetItemString(d, "__builtins__",
601 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000602 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000603 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000604 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000605 v = NULL;
606 if (pathname != NULL) {
607 v = PyString_FromString(pathname);
608 if (v == NULL)
609 PyErr_Clear();
610 }
611 if (v == NULL) {
612 v = ((PyCodeObject *)co)->co_filename;
613 Py_INCREF(v);
614 }
615 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000617 Py_DECREF(v);
618
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000619 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000621 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000623
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000625 PyErr_Format(PyExc_ImportError,
626 "Loaded module %.200s not found in sys.modules",
627 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000628 return NULL;
629 }
630
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632
633 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000634
635 error:
636 _RemoveModule(name);
637 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638}
639
640
641/* Given a pathname for a Python source file, fill a buffer with the
642 pathname for the corresponding compiled file. Return the pathname
643 for the compiled file, or NULL if there's no space in the buffer.
644 Doesn't set an exception. */
645
646static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000647make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648{
Tim Petersc1731372001-08-04 08:12:36 +0000649 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650 if (len+2 > buflen)
651 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000652
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000653#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000654 /* Treat .pyw as if it were .py. The case of ".pyw" must match
655 that used in _PyImport_StandardFiletab. */
656 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
657 --len; /* pretend 'w' isn't there */
658#endif
659 memcpy(buf, pathname, len);
660 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
661 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662
663 return buf;
664}
665
666
667/* Given a pathname for a Python source file, its time of last
668 modification, and a pathname for a compiled file, check whether the
669 compiled file represents the same version of the source. If so,
670 return a FILE pointer for the compiled file, positioned just after
671 the header; if not, return NULL.
672 Doesn't set an exception. */
673
674static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000675check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676{
677 FILE *fp;
678 long magic;
679 long pyc_mtime;
680
681 fp = fopen(cpathname, "rb");
682 if (fp == NULL)
683 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000685 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000687 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688 fclose(fp);
689 return NULL;
690 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000694 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 fclose(fp);
696 return NULL;
697 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000699 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700 return fp;
701}
702
703
704/* Read a code object from a file and check it for validity */
705
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000707read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000710
Tim Petersd9b9ac82001-01-28 00:27:39 +0000711 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000712 if (co == NULL)
713 return NULL;
714 if (!PyCode_Check(co)) {
715 PyErr_Format(PyExc_ImportError,
716 "Non-code object in %.200s", cpathname);
717 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718 return NULL;
719 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721}
722
723
724/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000725 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000728load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729{
730 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 PyCodeObject *co;
732 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000733
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000735 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000736 PyErr_Format(PyExc_ImportError,
737 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 return NULL;
739 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000741 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 if (co == NULL)
743 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000745 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000747 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749
750 return m;
751}
752
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000753/* Parse a source file and return the corresponding code object */
754
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 node *n;
760
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000761 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762 if (n == NULL)
763 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 co = PyNode_Compile(n, pathname);
765 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766
767 return co;
768}
769
770
Guido van Rossum55a83382000-09-20 20:31:38 +0000771/* Helper to open a bytecode file for writing in exclusive mode */
772
773static FILE *
774open_exclusive(char *filename)
775{
776#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
777 /* Use O_EXCL to avoid a race condition when another process tries to
778 write the same file. When that happens, our open() call fails,
779 which is just fine (since it's only a cache).
780 XXX If the file exists and is writable but the directory is not
781 writable, the file will never be written. Oh well.
782 */
783 int fd;
784 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000785 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
786#ifdef O_BINARY
787 |O_BINARY /* necessary for Windows */
788#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000789#ifdef __VMS
790 , 0666, "ctxt=bin", "shr=nil");
791#else
792 , 0666);
793#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000794 if (fd < 0)
795 return NULL;
796 return fdopen(fd, "wb");
797#else
798 /* Best we can do -- on Windows this can't happen anyway */
799 return fopen(filename, "wb");
800#endif
801}
802
803
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804/* Write a compiled module to a file, placing the time of last
805 modification of its source into the header.
806 Errors are ignored, if a write error occurs an attempt is made to
807 remove the file. */
808
809static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000810write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000811{
812 FILE *fp;
813
Guido van Rossum55a83382000-09-20 20:31:38 +0000814 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000817 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000818 "# can't create %s\n", cpathname);
819 return;
820 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000821 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000823 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
824 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000825 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000827 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828 /* Don't keep partial file */
829 fclose(fp);
830 (void) unlink(cpathname);
831 return;
832 }
833 /* Now write the true mtime */
834 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000835 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836 fflush(fp);
837 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000839 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840}
841
842
843/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000844 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
845 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000848load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849{
Fred Drake4c82b232000-06-30 16:18:57 +0000850 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851 FILE *fpc;
852 char buf[MAXPATHLEN+1];
853 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 PyCodeObject *co;
855 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000857 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000858 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000859 return NULL;
860#if SIZEOF_TIME_T > 4
861 /* Python's .pyc timestamp handling presumes that the timestamp fits
862 in 4 bytes. This will be fine until sometime in the year 2038,
863 when a 4-byte signed time_t will overflow.
864 */
865 if (mtime >> 32) {
866 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000867 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000868 return NULL;
869 }
870#endif
Tim Peters36515e22001-11-18 04:06:29 +0000871 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000872 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 if (cpathname != NULL &&
874 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000875 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876 fclose(fpc);
877 if (co == NULL)
878 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000880 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000882 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 }
884 else {
885 co = parse_source_module(pathname, fp);
886 if (co == NULL)
887 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000889 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890 name, pathname);
891 write_compiled_module(co, cpathname, mtime);
892 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000893 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000894 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000895
896 return m;
897}
898
899
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000900/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000901static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
902static struct filedescr *find_module(char *, char *, PyObject *,
903 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000904static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000905
906/* Load a package and return its module object WITH INCREMENTED
907 REFERENCE COUNT */
908
909static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000910load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000911{
Tim Peters1cd70172004-08-02 03:52:12 +0000912 PyObject *m, *d;
913 PyObject *file = NULL;
914 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000915 int err;
916 char buf[MAXPATHLEN+1];
917 FILE *fp = NULL;
918 struct filedescr *fdp;
919
920 m = PyImport_AddModule(name);
921 if (m == NULL)
922 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000923 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000924 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000925 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000926 d = PyModule_GetDict(m);
927 file = PyString_FromString(pathname);
928 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000929 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000930 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000931 if (path == NULL)
932 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000933 err = PyDict_SetItemString(d, "__file__", file);
934 if (err == 0)
935 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000936 if (err != 0)
937 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000938 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000939 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000940 if (fdp == NULL) {
941 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
942 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000943 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000944 }
945 else
946 m = NULL;
947 goto cleanup;
948 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000949 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000950 if (fp != NULL)
951 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000952 goto cleanup;
953
954 error:
955 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957 Py_XDECREF(path);
958 Py_XDECREF(file);
959 return m;
960}
961
962
963/* Helper to test for built-in module */
964
965static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000966is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000967{
968 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000969 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
970 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
971 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000972 return -1;
973 else
974 return 1;
975 }
976 }
977 return 0;
978}
979
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000980
Just van Rossum52e14d62002-12-30 22:08:05 +0000981/* Return an importer object for a sys.path/pkg.__path__ item 'p',
982 possibly by fetching it from the path_importer_cache dict. If it
983 wasn't yet cached, traverse path_hooks until it a hook is found
984 that can handle the path item. Return None if no hook could;
985 this tells our caller it should fall back to the builtin
986 import mechanism. Cache the result in path_importer_cache.
987 Returns a borrowed reference. */
988
989static PyObject *
990get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
991 PyObject *p)
992{
993 PyObject *importer;
994 int j, nhooks;
995
996 /* These conditions are the caller's responsibility: */
997 assert(PyList_Check(path_hooks));
998 assert(PyDict_Check(path_importer_cache));
999
1000 nhooks = PyList_Size(path_hooks);
1001 if (nhooks < 0)
1002 return NULL; /* Shouldn't happen */
1003
1004 importer = PyDict_GetItem(path_importer_cache, p);
1005 if (importer != NULL)
1006 return importer;
1007
1008 /* set path_importer_cache[p] to None to avoid recursion */
1009 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1010 return NULL;
1011
1012 for (j = 0; j < nhooks; j++) {
1013 PyObject *hook = PyList_GetItem(path_hooks, j);
1014 if (hook == NULL)
1015 return NULL;
1016 importer = PyObject_CallFunction(hook, "O", p);
1017 if (importer != NULL)
1018 break;
1019
1020 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1021 return NULL;
1022 }
1023 PyErr_Clear();
1024 }
1025 if (importer == NULL)
1026 importer = Py_None;
1027 else if (importer != Py_None) {
1028 int err = PyDict_SetItem(path_importer_cache, p, importer);
1029 Py_DECREF(importer);
1030 if (err != 0)
1031 return NULL;
1032 }
1033 return importer;
1034}
1035
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001036/* Search the path (default sys.path) for a module. Return the
1037 corresponding filedescr struct, and (via return arguments) the
1038 pathname and an open file. Return NULL if the module is not found. */
1039
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001040#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001041extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1042 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001043#endif
1044
Tim Peters50d8d372001-02-28 05:34:27 +00001045static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001046static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001047static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001048
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001050find_module(char *fullname, char *subname, PyObject *path, char *buf,
1051 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001052{
Fred Drake4c82b232000-06-30 16:18:57 +00001053 int i, npath;
1054 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001055 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001056 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001057 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001058 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001059#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001060 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001061#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001062 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1063 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1064 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001065 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001066#if defined(PYOS_OS2)
1067 size_t saved_len;
1068 size_t saved_namelen;
1069 char *saved_buf = NULL;
1070#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001071 if (p_loader != NULL)
1072 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001073
Just van Rossum52e14d62002-12-30 22:08:05 +00001074 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001075 PyErr_SetString(PyExc_OverflowError,
1076 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001077 return NULL;
1078 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001079 strcpy(name, subname);
1080
1081 /* sys.meta_path import hook */
1082 if (p_loader != NULL) {
1083 PyObject *meta_path;
1084
1085 meta_path = PySys_GetObject("meta_path");
1086 if (meta_path == NULL || !PyList_Check(meta_path)) {
1087 PyErr_SetString(PyExc_ImportError,
1088 "sys.meta_path must be a list of "
1089 "import hooks");
1090 return NULL;
1091 }
1092 Py_INCREF(meta_path); /* zap guard */
1093 npath = PyList_Size(meta_path);
1094 for (i = 0; i < npath; i++) {
1095 PyObject *loader;
1096 PyObject *hook = PyList_GetItem(meta_path, i);
1097 loader = PyObject_CallMethod(hook, "find_module",
1098 "sO", fullname,
1099 path != NULL ?
1100 path : Py_None);
1101 if (loader == NULL) {
1102 Py_DECREF(meta_path);
1103 return NULL; /* true error */
1104 }
1105 if (loader != Py_None) {
1106 /* a loader was found */
1107 *p_loader = loader;
1108 Py_DECREF(meta_path);
1109 return &importhookdescr;
1110 }
1111 Py_DECREF(loader);
1112 }
1113 Py_DECREF(meta_path);
1114 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001115
1116 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001117 /* The only type of submodule allowed inside a "frozen"
1118 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001119 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1120 PyErr_SetString(PyExc_ImportError,
1121 "full frozen module name too long");
1122 return NULL;
1123 }
1124 strcpy(buf, PyString_AsString(path));
1125 strcat(buf, ".");
1126 strcat(buf, name);
1127 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001128 if (find_frozen(name) != NULL) {
1129 strcpy(buf, name);
1130 return &fd_frozen;
1131 }
1132 PyErr_Format(PyExc_ImportError,
1133 "No frozen submodule named %.200s", name);
1134 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001135 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001136 if (path == NULL) {
1137 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001138 strcpy(buf, name);
1139 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001140 }
Greg Ward201baee2001-10-04 14:52:06 +00001141 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001142 strcpy(buf, name);
1143 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001144 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001145
Guido van Rossumac279101996-08-22 23:10:58 +00001146#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001147 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1148 if (fp != NULL) {
1149 *p_fp = fp;
1150 return fdp;
1151 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001152#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001153 path = PySys_GetObject("path");
1154 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155 if (path == NULL || !PyList_Check(path)) {
1156 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001157 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001158 return NULL;
1159 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001160
1161 path_hooks = PySys_GetObject("path_hooks");
1162 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1163 PyErr_SetString(PyExc_ImportError,
1164 "sys.path_hooks must be a list of "
1165 "import hooks");
1166 return NULL;
1167 }
1168 path_importer_cache = PySys_GetObject("path_importer_cache");
1169 if (path_importer_cache == NULL ||
1170 !PyDict_Check(path_importer_cache)) {
1171 PyErr_SetString(PyExc_ImportError,
1172 "sys.path_importer_cache must be a dict");
1173 return NULL;
1174 }
1175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001177 namelen = strlen(name);
1178 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001179 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001181#ifdef Py_USING_UNICODE
1182 if (PyUnicode_Check(v)) {
1183 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1184 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1185 if (copy == NULL)
1186 return NULL;
1187 v = copy;
1188 }
1189 else
1190#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001192 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001194 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1195 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001197 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001198 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001199 if (strlen(buf) != len) {
1200 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001201 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001202 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001203
1204 /* sys.path_hooks import hook */
1205 if (p_loader != NULL) {
1206 PyObject *importer;
1207
1208 importer = get_path_importer(path_importer_cache,
1209 path_hooks, v);
1210 if (importer == NULL)
1211 return NULL;
1212 /* Note: importer is a borrowed reference */
1213 if (importer != Py_None) {
1214 PyObject *loader;
1215 loader = PyObject_CallMethod(importer,
1216 "find_module",
1217 "s", fullname);
1218 if (loader == NULL)
1219 return NULL; /* error */
1220 if (loader != Py_None) {
1221 /* a loader was found */
1222 *p_loader = loader;
1223 return &importhookdescr;
1224 }
1225 Py_DECREF(loader);
1226 }
1227 /* no hook was successful, use builtin import */
1228 }
1229
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001230 if (len > 0 && buf[len-1] != SEP
1231#ifdef ALTSEP
1232 && buf[len-1] != ALTSEP
1233#endif
1234 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001236 strcpy(buf+len, name);
1237 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001238
1239 /* Check for package import (buf holds a directory name,
1240 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001241#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001242 if (stat(buf, &statbuf) == 0 && /* it exists */
1243 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1244 find_init_module(buf) && /* it has __init__.py */
1245 case_ok(buf, len, namelen, name)) { /* and case matches */
1246 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001247 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001248 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001249#else
1250 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001251#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001252 if (isdir(buf) &&
1253 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001254 case_ok(buf, len, namelen, name)) {
1255 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001256 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001257 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001258#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001259#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001260#if defined(PYOS_OS2)
1261 /* take a snapshot of the module spec for restoration
1262 * after the 8 character DLL hackery
1263 */
1264 saved_buf = strdup(buf);
1265 saved_len = len;
1266 saved_namelen = namelen;
1267#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001268 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001269#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001270 /* OS/2 limits DLLs to 8 character names (w/o
1271 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001272 * so if the name is longer than that and its a
1273 * dynamically loaded module we're going to try,
1274 * truncate the name before trying
1275 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001276 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001277 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001278 const struct filedescr *scan;
1279 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001280 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001281 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001282 break;
1283 else
1284 scan++;
1285 }
1286 if (scan->suffix != NULL) {
1287 /* yes, so truncate the name */
1288 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001289 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001290 buf[len] = '\0';
1291 }
1292 }
1293#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001294 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001295 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001296 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001297 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001298 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001299 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001300 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001301 if (fp != NULL) {
1302 if (case_ok(buf, len, namelen, name))
1303 break;
1304 else { /* continue search */
1305 fclose(fp);
1306 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001307 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001308 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001309#if defined(PYOS_OS2)
1310 /* restore the saved snapshot */
1311 strcpy(buf, saved_buf);
1312 len = saved_len;
1313 namelen = saved_namelen;
1314#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001315 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001316#if defined(PYOS_OS2)
1317 /* don't need/want the module name snapshot anymore */
1318 if (saved_buf)
1319 {
1320 free(saved_buf);
1321 saved_buf = NULL;
1322 }
1323#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001324 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325 if (fp != NULL)
1326 break;
1327 }
1328 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001329 PyErr_Format(PyExc_ImportError,
1330 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001331 return NULL;
1332 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001333 *p_fp = fp;
1334 return fdp;
1335}
1336
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001337/* Helpers for main.c
1338 * Find the source file corresponding to a named module
1339 */
1340struct filedescr *
1341_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1342 size_t buflen, FILE **p_fp, PyObject **p_loader)
1343{
1344 return find_module((char *) name, (char *) name, path,
1345 buf, buflen, p_fp, p_loader);
1346}
1347
1348PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1349{
1350 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1351}
1352
Tim Petersd1e87a82001-03-01 18:12:00 +00001353/* case_ok(char* buf, int len, int namelen, char* name)
1354 * The arguments here are tricky, best shown by example:
1355 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1356 * ^ ^ ^ ^
1357 * |--------------------- buf ---------------------|
1358 * |------------------- len ------------------|
1359 * |------ name -------|
1360 * |----- namelen -----|
1361 * buf is the full path, but len only counts up to (& exclusive of) the
1362 * extension. name is the module name, also exclusive of extension.
1363 *
1364 * We've already done a successful stat() or fopen() on buf, so know that
1365 * there's some match, possibly case-insensitive.
1366 *
Tim Peters50d8d372001-02-28 05:34:27 +00001367 * case_ok() is to return 1 if there's a case-sensitive match for
1368 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1369 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001370 *
Tim Peters50d8d372001-02-28 05:34:27 +00001371 * case_ok() is used to implement case-sensitive import semantics even
1372 * on platforms with case-insensitive filesystems. It's trivial to implement
1373 * for case-sensitive filesystems. It's pretty much a cross-platform
1374 * nightmare for systems with case-insensitive filesystems.
1375 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001376
Tim Peters50d8d372001-02-28 05:34:27 +00001377/* First we may need a pile of platform-specific header files; the sequence
1378 * of #if's here should match the sequence in the body of case_ok().
1379 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001380#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001381#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001382
Tim Peters50d8d372001-02-28 05:34:27 +00001383#elif defined(DJGPP)
1384#include <dir.h>
1385
Jason Tishler7961aa62005-05-20 00:56:54 +00001386#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001387#include <sys/types.h>
1388#include <dirent.h>
1389
Andrew MacIntyred9400542002-02-26 11:41:34 +00001390#elif defined(PYOS_OS2)
1391#define INCL_DOS
1392#define INCL_DOSERRORS
1393#define INCL_NOPMAPI
1394#include <os2.h>
1395
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001396#elif defined(RISCOS)
1397#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001398#endif
1399
Guido van Rossum0980bd91998-02-13 17:18:36 +00001400static int
Tim Peters50d8d372001-02-28 05:34:27 +00001401case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001402{
Tim Peters50d8d372001-02-28 05:34:27 +00001403/* Pick a platform-specific implementation; the sequence of #if's here should
1404 * match the sequence just above.
1405 */
1406
Jason Tishler7961aa62005-05-20 00:56:54 +00001407/* MS_WINDOWS */
1408#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001409 WIN32_FIND_DATA data;
1410 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001411
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001412 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001413 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001414
Guido van Rossum0980bd91998-02-13 17:18:36 +00001415 h = FindFirstFile(buf, &data);
1416 if (h == INVALID_HANDLE_VALUE) {
1417 PyErr_Format(PyExc_NameError,
1418 "Can't find file for module %.100s\n(filename %.300s)",
1419 name, buf);
1420 return 0;
1421 }
1422 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001423 return strncmp(data.cFileName, name, namelen) == 0;
1424
1425/* DJGPP */
1426#elif defined(DJGPP)
1427 struct ffblk ffblk;
1428 int done;
1429
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001430 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001431 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001432
1433 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1434 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001435 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001436 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001437 name, buf);
1438 return 0;
1439 }
Tim Peters50d8d372001-02-28 05:34:27 +00001440 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001441
Jason Tishler7961aa62005-05-20 00:56:54 +00001442/* new-fangled macintosh (macosx) or Cygwin */
1443#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001444 DIR *dirp;
1445 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001446 char dirname[MAXPATHLEN + 1];
1447 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001448
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001449 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001450 return 1;
1451
Tim Petersd1e87a82001-03-01 18:12:00 +00001452 /* Copy the dir component into dirname; substitute "." if empty */
1453 if (dirlen <= 0) {
1454 dirname[0] = '.';
1455 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001456 }
1457 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001458 assert(dirlen <= MAXPATHLEN);
1459 memcpy(dirname, buf, dirlen);
1460 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001461 }
1462 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001463 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001464 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001465 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001466 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001467 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001468#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001469 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001470#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001471 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001472#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001473 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001474 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001475 (void)closedir(dirp);
1476 return 1; /* Found */
1477 }
1478 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001479 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001480 }
Tim Peters430f5d42001-03-01 01:30:56 +00001481 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001482
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001483/* RISC OS */
1484#elif defined(RISCOS)
1485 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1486 char buf2[MAXPATHLEN+2];
1487 char *nameWithExt = buf+len-namelen;
1488 int canonlen;
1489 os_error *e;
1490
1491 if (Py_GETENV("PYTHONCASEOK") != NULL)
1492 return 1;
1493
1494 /* workaround:
1495 append wildcard, otherwise case of filename wouldn't be touched */
1496 strcpy(buf2, buf);
1497 strcat(buf2, "*");
1498
1499 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1500 canonlen = MAXPATHLEN+1-canonlen;
1501 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1502 return 0;
1503 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1504 return 1; /* match */
1505
1506 return 0;
1507
Andrew MacIntyred9400542002-02-26 11:41:34 +00001508/* OS/2 */
1509#elif defined(PYOS_OS2)
1510 HDIR hdir = 1;
1511 ULONG srchcnt = 1;
1512 FILEFINDBUF3 ffbuf;
1513 APIRET rc;
1514
1515 if (getenv("PYTHONCASEOK") != NULL)
1516 return 1;
1517
1518 rc = DosFindFirst(buf,
1519 &hdir,
1520 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1521 &ffbuf, sizeof(ffbuf),
1522 &srchcnt,
1523 FIL_STANDARD);
1524 if (rc != NO_ERROR)
1525 return 0;
1526 return strncmp(ffbuf.achName, name, namelen) == 0;
1527
Tim Peters50d8d372001-02-28 05:34:27 +00001528/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1529#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001530 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001531
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001532#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001533}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001534
Guido van Rossum0980bd91998-02-13 17:18:36 +00001535
Guido van Rossum197346f1997-10-31 18:38:52 +00001536#ifdef HAVE_STAT
1537/* Helper to look for __init__.py or __init__.py[co] in potential package */
1538static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001539find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001540{
Tim Peters0f9431f2001-07-05 03:47:53 +00001541 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001542 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001543 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001544 struct stat statbuf;
1545
Tim Peters0f9431f2001-07-05 03:47:53 +00001546/* For calling case_ok(buf, len, namelen, name):
1547 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1548 * ^ ^ ^ ^
1549 * |--------------------- buf ---------------------|
1550 * |------------------- len ------------------|
1551 * |------ name -------|
1552 * |----- namelen -----|
1553 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001554 if (save_len + 13 >= MAXPATHLEN)
1555 return 0;
1556 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001557 pname = buf + i;
1558 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001559 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001560 if (case_ok(buf,
1561 save_len + 9, /* len("/__init__") */
1562 8, /* len("__init__") */
1563 pname)) {
1564 buf[save_len] = '\0';
1565 return 1;
1566 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001567 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001568 i += strlen(pname);
1569 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001570 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001571 if (case_ok(buf,
1572 save_len + 9, /* len("/__init__") */
1573 8, /* len("__init__") */
1574 pname)) {
1575 buf[save_len] = '\0';
1576 return 1;
1577 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001578 }
1579 buf[save_len] = '\0';
1580 return 0;
1581}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001582
1583#else
1584
1585#ifdef RISCOS
1586static int
1587find_init_module(buf)
1588 char *buf;
1589{
1590 int save_len = strlen(buf);
1591 int i = save_len;
1592
1593 if (save_len + 13 >= MAXPATHLEN)
1594 return 0;
1595 buf[i++] = SEP;
1596 strcpy(buf+i, "__init__/py");
1597 if (isfile(buf)) {
1598 buf[save_len] = '\0';
1599 return 1;
1600 }
1601
1602 if (Py_OptimizeFlag)
1603 strcpy(buf+i, "o");
1604 else
1605 strcpy(buf+i, "c");
1606 if (isfile(buf)) {
1607 buf[save_len] = '\0';
1608 return 1;
1609 }
1610 buf[save_len] = '\0';
1611 return 0;
1612}
1613#endif /*RISCOS*/
1614
Guido van Rossum197346f1997-10-31 18:38:52 +00001615#endif /* HAVE_STAT */
1616
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001617
Tim Petersdbd9ba62000-07-09 03:09:57 +00001618static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001619
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001620/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001621 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001622
Guido van Rossum79f25d91997-04-29 20:08:16 +00001623static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001624load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001625{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001626 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001628 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001629
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001630 /* First check that there's an open file (if we need one) */
1631 switch (type) {
1632 case PY_SOURCE:
1633 case PY_COMPILED:
1634 if (fp == NULL) {
1635 PyErr_Format(PyExc_ValueError,
1636 "file object required for import (type code %d)",
1637 type);
1638 return NULL;
1639 }
1640 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001641
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001642 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001643
1644 case PY_SOURCE:
1645 m = load_source_module(name, buf, fp);
1646 break;
1647
1648 case PY_COMPILED:
1649 m = load_compiled_module(name, buf, fp);
1650 break;
1651
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001652#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001653 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001654 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001655 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001656#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001657
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001658 case PKG_DIRECTORY:
1659 m = load_package(name, buf);
1660 break;
1661
1662 case C_BUILTIN:
1663 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001664 if (buf != NULL && buf[0] != '\0')
1665 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001666 if (type == C_BUILTIN)
1667 err = init_builtin(name);
1668 else
1669 err = PyImport_ImportFrozenModule(name);
1670 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001671 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001672 if (err == 0) {
1673 PyErr_Format(PyExc_ImportError,
1674 "Purported %s module %.200s not found",
1675 type == C_BUILTIN ?
1676 "builtin" : "frozen",
1677 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001678 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001679 }
1680 modules = PyImport_GetModuleDict();
1681 m = PyDict_GetItemString(modules, name);
1682 if (m == NULL) {
1683 PyErr_Format(
1684 PyExc_ImportError,
1685 "%s module %.200s not properly initialized",
1686 type == C_BUILTIN ?
1687 "builtin" : "frozen",
1688 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001689 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001690 }
1691 Py_INCREF(m);
1692 break;
1693
Just van Rossum52e14d62002-12-30 22:08:05 +00001694 case IMP_HOOK: {
1695 if (loader == NULL) {
1696 PyErr_SetString(PyExc_ImportError,
1697 "import hook without loader");
1698 return NULL;
1699 }
1700 m = PyObject_CallMethod(loader, "load_module", "s", name);
1701 break;
1702 }
1703
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001705 PyErr_Format(PyExc_ImportError,
1706 "Don't know how to import %.200s (type code %d)",
1707 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001708 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001709
1710 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001711
1712 return m;
1713}
1714
1715
1716/* Initialize a built-in module.
1717 Return 1 for succes, 0 if the module is not found, and -1 with
1718 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001719
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001720static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001721init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001722{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001723 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001724
Greg Ward201baee2001-10-04 14:52:06 +00001725 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001726 return 1;
1727
Guido van Rossum771c6c81997-10-31 18:37:24 +00001728 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001729 if (strcmp(name, p->name) == 0) {
1730 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001731 PyErr_Format(PyExc_ImportError,
1732 "Cannot re-init internal module %.200s",
1733 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001734 return -1;
1735 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001737 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001738 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001741 if (_PyImport_FixupExtension(name, name) == NULL)
1742 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001743 return 1;
1744 }
1745 }
1746 return 0;
1747}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001748
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001749
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001750/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001751
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001752static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001753find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001754{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001755 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001756
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001758 if (p->name == NULL)
1759 return NULL;
1760 if (strcmp(p->name, name) == 0)
1761 break;
1762 }
1763 return p;
1764}
1765
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001767get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001768{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001769 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001770 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001771
1772 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001773 PyErr_Format(PyExc_ImportError,
1774 "No such frozen object named %.200s",
1775 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001776 return NULL;
1777 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001778 if (p->code == NULL) {
1779 PyErr_Format(PyExc_ImportError,
1780 "Excluded frozen object named %.200s",
1781 name);
1782 return NULL;
1783 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001784 size = p->size;
1785 if (size < 0)
1786 size = -size;
1787 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001788}
1789
1790/* Initialize a frozen module.
1791 Return 1 for succes, 0 if the module is not found, and -1 with
1792 an exception set if the initialization failed.
1793 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001794
1795int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001796PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001797{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001798 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799 PyObject *co;
1800 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001801 int ispackage;
1802 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001803
1804 if (p == NULL)
1805 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001806 if (p->code == NULL) {
1807 PyErr_Format(PyExc_ImportError,
1808 "Excluded frozen object named %.200s",
1809 name);
1810 return -1;
1811 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001812 size = p->size;
1813 ispackage = (size < 0);
1814 if (ispackage)
1815 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001817 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001818 name, ispackage ? " package" : "");
1819 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001820 if (co == NULL)
1821 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001822 if (!PyCode_Check(co)) {
1823 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001824 PyErr_Format(PyExc_TypeError,
1825 "frozen object %.200s is not a code object",
1826 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001827 return -1;
1828 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001829 if (ispackage) {
1830 /* Set __path__ to the package name */
1831 PyObject *d, *s;
1832 int err;
1833 m = PyImport_AddModule(name);
1834 if (m == NULL)
1835 return -1;
1836 d = PyModule_GetDict(m);
1837 s = PyString_InternFromString(name);
1838 if (s == NULL)
1839 return -1;
1840 err = PyDict_SetItemString(d, "__path__", s);
1841 Py_DECREF(s);
1842 if (err != 0)
1843 return err;
1844 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001845 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001847 if (m == NULL)
1848 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001850 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001851}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001852
1853
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001854/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001855 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001856
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001858PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001859{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001860 PyObject *pname;
1861 PyObject *result;
1862
1863 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001864 if (pname == NULL)
1865 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001866 result = PyImport_Import(pname);
1867 Py_DECREF(pname);
1868 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001869}
1870
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001871/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001872static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1873static PyObject *load_next(PyObject *mod, PyObject *altmod,
1874 char **p_name, char *buf, int *p_buflen);
1875static int mark_miss(char *name);
1876static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1877 char *buf, int buflen, int recursive);
1878static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001879
1880/* The Magnum Opus of dotted-name import :-) */
1881
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001882static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001883import_module_ex(char *name, PyObject *globals, PyObject *locals,
1884 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001885{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001886 char buf[MAXPATHLEN+1];
1887 int buflen = 0;
1888 PyObject *parent, *head, *next, *tail;
1889
1890 parent = get_parent(globals, buf, &buflen);
1891 if (parent == NULL)
1892 return NULL;
1893
1894 head = load_next(parent, Py_None, &name, buf, &buflen);
1895 if (head == NULL)
1896 return NULL;
1897
1898 tail = head;
1899 Py_INCREF(tail);
1900 while (name) {
1901 next = load_next(tail, tail, &name, buf, &buflen);
1902 Py_DECREF(tail);
1903 if (next == NULL) {
1904 Py_DECREF(head);
1905 return NULL;
1906 }
1907 tail = next;
1908 }
1909
1910 if (fromlist != NULL) {
1911 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1912 fromlist = NULL;
1913 }
1914
1915 if (fromlist == NULL) {
1916 Py_DECREF(tail);
1917 return head;
1918 }
1919
1920 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001921 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001922 Py_DECREF(tail);
1923 return NULL;
1924 }
1925
1926 return tail;
1927}
1928
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001929PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001930PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1931 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001932{
1933 PyObject *result;
1934 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001935 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001936 if (unlock_import() < 0) {
1937 Py_XDECREF(result);
1938 PyErr_SetString(PyExc_RuntimeError,
1939 "not holding the import lock");
1940 return NULL;
1941 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001942 return result;
1943}
1944
Fred Drake87590902004-05-28 20:21:36 +00001945/* Return the package that an import is being performed in. If globals comes
1946 from the module foo.bar.bat (not itself a package), this returns the
1947 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1948 the package's entry in sys.modules is returned.
1949
1950 The *name* of the returned package is returned in buf, with the length of
1951 the name in *p_buflen.
1952
1953 If globals doesn't come from a package or a module in a package, or a
1954 corresponding entry is not found in sys.modules, Py_None is returned.
1955*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001956static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001957get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001958{
1959 static PyObject *namestr = NULL;
1960 static PyObject *pathstr = NULL;
1961 PyObject *modname, *modpath, *modules, *parent;
1962
1963 if (globals == NULL || !PyDict_Check(globals))
1964 return Py_None;
1965
1966 if (namestr == NULL) {
1967 namestr = PyString_InternFromString("__name__");
1968 if (namestr == NULL)
1969 return NULL;
1970 }
1971 if (pathstr == NULL) {
1972 pathstr = PyString_InternFromString("__path__");
1973 if (pathstr == NULL)
1974 return NULL;
1975 }
1976
1977 *buf = '\0';
1978 *p_buflen = 0;
1979 modname = PyDict_GetItem(globals, namestr);
1980 if (modname == NULL || !PyString_Check(modname))
1981 return Py_None;
1982
1983 modpath = PyDict_GetItem(globals, pathstr);
1984 if (modpath != NULL) {
1985 int len = PyString_GET_SIZE(modname);
1986 if (len > MAXPATHLEN) {
1987 PyErr_SetString(PyExc_ValueError,
1988 "Module name too long");
1989 return NULL;
1990 }
1991 strcpy(buf, PyString_AS_STRING(modname));
1992 *p_buflen = len;
1993 }
1994 else {
1995 char *start = PyString_AS_STRING(modname);
1996 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001997 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001998 if (lastdot == NULL)
1999 return Py_None;
2000 len = lastdot - start;
2001 if (len >= MAXPATHLEN) {
2002 PyErr_SetString(PyExc_ValueError,
2003 "Module name too long");
2004 return NULL;
2005 }
2006 strncpy(buf, start, len);
2007 buf[len] = '\0';
2008 *p_buflen = len;
2009 }
2010
2011 modules = PyImport_GetModuleDict();
2012 parent = PyDict_GetItemString(modules, buf);
2013 if (parent == NULL)
2014 parent = Py_None;
2015 return parent;
2016 /* We expect, but can't guarantee, if parent != None, that:
2017 - parent.__name__ == buf
2018 - parent.__dict__ is globals
2019 If this is violated... Who cares? */
2020}
2021
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002022/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002023static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002024load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2025 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002026{
2027 char *name = *p_name;
2028 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002029 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002030 char *p;
2031 PyObject *result;
2032
2033 if (dot == NULL) {
2034 *p_name = NULL;
2035 len = strlen(name);
2036 }
2037 else {
2038 *p_name = dot+1;
2039 len = dot-name;
2040 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002041 if (len == 0) {
2042 PyErr_SetString(PyExc_ValueError,
2043 "Empty module name");
2044 return NULL;
2045 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002046
2047 p = buf + *p_buflen;
2048 if (p != buf)
2049 *p++ = '.';
2050 if (p+len-buf >= MAXPATHLEN) {
2051 PyErr_SetString(PyExc_ValueError,
2052 "Module name too long");
2053 return NULL;
2054 }
2055 strncpy(p, name, len);
2056 p[len] = '\0';
2057 *p_buflen = p+len-buf;
2058
2059 result = import_submodule(mod, p, buf);
2060 if (result == Py_None && altmod != mod) {
2061 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002062 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002063 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002064 if (result != NULL && result != Py_None) {
2065 if (mark_miss(buf) != 0) {
2066 Py_DECREF(result);
2067 return NULL;
2068 }
2069 strncpy(buf, name, len);
2070 buf[len] = '\0';
2071 *p_buflen = len;
2072 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002073 }
2074 if (result == NULL)
2075 return NULL;
2076
2077 if (result == Py_None) {
2078 Py_DECREF(result);
2079 PyErr_Format(PyExc_ImportError,
2080 "No module named %.200s", name);
2081 return NULL;
2082 }
2083
2084 return result;
2085}
2086
2087static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002088mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002089{
2090 PyObject *modules = PyImport_GetModuleDict();
2091 return PyDict_SetItemString(modules, name, Py_None);
2092}
2093
2094static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002095ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2096 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002097{
2098 int i;
2099
2100 if (!PyObject_HasAttrString(mod, "__path__"))
2101 return 1;
2102
2103 for (i = 0; ; i++) {
2104 PyObject *item = PySequence_GetItem(fromlist, i);
2105 int hasit;
2106 if (item == NULL) {
2107 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2108 PyErr_Clear();
2109 return 1;
2110 }
2111 return 0;
2112 }
2113 if (!PyString_Check(item)) {
2114 PyErr_SetString(PyExc_TypeError,
2115 "Item in ``from list'' not a string");
2116 Py_DECREF(item);
2117 return 0;
2118 }
2119 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002120 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002121 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002122 /* See if the package defines __all__ */
2123 if (recursive)
2124 continue; /* Avoid endless recursion */
2125 all = PyObject_GetAttrString(mod, "__all__");
2126 if (all == NULL)
2127 PyErr_Clear();
2128 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002129 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002130 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002131 if (!ret)
2132 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002133 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002134 continue;
2135 }
2136 hasit = PyObject_HasAttr(mod, item);
2137 if (!hasit) {
2138 char *subname = PyString_AS_STRING(item);
2139 PyObject *submod;
2140 char *p;
2141 if (buflen + strlen(subname) >= MAXPATHLEN) {
2142 PyErr_SetString(PyExc_ValueError,
2143 "Module name too long");
2144 Py_DECREF(item);
2145 return 0;
2146 }
2147 p = buf + buflen;
2148 *p++ = '.';
2149 strcpy(p, subname);
2150 submod = import_submodule(mod, subname, buf);
2151 Py_XDECREF(submod);
2152 if (submod == NULL) {
2153 Py_DECREF(item);
2154 return 0;
2155 }
2156 }
2157 Py_DECREF(item);
2158 }
2159
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002160 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002161}
2162
Neil Schemenauer00b09662003-06-16 21:03:07 +00002163static int
2164add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2165 PyObject *modules)
2166{
2167 if (mod == Py_None)
2168 return 1;
2169 /* Irrespective of the success of this load, make a
2170 reference to it in the parent package module. A copy gets
2171 saved in the modules dictionary under the full name, so get a
2172 reference from there, if need be. (The exception is when the
2173 load failed with a SyntaxError -- then there's no trace in
2174 sys.modules. In that case, of course, do nothing extra.) */
2175 if (submod == NULL) {
2176 submod = PyDict_GetItemString(modules, fullname);
2177 if (submod == NULL)
2178 return 1;
2179 }
2180 if (PyModule_Check(mod)) {
2181 /* We can't use setattr here since it can give a
2182 * spurious warning if the submodule name shadows a
2183 * builtin name */
2184 PyObject *dict = PyModule_GetDict(mod);
2185 if (!dict)
2186 return 0;
2187 if (PyDict_SetItemString(dict, subname, submod) < 0)
2188 return 0;
2189 }
2190 else {
2191 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2192 return 0;
2193 }
2194 return 1;
2195}
2196
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002197static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002198import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002199{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002200 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002201 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002202
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002203 /* Require:
2204 if mod == None: subname == fullname
2205 else: mod.__name__ + "." + subname == fullname
2206 */
2207
Tim Peters50d8d372001-02-28 05:34:27 +00002208 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002209 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002210 }
2211 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002212 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002213 char buf[MAXPATHLEN+1];
2214 struct filedescr *fdp;
2215 FILE *fp = NULL;
2216
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002217 if (mod == Py_None)
2218 path = NULL;
2219 else {
2220 path = PyObject_GetAttrString(mod, "__path__");
2221 if (path == NULL) {
2222 PyErr_Clear();
2223 Py_INCREF(Py_None);
2224 return Py_None;
2225 }
2226 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002227
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002228 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002229 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2230 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002231 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002232 if (fdp == NULL) {
2233 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2234 return NULL;
2235 PyErr_Clear();
2236 Py_INCREF(Py_None);
2237 return Py_None;
2238 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002239 m = load_module(fullname, fp, buf, fdp->type, loader);
2240 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002241 if (fp)
2242 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002243 if (!add_submodule(mod, m, fullname, subname, modules)) {
2244 Py_XDECREF(m);
2245 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002246 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002247 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002248
2249 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002250}
2251
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002252
2253/* Re-import a module of any kind and return its module object, WITH
2254 INCREMENTED REFERENCE COUNT */
2255
Guido van Rossum79f25d91997-04-29 20:08:16 +00002256PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002257PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002258{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002259 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002260 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002261 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002262 char buf[MAXPATHLEN+1];
2263 struct filedescr *fdp;
2264 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002265 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002266
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 if (m == NULL || !PyModule_Check(m)) {
2268 PyErr_SetString(PyExc_TypeError,
2269 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002270 return NULL;
2271 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002272 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002273 if (name == NULL)
2274 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002275 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002276 PyErr_Format(PyExc_ImportError,
2277 "reload(): module %.200s not in sys.modules",
2278 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002279 return NULL;
2280 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002281 subname = strrchr(name, '.');
2282 if (subname == NULL)
2283 subname = name;
2284 else {
2285 PyObject *parentname, *parent;
2286 parentname = PyString_FromStringAndSize(name, (subname-name));
2287 if (parentname == NULL)
2288 return NULL;
2289 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002290 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002291 if (parent == NULL) {
2292 PyErr_Format(PyExc_ImportError,
2293 "reload(): parent %.200s not in sys.modules",
2294 name);
2295 return NULL;
2296 }
2297 subname++;
2298 path = PyObject_GetAttrString(parent, "__path__");
2299 if (path == NULL)
2300 PyErr_Clear();
2301 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002302 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002303 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002304 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002305
2306 if (fdp == NULL) {
2307 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002308 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002309 }
2310
2311 newm = load_module(name, fp, buf, fdp->type, loader);
2312 Py_XDECREF(loader);
2313
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002314 if (fp)
2315 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002316 if (newm == NULL) {
2317 /* load_module probably removed name from modules because of
2318 * the error. Put back the original module object. We're
2319 * going to return NULL in this case regardless of whether
2320 * replacing name succeeds, so the return value is ignored.
2321 */
2322 PyDict_SetItemString(modules, name, m);
2323 }
2324 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002325}
2326
2327
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002328/* Higher-level import emulator which emulates the "import" statement
2329 more accurately -- it invokes the __import__() function from the
2330 builtins of the current globals. This means that the import is
2331 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002332 environment, e.g. by "rexec".
2333 A dummy list ["__doc__"] is passed as the 4th argument so that
2334 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2335 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002336
2337PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002338PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002339{
2340 static PyObject *silly_list = NULL;
2341 static PyObject *builtins_str = NULL;
2342 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002343 PyObject *globals = NULL;
2344 PyObject *import = NULL;
2345 PyObject *builtins = NULL;
2346 PyObject *r = NULL;
2347
2348 /* Initialize constant string objects */
2349 if (silly_list == NULL) {
2350 import_str = PyString_InternFromString("__import__");
2351 if (import_str == NULL)
2352 return NULL;
2353 builtins_str = PyString_InternFromString("__builtins__");
2354 if (builtins_str == NULL)
2355 return NULL;
2356 silly_list = Py_BuildValue("[s]", "__doc__");
2357 if (silly_list == NULL)
2358 return NULL;
2359 }
2360
2361 /* Get the builtins from current globals */
2362 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002363 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002364 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002365 builtins = PyObject_GetItem(globals, builtins_str);
2366 if (builtins == NULL)
2367 goto err;
2368 }
2369 else {
2370 /* No globals -- use standard builtins, and fake globals */
2371 PyErr_Clear();
2372
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002373 builtins = PyImport_ImportModuleEx("__builtin__",
2374 NULL, NULL, NULL);
2375 if (builtins == NULL)
2376 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002377 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2378 if (globals == NULL)
2379 goto err;
2380 }
2381
2382 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002383 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002384 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002385 if (import == NULL)
2386 PyErr_SetObject(PyExc_KeyError, import_str);
2387 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002388 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002389 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002390 if (import == NULL)
2391 goto err;
2392
2393 /* Call the _import__ function with the proper argument list */
2394 r = PyObject_CallFunction(import, "OOOO",
2395 module_name, globals, globals, silly_list);
2396
2397 err:
2398 Py_XDECREF(globals);
2399 Py_XDECREF(builtins);
2400 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002401
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002402 return r;
2403}
2404
2405
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002406/* Module 'imp' provides Python access to the primitives used for
2407 importing modules.
2408*/
2409
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002411imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002412{
2413 char buf[4];
2414
Guido van Rossum96774c12000-05-01 20:19:08 +00002415 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2416 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2417 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2418 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002419
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002421}
2422
Guido van Rossum79f25d91997-04-29 20:08:16 +00002423static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002424imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002425{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002426 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002427 struct filedescr *fdp;
2428
Guido van Rossum79f25d91997-04-29 20:08:16 +00002429 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002430 if (list == NULL)
2431 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002432 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2433 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002434 fdp->suffix, fdp->mode, fdp->type);
2435 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002436 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002437 return NULL;
2438 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 if (PyList_Append(list, item) < 0) {
2440 Py_DECREF(list);
2441 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002442 return NULL;
2443 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002445 }
2446 return list;
2447}
2448
Guido van Rossum79f25d91997-04-29 20:08:16 +00002449static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002450call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002451{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002452 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002454 struct filedescr *fdp;
2455 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002456 FILE *fp = NULL;
2457
2458 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002459 if (path == Py_None)
2460 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002461 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002462 if (fdp == NULL)
2463 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002464 if (fp != NULL) {
2465 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2466 if (fob == NULL) {
2467 fclose(fp);
2468 return NULL;
2469 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002470 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002471 else {
2472 fob = Py_None;
2473 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002474 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002475 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002476 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002477 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002478 return ret;
2479}
2480
Guido van Rossum79f25d91997-04-29 20:08:16 +00002481static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002482imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002483{
2484 char *name;
2485 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002486 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002487 return NULL;
2488 return call_find_module(name, path);
2489}
2490
2491static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002492imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002493{
2494 char *name;
2495 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002496 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002497 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002498 return NULL;
2499 ret = init_builtin(name);
2500 if (ret < 0)
2501 return NULL;
2502 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002503 Py_INCREF(Py_None);
2504 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002505 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506 m = PyImport_AddModule(name);
2507 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002508 return m;
2509}
2510
Guido van Rossum79f25d91997-04-29 20:08:16 +00002511static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002512imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002513{
2514 char *name;
2515 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002516 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002517 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002518 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002519 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002520 if (ret < 0)
2521 return NULL;
2522 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002523 Py_INCREF(Py_None);
2524 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002525 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002526 m = PyImport_AddModule(name);
2527 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002528 return m;
2529}
2530
Guido van Rossum79f25d91997-04-29 20:08:16 +00002531static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002532imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002533{
2534 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002535
Guido van Rossum43713e52000-02-29 13:59:29 +00002536 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002537 return NULL;
2538 return get_frozen_object(name);
2539}
2540
Guido van Rossum79f25d91997-04-29 20:08:16 +00002541static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002542imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002543{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002544 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002545 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002546 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002547 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002548}
2549
Guido van Rossum79f25d91997-04-29 20:08:16 +00002550static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002551imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002552{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002553 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002554 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002555 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002556 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002557 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002558 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002559}
2560
2561static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002562get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563{
2564 FILE *fp;
2565 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002566 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002567 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002568 fp = fopen(pathname, mode);
2569 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002571 }
2572 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002573 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002574 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002575 PyErr_SetString(PyExc_ValueError,
2576 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577 }
2578 return fp;
2579}
2580
Guido van Rossum79f25d91997-04-29 20:08:16 +00002581static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002582imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002583{
2584 char *name;
2585 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002586 PyObject *fob = NULL;
2587 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002588 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002589 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002591 return NULL;
2592 fp = get_file(pathname, fob, "rb");
2593 if (fp == NULL)
2594 return NULL;
2595 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002596 if (fob == NULL)
2597 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002598 return m;
2599}
2600
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002601#ifdef HAVE_DYNAMIC_LOADING
2602
Guido van Rossum79f25d91997-04-29 20:08:16 +00002603static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002604imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002605{
2606 char *name;
2607 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 PyObject *fob = NULL;
2609 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002610 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002611 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002612 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002613 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002614 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002615 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002616 if (fp == NULL)
2617 return NULL;
2618 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002619 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002620 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002621}
2622
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002623#endif /* HAVE_DYNAMIC_LOADING */
2624
Guido van Rossum79f25d91997-04-29 20:08:16 +00002625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002626imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002627{
2628 char *name;
2629 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002630 PyObject *fob = NULL;
2631 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002632 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002633 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002634 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002635 return NULL;
2636 fp = get_file(pathname, fob, "r");
2637 if (fp == NULL)
2638 return NULL;
2639 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002640 if (fob == NULL)
2641 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002642 return m;
2643}
2644
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002646imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002647{
2648 char *name;
2649 PyObject *fob;
2650 char *pathname;
2651 char *suffix; /* Unused */
2652 char *mode;
2653 int type;
2654 FILE *fp;
2655
Guido van Rossum43713e52000-02-29 13:59:29 +00002656 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002657 &name, &fob, &pathname,
2658 &suffix, &mode, &type))
2659 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002660 if (*mode) {
2661 /* Mode must start with 'r' or 'U' and must not contain '+'.
2662 Implicit in this test is the assumption that the mode
2663 may contain other modifiers like 'b' or 't'. */
2664
2665 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002666 PyErr_Format(PyExc_ValueError,
2667 "invalid file open mode %.200s", mode);
2668 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002669 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002670 }
2671 if (fob == Py_None)
2672 fp = NULL;
2673 else {
2674 if (!PyFile_Check(fob)) {
2675 PyErr_SetString(PyExc_ValueError,
2676 "load_module arg#2 should be a file or None");
2677 return NULL;
2678 }
2679 fp = get_file(pathname, fob, mode);
2680 if (fp == NULL)
2681 return NULL;
2682 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002683 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002684}
2685
2686static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002687imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002688{
2689 char *name;
2690 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002691 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002692 return NULL;
2693 return load_package(name, pathname);
2694}
2695
2696static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002697imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002698{
2699 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002700 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002701 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002702 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002703}
2704
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002705/* Doc strings */
2706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002707PyDoc_STRVAR(doc_imp,
2708"This module provides the components needed to build your own\n\
2709__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002711PyDoc_STRVAR(doc_find_module,
2712"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002713Search for a module. If path is omitted or None, search for a\n\
2714built-in, frozen or special module and continue search in sys.path.\n\
2715The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002716package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(doc_load_module,
2719"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002720Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002721The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002723PyDoc_STRVAR(doc_get_magic,
2724"get_magic() -> string\n\
2725Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727PyDoc_STRVAR(doc_get_suffixes,
2728"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002729Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002730that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732PyDoc_STRVAR(doc_new_module,
2733"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002734Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002735The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002737PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002738"lock_held() -> boolean\n\
2739Return True if the import lock is currently held, else False.\n\
2740On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002741
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002742PyDoc_STRVAR(doc_acquire_lock,
2743"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002744Acquires the interpreter's import lock for the current thread.\n\
2745This lock should be used by import hooks to ensure thread-safety\n\
2746when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002747On platforms without threads, this function does nothing.");
2748
2749PyDoc_STRVAR(doc_release_lock,
2750"release_lock() -> None\n\
2751Release the interpreter's import lock.\n\
2752On platforms without threads, this function does nothing.");
2753
Guido van Rossum79f25d91997-04-29 20:08:16 +00002754static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002755 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2756 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2757 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2758 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2759 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2760 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2761 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2762 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002763 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002764 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2765 {"init_builtin", imp_init_builtin, METH_VARARGS},
2766 {"init_frozen", imp_init_frozen, METH_VARARGS},
2767 {"is_builtin", imp_is_builtin, METH_VARARGS},
2768 {"is_frozen", imp_is_frozen, METH_VARARGS},
2769 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002770#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002771 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002772#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002773 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002774 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002775 {NULL, NULL} /* sentinel */
2776};
2777
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002778static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002779setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002780{
2781 PyObject *v;
2782 int err;
2783
2784 v = PyInt_FromLong((long)value);
2785 err = PyDict_SetItemString(d, name, v);
2786 Py_XDECREF(v);
2787 return err;
2788}
2789
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002790PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002791initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002792{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002793 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002794
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002795 m = Py_InitModule4("imp", imp_methods, doc_imp,
2796 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002797 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002798
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002799 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2800 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2801 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2802 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2803 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2804 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2805 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2806 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002807 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002808 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002809
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002810 failure:
2811 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002813
2814
Guido van Rossumb18618d2000-05-03 23:44:39 +00002815/* API for embedding applications that want to add their own entries
2816 to the table of built-in modules. This should normally be called
2817 *before* Py_Initialize(). When the table resize fails, -1 is
2818 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002819
2820 After a similar function by Just van Rossum. */
2821
2822int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002823PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002824{
2825 static struct _inittab *our_copy = NULL;
2826 struct _inittab *p;
2827 int i, n;
2828
2829 /* Count the number of entries in both tables */
2830 for (n = 0; newtab[n].name != NULL; n++)
2831 ;
2832 if (n == 0)
2833 return 0; /* Nothing to do */
2834 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2835 ;
2836
2837 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002838 p = our_copy;
2839 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002840 if (p == NULL)
2841 return -1;
2842
2843 /* Copy the tables into the new memory */
2844 if (our_copy != PyImport_Inittab)
2845 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2846 PyImport_Inittab = our_copy = p;
2847 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2848
2849 return 0;
2850}
2851
2852/* Shorthand to add a single entry given a name and a function */
2853
2854int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002855PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002856{
2857 struct _inittab newtab[2];
2858
2859 memset(newtab, '\0', sizeof newtab);
2860
2861 newtab[0].name = name;
2862 newtab[0].initfunc = initfunc;
2863
2864 return PyImport_ExtendInittab(newtab);
2865}