blob: dcbca38e974cb1e166483e4198b7c66794f2bd3f [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
7#include "pythonrun.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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010#include "code.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000011#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000012#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000013#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000014#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000015
Guido van Rossum55a83382000-09-20 20:31:38 +000016#ifdef HAVE_FCNTL_H
17#include <fcntl.h>
18#endif
19
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000020extern time_t PyOS_GetLastModificationTime(char *, FILE *);
21 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000022
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000023/* Magic word to reject .pyc files generated by other Python versions.
24 It should change for each incompatible change to the bytecode.
25
26 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000027 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000028 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000029
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000030 The magic numbers must be spaced apart atleast 2 values, as the
31 -U interpeter flag will cause MAGIC+1 being used. They have been
32 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000033
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000034 There were a variety of old schemes for setting the magic number.
35 The current working scheme is to increment the previous value by
36 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000037
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000038 Known values:
39 Python 1.5: 20121
40 Python 1.5.1: 20121
41 Python 1.5.2: 20121
42 Python 2.0: 50823
43 Python 2.0.1: 50823
44 Python 2.1: 60202
45 Python 2.1.1: 60202
46 Python 2.1.2: 60202
47 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000048 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000049 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000050 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000051 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000052 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000053 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000054 Python 2.5a0: 62071
Tim Peters36515e22001-11-18 04:06:29 +000055*/
Michael W. Hudsondf888462005-06-03 14:41:55 +000056#define MAGIC (62071 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000057
Guido van Rossum96774c12000-05-01 20:19:08 +000058/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000059 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000060 compiler works which are enabled by command line switches. */
61static long pyc_magic = MAGIC;
62
Guido van Rossum25ce5661997-08-02 03:10:38 +000063/* See _PyImport_FixupExtension() below */
64static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000065
Guido van Rossum771c6c81997-10-31 18:37:24 +000066/* This table is defined in config.c: */
67extern struct _inittab _PyImport_Inittab[];
68
69struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000070
Guido van Rossumed1170e1999-12-20 21:23:41 +000071/* these tables define the module suffixes that Python recognizes */
72struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000073
74#ifdef RISCOS
75static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000076 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000077 {"/pyc", "rb", PY_COMPILED},
78 {0, 0}
79};
80#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000081static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000082 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000083#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000084 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000085#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000086 {".pyc", "rb", PY_COMPILED},
87 {0, 0}
88};
Guido van Rossum48a680c2001-03-02 06:34:14 +000089#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000090
Guido van Rossum1ae940a1995-01-02 19:04:15 +000091/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092
93void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000094_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095{
Guido van Rossumed1170e1999-12-20 21:23:41 +000096 const struct filedescr *scan;
97 struct filedescr *filetab;
98 int countD = 0;
99 int countS = 0;
100
101 /* prepare _PyImport_Filetab: copy entries from
102 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
103 */
104 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
105 ++countD;
106 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
107 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000109 memcpy(filetab, _PyImport_DynLoadFiletab,
110 countD * sizeof(struct filedescr));
111 memcpy(filetab + countD, _PyImport_StandardFiletab,
112 countS * sizeof(struct filedescr));
113 filetab[countD + countS].suffix = NULL;
114
115 _PyImport_Filetab = filetab;
116
Guido van Rossum0824f631997-03-11 18:37:35 +0000117 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000118 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
119 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000120#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000121 if (strcmp(filetab->suffix, ".pyc") == 0)
122 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000123#else
124 if (strcmp(filetab->suffix, "/pyc") == 0)
125 filetab->suffix = "/pyo";
126#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000127 }
128 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000129
130 if (Py_UnicodeFlag) {
131 /* Fix the pyc_magic so that byte compiled code created
132 using the all-Unicode method doesn't interfere with
133 code created in normal operation mode. */
134 pyc_magic = MAGIC + 1;
135 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossum25ce5661997-08-02 03:10:38 +0000138void
Just van Rossum52e14d62002-12-30 22:08:05 +0000139_PyImportHooks_Init(void)
140{
141 PyObject *v, *path_hooks = NULL, *zimpimport;
142 int err = 0;
143
144 /* adding sys.path_hooks and sys.path_importer_cache, setting up
145 zipimport */
146
147 if (Py_VerboseFlag)
148 PySys_WriteStderr("# installing zipimport hook\n");
149
150 v = PyList_New(0);
151 if (v == NULL)
152 goto error;
153 err = PySys_SetObject("meta_path", v);
154 Py_DECREF(v);
155 if (err)
156 goto error;
157 v = PyDict_New();
158 if (v == NULL)
159 goto error;
160 err = PySys_SetObject("path_importer_cache", v);
161 Py_DECREF(v);
162 if (err)
163 goto error;
164 path_hooks = PyList_New(0);
165 if (path_hooks == NULL)
166 goto error;
167 err = PySys_SetObject("path_hooks", path_hooks);
168 if (err) {
169 error:
170 PyErr_Print();
171 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
172 "path_importer_cache failed");
173 }
174 zimpimport = PyImport_ImportModule("zipimport");
175 if (zimpimport == NULL) {
176 PyErr_Clear(); /* No zip import module -- okay */
177 if (Py_VerboseFlag)
178 PySys_WriteStderr("# can't import zipimport\n");
179 }
180 else {
181 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
182 "zipimporter");
183 Py_DECREF(zimpimport);
184 if (zipimporter == NULL) {
185 PyErr_Clear(); /* No zipimporter object -- okay */
186 if (Py_VerboseFlag)
187 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000188 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000189 }
190 else {
191 /* sys.path_hooks.append(zipimporter) */
192 err = PyList_Append(path_hooks, zipimporter);
193 Py_DECREF(zipimporter);
194 if (err)
195 goto error;
196 if (Py_VerboseFlag)
197 PySys_WriteStderr(
198 "# installed zipimport hook\n");
199 }
200 }
201 Py_DECREF(path_hooks);
202}
203
204void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206{
207 Py_XDECREF(extensions);
208 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000209 PyMem_DEL(_PyImport_Filetab);
210 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211}
212
213
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000214/* Locking primitives to prevent parallel imports of the same module
215 in different threads to return with a partially loaded module.
216 These calls are serialized by the global interpreter lock. */
217
218#ifdef WITH_THREAD
219
Guido van Rossum49b56061998-10-01 20:42:43 +0000220#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000221
Guido van Rossum65d5b571998-12-21 19:32:43 +0000222static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000223static long import_lock_thread = -1;
224static int import_lock_level = 0;
225
226static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000228{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000230 if (me == -1)
231 return; /* Too bad */
232 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000234 if (import_lock_thread == me) {
235 import_lock_level++;
236 return;
237 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000238 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
239 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000240 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000242 PyEval_RestoreThread(tstate);
243 }
244 import_lock_thread = me;
245 import_lock_level = 1;
246}
247
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000248static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000253 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000255 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256 import_lock_level--;
257 if (import_lock_level == 0) {
258 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000260 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000261 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000262}
263
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000264/* This function is called from PyOS_AfterFork to ensure that newly
265 created child processes do not share locks with the parent. */
266
267void
268_PyImport_ReInitLock(void)
269{
270#ifdef _AIX
271 if (import_lock != NULL)
272 import_lock = PyThread_allocate_lock();
273#endif
274}
275
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276#else
277
278#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000280
281#endif
282
Tim Peters69232342001-08-30 05:16:13 +0000283static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000284imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000285{
Tim Peters69232342001-08-30 05:16:13 +0000286#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000287 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000288#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000289 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000290#endif
291}
292
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000293static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000294imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000296#ifdef WITH_THREAD
297 lock_import();
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
303static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000304imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000305{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000306#ifdef WITH_THREAD
307 if (unlock_import() < 0) {
308 PyErr_SetString(PyExc_RuntimeError,
309 "not holding the import lock");
310 return NULL;
311 }
312#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000313 Py_INCREF(Py_None);
314 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000315}
316
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317/* Helper for sys */
318
319PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000321{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000322 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000323 if (interp->modules == NULL)
324 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
325 return interp->modules;
326}
327
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000329/* List of names to clear in sys */
330static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000331 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000332 "exc_type", "exc_value", "exc_traceback",
333 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000334 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000335 NULL
336};
337
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000338static char* sys_files[] = {
339 "stdin", "__stdin__",
340 "stdout", "__stdout__",
341 "stderr", "__stderr__",
342 NULL
343};
344
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000345
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000346/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000347
Guido van Rossum3f5da241990-12-20 15:06:42 +0000348void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000349PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000350{
Guido van Rossum758eec01998-01-19 21:58:26 +0000351 int pos, ndone;
352 char *name;
353 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000354 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000355 PyObject *modules = interp->modules;
356
357 if (modules == NULL)
358 return; /* Already done */
359
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000360 /* Delete some special variables first. These are common
361 places where user values hide and people complain when their
362 destructors fail. Since the modules containing them are
363 deleted *last* of all, they would come too late in the normal
364 destruction order. Sigh. */
365
366 value = PyDict_GetItemString(modules, "__builtin__");
367 if (value != NULL && PyModule_Check(value)) {
368 dict = PyModule_GetDict(value);
369 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000370 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371 PyDict_SetItemString(dict, "_", Py_None);
372 }
373 value = PyDict_GetItemString(modules, "sys");
374 if (value != NULL && PyModule_Check(value)) {
375 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000376 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000377 dict = PyModule_GetDict(value);
378 for (p = sys_deletes; *p != NULL; p++) {
379 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000380 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000381 PyDict_SetItemString(dict, *p, Py_None);
382 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000383 for (p = sys_files; *p != NULL; p+=2) {
384 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000385 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000386 v = PyDict_GetItemString(dict, *(p+1));
387 if (v == NULL)
388 v = Py_None;
389 PyDict_SetItemString(dict, *p, v);
390 }
391 }
392
393 /* First, delete __main__ */
394 value = PyDict_GetItemString(modules, "__main__");
395 if (value != NULL && PyModule_Check(value)) {
396 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000397 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000398 _PyModule_Clear(value);
399 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000400 }
401
Guido van Rossum758eec01998-01-19 21:58:26 +0000402 /* The special treatment of __builtin__ here is because even
403 when it's not referenced as a module, its dictionary is
404 referenced by almost every module's __builtins__. Since
405 deleting a module clears its dictionary (even if there are
406 references left to it), we need to delete the __builtin__
407 module last. Likewise, we don't delete sys until the very
408 end because it is implicitly referenced (e.g. by print).
409
410 Also note that we 'delete' modules by replacing their entry
411 in the modules dict with None, rather than really deleting
412 them; this avoids a rehash of the modules dictionary and
413 also marks them as "non existent" so they won't be
414 re-imported. */
415
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000416 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000417 one (skipping __builtin__ and sys) and delete them */
418 do {
419 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000420 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000421 while (PyDict_Next(modules, &pos, &key, &value)) {
422 if (value->ob_refcnt != 1)
423 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000424 if (PyString_Check(key) && PyModule_Check(value)) {
425 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000426 if (strcmp(name, "__builtin__") == 0)
427 continue;
428 if (strcmp(name, "sys") == 0)
429 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000430 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000431 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000432 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000433 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000434 PyDict_SetItem(modules, key, Py_None);
435 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000436 }
437 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000438 } while (ndone > 0);
439
Guido van Rossum758eec01998-01-19 21:58:26 +0000440 /* Next, delete all modules (still skipping __builtin__ and sys) */
441 pos = 0;
442 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000443 if (PyString_Check(key) && PyModule_Check(value)) {
444 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000445 if (strcmp(name, "__builtin__") == 0)
446 continue;
447 if (strcmp(name, "sys") == 0)
448 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000449 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000450 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000451 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000452 PyDict_SetItem(modules, key, Py_None);
453 }
454 }
455
456 /* Next, delete sys and __builtin__ (in that order) */
457 value = PyDict_GetItemString(modules, "sys");
458 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000459 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000460 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000461 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000462 PyDict_SetItemString(modules, "sys", Py_None);
463 }
464 value = PyDict_GetItemString(modules, "__builtin__");
465 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000466 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000467 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000468 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000469 PyDict_SetItemString(modules, "__builtin__", Py_None);
470 }
471
472 /* Finally, clear and delete the modules directory */
473 PyDict_Clear(modules);
474 interp->modules = NULL;
475 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000476}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000477
478
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000479/* Helper for pythonrun.c -- return magic number */
480
481long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000482PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483{
Guido van Rossum96774c12000-05-01 20:19:08 +0000484 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000485}
486
487
Guido van Rossum25ce5661997-08-02 03:10:38 +0000488/* Magic for extension modules (built-in as well as dynamically
489 loaded). To prevent initializing an extension module more than
490 once, we keep a static dictionary 'extensions' keyed by module name
491 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000492 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000493 dictionary is stored by calling _PyImport_FixupExtension()
494 immediately after the module initialization function succeeds. A
495 copy can be retrieved from there by calling
496 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497
Guido van Rossum79f25d91997-04-29 20:08:16 +0000498PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000499_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000501 PyObject *modules, *mod, *dict, *copy;
502 if (extensions == NULL) {
503 extensions = PyDict_New();
504 if (extensions == NULL)
505 return NULL;
506 }
507 modules = PyImport_GetModuleDict();
508 mod = PyDict_GetItemString(modules, name);
509 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000510 PyErr_Format(PyExc_SystemError,
511 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000512 return NULL;
513 }
514 dict = PyModule_GetDict(mod);
515 if (dict == NULL)
516 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000517 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000518 if (copy == NULL)
519 return NULL;
520 PyDict_SetItemString(extensions, filename, copy);
521 Py_DECREF(copy);
522 return copy;
523}
524
525PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000527{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000528 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529 if (extensions == NULL)
530 return NULL;
531 dict = PyDict_GetItemString(extensions, filename);
532 if (dict == NULL)
533 return NULL;
534 mod = PyImport_AddModule(name);
535 if (mod == NULL)
536 return NULL;
537 mdict = PyModule_GetDict(mod);
538 if (mdict == NULL)
539 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000540 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000543 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 name, filename);
545 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546}
547
548
549/* Get the module object corresponding to a module name.
550 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000551 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000552 Because the former action is most common, THIS DOES NOT RETURN A
553 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000556PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000558 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560
Guido van Rossum25ce5661997-08-02 03:10:38 +0000561 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000562 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565 if (m == NULL)
566 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569 return NULL;
570 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572
573 return m;
574}
575
Tim Peters1cd70172004-08-02 03:52:12 +0000576/* Remove name from sys.modules, if it's there. */
577static void
578_RemoveModule(const char *name)
579{
580 PyObject *modules = PyImport_GetModuleDict();
581 if (PyDict_GetItemString(modules, name) == NULL)
582 return;
583 if (PyDict_DelItemString(modules, name) < 0)
584 Py_FatalError("import: deleting existing key in"
585 "sys.modules failed");
586}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000587
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000588/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000589 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
590 * removed from sys.modules, to avoid leaving damaged module objects
591 * in sys.modules. The caller may wish to restore the original
592 * module object (if any) in this case; PyImport_ReloadModule is an
593 * example.
594 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000596PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000597{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000598 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
599}
600
601PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000602PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000603{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608 if (m == NULL)
609 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000610 /* If the module is being reloaded, we get the old module back
611 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 d = PyModule_GetDict(m);
613 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
614 if (PyDict_SetItemString(d, "__builtins__",
615 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000616 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000617 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000618 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000619 v = NULL;
620 if (pathname != NULL) {
621 v = PyString_FromString(pathname);
622 if (v == NULL)
623 PyErr_Clear();
624 }
625 if (v == NULL) {
626 v = ((PyCodeObject *)co)->co_filename;
627 Py_INCREF(v);
628 }
629 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000631 Py_DECREF(v);
632
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000633 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000634 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000635 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000637
Guido van Rossum25ce5661997-08-02 03:10:38 +0000638 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000639 PyErr_Format(PyExc_ImportError,
640 "Loaded module %.200s not found in sys.modules",
641 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000642 return NULL;
643 }
644
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646
647 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000648
649 error:
650 _RemoveModule(name);
651 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000652}
653
654
655/* Given a pathname for a Python source file, fill a buffer with the
656 pathname for the corresponding compiled file. Return the pathname
657 for the compiled file, or NULL if there's no space in the buffer.
658 Doesn't set an exception. */
659
660static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000661make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662{
Tim Petersc1731372001-08-04 08:12:36 +0000663 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664 if (len+2 > buflen)
665 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000666
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000667#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000668 /* Treat .pyw as if it were .py. The case of ".pyw" must match
669 that used in _PyImport_StandardFiletab. */
670 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
671 --len; /* pretend 'w' isn't there */
672#endif
673 memcpy(buf, pathname, len);
674 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
675 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676
677 return buf;
678}
679
680
681/* Given a pathname for a Python source file, its time of last
682 modification, and a pathname for a compiled file, check whether the
683 compiled file represents the same version of the source. If so,
684 return a FILE pointer for the compiled file, positioned just after
685 the header; if not, return NULL.
686 Doesn't set an exception. */
687
688static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000689check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000690{
691 FILE *fp;
692 long magic;
693 long pyc_mtime;
694
695 fp = fopen(cpathname, "rb");
696 if (fp == NULL)
697 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000699 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000701 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702 fclose(fp);
703 return NULL;
704 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000708 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000709 fclose(fp);
710 return NULL;
711 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000713 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 return fp;
715}
716
717
718/* Read a code object from a file and check it for validity */
719
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000721read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000723 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000724
Tim Petersd9b9ac82001-01-28 00:27:39 +0000725 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000726 if (co == NULL)
727 return NULL;
728 if (!PyCode_Check(co)) {
729 PyErr_Format(PyExc_ImportError,
730 "Non-code object in %.200s", cpathname);
731 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000732 return NULL;
733 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000735}
736
737
738/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000739 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000742load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743{
744 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 PyCodeObject *co;
746 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000749 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000750 PyErr_Format(PyExc_ImportError,
751 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000752 return NULL;
753 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000755 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756 if (co == NULL)
757 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000759 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000761 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763
764 return m;
765}
766
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000767/* Parse a source file and return the corresponding code object */
768
Guido van Rossum79f25d91997-04-29 20:08:16 +0000769static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 PyCodeObject *co = NULL;
773 mod_ty mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
776 NULL);
777 if (mod) {
778 co = PyAST_Compile(mod, pathname, NULL);
779 free_mod(mod);
780 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000781 return co;
782}
783
784
Guido van Rossum55a83382000-09-20 20:31:38 +0000785/* Helper to open a bytecode file for writing in exclusive mode */
786
787static FILE *
788open_exclusive(char *filename)
789{
790#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
791 /* Use O_EXCL to avoid a race condition when another process tries to
792 write the same file. When that happens, our open() call fails,
793 which is just fine (since it's only a cache).
794 XXX If the file exists and is writable but the directory is not
795 writable, the file will never be written. Oh well.
796 */
797 int fd;
798 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000799 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
800#ifdef O_BINARY
801 |O_BINARY /* necessary for Windows */
802#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000803#ifdef __VMS
804 , 0666, "ctxt=bin", "shr=nil");
805#else
806 , 0666);
807#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000808 if (fd < 0)
809 return NULL;
810 return fdopen(fd, "wb");
811#else
812 /* Best we can do -- on Windows this can't happen anyway */
813 return fopen(filename, "wb");
814#endif
815}
816
817
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000818/* Write a compiled module to a file, placing the time of last
819 modification of its source into the header.
820 Errors are ignored, if a write error occurs an attempt is made to
821 remove the file. */
822
823static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000824write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000825{
826 FILE *fp;
827
Guido van Rossum55a83382000-09-20 20:31:38 +0000828 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000831 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000832 "# can't create %s\n", cpathname);
833 return;
834 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000835 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000837 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
838 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000839 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000841 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000842 /* Don't keep partial file */
843 fclose(fp);
844 (void) unlink(cpathname);
845 return;
846 }
847 /* Now write the true mtime */
848 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000849 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000850 fflush(fp);
851 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000853 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854}
855
856
857/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000858 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
859 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000862load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000863{
Fred Drake4c82b232000-06-30 16:18:57 +0000864 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000865 FILE *fpc;
866 char buf[MAXPATHLEN+1];
867 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000868 PyCodeObject *co;
869 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000870
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000871 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000872 if (mtime == (time_t)(-1)) {
873 PyErr_Format(PyExc_RuntimeError,
874 "unable to get modification time from '%s'",
875 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000876 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000877 }
Fred Drake4c82b232000-06-30 16:18:57 +0000878#if SIZEOF_TIME_T > 4
879 /* Python's .pyc timestamp handling presumes that the timestamp fits
880 in 4 bytes. This will be fine until sometime in the year 2038,
881 when a 4-byte signed time_t will overflow.
882 */
883 if (mtime >> 32) {
884 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000885 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000886 return NULL;
887 }
888#endif
Tim Peters36515e22001-11-18 04:06:29 +0000889 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000890 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 if (cpathname != NULL &&
892 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000893 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894 fclose(fpc);
895 if (co == NULL)
896 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000898 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000899 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000900 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901 }
902 else {
903 co = parse_source_module(pathname, fp);
904 if (co == NULL)
905 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000907 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000908 name, pathname);
909 write_compiled_module(co, cpathname, mtime);
910 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000911 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000912 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000913
914 return m;
915}
916
917
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000918/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000919static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
920static struct filedescr *find_module(char *, char *, PyObject *,
921 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000922static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000923
924/* Load a package and return its module object WITH INCREMENTED
925 REFERENCE COUNT */
926
927static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000928load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000929{
Tim Peters1cd70172004-08-02 03:52:12 +0000930 PyObject *m, *d;
931 PyObject *file = NULL;
932 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000933 int err;
934 char buf[MAXPATHLEN+1];
935 FILE *fp = NULL;
936 struct filedescr *fdp;
937
938 m = PyImport_AddModule(name);
939 if (m == NULL)
940 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000941 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000942 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000943 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000944 d = PyModule_GetDict(m);
945 file = PyString_FromString(pathname);
946 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000947 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000948 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000949 if (path == NULL)
950 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000951 err = PyDict_SetItemString(d, "__file__", file);
952 if (err == 0)
953 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000954 if (err != 0)
955 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000957 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000958 if (fdp == NULL) {
959 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
960 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000961 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000962 }
963 else
964 m = NULL;
965 goto cleanup;
966 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000967 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000968 if (fp != NULL)
969 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000970 goto cleanup;
971
972 error:
973 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000974 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000975 Py_XDECREF(path);
976 Py_XDECREF(file);
977 return m;
978}
979
980
981/* Helper to test for built-in module */
982
983static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000984is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000985{
986 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000987 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
988 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
989 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000990 return -1;
991 else
992 return 1;
993 }
994 }
995 return 0;
996}
997
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000998
Just van Rossum52e14d62002-12-30 22:08:05 +0000999/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1000 possibly by fetching it from the path_importer_cache dict. If it
1001 wasn't yet cached, traverse path_hooks until it a hook is found
1002 that can handle the path item. Return None if no hook could;
1003 this tells our caller it should fall back to the builtin
1004 import mechanism. Cache the result in path_importer_cache.
1005 Returns a borrowed reference. */
1006
1007static PyObject *
1008get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1009 PyObject *p)
1010{
1011 PyObject *importer;
1012 int j, nhooks;
1013
1014 /* These conditions are the caller's responsibility: */
1015 assert(PyList_Check(path_hooks));
1016 assert(PyDict_Check(path_importer_cache));
1017
1018 nhooks = PyList_Size(path_hooks);
1019 if (nhooks < 0)
1020 return NULL; /* Shouldn't happen */
1021
1022 importer = PyDict_GetItem(path_importer_cache, p);
1023 if (importer != NULL)
1024 return importer;
1025
1026 /* set path_importer_cache[p] to None to avoid recursion */
1027 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1028 return NULL;
1029
1030 for (j = 0; j < nhooks; j++) {
1031 PyObject *hook = PyList_GetItem(path_hooks, j);
1032 if (hook == NULL)
1033 return NULL;
1034 importer = PyObject_CallFunction(hook, "O", p);
1035 if (importer != NULL)
1036 break;
1037
1038 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1039 return NULL;
1040 }
1041 PyErr_Clear();
1042 }
1043 if (importer == NULL)
1044 importer = Py_None;
1045 else if (importer != Py_None) {
1046 int err = PyDict_SetItem(path_importer_cache, p, importer);
1047 Py_DECREF(importer);
1048 if (err != 0)
1049 return NULL;
1050 }
1051 return importer;
1052}
1053
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001054/* Search the path (default sys.path) for a module. Return the
1055 corresponding filedescr struct, and (via return arguments) the
1056 pathname and an open file. Return NULL if the module is not found. */
1057
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001058#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001059extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1060 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001061#endif
1062
Tim Peters50d8d372001-02-28 05:34:27 +00001063static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001064static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001065static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001066
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001067static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001068find_module(char *fullname, char *subname, PyObject *path, char *buf,
1069 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001070{
Fred Drake4c82b232000-06-30 16:18:57 +00001071 int i, npath;
1072 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001073 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001074 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001075 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001076 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001077#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001078 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001079#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001080 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1081 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1082 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001083 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001084#if defined(PYOS_OS2)
1085 size_t saved_len;
1086 size_t saved_namelen;
1087 char *saved_buf = NULL;
1088#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001089 if (p_loader != NULL)
1090 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001091
Just van Rossum52e14d62002-12-30 22:08:05 +00001092 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001093 PyErr_SetString(PyExc_OverflowError,
1094 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001095 return NULL;
1096 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001097 strcpy(name, subname);
1098
1099 /* sys.meta_path import hook */
1100 if (p_loader != NULL) {
1101 PyObject *meta_path;
1102
1103 meta_path = PySys_GetObject("meta_path");
1104 if (meta_path == NULL || !PyList_Check(meta_path)) {
1105 PyErr_SetString(PyExc_ImportError,
1106 "sys.meta_path must be a list of "
1107 "import hooks");
1108 return NULL;
1109 }
1110 Py_INCREF(meta_path); /* zap guard */
1111 npath = PyList_Size(meta_path);
1112 for (i = 0; i < npath; i++) {
1113 PyObject *loader;
1114 PyObject *hook = PyList_GetItem(meta_path, i);
1115 loader = PyObject_CallMethod(hook, "find_module",
1116 "sO", fullname,
1117 path != NULL ?
1118 path : Py_None);
1119 if (loader == NULL) {
1120 Py_DECREF(meta_path);
1121 return NULL; /* true error */
1122 }
1123 if (loader != Py_None) {
1124 /* a loader was found */
1125 *p_loader = loader;
1126 Py_DECREF(meta_path);
1127 return &importhookdescr;
1128 }
1129 Py_DECREF(loader);
1130 }
1131 Py_DECREF(meta_path);
1132 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001133
1134 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001135 /* The only type of submodule allowed inside a "frozen"
1136 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001137 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1138 PyErr_SetString(PyExc_ImportError,
1139 "full frozen module name too long");
1140 return NULL;
1141 }
1142 strcpy(buf, PyString_AsString(path));
1143 strcat(buf, ".");
1144 strcat(buf, name);
1145 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001146 if (find_frozen(name) != NULL) {
1147 strcpy(buf, name);
1148 return &fd_frozen;
1149 }
1150 PyErr_Format(PyExc_ImportError,
1151 "No frozen submodule named %.200s", name);
1152 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001153 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001154 if (path == NULL) {
1155 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001156 strcpy(buf, name);
1157 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001158 }
Greg Ward201baee2001-10-04 14:52:06 +00001159 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001160 strcpy(buf, name);
1161 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001162 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001163
Guido van Rossumac279101996-08-22 23:10:58 +00001164#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001165 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1166 if (fp != NULL) {
1167 *p_fp = fp;
1168 return fdp;
1169 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001170#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001171 path = PySys_GetObject("path");
1172 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173 if (path == NULL || !PyList_Check(path)) {
1174 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001175 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001176 return NULL;
1177 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001178
1179 path_hooks = PySys_GetObject("path_hooks");
1180 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1181 PyErr_SetString(PyExc_ImportError,
1182 "sys.path_hooks must be a list of "
1183 "import hooks");
1184 return NULL;
1185 }
1186 path_importer_cache = PySys_GetObject("path_importer_cache");
1187 if (path_importer_cache == NULL ||
1188 !PyDict_Check(path_importer_cache)) {
1189 PyErr_SetString(PyExc_ImportError,
1190 "sys.path_importer_cache must be a dict");
1191 return NULL;
1192 }
1193
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001195 namelen = strlen(name);
1196 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001197 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001198 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001199#ifdef Py_USING_UNICODE
1200 if (PyUnicode_Check(v)) {
1201 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1202 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1203 if (copy == NULL)
1204 return NULL;
1205 v = copy;
1206 }
1207 else
1208#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001211 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001212 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1213 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001215 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001217 if (strlen(buf) != len) {
1218 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001220 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001221
1222 /* sys.path_hooks import hook */
1223 if (p_loader != NULL) {
1224 PyObject *importer;
1225
1226 importer = get_path_importer(path_importer_cache,
1227 path_hooks, v);
1228 if (importer == NULL)
1229 return NULL;
1230 /* Note: importer is a borrowed reference */
1231 if (importer != Py_None) {
1232 PyObject *loader;
1233 loader = PyObject_CallMethod(importer,
1234 "find_module",
1235 "s", fullname);
1236 if (loader == NULL)
1237 return NULL; /* error */
1238 if (loader != Py_None) {
1239 /* a loader was found */
1240 *p_loader = loader;
1241 return &importhookdescr;
1242 }
1243 Py_DECREF(loader);
1244 }
1245 /* no hook was successful, use builtin import */
1246 }
1247
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001248 if (len > 0 && buf[len-1] != SEP
1249#ifdef ALTSEP
1250 && buf[len-1] != ALTSEP
1251#endif
1252 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001253 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001254 strcpy(buf+len, name);
1255 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001256
1257 /* Check for package import (buf holds a directory name,
1258 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001259#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001260 if (stat(buf, &statbuf) == 0 && /* it exists */
1261 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1262 find_init_module(buf) && /* it has __init__.py */
1263 case_ok(buf, len, namelen, name)) { /* and case matches */
1264 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001265 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001266 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001267#else
1268 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001269#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001270 if (isdir(buf) &&
1271 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001272 case_ok(buf, len, namelen, name)) {
1273 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001274 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001275 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001276#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001277#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001278#if defined(PYOS_OS2)
1279 /* take a snapshot of the module spec for restoration
1280 * after the 8 character DLL hackery
1281 */
1282 saved_buf = strdup(buf);
1283 saved_len = len;
1284 saved_namelen = namelen;
1285#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001286 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001287#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001288 /* OS/2 limits DLLs to 8 character names (w/o
1289 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001290 * so if the name is longer than that and its a
1291 * dynamically loaded module we're going to try,
1292 * truncate the name before trying
1293 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001294 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001295 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001296 const struct filedescr *scan;
1297 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001298 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001299 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001300 break;
1301 else
1302 scan++;
1303 }
1304 if (scan->suffix != NULL) {
1305 /* yes, so truncate the name */
1306 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001307 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001308 buf[len] = '\0';
1309 }
1310 }
1311#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001312 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001314 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001315 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001316 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001317 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001318 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001319 if (fp != NULL) {
1320 if (case_ok(buf, len, namelen, name))
1321 break;
1322 else { /* continue search */
1323 fclose(fp);
1324 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001325 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001326 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001327#if defined(PYOS_OS2)
1328 /* restore the saved snapshot */
1329 strcpy(buf, saved_buf);
1330 len = saved_len;
1331 namelen = saved_namelen;
1332#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001333 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001334#if defined(PYOS_OS2)
1335 /* don't need/want the module name snapshot anymore */
1336 if (saved_buf)
1337 {
1338 free(saved_buf);
1339 saved_buf = NULL;
1340 }
1341#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001342 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001343 if (fp != NULL)
1344 break;
1345 }
1346 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001347 PyErr_Format(PyExc_ImportError,
1348 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001349 return NULL;
1350 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001351 *p_fp = fp;
1352 return fdp;
1353}
1354
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001355/* Helpers for main.c
1356 * Find the source file corresponding to a named module
1357 */
1358struct filedescr *
1359_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1360 size_t buflen, FILE **p_fp, PyObject **p_loader)
1361{
1362 return find_module((char *) name, (char *) name, path,
1363 buf, buflen, p_fp, p_loader);
1364}
1365
1366PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1367{
1368 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1369}
1370
Tim Petersd1e87a82001-03-01 18:12:00 +00001371/* case_ok(char* buf, int len, int namelen, char* name)
1372 * The arguments here are tricky, best shown by example:
1373 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1374 * ^ ^ ^ ^
1375 * |--------------------- buf ---------------------|
1376 * |------------------- len ------------------|
1377 * |------ name -------|
1378 * |----- namelen -----|
1379 * buf is the full path, but len only counts up to (& exclusive of) the
1380 * extension. name is the module name, also exclusive of extension.
1381 *
1382 * We've already done a successful stat() or fopen() on buf, so know that
1383 * there's some match, possibly case-insensitive.
1384 *
Tim Peters50d8d372001-02-28 05:34:27 +00001385 * case_ok() is to return 1 if there's a case-sensitive match for
1386 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1387 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001388 *
Tim Peters50d8d372001-02-28 05:34:27 +00001389 * case_ok() is used to implement case-sensitive import semantics even
1390 * on platforms with case-insensitive filesystems. It's trivial to implement
1391 * for case-sensitive filesystems. It's pretty much a cross-platform
1392 * nightmare for systems with case-insensitive filesystems.
1393 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001394
Tim Peters50d8d372001-02-28 05:34:27 +00001395/* First we may need a pile of platform-specific header files; the sequence
1396 * of #if's here should match the sequence in the body of case_ok().
1397 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001398#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001399#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001400
Tim Peters50d8d372001-02-28 05:34:27 +00001401#elif defined(DJGPP)
1402#include <dir.h>
1403
Jason Tishler7961aa62005-05-20 00:56:54 +00001404#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001405#include <sys/types.h>
1406#include <dirent.h>
1407
Andrew MacIntyred9400542002-02-26 11:41:34 +00001408#elif defined(PYOS_OS2)
1409#define INCL_DOS
1410#define INCL_DOSERRORS
1411#define INCL_NOPMAPI
1412#include <os2.h>
1413
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001414#elif defined(RISCOS)
1415#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001416#endif
1417
Guido van Rossum0980bd91998-02-13 17:18:36 +00001418static int
Tim Peters50d8d372001-02-28 05:34:27 +00001419case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001420{
Tim Peters50d8d372001-02-28 05:34:27 +00001421/* Pick a platform-specific implementation; the sequence of #if's here should
1422 * match the sequence just above.
1423 */
1424
Jason Tishler7961aa62005-05-20 00:56:54 +00001425/* MS_WINDOWS */
1426#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001427 WIN32_FIND_DATA data;
1428 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001429
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001430 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001431 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001432
Guido van Rossum0980bd91998-02-13 17:18:36 +00001433 h = FindFirstFile(buf, &data);
1434 if (h == INVALID_HANDLE_VALUE) {
1435 PyErr_Format(PyExc_NameError,
1436 "Can't find file for module %.100s\n(filename %.300s)",
1437 name, buf);
1438 return 0;
1439 }
1440 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001441 return strncmp(data.cFileName, name, namelen) == 0;
1442
1443/* DJGPP */
1444#elif defined(DJGPP)
1445 struct ffblk ffblk;
1446 int done;
1447
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001448 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001449 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001450
1451 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1452 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001453 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001454 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001455 name, buf);
1456 return 0;
1457 }
Tim Peters50d8d372001-02-28 05:34:27 +00001458 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001459
Jason Tishler7961aa62005-05-20 00:56:54 +00001460/* new-fangled macintosh (macosx) or Cygwin */
1461#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001462 DIR *dirp;
1463 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001464 char dirname[MAXPATHLEN + 1];
1465 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001466
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001467 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001468 return 1;
1469
Tim Petersd1e87a82001-03-01 18:12:00 +00001470 /* Copy the dir component into dirname; substitute "." if empty */
1471 if (dirlen <= 0) {
1472 dirname[0] = '.';
1473 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001474 }
1475 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001476 assert(dirlen <= MAXPATHLEN);
1477 memcpy(dirname, buf, dirlen);
1478 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001479 }
1480 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001481 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001482 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001483 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001484 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001485 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001486#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001487 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001488#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001489 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001490#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001491 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001492 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001493 (void)closedir(dirp);
1494 return 1; /* Found */
1495 }
1496 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001497 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001498 }
Tim Peters430f5d42001-03-01 01:30:56 +00001499 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001500
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001501/* RISC OS */
1502#elif defined(RISCOS)
1503 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1504 char buf2[MAXPATHLEN+2];
1505 char *nameWithExt = buf+len-namelen;
1506 int canonlen;
1507 os_error *e;
1508
1509 if (Py_GETENV("PYTHONCASEOK") != NULL)
1510 return 1;
1511
1512 /* workaround:
1513 append wildcard, otherwise case of filename wouldn't be touched */
1514 strcpy(buf2, buf);
1515 strcat(buf2, "*");
1516
1517 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1518 canonlen = MAXPATHLEN+1-canonlen;
1519 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1520 return 0;
1521 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1522 return 1; /* match */
1523
1524 return 0;
1525
Andrew MacIntyred9400542002-02-26 11:41:34 +00001526/* OS/2 */
1527#elif defined(PYOS_OS2)
1528 HDIR hdir = 1;
1529 ULONG srchcnt = 1;
1530 FILEFINDBUF3 ffbuf;
1531 APIRET rc;
1532
1533 if (getenv("PYTHONCASEOK") != NULL)
1534 return 1;
1535
1536 rc = DosFindFirst(buf,
1537 &hdir,
1538 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1539 &ffbuf, sizeof(ffbuf),
1540 &srchcnt,
1541 FIL_STANDARD);
1542 if (rc != NO_ERROR)
1543 return 0;
1544 return strncmp(ffbuf.achName, name, namelen) == 0;
1545
Tim Peters50d8d372001-02-28 05:34:27 +00001546/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1547#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001548 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001549
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001550#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001551}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001552
Guido van Rossum0980bd91998-02-13 17:18:36 +00001553
Guido van Rossum197346f1997-10-31 18:38:52 +00001554#ifdef HAVE_STAT
1555/* Helper to look for __init__.py or __init__.py[co] in potential package */
1556static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001557find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001558{
Tim Peters0f9431f2001-07-05 03:47:53 +00001559 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001560 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001561 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001562 struct stat statbuf;
1563
Tim Peters0f9431f2001-07-05 03:47:53 +00001564/* For calling case_ok(buf, len, namelen, name):
1565 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1566 * ^ ^ ^ ^
1567 * |--------------------- buf ---------------------|
1568 * |------------------- len ------------------|
1569 * |------ name -------|
1570 * |----- namelen -----|
1571 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001572 if (save_len + 13 >= MAXPATHLEN)
1573 return 0;
1574 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001575 pname = buf + i;
1576 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001577 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001578 if (case_ok(buf,
1579 save_len + 9, /* len("/__init__") */
1580 8, /* len("__init__") */
1581 pname)) {
1582 buf[save_len] = '\0';
1583 return 1;
1584 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001585 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001586 i += strlen(pname);
1587 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001588 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001589 if (case_ok(buf,
1590 save_len + 9, /* len("/__init__") */
1591 8, /* len("__init__") */
1592 pname)) {
1593 buf[save_len] = '\0';
1594 return 1;
1595 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001596 }
1597 buf[save_len] = '\0';
1598 return 0;
1599}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001600
1601#else
1602
1603#ifdef RISCOS
1604static int
1605find_init_module(buf)
1606 char *buf;
1607{
1608 int save_len = strlen(buf);
1609 int i = save_len;
1610
1611 if (save_len + 13 >= MAXPATHLEN)
1612 return 0;
1613 buf[i++] = SEP;
1614 strcpy(buf+i, "__init__/py");
1615 if (isfile(buf)) {
1616 buf[save_len] = '\0';
1617 return 1;
1618 }
1619
1620 if (Py_OptimizeFlag)
1621 strcpy(buf+i, "o");
1622 else
1623 strcpy(buf+i, "c");
1624 if (isfile(buf)) {
1625 buf[save_len] = '\0';
1626 return 1;
1627 }
1628 buf[save_len] = '\0';
1629 return 0;
1630}
1631#endif /*RISCOS*/
1632
Guido van Rossum197346f1997-10-31 18:38:52 +00001633#endif /* HAVE_STAT */
1634
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001635
Tim Petersdbd9ba62000-07-09 03:09:57 +00001636static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001637
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001638/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001639 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001640
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001642load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001643{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001644 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001646 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001647
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001648 /* First check that there's an open file (if we need one) */
1649 switch (type) {
1650 case PY_SOURCE:
1651 case PY_COMPILED:
1652 if (fp == NULL) {
1653 PyErr_Format(PyExc_ValueError,
1654 "file object required for import (type code %d)",
1655 type);
1656 return NULL;
1657 }
1658 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001659
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001660 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001661
1662 case PY_SOURCE:
1663 m = load_source_module(name, buf, fp);
1664 break;
1665
1666 case PY_COMPILED:
1667 m = load_compiled_module(name, buf, fp);
1668 break;
1669
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001670#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001671 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001672 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001673 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001674#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001675
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001676 case PKG_DIRECTORY:
1677 m = load_package(name, buf);
1678 break;
1679
1680 case C_BUILTIN:
1681 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001682 if (buf != NULL && buf[0] != '\0')
1683 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001684 if (type == C_BUILTIN)
1685 err = init_builtin(name);
1686 else
1687 err = PyImport_ImportFrozenModule(name);
1688 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001689 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001690 if (err == 0) {
1691 PyErr_Format(PyExc_ImportError,
1692 "Purported %s module %.200s not found",
1693 type == C_BUILTIN ?
1694 "builtin" : "frozen",
1695 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001696 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001697 }
1698 modules = PyImport_GetModuleDict();
1699 m = PyDict_GetItemString(modules, name);
1700 if (m == NULL) {
1701 PyErr_Format(
1702 PyExc_ImportError,
1703 "%s module %.200s not properly initialized",
1704 type == C_BUILTIN ?
1705 "builtin" : "frozen",
1706 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001707 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001708 }
1709 Py_INCREF(m);
1710 break;
1711
Just van Rossum52e14d62002-12-30 22:08:05 +00001712 case IMP_HOOK: {
1713 if (loader == NULL) {
1714 PyErr_SetString(PyExc_ImportError,
1715 "import hook without loader");
1716 return NULL;
1717 }
1718 m = PyObject_CallMethod(loader, "load_module", "s", name);
1719 break;
1720 }
1721
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001723 PyErr_Format(PyExc_ImportError,
1724 "Don't know how to import %.200s (type code %d)",
1725 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001726 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001727
1728 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729
1730 return m;
1731}
1732
1733
1734/* Initialize a built-in module.
1735 Return 1 for succes, 0 if the module is not found, and -1 with
1736 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001737
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001738static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001739init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001740{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001741 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001742
Greg Ward201baee2001-10-04 14:52:06 +00001743 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001744 return 1;
1745
Guido van Rossum771c6c81997-10-31 18:37:24 +00001746 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001747 if (strcmp(name, p->name) == 0) {
1748 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001749 PyErr_Format(PyExc_ImportError,
1750 "Cannot re-init internal module %.200s",
1751 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001752 return -1;
1753 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001755 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001756 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001758 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001759 if (_PyImport_FixupExtension(name, name) == NULL)
1760 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001761 return 1;
1762 }
1763 }
1764 return 0;
1765}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001766
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001767
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001768/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001770static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001771find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001772{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001773 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001774
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001776 if (p->name == NULL)
1777 return NULL;
1778 if (strcmp(p->name, name) == 0)
1779 break;
1780 }
1781 return p;
1782}
1783
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001785get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001786{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001787 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001788 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001789
1790 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001791 PyErr_Format(PyExc_ImportError,
1792 "No such frozen object named %.200s",
1793 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001794 return NULL;
1795 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001796 if (p->code == NULL) {
1797 PyErr_Format(PyExc_ImportError,
1798 "Excluded frozen object named %.200s",
1799 name);
1800 return NULL;
1801 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001802 size = p->size;
1803 if (size < 0)
1804 size = -size;
1805 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001806}
1807
1808/* Initialize a frozen module.
1809 Return 1 for succes, 0 if the module is not found, and -1 with
1810 an exception set if the initialization failed.
1811 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001812
1813int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001814PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001815{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001816 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 PyObject *co;
1818 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001819 int ispackage;
1820 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001821
1822 if (p == NULL)
1823 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001824 if (p->code == NULL) {
1825 PyErr_Format(PyExc_ImportError,
1826 "Excluded frozen object named %.200s",
1827 name);
1828 return -1;
1829 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001830 size = p->size;
1831 ispackage = (size < 0);
1832 if (ispackage)
1833 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001834 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001835 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001836 name, ispackage ? " package" : "");
1837 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001838 if (co == NULL)
1839 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 if (!PyCode_Check(co)) {
1841 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001842 PyErr_Format(PyExc_TypeError,
1843 "frozen object %.200s is not a code object",
1844 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001845 return -1;
1846 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001847 if (ispackage) {
1848 /* Set __path__ to the package name */
1849 PyObject *d, *s;
1850 int err;
1851 m = PyImport_AddModule(name);
1852 if (m == NULL)
1853 return -1;
1854 d = PyModule_GetDict(m);
1855 s = PyString_InternFromString(name);
1856 if (s == NULL)
1857 return -1;
1858 err = PyDict_SetItemString(d, "__path__", s);
1859 Py_DECREF(s);
1860 if (err != 0)
1861 return err;
1862 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001863 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001865 if (m == NULL)
1866 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001868 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001869}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001870
1871
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001872/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001873 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001874
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001876PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001877{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001878 PyObject *pname;
1879 PyObject *result;
1880
1881 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001882 if (pname == NULL)
1883 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001884 result = PyImport_Import(pname);
1885 Py_DECREF(pname);
1886 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001887}
1888
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001889/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001890static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1891static PyObject *load_next(PyObject *mod, PyObject *altmod,
1892 char **p_name, char *buf, int *p_buflen);
1893static int mark_miss(char *name);
1894static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1895 char *buf, int buflen, int recursive);
1896static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001897
1898/* The Magnum Opus of dotted-name import :-) */
1899
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001900static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001901import_module_ex(char *name, PyObject *globals, PyObject *locals,
1902 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001903{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001904 char buf[MAXPATHLEN+1];
1905 int buflen = 0;
1906 PyObject *parent, *head, *next, *tail;
1907
1908 parent = get_parent(globals, buf, &buflen);
1909 if (parent == NULL)
1910 return NULL;
1911
1912 head = load_next(parent, Py_None, &name, buf, &buflen);
1913 if (head == NULL)
1914 return NULL;
1915
1916 tail = head;
1917 Py_INCREF(tail);
1918 while (name) {
1919 next = load_next(tail, tail, &name, buf, &buflen);
1920 Py_DECREF(tail);
1921 if (next == NULL) {
1922 Py_DECREF(head);
1923 return NULL;
1924 }
1925 tail = next;
1926 }
1927
1928 if (fromlist != NULL) {
1929 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1930 fromlist = NULL;
1931 }
1932
1933 if (fromlist == NULL) {
1934 Py_DECREF(tail);
1935 return head;
1936 }
1937
1938 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001939 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001940 Py_DECREF(tail);
1941 return NULL;
1942 }
1943
1944 return tail;
1945}
1946
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001947PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001948PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1949 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001950{
1951 PyObject *result;
1952 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001953 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001954 if (unlock_import() < 0) {
1955 Py_XDECREF(result);
1956 PyErr_SetString(PyExc_RuntimeError,
1957 "not holding the import lock");
1958 return NULL;
1959 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001960 return result;
1961}
1962
Fred Drake87590902004-05-28 20:21:36 +00001963/* Return the package that an import is being performed in. If globals comes
1964 from the module foo.bar.bat (not itself a package), this returns the
1965 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1966 the package's entry in sys.modules is returned.
1967
1968 The *name* of the returned package is returned in buf, with the length of
1969 the name in *p_buflen.
1970
1971 If globals doesn't come from a package or a module in a package, or a
1972 corresponding entry is not found in sys.modules, Py_None is returned.
1973*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001974static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001975get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001976{
1977 static PyObject *namestr = NULL;
1978 static PyObject *pathstr = NULL;
1979 PyObject *modname, *modpath, *modules, *parent;
1980
1981 if (globals == NULL || !PyDict_Check(globals))
1982 return Py_None;
1983
1984 if (namestr == NULL) {
1985 namestr = PyString_InternFromString("__name__");
1986 if (namestr == NULL)
1987 return NULL;
1988 }
1989 if (pathstr == NULL) {
1990 pathstr = PyString_InternFromString("__path__");
1991 if (pathstr == NULL)
1992 return NULL;
1993 }
1994
1995 *buf = '\0';
1996 *p_buflen = 0;
1997 modname = PyDict_GetItem(globals, namestr);
1998 if (modname == NULL || !PyString_Check(modname))
1999 return Py_None;
2000
2001 modpath = PyDict_GetItem(globals, pathstr);
2002 if (modpath != NULL) {
2003 int len = PyString_GET_SIZE(modname);
2004 if (len > MAXPATHLEN) {
2005 PyErr_SetString(PyExc_ValueError,
2006 "Module name too long");
2007 return NULL;
2008 }
2009 strcpy(buf, PyString_AS_STRING(modname));
2010 *p_buflen = len;
2011 }
2012 else {
2013 char *start = PyString_AS_STRING(modname);
2014 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002015 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002016 if (lastdot == NULL)
2017 return Py_None;
2018 len = lastdot - start;
2019 if (len >= MAXPATHLEN) {
2020 PyErr_SetString(PyExc_ValueError,
2021 "Module name too long");
2022 return NULL;
2023 }
2024 strncpy(buf, start, len);
2025 buf[len] = '\0';
2026 *p_buflen = len;
2027 }
2028
2029 modules = PyImport_GetModuleDict();
2030 parent = PyDict_GetItemString(modules, buf);
2031 if (parent == NULL)
2032 parent = Py_None;
2033 return parent;
2034 /* We expect, but can't guarantee, if parent != None, that:
2035 - parent.__name__ == buf
2036 - parent.__dict__ is globals
2037 If this is violated... Who cares? */
2038}
2039
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002040/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002041static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002042load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2043 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002044{
2045 char *name = *p_name;
2046 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002047 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002048 char *p;
2049 PyObject *result;
2050
2051 if (dot == NULL) {
2052 *p_name = NULL;
2053 len = strlen(name);
2054 }
2055 else {
2056 *p_name = dot+1;
2057 len = dot-name;
2058 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002059 if (len == 0) {
2060 PyErr_SetString(PyExc_ValueError,
2061 "Empty module name");
2062 return NULL;
2063 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002064
2065 p = buf + *p_buflen;
2066 if (p != buf)
2067 *p++ = '.';
2068 if (p+len-buf >= MAXPATHLEN) {
2069 PyErr_SetString(PyExc_ValueError,
2070 "Module name too long");
2071 return NULL;
2072 }
2073 strncpy(p, name, len);
2074 p[len] = '\0';
2075 *p_buflen = p+len-buf;
2076
2077 result = import_submodule(mod, p, buf);
2078 if (result == Py_None && altmod != mod) {
2079 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002080 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002081 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002082 if (result != NULL && result != Py_None) {
2083 if (mark_miss(buf) != 0) {
2084 Py_DECREF(result);
2085 return NULL;
2086 }
2087 strncpy(buf, name, len);
2088 buf[len] = '\0';
2089 *p_buflen = len;
2090 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002091 }
2092 if (result == NULL)
2093 return NULL;
2094
2095 if (result == Py_None) {
2096 Py_DECREF(result);
2097 PyErr_Format(PyExc_ImportError,
2098 "No module named %.200s", name);
2099 return NULL;
2100 }
2101
2102 return result;
2103}
2104
2105static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002106mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002107{
2108 PyObject *modules = PyImport_GetModuleDict();
2109 return PyDict_SetItemString(modules, name, Py_None);
2110}
2111
2112static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002113ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2114 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002115{
2116 int i;
2117
2118 if (!PyObject_HasAttrString(mod, "__path__"))
2119 return 1;
2120
2121 for (i = 0; ; i++) {
2122 PyObject *item = PySequence_GetItem(fromlist, i);
2123 int hasit;
2124 if (item == NULL) {
2125 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2126 PyErr_Clear();
2127 return 1;
2128 }
2129 return 0;
2130 }
2131 if (!PyString_Check(item)) {
2132 PyErr_SetString(PyExc_TypeError,
2133 "Item in ``from list'' not a string");
2134 Py_DECREF(item);
2135 return 0;
2136 }
2137 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002138 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002139 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002140 /* See if the package defines __all__ */
2141 if (recursive)
2142 continue; /* Avoid endless recursion */
2143 all = PyObject_GetAttrString(mod, "__all__");
2144 if (all == NULL)
2145 PyErr_Clear();
2146 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002147 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002148 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002149 if (!ret)
2150 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002151 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002152 continue;
2153 }
2154 hasit = PyObject_HasAttr(mod, item);
2155 if (!hasit) {
2156 char *subname = PyString_AS_STRING(item);
2157 PyObject *submod;
2158 char *p;
2159 if (buflen + strlen(subname) >= MAXPATHLEN) {
2160 PyErr_SetString(PyExc_ValueError,
2161 "Module name too long");
2162 Py_DECREF(item);
2163 return 0;
2164 }
2165 p = buf + buflen;
2166 *p++ = '.';
2167 strcpy(p, subname);
2168 submod = import_submodule(mod, subname, buf);
2169 Py_XDECREF(submod);
2170 if (submod == NULL) {
2171 Py_DECREF(item);
2172 return 0;
2173 }
2174 }
2175 Py_DECREF(item);
2176 }
2177
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002178 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002179}
2180
Neil Schemenauer00b09662003-06-16 21:03:07 +00002181static int
2182add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2183 PyObject *modules)
2184{
2185 if (mod == Py_None)
2186 return 1;
2187 /* Irrespective of the success of this load, make a
2188 reference to it in the parent package module. A copy gets
2189 saved in the modules dictionary under the full name, so get a
2190 reference from there, if need be. (The exception is when the
2191 load failed with a SyntaxError -- then there's no trace in
2192 sys.modules. In that case, of course, do nothing extra.) */
2193 if (submod == NULL) {
2194 submod = PyDict_GetItemString(modules, fullname);
2195 if (submod == NULL)
2196 return 1;
2197 }
2198 if (PyModule_Check(mod)) {
2199 /* We can't use setattr here since it can give a
2200 * spurious warning if the submodule name shadows a
2201 * builtin name */
2202 PyObject *dict = PyModule_GetDict(mod);
2203 if (!dict)
2204 return 0;
2205 if (PyDict_SetItemString(dict, subname, submod) < 0)
2206 return 0;
2207 }
2208 else {
2209 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2210 return 0;
2211 }
2212 return 1;
2213}
2214
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002215static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002216import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002217{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002218 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002219 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002220
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002221 /* Require:
2222 if mod == None: subname == fullname
2223 else: mod.__name__ + "." + subname == fullname
2224 */
2225
Tim Peters50d8d372001-02-28 05:34:27 +00002226 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002227 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002228 }
2229 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002230 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002231 char buf[MAXPATHLEN+1];
2232 struct filedescr *fdp;
2233 FILE *fp = NULL;
2234
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002235 if (mod == Py_None)
2236 path = NULL;
2237 else {
2238 path = PyObject_GetAttrString(mod, "__path__");
2239 if (path == NULL) {
2240 PyErr_Clear();
2241 Py_INCREF(Py_None);
2242 return Py_None;
2243 }
2244 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002245
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002246 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002247 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2248 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002249 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002250 if (fdp == NULL) {
2251 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2252 return NULL;
2253 PyErr_Clear();
2254 Py_INCREF(Py_None);
2255 return Py_None;
2256 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002257 m = load_module(fullname, fp, buf, fdp->type, loader);
2258 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002259 if (fp)
2260 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002261 if (!add_submodule(mod, m, fullname, subname, modules)) {
2262 Py_XDECREF(m);
2263 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002264 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002265 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002266
2267 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002268}
2269
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002270
2271/* Re-import a module of any kind and return its module object, WITH
2272 INCREMENTED REFERENCE COUNT */
2273
Guido van Rossum79f25d91997-04-29 20:08:16 +00002274PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002275PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002276{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002277 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002278 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002279 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002280 char buf[MAXPATHLEN+1];
2281 struct filedescr *fdp;
2282 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002283 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002284
Guido van Rossum79f25d91997-04-29 20:08:16 +00002285 if (m == NULL || !PyModule_Check(m)) {
2286 PyErr_SetString(PyExc_TypeError,
2287 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002288 return NULL;
2289 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002290 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002291 if (name == NULL)
2292 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002293 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002294 PyErr_Format(PyExc_ImportError,
2295 "reload(): module %.200s not in sys.modules",
2296 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002297 return NULL;
2298 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002299 subname = strrchr(name, '.');
2300 if (subname == NULL)
2301 subname = name;
2302 else {
2303 PyObject *parentname, *parent;
2304 parentname = PyString_FromStringAndSize(name, (subname-name));
2305 if (parentname == NULL)
2306 return NULL;
2307 parent = PyDict_GetItem(modules, parentname);
2308 if (parent == NULL) {
2309 PyErr_Format(PyExc_ImportError,
2310 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002311 PyString_AS_STRING(parentname));
2312 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002313 return NULL;
2314 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002315 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002316 subname++;
2317 path = PyObject_GetAttrString(parent, "__path__");
2318 if (path == NULL)
2319 PyErr_Clear();
2320 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002321 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002322 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002323 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002324
2325 if (fdp == NULL) {
2326 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002327 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002328 }
2329
2330 newm = load_module(name, fp, buf, fdp->type, loader);
2331 Py_XDECREF(loader);
2332
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002333 if (fp)
2334 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002335 if (newm == NULL) {
2336 /* load_module probably removed name from modules because of
2337 * the error. Put back the original module object. We're
2338 * going to return NULL in this case regardless of whether
2339 * replacing name succeeds, so the return value is ignored.
2340 */
2341 PyDict_SetItemString(modules, name, m);
2342 }
2343 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002344}
2345
2346
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002347/* Higher-level import emulator which emulates the "import" statement
2348 more accurately -- it invokes the __import__() function from the
2349 builtins of the current globals. This means that the import is
2350 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002351 environment, e.g. by "rexec".
2352 A dummy list ["__doc__"] is passed as the 4th argument so that
2353 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2354 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002355
2356PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002357PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002358{
2359 static PyObject *silly_list = NULL;
2360 static PyObject *builtins_str = NULL;
2361 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002362 PyObject *globals = NULL;
2363 PyObject *import = NULL;
2364 PyObject *builtins = NULL;
2365 PyObject *r = NULL;
2366
2367 /* Initialize constant string objects */
2368 if (silly_list == NULL) {
2369 import_str = PyString_InternFromString("__import__");
2370 if (import_str == NULL)
2371 return NULL;
2372 builtins_str = PyString_InternFromString("__builtins__");
2373 if (builtins_str == NULL)
2374 return NULL;
2375 silly_list = Py_BuildValue("[s]", "__doc__");
2376 if (silly_list == NULL)
2377 return NULL;
2378 }
2379
2380 /* Get the builtins from current globals */
2381 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002382 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002383 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002384 builtins = PyObject_GetItem(globals, builtins_str);
2385 if (builtins == NULL)
2386 goto err;
2387 }
2388 else {
2389 /* No globals -- use standard builtins, and fake globals */
2390 PyErr_Clear();
2391
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002392 builtins = PyImport_ImportModuleEx("__builtin__",
2393 NULL, NULL, NULL);
2394 if (builtins == NULL)
2395 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002396 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2397 if (globals == NULL)
2398 goto err;
2399 }
2400
2401 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002402 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002403 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002404 if (import == NULL)
2405 PyErr_SetObject(PyExc_KeyError, import_str);
2406 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002407 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002408 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002409 if (import == NULL)
2410 goto err;
2411
2412 /* Call the _import__ function with the proper argument list */
2413 r = PyObject_CallFunction(import, "OOOO",
2414 module_name, globals, globals, silly_list);
2415
2416 err:
2417 Py_XDECREF(globals);
2418 Py_XDECREF(builtins);
2419 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002420
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002421 return r;
2422}
2423
2424
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002425/* Module 'imp' provides Python access to the primitives used for
2426 importing modules.
2427*/
2428
Guido van Rossum79f25d91997-04-29 20:08:16 +00002429static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002430imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002431{
2432 char buf[4];
2433
Guido van Rossum96774c12000-05-01 20:19:08 +00002434 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2435 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2436 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2437 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002440}
2441
Guido van Rossum79f25d91997-04-29 20:08:16 +00002442static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002443imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002444{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002445 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002446 struct filedescr *fdp;
2447
Guido van Rossum79f25d91997-04-29 20:08:16 +00002448 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002449 if (list == NULL)
2450 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002451 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2452 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002453 fdp->suffix, fdp->mode, fdp->type);
2454 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002455 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002456 return NULL;
2457 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002458 if (PyList_Append(list, item) < 0) {
2459 Py_DECREF(list);
2460 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002461 return NULL;
2462 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002464 }
2465 return list;
2466}
2467
Guido van Rossum79f25d91997-04-29 20:08:16 +00002468static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002469call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002470{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002471 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002472 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002473 struct filedescr *fdp;
2474 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002475 FILE *fp = NULL;
2476
2477 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002478 if (path == Py_None)
2479 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002480 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002481 if (fdp == NULL)
2482 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002483 if (fp != NULL) {
2484 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2485 if (fob == NULL) {
2486 fclose(fp);
2487 return NULL;
2488 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002489 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002490 else {
2491 fob = Py_None;
2492 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002493 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002494 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002495 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002496 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002497 return ret;
2498}
2499
Guido van Rossum79f25d91997-04-29 20:08:16 +00002500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002501imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002502{
2503 char *name;
2504 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002505 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002506 return NULL;
2507 return call_find_module(name, path);
2508}
2509
2510static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002511imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002512{
2513 char *name;
2514 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002515 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002516 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002517 return NULL;
2518 ret = init_builtin(name);
2519 if (ret < 0)
2520 return NULL;
2521 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002522 Py_INCREF(Py_None);
2523 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002524 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002525 m = PyImport_AddModule(name);
2526 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002527 return m;
2528}
2529
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002531imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002532{
2533 char *name;
2534 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002535 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002536 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002537 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002538 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539 if (ret < 0)
2540 return NULL;
2541 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002542 Py_INCREF(Py_None);
2543 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002544 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002545 m = PyImport_AddModule(name);
2546 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002547 return m;
2548}
2549
Guido van Rossum79f25d91997-04-29 20:08:16 +00002550static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002551imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002552{
2553 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002554
Guido van Rossum43713e52000-02-29 13:59:29 +00002555 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002556 return NULL;
2557 return get_frozen_object(name);
2558}
2559
Guido van Rossum79f25d91997-04-29 20:08:16 +00002560static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002561imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002562{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002564 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002565 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002566 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002567}
2568
Guido van Rossum79f25d91997-04-29 20:08:16 +00002569static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002570imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002571{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002573 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002574 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002575 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002576 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002577 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002578}
2579
2580static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002581get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002582{
2583 FILE *fp;
2584 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002585 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002586 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002587 fp = fopen(pathname, mode);
2588 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002589 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002590 }
2591 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002592 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002593 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 PyErr_SetString(PyExc_ValueError,
2595 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002596 }
2597 return fp;
2598}
2599
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002601imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602{
2603 char *name;
2604 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605 PyObject *fob = NULL;
2606 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002607 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002608 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002609 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002610 return NULL;
2611 fp = get_file(pathname, fob, "rb");
2612 if (fp == NULL)
2613 return NULL;
2614 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002615 if (fob == NULL)
2616 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002617 return m;
2618}
2619
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002620#ifdef HAVE_DYNAMIC_LOADING
2621
Guido van Rossum79f25d91997-04-29 20:08:16 +00002622static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002623imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002624{
2625 char *name;
2626 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627 PyObject *fob = NULL;
2628 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002629 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002630 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002631 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002632 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002633 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002634 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002635 if (fp == NULL)
2636 return NULL;
2637 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002638 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002639 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002640}
2641
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002642#endif /* HAVE_DYNAMIC_LOADING */
2643
Guido van Rossum79f25d91997-04-29 20:08:16 +00002644static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002645imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002646{
2647 char *name;
2648 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649 PyObject *fob = NULL;
2650 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002651 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002652 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002654 return NULL;
2655 fp = get_file(pathname, fob, "r");
2656 if (fp == NULL)
2657 return NULL;
2658 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002659 if (fob == NULL)
2660 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002661 return m;
2662}
2663
Guido van Rossum79f25d91997-04-29 20:08:16 +00002664static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002665imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002666{
2667 char *name;
2668 PyObject *fob;
2669 char *pathname;
2670 char *suffix; /* Unused */
2671 char *mode;
2672 int type;
2673 FILE *fp;
2674
Guido van Rossum43713e52000-02-29 13:59:29 +00002675 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002676 &name, &fob, &pathname,
2677 &suffix, &mode, &type))
2678 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002679 if (*mode) {
2680 /* Mode must start with 'r' or 'U' and must not contain '+'.
2681 Implicit in this test is the assumption that the mode
2682 may contain other modifiers like 'b' or 't'. */
2683
2684 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002685 PyErr_Format(PyExc_ValueError,
2686 "invalid file open mode %.200s", mode);
2687 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002688 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002689 }
2690 if (fob == Py_None)
2691 fp = NULL;
2692 else {
2693 if (!PyFile_Check(fob)) {
2694 PyErr_SetString(PyExc_ValueError,
2695 "load_module arg#2 should be a file or None");
2696 return NULL;
2697 }
2698 fp = get_file(pathname, fob, mode);
2699 if (fp == NULL)
2700 return NULL;
2701 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002702 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002703}
2704
2705static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002706imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002707{
2708 char *name;
2709 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002710 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002711 return NULL;
2712 return load_package(name, pathname);
2713}
2714
2715static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002716imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002717{
2718 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002719 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002720 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002721 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002722}
2723
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002724/* Doc strings */
2725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002726PyDoc_STRVAR(doc_imp,
2727"This module provides the components needed to build your own\n\
2728__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002730PyDoc_STRVAR(doc_find_module,
2731"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002732Search for a module. If path is omitted or None, search for a\n\
2733built-in, frozen or special module and continue search in sys.path.\n\
2734The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002735package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002737PyDoc_STRVAR(doc_load_module,
2738"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002739Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002740The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002742PyDoc_STRVAR(doc_get_magic,
2743"get_magic() -> string\n\
2744Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(doc_get_suffixes,
2747"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002748Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002749that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002751PyDoc_STRVAR(doc_new_module,
2752"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002753Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002754The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002756PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002757"lock_held() -> boolean\n\
2758Return True if the import lock is currently held, else False.\n\
2759On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002760
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002761PyDoc_STRVAR(doc_acquire_lock,
2762"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002763Acquires the interpreter's import lock for the current thread.\n\
2764This lock should be used by import hooks to ensure thread-safety\n\
2765when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002766On platforms without threads, this function does nothing.");
2767
2768PyDoc_STRVAR(doc_release_lock,
2769"release_lock() -> None\n\
2770Release the interpreter's import lock.\n\
2771On platforms without threads, this function does nothing.");
2772
Guido van Rossum79f25d91997-04-29 20:08:16 +00002773static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002774 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2775 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2776 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2777 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2778 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2779 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2780 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2781 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002782 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002783 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2784 {"init_builtin", imp_init_builtin, METH_VARARGS},
2785 {"init_frozen", imp_init_frozen, METH_VARARGS},
2786 {"is_builtin", imp_is_builtin, METH_VARARGS},
2787 {"is_frozen", imp_is_frozen, METH_VARARGS},
2788 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002789#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002790 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002791#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002792 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002793 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002794 {NULL, NULL} /* sentinel */
2795};
2796
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002797static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002798setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002799{
2800 PyObject *v;
2801 int err;
2802
2803 v = PyInt_FromLong((long)value);
2804 err = PyDict_SetItemString(d, name, v);
2805 Py_XDECREF(v);
2806 return err;
2807}
2808
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002809PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002810initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002811{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002812 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002813
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002814 m = Py_InitModule4("imp", imp_methods, doc_imp,
2815 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002816 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002817
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002818 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2819 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2820 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2821 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2822 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2823 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2824 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2825 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002826 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002827 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002828
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002829 failure:
2830 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002831}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002832
2833
Guido van Rossumb18618d2000-05-03 23:44:39 +00002834/* API for embedding applications that want to add their own entries
2835 to the table of built-in modules. This should normally be called
2836 *before* Py_Initialize(). When the table resize fails, -1 is
2837 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002838
2839 After a similar function by Just van Rossum. */
2840
2841int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002842PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002843{
2844 static struct _inittab *our_copy = NULL;
2845 struct _inittab *p;
2846 int i, n;
2847
2848 /* Count the number of entries in both tables */
2849 for (n = 0; newtab[n].name != NULL; n++)
2850 ;
2851 if (n == 0)
2852 return 0; /* Nothing to do */
2853 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2854 ;
2855
2856 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002857 p = our_copy;
2858 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002859 if (p == NULL)
2860 return -1;
2861
2862 /* Copy the tables into the new memory */
2863 if (our_copy != PyImport_Inittab)
2864 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2865 PyImport_Inittab = our_copy = p;
2866 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2867
2868 return 0;
2869}
2870
2871/* Shorthand to add a single entry given a name and a function */
2872
2873int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002874PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002875{
2876 struct _inittab newtab[2];
2877
2878 memset(newtab, '\0', sizeof newtab);
2879
2880 newtab[0].name = name;
2881 newtab[0].initfunc = initfunc;
2882
2883 return PyImport_ExtendInittab(newtab);
2884}