blob: 35de13e5f9b8bb605b986155f0db36aee49157c7 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
10#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000011#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000014
Guido van Rossum55a83382000-09-20 20:31:38 +000015#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
18
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000019extern time_t PyOS_GetLastModificationTime(char *, FILE *);
20 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000021
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000022/* Magic word to reject .pyc files generated by other Python versions.
23 It should change for each incompatible change to the bytecode.
24
25 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000026 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000027 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000028
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000029 The magic numbers must be spaced apart atleast 2 values, as the
30 -U interpeter flag will cause MAGIC+1 being used. They have been
31 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000032
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000033 There were a variety of old schemes for setting the magic number.
34 The current working scheme is to increment the previous value by
35 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000036
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037 Known values:
38 Python 1.5: 20121
39 Python 1.5.1: 20121
40 Python 1.5.2: 20121
41 Python 2.0: 50823
42 Python 2.0.1: 50823
43 Python 2.1: 60202
44 Python 2.1.1: 60202
45 Python 2.1.2: 60202
46 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000047 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000048 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000049 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000050 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000051 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000052 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000053 Python 2.5a0: 62071
Tim Peters36515e22001-11-18 04:06:29 +000054*/
Michael W. Hudsondf888462005-06-03 14:41:55 +000055#define MAGIC (62071 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000056
Guido van Rossum96774c12000-05-01 20:19:08 +000057/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000058 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000059 compiler works which are enabled by command line switches. */
60static long pyc_magic = MAGIC;
61
Guido van Rossum25ce5661997-08-02 03:10:38 +000062/* See _PyImport_FixupExtension() below */
63static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000064
Guido van Rossum771c6c81997-10-31 18:37:24 +000065/* This table is defined in config.c: */
66extern struct _inittab _PyImport_Inittab[];
67
68struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000069
Guido van Rossumed1170e1999-12-20 21:23:41 +000070/* these tables define the module suffixes that Python recognizes */
71struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000072
73#ifdef RISCOS
74static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000075 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000076 {"/pyc", "rb", PY_COMPILED},
77 {0, 0}
78};
79#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000080static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000081 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000082#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000083 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000084#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000085 {".pyc", "rb", PY_COMPILED},
86 {0, 0}
87};
Guido van Rossum48a680c2001-03-02 06:34:14 +000088#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000089
Guido van Rossum1ae940a1995-01-02 19:04:15 +000090/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091
92void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000093_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Guido van Rossumed1170e1999-12-20 21:23:41 +000095 const struct filedescr *scan;
96 struct filedescr *filetab;
97 int countD = 0;
98 int countS = 0;
99
100 /* prepare _PyImport_Filetab: copy entries from
101 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
102 */
103 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
104 ++countD;
105 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
106 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000108 memcpy(filetab, _PyImport_DynLoadFiletab,
109 countD * sizeof(struct filedescr));
110 memcpy(filetab + countD, _PyImport_StandardFiletab,
111 countS * sizeof(struct filedescr));
112 filetab[countD + countS].suffix = NULL;
113
114 _PyImport_Filetab = filetab;
115
Guido van Rossum0824f631997-03-11 18:37:35 +0000116 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000117 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
118 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000119#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000120 if (strcmp(filetab->suffix, ".pyc") == 0)
121 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000122#else
123 if (strcmp(filetab->suffix, "/pyc") == 0)
124 filetab->suffix = "/pyo";
125#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000126 }
127 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000128
129 if (Py_UnicodeFlag) {
130 /* Fix the pyc_magic so that byte compiled code created
131 using the all-Unicode method doesn't interfere with
132 code created in normal operation mode. */
133 pyc_magic = MAGIC + 1;
134 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137void
Just van Rossum52e14d62002-12-30 22:08:05 +0000138_PyImportHooks_Init(void)
139{
140 PyObject *v, *path_hooks = NULL, *zimpimport;
141 int err = 0;
142
143 /* adding sys.path_hooks and sys.path_importer_cache, setting up
144 zipimport */
145
146 if (Py_VerboseFlag)
147 PySys_WriteStderr("# installing zipimport hook\n");
148
149 v = PyList_New(0);
150 if (v == NULL)
151 goto error;
152 err = PySys_SetObject("meta_path", v);
153 Py_DECREF(v);
154 if (err)
155 goto error;
156 v = PyDict_New();
157 if (v == NULL)
158 goto error;
159 err = PySys_SetObject("path_importer_cache", v);
160 Py_DECREF(v);
161 if (err)
162 goto error;
163 path_hooks = PyList_New(0);
164 if (path_hooks == NULL)
165 goto error;
166 err = PySys_SetObject("path_hooks", path_hooks);
167 if (err) {
168 error:
169 PyErr_Print();
170 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
171 "path_importer_cache failed");
172 }
173 zimpimport = PyImport_ImportModule("zipimport");
174 if (zimpimport == NULL) {
175 PyErr_Clear(); /* No zip import module -- okay */
176 if (Py_VerboseFlag)
177 PySys_WriteStderr("# can't import zipimport\n");
178 }
179 else {
180 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
181 "zipimporter");
182 Py_DECREF(zimpimport);
183 if (zipimporter == NULL) {
184 PyErr_Clear(); /* No zipimporter object -- okay */
185 if (Py_VerboseFlag)
186 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000187 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000188 }
189 else {
190 /* sys.path_hooks.append(zipimporter) */
191 err = PyList_Append(path_hooks, zipimporter);
192 Py_DECREF(zipimporter);
193 if (err)
194 goto error;
195 if (Py_VerboseFlag)
196 PySys_WriteStderr(
197 "# installed zipimport hook\n");
198 }
199 }
200 Py_DECREF(path_hooks);
201}
202
203void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000204_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205{
206 Py_XDECREF(extensions);
207 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000208 PyMem_DEL(_PyImport_Filetab);
209 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210}
211
212
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000213/* Locking primitives to prevent parallel imports of the same module
214 in different threads to return with a partially loaded module.
215 These calls are serialized by the global interpreter lock. */
216
217#ifdef WITH_THREAD
218
Guido van Rossum49b56061998-10-01 20:42:43 +0000219#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000220
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000222static long import_lock_thread = -1;
223static int import_lock_level = 0;
224
225static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000227{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000228 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000229 if (me == -1)
230 return; /* Too bad */
231 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000233 if (import_lock_thread == me) {
234 import_lock_level++;
235 return;
236 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000237 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
238 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000239 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000241 PyEval_RestoreThread(tstate);
242 }
243 import_lock_thread = me;
244 import_lock_level = 1;
245}
246
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000247static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000252 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000253 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000254 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000255 import_lock_level--;
256 if (import_lock_level == 0) {
257 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000260 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000261}
262
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes do not share locks with the parent. */
265
266void
267_PyImport_ReInitLock(void)
268{
269#ifdef _AIX
270 if (import_lock != NULL)
271 import_lock = PyThread_allocate_lock();
272#endif
273}
274
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000275#else
276
277#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000279
280#endif
281
Tim Peters69232342001-08-30 05:16:13 +0000282static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000283imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000284{
Tim Peters69232342001-08-30 05:16:13 +0000285#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000286 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000287#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000288 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000289#endif
290}
291
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000293imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000294{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295#ifdef WITH_THREAD
296 lock_import();
297#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000298 Py_INCREF(Py_None);
299 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000300}
301
302static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000303imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000304{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000305#ifdef WITH_THREAD
306 if (unlock_import() < 0) {
307 PyErr_SetString(PyExc_RuntimeError,
308 "not holding the import lock");
309 return NULL;
310 }
311#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000312 Py_INCREF(Py_None);
313 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000314}
315
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316/* Helper for sys */
317
318PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000319PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000320{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000321 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322 if (interp->modules == NULL)
323 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
324 return interp->modules;
325}
326
Guido van Rossum3f5da241990-12-20 15:06:42 +0000327
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000328/* List of names to clear in sys */
329static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000330 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331 "exc_type", "exc_value", "exc_traceback",
332 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000333 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000334 NULL
335};
336
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000337static char* sys_files[] = {
338 "stdin", "__stdin__",
339 "stdout", "__stdout__",
340 "stderr", "__stderr__",
341 NULL
342};
343
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000344
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000345/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000346
Guido van Rossum3f5da241990-12-20 15:06:42 +0000347void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000349{
Guido van Rossum758eec01998-01-19 21:58:26 +0000350 int pos, ndone;
351 char *name;
352 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000353 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000354 PyObject *modules = interp->modules;
355
356 if (modules == NULL)
357 return; /* Already done */
358
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000359 /* Delete some special variables first. These are common
360 places where user values hide and people complain when their
361 destructors fail. Since the modules containing them are
362 deleted *last* of all, they would come too late in the normal
363 destruction order. Sigh. */
364
365 value = PyDict_GetItemString(modules, "__builtin__");
366 if (value != NULL && PyModule_Check(value)) {
367 dict = PyModule_GetDict(value);
368 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000369 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000370 PyDict_SetItemString(dict, "_", Py_None);
371 }
372 value = PyDict_GetItemString(modules, "sys");
373 if (value != NULL && PyModule_Check(value)) {
374 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000375 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000376 dict = PyModule_GetDict(value);
377 for (p = sys_deletes; *p != NULL; p++) {
378 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000379 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000380 PyDict_SetItemString(dict, *p, Py_None);
381 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000382 for (p = sys_files; *p != NULL; p+=2) {
383 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000384 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000385 v = PyDict_GetItemString(dict, *(p+1));
386 if (v == NULL)
387 v = Py_None;
388 PyDict_SetItemString(dict, *p, v);
389 }
390 }
391
392 /* First, delete __main__ */
393 value = PyDict_GetItemString(modules, "__main__");
394 if (value != NULL && PyModule_Check(value)) {
395 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000396 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000397 _PyModule_Clear(value);
398 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000399 }
400
Guido van Rossum758eec01998-01-19 21:58:26 +0000401 /* The special treatment of __builtin__ here is because even
402 when it's not referenced as a module, its dictionary is
403 referenced by almost every module's __builtins__. Since
404 deleting a module clears its dictionary (even if there are
405 references left to it), we need to delete the __builtin__
406 module last. Likewise, we don't delete sys until the very
407 end because it is implicitly referenced (e.g. by print).
408
409 Also note that we 'delete' modules by replacing their entry
410 in the modules dict with None, rather than really deleting
411 them; this avoids a rehash of the modules dictionary and
412 also marks them as "non existent" so they won't be
413 re-imported. */
414
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000415 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000416 one (skipping __builtin__ and sys) and delete them */
417 do {
418 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000419 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000420 while (PyDict_Next(modules, &pos, &key, &value)) {
421 if (value->ob_refcnt != 1)
422 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000423 if (PyString_Check(key) && PyModule_Check(value)) {
424 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000425 if (strcmp(name, "__builtin__") == 0)
426 continue;
427 if (strcmp(name, "sys") == 0)
428 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000429 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000430 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000431 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000432 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000433 PyDict_SetItem(modules, key, Py_None);
434 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000435 }
436 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000437 } while (ndone > 0);
438
Guido van Rossum758eec01998-01-19 21:58:26 +0000439 /* Next, delete all modules (still skipping __builtin__ and sys) */
440 pos = 0;
441 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000442 if (PyString_Check(key) && PyModule_Check(value)) {
443 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000444 if (strcmp(name, "__builtin__") == 0)
445 continue;
446 if (strcmp(name, "sys") == 0)
447 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000448 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000449 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000450 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000451 PyDict_SetItem(modules, key, Py_None);
452 }
453 }
454
455 /* Next, delete sys and __builtin__ (in that order) */
456 value = PyDict_GetItemString(modules, "sys");
457 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000458 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000459 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000460 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000461 PyDict_SetItemString(modules, "sys", Py_None);
462 }
463 value = PyDict_GetItemString(modules, "__builtin__");
464 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000465 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000466 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000467 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000468 PyDict_SetItemString(modules, "__builtin__", Py_None);
469 }
470
471 /* Finally, clear and delete the modules directory */
472 PyDict_Clear(modules);
473 interp->modules = NULL;
474 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000475}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000476
477
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000478/* Helper for pythonrun.c -- return magic number */
479
480long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000481PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482{
Guido van Rossum96774c12000-05-01 20:19:08 +0000483 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484}
485
486
Guido van Rossum25ce5661997-08-02 03:10:38 +0000487/* Magic for extension modules (built-in as well as dynamically
488 loaded). To prevent initializing an extension module more than
489 once, we keep a static dictionary 'extensions' keyed by module name
490 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000491 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000492 dictionary is stored by calling _PyImport_FixupExtension()
493 immediately after the module initialization function succeeds. A
494 copy can be retrieved from there by calling
495 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496
Guido van Rossum79f25d91997-04-29 20:08:16 +0000497PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000498_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000500 PyObject *modules, *mod, *dict, *copy;
501 if (extensions == NULL) {
502 extensions = PyDict_New();
503 if (extensions == NULL)
504 return NULL;
505 }
506 modules = PyImport_GetModuleDict();
507 mod = PyDict_GetItemString(modules, name);
508 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000509 PyErr_Format(PyExc_SystemError,
510 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000511 return NULL;
512 }
513 dict = PyModule_GetDict(mod);
514 if (dict == NULL)
515 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000516 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000517 if (copy == NULL)
518 return NULL;
519 PyDict_SetItemString(extensions, filename, copy);
520 Py_DECREF(copy);
521 return copy;
522}
523
524PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000525_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000527 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 if (extensions == NULL)
529 return NULL;
530 dict = PyDict_GetItemString(extensions, filename);
531 if (dict == NULL)
532 return NULL;
533 mod = PyImport_AddModule(name);
534 if (mod == NULL)
535 return NULL;
536 mdict = PyModule_GetDict(mod);
537 if (mdict == NULL)
538 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000539 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000542 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 name, filename);
544 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545}
546
547
548/* Get the module object corresponding to a module name.
549 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000550 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000551 Because the former action is most common, THIS DOES NOT RETURN A
552 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000555PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000557 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000563 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000564 if (m == NULL)
565 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568 return NULL;
569 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
572 return m;
573}
574
Tim Peters1cd70172004-08-02 03:52:12 +0000575/* Remove name from sys.modules, if it's there. */
576static void
577_RemoveModule(const char *name)
578{
579 PyObject *modules = PyImport_GetModuleDict();
580 if (PyDict_GetItemString(modules, name) == NULL)
581 return;
582 if (PyDict_DelItemString(modules, name) < 0)
583 Py_FatalError("import: deleting existing key in"
584 "sys.modules failed");
585}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000587/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000588 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
589 * removed from sys.modules, to avoid leaving damaged module objects
590 * in sys.modules. The caller may wish to restore the original
591 * module object (if any) in this case; PyImport_ReloadModule is an
592 * example.
593 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000595PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000597 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
598}
599
600PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000601PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000602{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607 if (m == NULL)
608 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000609 /* If the module is being reloaded, we get the old module back
610 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 d = PyModule_GetDict(m);
612 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
613 if (PyDict_SetItemString(d, "__builtins__",
614 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000615 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000616 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000617 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000618 v = NULL;
619 if (pathname != NULL) {
620 v = PyString_FromString(pathname);
621 if (v == NULL)
622 PyErr_Clear();
623 }
624 if (v == NULL) {
625 v = ((PyCodeObject *)co)->co_filename;
626 Py_INCREF(v);
627 }
628 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000630 Py_DECREF(v);
631
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000632 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000634 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000636
Guido van Rossum25ce5661997-08-02 03:10:38 +0000637 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000638 PyErr_Format(PyExc_ImportError,
639 "Loaded module %.200s not found in sys.modules",
640 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000641 return NULL;
642 }
643
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645
646 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000647
648 error:
649 _RemoveModule(name);
650 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651}
652
653
654/* Given a pathname for a Python source file, fill a buffer with the
655 pathname for the corresponding compiled file. Return the pathname
656 for the compiled file, or NULL if there's no space in the buffer.
657 Doesn't set an exception. */
658
659static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661{
Tim Petersc1731372001-08-04 08:12:36 +0000662 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663 if (len+2 > buflen)
664 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000665
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000666#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000667 /* Treat .pyw as if it were .py. The case of ".pyw" must match
668 that used in _PyImport_StandardFiletab. */
669 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
670 --len; /* pretend 'w' isn't there */
671#endif
672 memcpy(buf, pathname, len);
673 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
674 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000675
676 return buf;
677}
678
679
680/* Given a pathname for a Python source file, its time of last
681 modification, and a pathname for a compiled file, check whether the
682 compiled file represents the same version of the source. If so,
683 return a FILE pointer for the compiled file, positioned just after
684 the header; if not, return NULL.
685 Doesn't set an exception. */
686
687static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689{
690 FILE *fp;
691 long magic;
692 long pyc_mtime;
693
694 fp = fopen(cpathname, "rb");
695 if (fp == NULL)
696 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000698 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000700 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701 fclose(fp);
702 return NULL;
703 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000707 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708 fclose(fp);
709 return NULL;
710 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000712 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713 return fp;
714}
715
716
717/* Read a code object from a file and check it for validity */
718
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000720read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723
Tim Petersd9b9ac82001-01-28 00:27:39 +0000724 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000725 if (co == NULL)
726 return NULL;
727 if (!PyCode_Check(co)) {
728 PyErr_Format(PyExc_ImportError,
729 "Non-code object in %.200s", cpathname);
730 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731 return NULL;
732 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734}
735
736
737/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000738 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000741load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742{
743 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 PyCodeObject *co;
745 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000748 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000749 PyErr_Format(PyExc_ImportError,
750 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 return NULL;
752 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000754 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755 if (co == NULL)
756 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000758 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000760 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762
763 return m;
764}
765
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766/* Parse a source file and return the corresponding code object */
767
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000769parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772 node *n;
773
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000774 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775 if (n == NULL)
776 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 co = PyNode_Compile(n, pathname);
778 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000779
780 return co;
781}
782
783
Guido van Rossum55a83382000-09-20 20:31:38 +0000784/* Helper to open a bytecode file for writing in exclusive mode */
785
786static FILE *
787open_exclusive(char *filename)
788{
789#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
790 /* Use O_EXCL to avoid a race condition when another process tries to
791 write the same file. When that happens, our open() call fails,
792 which is just fine (since it's only a cache).
793 XXX If the file exists and is writable but the directory is not
794 writable, the file will never be written. Oh well.
795 */
796 int fd;
797 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000798 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
799#ifdef O_BINARY
800 |O_BINARY /* necessary for Windows */
801#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000802#ifdef __VMS
803 , 0666, "ctxt=bin", "shr=nil");
804#else
805 , 0666);
806#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000807 if (fd < 0)
808 return NULL;
809 return fdopen(fd, "wb");
810#else
811 /* Best we can do -- on Windows this can't happen anyway */
812 return fopen(filename, "wb");
813#endif
814}
815
816
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817/* Write a compiled module to a file, placing the time of last
818 modification of its source into the header.
819 Errors are ignored, if a write error occurs an attempt is made to
820 remove the file. */
821
822static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000823write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824{
825 FILE *fp;
826
Guido van Rossum55a83382000-09-20 20:31:38 +0000827 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000830 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 "# can't create %s\n", cpathname);
832 return;
833 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000834 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000836 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
837 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000838 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000840 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000841 /* Don't keep partial file */
842 fclose(fp);
843 (void) unlink(cpathname);
844 return;
845 }
846 /* Now write the true mtime */
847 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000848 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849 fflush(fp);
850 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000852 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853}
854
855
856/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000857 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
858 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000859
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000861load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862{
Fred Drake4c82b232000-06-30 16:18:57 +0000863 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864 FILE *fpc;
865 char buf[MAXPATHLEN+1];
866 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyCodeObject *co;
868 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000870 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000871 if (mtime == (time_t)(-1)) {
872 PyErr_Format(PyExc_RuntimeError,
873 "unable to get modification time from '%s'",
874 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000875 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000876 }
Fred Drake4c82b232000-06-30 16:18:57 +0000877#if SIZEOF_TIME_T > 4
878 /* Python's .pyc timestamp handling presumes that the timestamp fits
879 in 4 bytes. This will be fine until sometime in the year 2038,
880 when a 4-byte signed time_t will overflow.
881 */
882 if (mtime >> 32) {
883 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000884 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000885 return NULL;
886 }
887#endif
Tim Peters36515e22001-11-18 04:06:29 +0000888 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000889 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890 if (cpathname != NULL &&
891 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000892 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893 fclose(fpc);
894 if (co == NULL)
895 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000897 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000898 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000899 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000900 }
901 else {
902 co = parse_source_module(pathname, fp);
903 if (co == NULL)
904 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000906 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000907 name, pathname);
908 write_compiled_module(co, cpathname, mtime);
909 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000910 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000912
913 return m;
914}
915
916
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000917/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000918static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
919static struct filedescr *find_module(char *, char *, PyObject *,
920 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000921static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000922
923/* Load a package and return its module object WITH INCREMENTED
924 REFERENCE COUNT */
925
926static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000927load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000928{
Tim Peters1cd70172004-08-02 03:52:12 +0000929 PyObject *m, *d;
930 PyObject *file = NULL;
931 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000932 int err;
933 char buf[MAXPATHLEN+1];
934 FILE *fp = NULL;
935 struct filedescr *fdp;
936
937 m = PyImport_AddModule(name);
938 if (m == NULL)
939 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000940 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000941 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000942 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000943 d = PyModule_GetDict(m);
944 file = PyString_FromString(pathname);
945 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000946 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000947 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000948 if (path == NULL)
949 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000950 err = PyDict_SetItemString(d, "__file__", file);
951 if (err == 0)
952 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000953 if (err != 0)
954 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000955 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000956 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957 if (fdp == NULL) {
958 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
959 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000960 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000961 }
962 else
963 m = NULL;
964 goto cleanup;
965 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000966 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000967 if (fp != NULL)
968 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000969 goto cleanup;
970
971 error:
972 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000973 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000974 Py_XDECREF(path);
975 Py_XDECREF(file);
976 return m;
977}
978
979
980/* Helper to test for built-in module */
981
982static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000983is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000984{
985 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000986 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
987 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
988 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000989 return -1;
990 else
991 return 1;
992 }
993 }
994 return 0;
995}
996
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000997
Just van Rossum52e14d62002-12-30 22:08:05 +0000998/* Return an importer object for a sys.path/pkg.__path__ item 'p',
999 possibly by fetching it from the path_importer_cache dict. If it
1000 wasn't yet cached, traverse path_hooks until it a hook is found
1001 that can handle the path item. Return None if no hook could;
1002 this tells our caller it should fall back to the builtin
1003 import mechanism. Cache the result in path_importer_cache.
1004 Returns a borrowed reference. */
1005
1006static PyObject *
1007get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1008 PyObject *p)
1009{
1010 PyObject *importer;
1011 int j, nhooks;
1012
1013 /* These conditions are the caller's responsibility: */
1014 assert(PyList_Check(path_hooks));
1015 assert(PyDict_Check(path_importer_cache));
1016
1017 nhooks = PyList_Size(path_hooks);
1018 if (nhooks < 0)
1019 return NULL; /* Shouldn't happen */
1020
1021 importer = PyDict_GetItem(path_importer_cache, p);
1022 if (importer != NULL)
1023 return importer;
1024
1025 /* set path_importer_cache[p] to None to avoid recursion */
1026 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1027 return NULL;
1028
1029 for (j = 0; j < nhooks; j++) {
1030 PyObject *hook = PyList_GetItem(path_hooks, j);
1031 if (hook == NULL)
1032 return NULL;
1033 importer = PyObject_CallFunction(hook, "O", p);
1034 if (importer != NULL)
1035 break;
1036
1037 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1038 return NULL;
1039 }
1040 PyErr_Clear();
1041 }
1042 if (importer == NULL)
1043 importer = Py_None;
1044 else if (importer != Py_None) {
1045 int err = PyDict_SetItem(path_importer_cache, p, importer);
1046 Py_DECREF(importer);
1047 if (err != 0)
1048 return NULL;
1049 }
1050 return importer;
1051}
1052
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053/* Search the path (default sys.path) for a module. Return the
1054 corresponding filedescr struct, and (via return arguments) the
1055 pathname and an open file. Return NULL if the module is not found. */
1056
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001057#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001058extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1059 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001060#endif
1061
Tim Peters50d8d372001-02-28 05:34:27 +00001062static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001063static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001064static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001065
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001066static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001067find_module(char *fullname, char *subname, PyObject *path, char *buf,
1068 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001069{
Fred Drake4c82b232000-06-30 16:18:57 +00001070 int i, npath;
1071 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001072 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001073 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001074 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001075 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001076#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001077 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001078#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001079 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1080 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1081 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001082 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001083#if defined(PYOS_OS2)
1084 size_t saved_len;
1085 size_t saved_namelen;
1086 char *saved_buf = NULL;
1087#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001088 if (p_loader != NULL)
1089 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001090
Just van Rossum52e14d62002-12-30 22:08:05 +00001091 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001092 PyErr_SetString(PyExc_OverflowError,
1093 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001094 return NULL;
1095 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001096 strcpy(name, subname);
1097
1098 /* sys.meta_path import hook */
1099 if (p_loader != NULL) {
1100 PyObject *meta_path;
1101
1102 meta_path = PySys_GetObject("meta_path");
1103 if (meta_path == NULL || !PyList_Check(meta_path)) {
1104 PyErr_SetString(PyExc_ImportError,
1105 "sys.meta_path must be a list of "
1106 "import hooks");
1107 return NULL;
1108 }
1109 Py_INCREF(meta_path); /* zap guard */
1110 npath = PyList_Size(meta_path);
1111 for (i = 0; i < npath; i++) {
1112 PyObject *loader;
1113 PyObject *hook = PyList_GetItem(meta_path, i);
1114 loader = PyObject_CallMethod(hook, "find_module",
1115 "sO", fullname,
1116 path != NULL ?
1117 path : Py_None);
1118 if (loader == NULL) {
1119 Py_DECREF(meta_path);
1120 return NULL; /* true error */
1121 }
1122 if (loader != Py_None) {
1123 /* a loader was found */
1124 *p_loader = loader;
1125 Py_DECREF(meta_path);
1126 return &importhookdescr;
1127 }
1128 Py_DECREF(loader);
1129 }
1130 Py_DECREF(meta_path);
1131 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001132
1133 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001134 /* The only type of submodule allowed inside a "frozen"
1135 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001136 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1137 PyErr_SetString(PyExc_ImportError,
1138 "full frozen module name too long");
1139 return NULL;
1140 }
1141 strcpy(buf, PyString_AsString(path));
1142 strcat(buf, ".");
1143 strcat(buf, name);
1144 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001145 if (find_frozen(name) != NULL) {
1146 strcpy(buf, name);
1147 return &fd_frozen;
1148 }
1149 PyErr_Format(PyExc_ImportError,
1150 "No frozen submodule named %.200s", name);
1151 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001152 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001153 if (path == NULL) {
1154 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001155 strcpy(buf, name);
1156 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001157 }
Greg Ward201baee2001-10-04 14:52:06 +00001158 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001159 strcpy(buf, name);
1160 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001161 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001162
Guido van Rossumac279101996-08-22 23:10:58 +00001163#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001164 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1165 if (fp != NULL) {
1166 *p_fp = fp;
1167 return fdp;
1168 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001169#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001170 path = PySys_GetObject("path");
1171 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 if (path == NULL || !PyList_Check(path)) {
1173 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001174 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001175 return NULL;
1176 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001177
1178 path_hooks = PySys_GetObject("path_hooks");
1179 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1180 PyErr_SetString(PyExc_ImportError,
1181 "sys.path_hooks must be a list of "
1182 "import hooks");
1183 return NULL;
1184 }
1185 path_importer_cache = PySys_GetObject("path_importer_cache");
1186 if (path_importer_cache == NULL ||
1187 !PyDict_Check(path_importer_cache)) {
1188 PyErr_SetString(PyExc_ImportError,
1189 "sys.path_importer_cache must be a dict");
1190 return NULL;
1191 }
1192
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001194 namelen = strlen(name);
1195 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001196 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001198#ifdef Py_USING_UNICODE
1199 if (PyUnicode_Check(v)) {
1200 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1201 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1202 if (copy == NULL)
1203 return NULL;
1204 v = copy;
1205 }
1206 else
1207#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001208 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001209 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001211 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1212 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001213 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001214 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001216 if (strlen(buf) != len) {
1217 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001218 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001219 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001220
1221 /* sys.path_hooks import hook */
1222 if (p_loader != NULL) {
1223 PyObject *importer;
1224
1225 importer = get_path_importer(path_importer_cache,
1226 path_hooks, v);
1227 if (importer == NULL)
1228 return NULL;
1229 /* Note: importer is a borrowed reference */
1230 if (importer != Py_None) {
1231 PyObject *loader;
1232 loader = PyObject_CallMethod(importer,
1233 "find_module",
1234 "s", fullname);
1235 if (loader == NULL)
1236 return NULL; /* error */
1237 if (loader != Py_None) {
1238 /* a loader was found */
1239 *p_loader = loader;
1240 return &importhookdescr;
1241 }
1242 Py_DECREF(loader);
1243 }
1244 /* no hook was successful, use builtin import */
1245 }
1246
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001247 if (len > 0 && buf[len-1] != SEP
1248#ifdef ALTSEP
1249 && buf[len-1] != ALTSEP
1250#endif
1251 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001252 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001253 strcpy(buf+len, name);
1254 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001255
1256 /* Check for package import (buf holds a directory name,
1257 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001258#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001259 if (stat(buf, &statbuf) == 0 && /* it exists */
1260 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1261 find_init_module(buf) && /* it has __init__.py */
1262 case_ok(buf, len, namelen, name)) { /* and case matches */
1263 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001264 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001265 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001266#else
1267 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001268#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001269 if (isdir(buf) &&
1270 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001271 case_ok(buf, len, namelen, name)) {
1272 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001273 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001274 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001275#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001276#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001277#if defined(PYOS_OS2)
1278 /* take a snapshot of the module spec for restoration
1279 * after the 8 character DLL hackery
1280 */
1281 saved_buf = strdup(buf);
1282 saved_len = len;
1283 saved_namelen = namelen;
1284#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001286#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001287 /* OS/2 limits DLLs to 8 character names (w/o
1288 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001289 * so if the name is longer than that and its a
1290 * dynamically loaded module we're going to try,
1291 * truncate the name before trying
1292 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001293 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001294 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001295 const struct filedescr *scan;
1296 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001297 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001298 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001299 break;
1300 else
1301 scan++;
1302 }
1303 if (scan->suffix != NULL) {
1304 /* yes, so truncate the name */
1305 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001306 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001307 buf[len] = '\0';
1308 }
1309 }
1310#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001311 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001313 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001314 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001315 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001316 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001317 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001318 if (fp != NULL) {
1319 if (case_ok(buf, len, namelen, name))
1320 break;
1321 else { /* continue search */
1322 fclose(fp);
1323 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001324 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001325 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001326#if defined(PYOS_OS2)
1327 /* restore the saved snapshot */
1328 strcpy(buf, saved_buf);
1329 len = saved_len;
1330 namelen = saved_namelen;
1331#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001332 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001333#if defined(PYOS_OS2)
1334 /* don't need/want the module name snapshot anymore */
1335 if (saved_buf)
1336 {
1337 free(saved_buf);
1338 saved_buf = NULL;
1339 }
1340#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001341 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001342 if (fp != NULL)
1343 break;
1344 }
1345 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001346 PyErr_Format(PyExc_ImportError,
1347 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001348 return NULL;
1349 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001350 *p_fp = fp;
1351 return fdp;
1352}
1353
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001354/* Helpers for main.c
1355 * Find the source file corresponding to a named module
1356 */
1357struct filedescr *
1358_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1359 size_t buflen, FILE **p_fp, PyObject **p_loader)
1360{
1361 return find_module((char *) name, (char *) name, path,
1362 buf, buflen, p_fp, p_loader);
1363}
1364
1365PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1366{
1367 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1368}
1369
Tim Petersd1e87a82001-03-01 18:12:00 +00001370/* case_ok(char* buf, int len, int namelen, char* name)
1371 * The arguments here are tricky, best shown by example:
1372 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1373 * ^ ^ ^ ^
1374 * |--------------------- buf ---------------------|
1375 * |------------------- len ------------------|
1376 * |------ name -------|
1377 * |----- namelen -----|
1378 * buf is the full path, but len only counts up to (& exclusive of) the
1379 * extension. name is the module name, also exclusive of extension.
1380 *
1381 * We've already done a successful stat() or fopen() on buf, so know that
1382 * there's some match, possibly case-insensitive.
1383 *
Tim Peters50d8d372001-02-28 05:34:27 +00001384 * case_ok() is to return 1 if there's a case-sensitive match for
1385 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1386 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001387 *
Tim Peters50d8d372001-02-28 05:34:27 +00001388 * case_ok() is used to implement case-sensitive import semantics even
1389 * on platforms with case-insensitive filesystems. It's trivial to implement
1390 * for case-sensitive filesystems. It's pretty much a cross-platform
1391 * nightmare for systems with case-insensitive filesystems.
1392 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001393
Tim Peters50d8d372001-02-28 05:34:27 +00001394/* First we may need a pile of platform-specific header files; the sequence
1395 * of #if's here should match the sequence in the body of case_ok().
1396 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001397#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001398#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001399
Tim Peters50d8d372001-02-28 05:34:27 +00001400#elif defined(DJGPP)
1401#include <dir.h>
1402
Jason Tishler7961aa62005-05-20 00:56:54 +00001403#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001404#include <sys/types.h>
1405#include <dirent.h>
1406
Andrew MacIntyred9400542002-02-26 11:41:34 +00001407#elif defined(PYOS_OS2)
1408#define INCL_DOS
1409#define INCL_DOSERRORS
1410#define INCL_NOPMAPI
1411#include <os2.h>
1412
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001413#elif defined(RISCOS)
1414#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001415#endif
1416
Guido van Rossum0980bd91998-02-13 17:18:36 +00001417static int
Tim Peters50d8d372001-02-28 05:34:27 +00001418case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001419{
Tim Peters50d8d372001-02-28 05:34:27 +00001420/* Pick a platform-specific implementation; the sequence of #if's here should
1421 * match the sequence just above.
1422 */
1423
Jason Tishler7961aa62005-05-20 00:56:54 +00001424/* MS_WINDOWS */
1425#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001426 WIN32_FIND_DATA data;
1427 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001428
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001429 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001430 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001431
Guido van Rossum0980bd91998-02-13 17:18:36 +00001432 h = FindFirstFile(buf, &data);
1433 if (h == INVALID_HANDLE_VALUE) {
1434 PyErr_Format(PyExc_NameError,
1435 "Can't find file for module %.100s\n(filename %.300s)",
1436 name, buf);
1437 return 0;
1438 }
1439 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001440 return strncmp(data.cFileName, name, namelen) == 0;
1441
1442/* DJGPP */
1443#elif defined(DJGPP)
1444 struct ffblk ffblk;
1445 int done;
1446
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001447 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001448 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001449
1450 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1451 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001452 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001453 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001454 name, buf);
1455 return 0;
1456 }
Tim Peters50d8d372001-02-28 05:34:27 +00001457 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001458
Jason Tishler7961aa62005-05-20 00:56:54 +00001459/* new-fangled macintosh (macosx) or Cygwin */
1460#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001461 DIR *dirp;
1462 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001463 char dirname[MAXPATHLEN + 1];
1464 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001465
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001466 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001467 return 1;
1468
Tim Petersd1e87a82001-03-01 18:12:00 +00001469 /* Copy the dir component into dirname; substitute "." if empty */
1470 if (dirlen <= 0) {
1471 dirname[0] = '.';
1472 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001473 }
1474 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001475 assert(dirlen <= MAXPATHLEN);
1476 memcpy(dirname, buf, dirlen);
1477 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001478 }
1479 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001480 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001481 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001482 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001483 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001484 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001485#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001486 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001487#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001488 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001489#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001490 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001491 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001492 (void)closedir(dirp);
1493 return 1; /* Found */
1494 }
1495 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001496 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001497 }
Tim Peters430f5d42001-03-01 01:30:56 +00001498 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001499
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001500/* RISC OS */
1501#elif defined(RISCOS)
1502 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1503 char buf2[MAXPATHLEN+2];
1504 char *nameWithExt = buf+len-namelen;
1505 int canonlen;
1506 os_error *e;
1507
1508 if (Py_GETENV("PYTHONCASEOK") != NULL)
1509 return 1;
1510
1511 /* workaround:
1512 append wildcard, otherwise case of filename wouldn't be touched */
1513 strcpy(buf2, buf);
1514 strcat(buf2, "*");
1515
1516 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1517 canonlen = MAXPATHLEN+1-canonlen;
1518 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1519 return 0;
1520 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1521 return 1; /* match */
1522
1523 return 0;
1524
Andrew MacIntyred9400542002-02-26 11:41:34 +00001525/* OS/2 */
1526#elif defined(PYOS_OS2)
1527 HDIR hdir = 1;
1528 ULONG srchcnt = 1;
1529 FILEFINDBUF3 ffbuf;
1530 APIRET rc;
1531
1532 if (getenv("PYTHONCASEOK") != NULL)
1533 return 1;
1534
1535 rc = DosFindFirst(buf,
1536 &hdir,
1537 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1538 &ffbuf, sizeof(ffbuf),
1539 &srchcnt,
1540 FIL_STANDARD);
1541 if (rc != NO_ERROR)
1542 return 0;
1543 return strncmp(ffbuf.achName, name, namelen) == 0;
1544
Tim Peters50d8d372001-02-28 05:34:27 +00001545/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1546#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001547 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001548
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001549#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001550}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001551
Guido van Rossum0980bd91998-02-13 17:18:36 +00001552
Guido van Rossum197346f1997-10-31 18:38:52 +00001553#ifdef HAVE_STAT
1554/* Helper to look for __init__.py or __init__.py[co] in potential package */
1555static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001556find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001557{
Tim Peters0f9431f2001-07-05 03:47:53 +00001558 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001559 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001560 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001561 struct stat statbuf;
1562
Tim Peters0f9431f2001-07-05 03:47:53 +00001563/* For calling case_ok(buf, len, namelen, name):
1564 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1565 * ^ ^ ^ ^
1566 * |--------------------- buf ---------------------|
1567 * |------------------- len ------------------|
1568 * |------ name -------|
1569 * |----- namelen -----|
1570 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001571 if (save_len + 13 >= MAXPATHLEN)
1572 return 0;
1573 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001574 pname = buf + i;
1575 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001576 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001577 if (case_ok(buf,
1578 save_len + 9, /* len("/__init__") */
1579 8, /* len("__init__") */
1580 pname)) {
1581 buf[save_len] = '\0';
1582 return 1;
1583 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001584 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001585 i += strlen(pname);
1586 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001587 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001588 if (case_ok(buf,
1589 save_len + 9, /* len("/__init__") */
1590 8, /* len("__init__") */
1591 pname)) {
1592 buf[save_len] = '\0';
1593 return 1;
1594 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001595 }
1596 buf[save_len] = '\0';
1597 return 0;
1598}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001599
1600#else
1601
1602#ifdef RISCOS
1603static int
1604find_init_module(buf)
1605 char *buf;
1606{
1607 int save_len = strlen(buf);
1608 int i = save_len;
1609
1610 if (save_len + 13 >= MAXPATHLEN)
1611 return 0;
1612 buf[i++] = SEP;
1613 strcpy(buf+i, "__init__/py");
1614 if (isfile(buf)) {
1615 buf[save_len] = '\0';
1616 return 1;
1617 }
1618
1619 if (Py_OptimizeFlag)
1620 strcpy(buf+i, "o");
1621 else
1622 strcpy(buf+i, "c");
1623 if (isfile(buf)) {
1624 buf[save_len] = '\0';
1625 return 1;
1626 }
1627 buf[save_len] = '\0';
1628 return 0;
1629}
1630#endif /*RISCOS*/
1631
Guido van Rossum197346f1997-10-31 18:38:52 +00001632#endif /* HAVE_STAT */
1633
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001634
Tim Petersdbd9ba62000-07-09 03:09:57 +00001635static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001636
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001637/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001638 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001639
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001641load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001642{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001643 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001644 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001645 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001647 /* First check that there's an open file (if we need one) */
1648 switch (type) {
1649 case PY_SOURCE:
1650 case PY_COMPILED:
1651 if (fp == NULL) {
1652 PyErr_Format(PyExc_ValueError,
1653 "file object required for import (type code %d)",
1654 type);
1655 return NULL;
1656 }
1657 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001658
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001659 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001660
1661 case PY_SOURCE:
1662 m = load_source_module(name, buf, fp);
1663 break;
1664
1665 case PY_COMPILED:
1666 m = load_compiled_module(name, buf, fp);
1667 break;
1668
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001669#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001670 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001672 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001673#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001674
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001675 case PKG_DIRECTORY:
1676 m = load_package(name, buf);
1677 break;
1678
1679 case C_BUILTIN:
1680 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001681 if (buf != NULL && buf[0] != '\0')
1682 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001683 if (type == C_BUILTIN)
1684 err = init_builtin(name);
1685 else
1686 err = PyImport_ImportFrozenModule(name);
1687 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001688 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001689 if (err == 0) {
1690 PyErr_Format(PyExc_ImportError,
1691 "Purported %s module %.200s not found",
1692 type == C_BUILTIN ?
1693 "builtin" : "frozen",
1694 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001695 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001696 }
1697 modules = PyImport_GetModuleDict();
1698 m = PyDict_GetItemString(modules, name);
1699 if (m == NULL) {
1700 PyErr_Format(
1701 PyExc_ImportError,
1702 "%s module %.200s not properly initialized",
1703 type == C_BUILTIN ?
1704 "builtin" : "frozen",
1705 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001706 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001707 }
1708 Py_INCREF(m);
1709 break;
1710
Just van Rossum52e14d62002-12-30 22:08:05 +00001711 case IMP_HOOK: {
1712 if (loader == NULL) {
1713 PyErr_SetString(PyExc_ImportError,
1714 "import hook without loader");
1715 return NULL;
1716 }
1717 m = PyObject_CallMethod(loader, "load_module", "s", name);
1718 break;
1719 }
1720
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001722 PyErr_Format(PyExc_ImportError,
1723 "Don't know how to import %.200s (type code %d)",
1724 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001725 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726
1727 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001728
1729 return m;
1730}
1731
1732
1733/* Initialize a built-in module.
1734 Return 1 for succes, 0 if the module is not found, and -1 with
1735 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001736
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001737static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001738init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001739{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001740 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001741
Greg Ward201baee2001-10-04 14:52:06 +00001742 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001743 return 1;
1744
Guido van Rossum771c6c81997-10-31 18:37:24 +00001745 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001746 if (strcmp(name, p->name) == 0) {
1747 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001748 PyErr_Format(PyExc_ImportError,
1749 "Cannot re-init internal module %.200s",
1750 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001751 return -1;
1752 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001753 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001754 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001755 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001757 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001758 if (_PyImport_FixupExtension(name, name) == NULL)
1759 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001760 return 1;
1761 }
1762 }
1763 return 0;
1764}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001765
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001766
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001767/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001768
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001769static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001770find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001771{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001772 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001773
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001775 if (p->name == NULL)
1776 return NULL;
1777 if (strcmp(p->name, name) == 0)
1778 break;
1779 }
1780 return p;
1781}
1782
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001784get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001785{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001786 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001787 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001788
1789 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001790 PyErr_Format(PyExc_ImportError,
1791 "No such frozen object named %.200s",
1792 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001793 return NULL;
1794 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001795 if (p->code == NULL) {
1796 PyErr_Format(PyExc_ImportError,
1797 "Excluded frozen object named %.200s",
1798 name);
1799 return NULL;
1800 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001801 size = p->size;
1802 if (size < 0)
1803 size = -size;
1804 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001805}
1806
1807/* Initialize a frozen module.
1808 Return 1 for succes, 0 if the module is not found, and -1 with
1809 an exception set if the initialization failed.
1810 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001811
1812int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001813PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001814{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001815 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816 PyObject *co;
1817 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001818 int ispackage;
1819 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001820
1821 if (p == NULL)
1822 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001823 if (p->code == NULL) {
1824 PyErr_Format(PyExc_ImportError,
1825 "Excluded frozen object named %.200s",
1826 name);
1827 return -1;
1828 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001829 size = p->size;
1830 ispackage = (size < 0);
1831 if (ispackage)
1832 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001834 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001835 name, ispackage ? " package" : "");
1836 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001837 if (co == NULL)
1838 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 if (!PyCode_Check(co)) {
1840 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001841 PyErr_Format(PyExc_TypeError,
1842 "frozen object %.200s is not a code object",
1843 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001844 return -1;
1845 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001846 if (ispackage) {
1847 /* Set __path__ to the package name */
1848 PyObject *d, *s;
1849 int err;
1850 m = PyImport_AddModule(name);
1851 if (m == NULL)
1852 return -1;
1853 d = PyModule_GetDict(m);
1854 s = PyString_InternFromString(name);
1855 if (s == NULL)
1856 return -1;
1857 err = PyDict_SetItemString(d, "__path__", s);
1858 Py_DECREF(s);
1859 if (err != 0)
1860 return err;
1861 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001862 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001864 if (m == NULL)
1865 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001866 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001867 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001868}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001869
1870
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001871/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001872 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001873
Guido van Rossum79f25d91997-04-29 20:08:16 +00001874PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001875PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001876{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001877 PyObject *pname;
1878 PyObject *result;
1879
1880 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001881 if (pname == NULL)
1882 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001883 result = PyImport_Import(pname);
1884 Py_DECREF(pname);
1885 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001886}
1887
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001888/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001889static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1890static PyObject *load_next(PyObject *mod, PyObject *altmod,
1891 char **p_name, char *buf, int *p_buflen);
1892static int mark_miss(char *name);
1893static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1894 char *buf, int buflen, int recursive);
1895static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001896
1897/* The Magnum Opus of dotted-name import :-) */
1898
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001899static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001900import_module_ex(char *name, PyObject *globals, PyObject *locals,
1901 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001902{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001903 char buf[MAXPATHLEN+1];
1904 int buflen = 0;
1905 PyObject *parent, *head, *next, *tail;
1906
1907 parent = get_parent(globals, buf, &buflen);
1908 if (parent == NULL)
1909 return NULL;
1910
1911 head = load_next(parent, Py_None, &name, buf, &buflen);
1912 if (head == NULL)
1913 return NULL;
1914
1915 tail = head;
1916 Py_INCREF(tail);
1917 while (name) {
1918 next = load_next(tail, tail, &name, buf, &buflen);
1919 Py_DECREF(tail);
1920 if (next == NULL) {
1921 Py_DECREF(head);
1922 return NULL;
1923 }
1924 tail = next;
1925 }
1926
1927 if (fromlist != NULL) {
1928 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1929 fromlist = NULL;
1930 }
1931
1932 if (fromlist == NULL) {
1933 Py_DECREF(tail);
1934 return head;
1935 }
1936
1937 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001938 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001939 Py_DECREF(tail);
1940 return NULL;
1941 }
1942
1943 return tail;
1944}
1945
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001946PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001947PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1948 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001949{
1950 PyObject *result;
1951 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001952 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001953 if (unlock_import() < 0) {
1954 Py_XDECREF(result);
1955 PyErr_SetString(PyExc_RuntimeError,
1956 "not holding the import lock");
1957 return NULL;
1958 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001959 return result;
1960}
1961
Fred Drake87590902004-05-28 20:21:36 +00001962/* Return the package that an import is being performed in. If globals comes
1963 from the module foo.bar.bat (not itself a package), this returns the
1964 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1965 the package's entry in sys.modules is returned.
1966
1967 The *name* of the returned package is returned in buf, with the length of
1968 the name in *p_buflen.
1969
1970 If globals doesn't come from a package or a module in a package, or a
1971 corresponding entry is not found in sys.modules, Py_None is returned.
1972*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001973static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001974get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001975{
1976 static PyObject *namestr = NULL;
1977 static PyObject *pathstr = NULL;
1978 PyObject *modname, *modpath, *modules, *parent;
1979
1980 if (globals == NULL || !PyDict_Check(globals))
1981 return Py_None;
1982
1983 if (namestr == NULL) {
1984 namestr = PyString_InternFromString("__name__");
1985 if (namestr == NULL)
1986 return NULL;
1987 }
1988 if (pathstr == NULL) {
1989 pathstr = PyString_InternFromString("__path__");
1990 if (pathstr == NULL)
1991 return NULL;
1992 }
1993
1994 *buf = '\0';
1995 *p_buflen = 0;
1996 modname = PyDict_GetItem(globals, namestr);
1997 if (modname == NULL || !PyString_Check(modname))
1998 return Py_None;
1999
2000 modpath = PyDict_GetItem(globals, pathstr);
2001 if (modpath != NULL) {
2002 int len = PyString_GET_SIZE(modname);
2003 if (len > MAXPATHLEN) {
2004 PyErr_SetString(PyExc_ValueError,
2005 "Module name too long");
2006 return NULL;
2007 }
2008 strcpy(buf, PyString_AS_STRING(modname));
2009 *p_buflen = len;
2010 }
2011 else {
2012 char *start = PyString_AS_STRING(modname);
2013 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002014 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002015 if (lastdot == NULL)
2016 return Py_None;
2017 len = lastdot - start;
2018 if (len >= MAXPATHLEN) {
2019 PyErr_SetString(PyExc_ValueError,
2020 "Module name too long");
2021 return NULL;
2022 }
2023 strncpy(buf, start, len);
2024 buf[len] = '\0';
2025 *p_buflen = len;
2026 }
2027
2028 modules = PyImport_GetModuleDict();
2029 parent = PyDict_GetItemString(modules, buf);
2030 if (parent == NULL)
2031 parent = Py_None;
2032 return parent;
2033 /* We expect, but can't guarantee, if parent != None, that:
2034 - parent.__name__ == buf
2035 - parent.__dict__ is globals
2036 If this is violated... Who cares? */
2037}
2038
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002039/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002040static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002041load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2042 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002043{
2044 char *name = *p_name;
2045 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002046 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002047 char *p;
2048 PyObject *result;
2049
2050 if (dot == NULL) {
2051 *p_name = NULL;
2052 len = strlen(name);
2053 }
2054 else {
2055 *p_name = dot+1;
2056 len = dot-name;
2057 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002058 if (len == 0) {
2059 PyErr_SetString(PyExc_ValueError,
2060 "Empty module name");
2061 return NULL;
2062 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002063
2064 p = buf + *p_buflen;
2065 if (p != buf)
2066 *p++ = '.';
2067 if (p+len-buf >= MAXPATHLEN) {
2068 PyErr_SetString(PyExc_ValueError,
2069 "Module name too long");
2070 return NULL;
2071 }
2072 strncpy(p, name, len);
2073 p[len] = '\0';
2074 *p_buflen = p+len-buf;
2075
2076 result = import_submodule(mod, p, buf);
2077 if (result == Py_None && altmod != mod) {
2078 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002079 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002080 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002081 if (result != NULL && result != Py_None) {
2082 if (mark_miss(buf) != 0) {
2083 Py_DECREF(result);
2084 return NULL;
2085 }
2086 strncpy(buf, name, len);
2087 buf[len] = '\0';
2088 *p_buflen = len;
2089 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002090 }
2091 if (result == NULL)
2092 return NULL;
2093
2094 if (result == Py_None) {
2095 Py_DECREF(result);
2096 PyErr_Format(PyExc_ImportError,
2097 "No module named %.200s", name);
2098 return NULL;
2099 }
2100
2101 return result;
2102}
2103
2104static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002105mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002106{
2107 PyObject *modules = PyImport_GetModuleDict();
2108 return PyDict_SetItemString(modules, name, Py_None);
2109}
2110
2111static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002112ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2113 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002114{
2115 int i;
2116
2117 if (!PyObject_HasAttrString(mod, "__path__"))
2118 return 1;
2119
2120 for (i = 0; ; i++) {
2121 PyObject *item = PySequence_GetItem(fromlist, i);
2122 int hasit;
2123 if (item == NULL) {
2124 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2125 PyErr_Clear();
2126 return 1;
2127 }
2128 return 0;
2129 }
2130 if (!PyString_Check(item)) {
2131 PyErr_SetString(PyExc_TypeError,
2132 "Item in ``from list'' not a string");
2133 Py_DECREF(item);
2134 return 0;
2135 }
2136 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002137 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002138 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002139 /* See if the package defines __all__ */
2140 if (recursive)
2141 continue; /* Avoid endless recursion */
2142 all = PyObject_GetAttrString(mod, "__all__");
2143 if (all == NULL)
2144 PyErr_Clear();
2145 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002146 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002147 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002148 if (!ret)
2149 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002150 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002151 continue;
2152 }
2153 hasit = PyObject_HasAttr(mod, item);
2154 if (!hasit) {
2155 char *subname = PyString_AS_STRING(item);
2156 PyObject *submod;
2157 char *p;
2158 if (buflen + strlen(subname) >= MAXPATHLEN) {
2159 PyErr_SetString(PyExc_ValueError,
2160 "Module name too long");
2161 Py_DECREF(item);
2162 return 0;
2163 }
2164 p = buf + buflen;
2165 *p++ = '.';
2166 strcpy(p, subname);
2167 submod = import_submodule(mod, subname, buf);
2168 Py_XDECREF(submod);
2169 if (submod == NULL) {
2170 Py_DECREF(item);
2171 return 0;
2172 }
2173 }
2174 Py_DECREF(item);
2175 }
2176
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002177 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002178}
2179
Neil Schemenauer00b09662003-06-16 21:03:07 +00002180static int
2181add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2182 PyObject *modules)
2183{
2184 if (mod == Py_None)
2185 return 1;
2186 /* Irrespective of the success of this load, make a
2187 reference to it in the parent package module. A copy gets
2188 saved in the modules dictionary under the full name, so get a
2189 reference from there, if need be. (The exception is when the
2190 load failed with a SyntaxError -- then there's no trace in
2191 sys.modules. In that case, of course, do nothing extra.) */
2192 if (submod == NULL) {
2193 submod = PyDict_GetItemString(modules, fullname);
2194 if (submod == NULL)
2195 return 1;
2196 }
2197 if (PyModule_Check(mod)) {
2198 /* We can't use setattr here since it can give a
2199 * spurious warning if the submodule name shadows a
2200 * builtin name */
2201 PyObject *dict = PyModule_GetDict(mod);
2202 if (!dict)
2203 return 0;
2204 if (PyDict_SetItemString(dict, subname, submod) < 0)
2205 return 0;
2206 }
2207 else {
2208 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2209 return 0;
2210 }
2211 return 1;
2212}
2213
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002214static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002215import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002216{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002217 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002218 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002219
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002220 /* Require:
2221 if mod == None: subname == fullname
2222 else: mod.__name__ + "." + subname == fullname
2223 */
2224
Tim Peters50d8d372001-02-28 05:34:27 +00002225 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002226 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002227 }
2228 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002229 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002230 char buf[MAXPATHLEN+1];
2231 struct filedescr *fdp;
2232 FILE *fp = NULL;
2233
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002234 if (mod == Py_None)
2235 path = NULL;
2236 else {
2237 path = PyObject_GetAttrString(mod, "__path__");
2238 if (path == NULL) {
2239 PyErr_Clear();
2240 Py_INCREF(Py_None);
2241 return Py_None;
2242 }
2243 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002244
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002245 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002246 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2247 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002248 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002249 if (fdp == NULL) {
2250 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2251 return NULL;
2252 PyErr_Clear();
2253 Py_INCREF(Py_None);
2254 return Py_None;
2255 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002256 m = load_module(fullname, fp, buf, fdp->type, loader);
2257 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002258 if (fp)
2259 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002260 if (!add_submodule(mod, m, fullname, subname, modules)) {
2261 Py_XDECREF(m);
2262 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002263 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002264 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002265
2266 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002267}
2268
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002269
2270/* Re-import a module of any kind and return its module object, WITH
2271 INCREMENTED REFERENCE COUNT */
2272
Guido van Rossum79f25d91997-04-29 20:08:16 +00002273PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002274PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002275{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002276 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002277 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002278 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002279 char buf[MAXPATHLEN+1];
2280 struct filedescr *fdp;
2281 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002282 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002283
Guido van Rossum79f25d91997-04-29 20:08:16 +00002284 if (m == NULL || !PyModule_Check(m)) {
2285 PyErr_SetString(PyExc_TypeError,
2286 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002287 return NULL;
2288 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002289 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002290 if (name == NULL)
2291 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002292 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002293 PyErr_Format(PyExc_ImportError,
2294 "reload(): module %.200s not in sys.modules",
2295 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002296 return NULL;
2297 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002298 subname = strrchr(name, '.');
2299 if (subname == NULL)
2300 subname = name;
2301 else {
2302 PyObject *parentname, *parent;
2303 parentname = PyString_FromStringAndSize(name, (subname-name));
2304 if (parentname == NULL)
2305 return NULL;
2306 parent = PyDict_GetItem(modules, parentname);
2307 if (parent == NULL) {
2308 PyErr_Format(PyExc_ImportError,
2309 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002310 PyString_AS_STRING(parentname));
2311 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002312 return NULL;
2313 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002314 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002315 subname++;
2316 path = PyObject_GetAttrString(parent, "__path__");
2317 if (path == NULL)
2318 PyErr_Clear();
2319 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002320 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002321 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002322 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002323
2324 if (fdp == NULL) {
2325 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002326 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002327 }
2328
2329 newm = load_module(name, fp, buf, fdp->type, loader);
2330 Py_XDECREF(loader);
2331
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002332 if (fp)
2333 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002334 if (newm == NULL) {
2335 /* load_module probably removed name from modules because of
2336 * the error. Put back the original module object. We're
2337 * going to return NULL in this case regardless of whether
2338 * replacing name succeeds, so the return value is ignored.
2339 */
2340 PyDict_SetItemString(modules, name, m);
2341 }
2342 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002343}
2344
2345
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002346/* Higher-level import emulator which emulates the "import" statement
2347 more accurately -- it invokes the __import__() function from the
2348 builtins of the current globals. This means that the import is
2349 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002350 environment, e.g. by "rexec".
2351 A dummy list ["__doc__"] is passed as the 4th argument so that
2352 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2353 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002354
2355PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002356PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002357{
2358 static PyObject *silly_list = NULL;
2359 static PyObject *builtins_str = NULL;
2360 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002361 PyObject *globals = NULL;
2362 PyObject *import = NULL;
2363 PyObject *builtins = NULL;
2364 PyObject *r = NULL;
2365
2366 /* Initialize constant string objects */
2367 if (silly_list == NULL) {
2368 import_str = PyString_InternFromString("__import__");
2369 if (import_str == NULL)
2370 return NULL;
2371 builtins_str = PyString_InternFromString("__builtins__");
2372 if (builtins_str == NULL)
2373 return NULL;
2374 silly_list = Py_BuildValue("[s]", "__doc__");
2375 if (silly_list == NULL)
2376 return NULL;
2377 }
2378
2379 /* Get the builtins from current globals */
2380 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002381 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002382 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002383 builtins = PyObject_GetItem(globals, builtins_str);
2384 if (builtins == NULL)
2385 goto err;
2386 }
2387 else {
2388 /* No globals -- use standard builtins, and fake globals */
2389 PyErr_Clear();
2390
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002391 builtins = PyImport_ImportModuleEx("__builtin__",
2392 NULL, NULL, NULL);
2393 if (builtins == NULL)
2394 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002395 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2396 if (globals == NULL)
2397 goto err;
2398 }
2399
2400 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002401 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002402 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002403 if (import == NULL)
2404 PyErr_SetObject(PyExc_KeyError, import_str);
2405 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002406 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002407 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002408 if (import == NULL)
2409 goto err;
2410
2411 /* Call the _import__ function with the proper argument list */
2412 r = PyObject_CallFunction(import, "OOOO",
2413 module_name, globals, globals, silly_list);
2414
2415 err:
2416 Py_XDECREF(globals);
2417 Py_XDECREF(builtins);
2418 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002419
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002420 return r;
2421}
2422
2423
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002424/* Module 'imp' provides Python access to the primitives used for
2425 importing modules.
2426*/
2427
Guido van Rossum79f25d91997-04-29 20:08:16 +00002428static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002429imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002430{
2431 char buf[4];
2432
Guido van Rossum96774c12000-05-01 20:19:08 +00002433 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2434 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2435 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2436 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002437
Guido van Rossum79f25d91997-04-29 20:08:16 +00002438 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002439}
2440
Guido van Rossum79f25d91997-04-29 20:08:16 +00002441static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002442imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002443{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002445 struct filedescr *fdp;
2446
Guido van Rossum79f25d91997-04-29 20:08:16 +00002447 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002448 if (list == NULL)
2449 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002450 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2451 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002452 fdp->suffix, fdp->mode, fdp->type);
2453 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002454 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002455 return NULL;
2456 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002457 if (PyList_Append(list, item) < 0) {
2458 Py_DECREF(list);
2459 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002460 return NULL;
2461 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002462 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002463 }
2464 return list;
2465}
2466
Guido van Rossum79f25d91997-04-29 20:08:16 +00002467static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002468call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002469{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002470 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002471 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002472 struct filedescr *fdp;
2473 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002474 FILE *fp = NULL;
2475
2476 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002477 if (path == Py_None)
2478 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002479 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002480 if (fdp == NULL)
2481 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002482 if (fp != NULL) {
2483 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2484 if (fob == NULL) {
2485 fclose(fp);
2486 return NULL;
2487 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002488 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002489 else {
2490 fob = Py_None;
2491 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002492 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002493 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002494 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002495 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002496 return ret;
2497}
2498
Guido van Rossum79f25d91997-04-29 20:08:16 +00002499static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002500imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002501{
2502 char *name;
2503 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002504 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002505 return NULL;
2506 return call_find_module(name, path);
2507}
2508
2509static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002510imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002511{
2512 char *name;
2513 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002514 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002515 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002516 return NULL;
2517 ret = init_builtin(name);
2518 if (ret < 0)
2519 return NULL;
2520 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002521 Py_INCREF(Py_None);
2522 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002523 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002524 m = PyImport_AddModule(name);
2525 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002526 return m;
2527}
2528
Guido van Rossum79f25d91997-04-29 20:08:16 +00002529static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002530imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002531{
2532 char *name;
2533 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002534 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002535 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002536 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002537 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002538 if (ret < 0)
2539 return NULL;
2540 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002541 Py_INCREF(Py_None);
2542 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002543 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002544 m = PyImport_AddModule(name);
2545 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002546 return m;
2547}
2548
Guido van Rossum79f25d91997-04-29 20:08:16 +00002549static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002550imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002551{
2552 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002553
Guido van Rossum43713e52000-02-29 13:59:29 +00002554 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002555 return NULL;
2556 return get_frozen_object(name);
2557}
2558
Guido van Rossum79f25d91997-04-29 20:08:16 +00002559static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002560imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002561{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002562 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002563 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002564 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002565 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002566}
2567
Guido van Rossum79f25d91997-04-29 20:08:16 +00002568static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002569imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002571 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002572 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002573 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002574 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002575 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002576 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577}
2578
2579static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002580get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002581{
2582 FILE *fp;
2583 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002584 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002585 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002586 fp = fopen(pathname, mode);
2587 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002588 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002589 }
2590 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002593 PyErr_SetString(PyExc_ValueError,
2594 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595 }
2596 return fp;
2597}
2598
Guido van Rossum79f25d91997-04-29 20:08:16 +00002599static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002600imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002601{
2602 char *name;
2603 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002604 PyObject *fob = NULL;
2605 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002606 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002607 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002609 return NULL;
2610 fp = get_file(pathname, fob, "rb");
2611 if (fp == NULL)
2612 return NULL;
2613 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002614 if (fob == NULL)
2615 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002616 return m;
2617}
2618
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002619#ifdef HAVE_DYNAMIC_LOADING
2620
Guido van Rossum79f25d91997-04-29 20:08:16 +00002621static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002622imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002623{
2624 char *name;
2625 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002626 PyObject *fob = NULL;
2627 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002628 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002629 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002630 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002631 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002632 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002633 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002634 if (fp == NULL)
2635 return NULL;
2636 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002637 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002638 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002639}
2640
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002641#endif /* HAVE_DYNAMIC_LOADING */
2642
Guido van Rossum79f25d91997-04-29 20:08:16 +00002643static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002644imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002645{
2646 char *name;
2647 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 PyObject *fob = NULL;
2649 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002650 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002651 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002652 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002653 return NULL;
2654 fp = get_file(pathname, fob, "r");
2655 if (fp == NULL)
2656 return NULL;
2657 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002658 if (fob == NULL)
2659 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002660 return m;
2661}
2662
Guido van Rossum79f25d91997-04-29 20:08:16 +00002663static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002664imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002665{
2666 char *name;
2667 PyObject *fob;
2668 char *pathname;
2669 char *suffix; /* Unused */
2670 char *mode;
2671 int type;
2672 FILE *fp;
2673
Guido van Rossum43713e52000-02-29 13:59:29 +00002674 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002675 &name, &fob, &pathname,
2676 &suffix, &mode, &type))
2677 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002678 if (*mode) {
2679 /* Mode must start with 'r' or 'U' and must not contain '+'.
2680 Implicit in this test is the assumption that the mode
2681 may contain other modifiers like 'b' or 't'. */
2682
2683 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002684 PyErr_Format(PyExc_ValueError,
2685 "invalid file open mode %.200s", mode);
2686 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002687 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002688 }
2689 if (fob == Py_None)
2690 fp = NULL;
2691 else {
2692 if (!PyFile_Check(fob)) {
2693 PyErr_SetString(PyExc_ValueError,
2694 "load_module arg#2 should be a file or None");
2695 return NULL;
2696 }
2697 fp = get_file(pathname, fob, mode);
2698 if (fp == NULL)
2699 return NULL;
2700 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002701 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002702}
2703
2704static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002705imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002706{
2707 char *name;
2708 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002709 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002710 return NULL;
2711 return load_package(name, pathname);
2712}
2713
2714static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002715imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002716{
2717 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002718 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002719 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002720 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002721}
2722
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002723/* Doc strings */
2724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725PyDoc_STRVAR(doc_imp,
2726"This module provides the components needed to build your own\n\
2727__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729PyDoc_STRVAR(doc_find_module,
2730"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002731Search for a module. If path is omitted or None, search for a\n\
2732built-in, frozen or special module and continue search in sys.path.\n\
2733The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002735
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002736PyDoc_STRVAR(doc_load_module,
2737"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002738Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002739The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741PyDoc_STRVAR(doc_get_magic,
2742"get_magic() -> string\n\
2743Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002745PyDoc_STRVAR(doc_get_suffixes,
2746"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002747Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002748that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002750PyDoc_STRVAR(doc_new_module,
2751"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002752Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002753The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002756"lock_held() -> boolean\n\
2757Return True if the import lock is currently held, else False.\n\
2758On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002759
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002760PyDoc_STRVAR(doc_acquire_lock,
2761"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002762Acquires the interpreter's import lock for the current thread.\n\
2763This lock should be used by import hooks to ensure thread-safety\n\
2764when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002765On platforms without threads, this function does nothing.");
2766
2767PyDoc_STRVAR(doc_release_lock,
2768"release_lock() -> None\n\
2769Release the interpreter's import lock.\n\
2770On platforms without threads, this function does nothing.");
2771
Guido van Rossum79f25d91997-04-29 20:08:16 +00002772static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002773 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2774 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2775 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2776 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2777 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2778 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2779 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2780 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002781 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002782 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2783 {"init_builtin", imp_init_builtin, METH_VARARGS},
2784 {"init_frozen", imp_init_frozen, METH_VARARGS},
2785 {"is_builtin", imp_is_builtin, METH_VARARGS},
2786 {"is_frozen", imp_is_frozen, METH_VARARGS},
2787 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002788#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002789 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002790#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002791 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002792 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002793 {NULL, NULL} /* sentinel */
2794};
2795
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002796static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002797setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002798{
2799 PyObject *v;
2800 int err;
2801
2802 v = PyInt_FromLong((long)value);
2803 err = PyDict_SetItemString(d, name, v);
2804 Py_XDECREF(v);
2805 return err;
2806}
2807
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002808PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002809initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002810{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002811 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002813 m = Py_InitModule4("imp", imp_methods, doc_imp,
2814 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002815 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002816
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002817 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2818 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2819 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2820 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2821 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2822 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2823 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2824 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002825 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002826 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002827
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002828 failure:
2829 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002830}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002831
2832
Guido van Rossumb18618d2000-05-03 23:44:39 +00002833/* API for embedding applications that want to add their own entries
2834 to the table of built-in modules. This should normally be called
2835 *before* Py_Initialize(). When the table resize fails, -1 is
2836 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002837
2838 After a similar function by Just van Rossum. */
2839
2840int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002841PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002842{
2843 static struct _inittab *our_copy = NULL;
2844 struct _inittab *p;
2845 int i, n;
2846
2847 /* Count the number of entries in both tables */
2848 for (n = 0; newtab[n].name != NULL; n++)
2849 ;
2850 if (n == 0)
2851 return 0; /* Nothing to do */
2852 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2853 ;
2854
2855 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002856 p = our_copy;
2857 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002858 if (p == NULL)
2859 return -1;
2860
2861 /* Copy the tables into the new memory */
2862 if (our_copy != PyImport_Inittab)
2863 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2864 PyImport_Inittab = our_copy = p;
2865 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2866
2867 return 0;
2868}
2869
2870/* Shorthand to add a single entry given a name and a function */
2871
2872int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002873PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002874{
2875 struct _inittab newtab[2];
2876
2877 memset(newtab, '\0', sizeof newtab);
2878
2879 newtab[0].name = name;
2880 newtab[0].initfunc = initfunc;
2881
2882 return PyImport_ExtendInittab(newtab);
2883}