blob: c03d4ccf28c0c11c4e80328aa409b04c448881fa [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
263#else
264
265#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000266#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000267
268#endif
269
Tim Peters69232342001-08-30 05:16:13 +0000270static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000271imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000272{
Tim Peters69232342001-08-30 05:16:13 +0000273#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000274 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000275#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000276 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000277#endif
278}
279
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000280static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000281imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000282{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000283#ifdef WITH_THREAD
284 lock_import();
285#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000286 Py_INCREF(Py_None);
287 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000288}
289
290static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000291imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000293#ifdef WITH_THREAD
294 if (unlock_import() < 0) {
295 PyErr_SetString(PyExc_RuntimeError,
296 "not holding the import lock");
297 return NULL;
298 }
299#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000300 Py_INCREF(Py_None);
301 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000302}
303
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304/* Helper for sys */
305
306PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000308{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000309 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000310 if (interp->modules == NULL)
311 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
312 return interp->modules;
313}
314
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000316/* List of names to clear in sys */
317static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000318 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000319 "exc_type", "exc_value", "exc_traceback",
320 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000321 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000322 NULL
323};
324
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000325static char* sys_files[] = {
326 "stdin", "__stdin__",
327 "stdout", "__stdout__",
328 "stderr", "__stderr__",
329 NULL
330};
331
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000332
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000337{
Guido van Rossum758eec01998-01-19 21:58:26 +0000338 int pos, ndone;
339 char *name;
340 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000341 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000342 PyObject *modules = interp->modules;
343
344 if (modules == NULL)
345 return; /* Already done */
346
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000347 /* Delete some special variables first. These are common
348 places where user values hide and people complain when their
349 destructors fail. Since the modules containing them are
350 deleted *last* of all, they would come too late in the normal
351 destruction order. Sigh. */
352
353 value = PyDict_GetItemString(modules, "__builtin__");
354 if (value != NULL && PyModule_Check(value)) {
355 dict = PyModule_GetDict(value);
356 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000357 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000358 PyDict_SetItemString(dict, "_", Py_None);
359 }
360 value = PyDict_GetItemString(modules, "sys");
361 if (value != NULL && PyModule_Check(value)) {
362 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000363 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000364 dict = PyModule_GetDict(value);
365 for (p = sys_deletes; *p != NULL; p++) {
366 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000367 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000368 PyDict_SetItemString(dict, *p, Py_None);
369 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000370 for (p = sys_files; *p != NULL; p+=2) {
371 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000372 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000373 v = PyDict_GetItemString(dict, *(p+1));
374 if (v == NULL)
375 v = Py_None;
376 PyDict_SetItemString(dict, *p, v);
377 }
378 }
379
380 /* First, delete __main__ */
381 value = PyDict_GetItemString(modules, "__main__");
382 if (value != NULL && PyModule_Check(value)) {
383 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000384 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000385 _PyModule_Clear(value);
386 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000387 }
388
Guido van Rossum758eec01998-01-19 21:58:26 +0000389 /* The special treatment of __builtin__ here is because even
390 when it's not referenced as a module, its dictionary is
391 referenced by almost every module's __builtins__. Since
392 deleting a module clears its dictionary (even if there are
393 references left to it), we need to delete the __builtin__
394 module last. Likewise, we don't delete sys until the very
395 end because it is implicitly referenced (e.g. by print).
396
397 Also note that we 'delete' modules by replacing their entry
398 in the modules dict with None, rather than really deleting
399 them; this avoids a rehash of the modules dictionary and
400 also marks them as "non existent" so they won't be
401 re-imported. */
402
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000403 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000404 one (skipping __builtin__ and sys) and delete them */
405 do {
406 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000407 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000408 while (PyDict_Next(modules, &pos, &key, &value)) {
409 if (value->ob_refcnt != 1)
410 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000411 if (PyString_Check(key) && PyModule_Check(value)) {
412 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000413 if (strcmp(name, "__builtin__") == 0)
414 continue;
415 if (strcmp(name, "sys") == 0)
416 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000417 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000418 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000419 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000420 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000421 PyDict_SetItem(modules, key, Py_None);
422 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423 }
424 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000425 } while (ndone > 0);
426
Guido van Rossum758eec01998-01-19 21:58:26 +0000427 /* Next, delete all modules (still skipping __builtin__ and sys) */
428 pos = 0;
429 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000430 if (PyString_Check(key) && PyModule_Check(value)) {
431 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000432 if (strcmp(name, "__builtin__") == 0)
433 continue;
434 if (strcmp(name, "sys") == 0)
435 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000436 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000437 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000438 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000439 PyDict_SetItem(modules, key, Py_None);
440 }
441 }
442
443 /* Next, delete sys and __builtin__ (in that order) */
444 value = PyDict_GetItemString(modules, "sys");
445 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000446 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000447 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000448 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000449 PyDict_SetItemString(modules, "sys", Py_None);
450 }
451 value = PyDict_GetItemString(modules, "__builtin__");
452 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000453 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000454 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000455 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000456 PyDict_SetItemString(modules, "__builtin__", Py_None);
457 }
458
459 /* Finally, clear and delete the modules directory */
460 PyDict_Clear(modules);
461 interp->modules = NULL;
462 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000463}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000464
465
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466/* Helper for pythonrun.c -- return magic number */
467
468long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000469PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470{
Guido van Rossum96774c12000-05-01 20:19:08 +0000471 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472}
473
474
Guido van Rossum25ce5661997-08-02 03:10:38 +0000475/* Magic for extension modules (built-in as well as dynamically
476 loaded). To prevent initializing an extension module more than
477 once, we keep a static dictionary 'extensions' keyed by module name
478 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000479 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000480 dictionary is stored by calling _PyImport_FixupExtension()
481 immediately after the module initialization function succeeds. A
482 copy can be retrieved from there by calling
483 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484
Guido van Rossum79f25d91997-04-29 20:08:16 +0000485PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000486_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000487{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000488 PyObject *modules, *mod, *dict, *copy;
489 if (extensions == NULL) {
490 extensions = PyDict_New();
491 if (extensions == NULL)
492 return NULL;
493 }
494 modules = PyImport_GetModuleDict();
495 mod = PyDict_GetItemString(modules, name);
496 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000497 PyErr_Format(PyExc_SystemError,
498 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000499 return NULL;
500 }
501 dict = PyModule_GetDict(mod);
502 if (dict == NULL)
503 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000504 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000505 if (copy == NULL)
506 return NULL;
507 PyDict_SetItemString(extensions, filename, copy);
508 Py_DECREF(copy);
509 return copy;
510}
511
512PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000513_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000514{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000515 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000516 if (extensions == NULL)
517 return NULL;
518 dict = PyDict_GetItemString(extensions, filename);
519 if (dict == NULL)
520 return NULL;
521 mod = PyImport_AddModule(name);
522 if (mod == NULL)
523 return NULL;
524 mdict = PyModule_GetDict(mod);
525 if (mdict == NULL)
526 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000527 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000530 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000531 name, filename);
532 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533}
534
535
536/* Get the module object corresponding to a module name.
537 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000538 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000539 Because the former action is most common, THIS DOES NOT RETURN A
540 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000543PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000545 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547
Guido van Rossum25ce5661997-08-02 03:10:38 +0000548 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552 if (m == NULL)
553 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 return NULL;
557 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559
560 return m;
561}
562
Tim Peters1cd70172004-08-02 03:52:12 +0000563/* Remove name from sys.modules, if it's there. */
564static void
565_RemoveModule(const char *name)
566{
567 PyObject *modules = PyImport_GetModuleDict();
568 if (PyDict_GetItemString(modules, name) == NULL)
569 return;
570 if (PyDict_DelItemString(modules, name) < 0)
571 Py_FatalError("import: deleting existing key in"
572 "sys.modules failed");
573}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000575/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000576 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
577 * removed from sys.modules, to avoid leaving damaged module objects
578 * in sys.modules. The caller may wish to restore the original
579 * module object (if any) in this case; PyImport_ReloadModule is an
580 * example.
581 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000583PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000585 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
586}
587
588PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000589PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000590{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 if (m == NULL)
596 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000597 /* If the module is being reloaded, we get the old module back
598 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 d = PyModule_GetDict(m);
600 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
601 if (PyDict_SetItemString(d, "__builtins__",
602 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000603 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000604 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000605 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000606 v = NULL;
607 if (pathname != NULL) {
608 v = PyString_FromString(pathname);
609 if (v == NULL)
610 PyErr_Clear();
611 }
612 if (v == NULL) {
613 v = ((PyCodeObject *)co)->co_filename;
614 Py_INCREF(v);
615 }
616 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000618 Py_DECREF(v);
619
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000620 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000622 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000624
Guido van Rossum25ce5661997-08-02 03:10:38 +0000625 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000626 PyErr_Format(PyExc_ImportError,
627 "Loaded module %.200s not found in sys.modules",
628 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000629 return NULL;
630 }
631
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633
634 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000635
636 error:
637 _RemoveModule(name);
638 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000639}
640
641
642/* Given a pathname for a Python source file, fill a buffer with the
643 pathname for the corresponding compiled file. Return the pathname
644 for the compiled file, or NULL if there's no space in the buffer.
645 Doesn't set an exception. */
646
647static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649{
Tim Petersc1731372001-08-04 08:12:36 +0000650 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 if (len+2 > buflen)
652 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000653
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000654#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000655 /* Treat .pyw as if it were .py. The case of ".pyw" must match
656 that used in _PyImport_StandardFiletab. */
657 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
658 --len; /* pretend 'w' isn't there */
659#endif
660 memcpy(buf, pathname, len);
661 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
662 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663
664 return buf;
665}
666
667
668/* Given a pathname for a Python source file, its time of last
669 modification, and a pathname for a compiled file, check whether the
670 compiled file represents the same version of the source. If so,
671 return a FILE pointer for the compiled file, positioned just after
672 the header; if not, return NULL.
673 Doesn't set an exception. */
674
675static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000676check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000677{
678 FILE *fp;
679 long magic;
680 long pyc_mtime;
681
682 fp = fopen(cpathname, "rb");
683 if (fp == NULL)
684 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000686 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000688 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689 fclose(fp);
690 return NULL;
691 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000695 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696 fclose(fp);
697 return NULL;
698 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000700 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701 return fp;
702}
703
704
705/* Read a code object from a file and check it for validity */
706
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000708read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000709{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711
Tim Petersd9b9ac82001-01-28 00:27:39 +0000712 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000713 if (co == NULL)
714 return NULL;
715 if (!PyCode_Check(co)) {
716 PyErr_Format(PyExc_ImportError,
717 "Non-code object in %.200s", cpathname);
718 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719 return NULL;
720 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722}
723
724
725/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000726 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000727
Guido van Rossum79f25d91997-04-29 20:08:16 +0000728static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000729load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730{
731 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyCodeObject *co;
733 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000736 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000737 PyErr_Format(PyExc_ImportError,
738 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739 return NULL;
740 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000742 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743 if (co == NULL)
744 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000746 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000748 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000750
751 return m;
752}
753
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754/* Parse a source file and return the corresponding code object */
755
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000757parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760 node *n;
761
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000762 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763 if (n == NULL)
764 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765 co = PyNode_Compile(n, pathname);
766 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000767
768 return co;
769}
770
771
Guido van Rossum55a83382000-09-20 20:31:38 +0000772/* Helper to open a bytecode file for writing in exclusive mode */
773
774static FILE *
775open_exclusive(char *filename)
776{
777#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
778 /* Use O_EXCL to avoid a race condition when another process tries to
779 write the same file. When that happens, our open() call fails,
780 which is just fine (since it's only a cache).
781 XXX If the file exists and is writable but the directory is not
782 writable, the file will never be written. Oh well.
783 */
784 int fd;
785 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000786 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
787#ifdef O_BINARY
788 |O_BINARY /* necessary for Windows */
789#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000790#ifdef __VMS
791 , 0666, "ctxt=bin", "shr=nil");
792#else
793 , 0666);
794#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000795 if (fd < 0)
796 return NULL;
797 return fdopen(fd, "wb");
798#else
799 /* Best we can do -- on Windows this can't happen anyway */
800 return fopen(filename, "wb");
801#endif
802}
803
804
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000805/* Write a compiled module to a file, placing the time of last
806 modification of its source into the header.
807 Errors are ignored, if a write error occurs an attempt is made to
808 remove the file. */
809
810static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000811write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812{
813 FILE *fp;
814
Guido van Rossum55a83382000-09-20 20:31:38 +0000815 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000818 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819 "# can't create %s\n", cpathname);
820 return;
821 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000822 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000823 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000824 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
825 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000826 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000828 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829 /* Don't keep partial file */
830 fclose(fp);
831 (void) unlink(cpathname);
832 return;
833 }
834 /* Now write the true mtime */
835 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000836 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837 fflush(fp);
838 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000840 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000841}
842
843
844/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000845 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
846 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000849load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000850{
Fred Drake4c82b232000-06-30 16:18:57 +0000851 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852 FILE *fpc;
853 char buf[MAXPATHLEN+1];
854 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855 PyCodeObject *co;
856 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000857
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000858 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000859 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000860 return NULL;
861#if SIZEOF_TIME_T > 4
862 /* Python's .pyc timestamp handling presumes that the timestamp fits
863 in 4 bytes. This will be fine until sometime in the year 2038,
864 when a 4-byte signed time_t will overflow.
865 */
866 if (mtime >> 32) {
867 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000868 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000869 return NULL;
870 }
871#endif
Tim Peters36515e22001-11-18 04:06:29 +0000872 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000873 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000874 if (cpathname != NULL &&
875 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000876 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000877 fclose(fpc);
878 if (co == NULL)
879 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000881 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000883 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000884 }
885 else {
886 co = parse_source_module(pathname, fp);
887 if (co == NULL)
888 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000890 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 name, pathname);
892 write_compiled_module(co, cpathname, mtime);
893 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000894 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000896
897 return m;
898}
899
900
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000901/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000902static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
903static struct filedescr *find_module(char *, char *, PyObject *,
904 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000905static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000906
907/* Load a package and return its module object WITH INCREMENTED
908 REFERENCE COUNT */
909
910static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000911load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000912{
Tim Peters1cd70172004-08-02 03:52:12 +0000913 PyObject *m, *d;
914 PyObject *file = NULL;
915 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000916 int err;
917 char buf[MAXPATHLEN+1];
918 FILE *fp = NULL;
919 struct filedescr *fdp;
920
921 m = PyImport_AddModule(name);
922 if (m == NULL)
923 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000924 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000925 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000926 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000927 d = PyModule_GetDict(m);
928 file = PyString_FromString(pathname);
929 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000930 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000932 if (path == NULL)
933 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000934 err = PyDict_SetItemString(d, "__file__", file);
935 if (err == 0)
936 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000937 if (err != 0)
938 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000939 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000940 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000941 if (fdp == NULL) {
942 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
943 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000944 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000945 }
946 else
947 m = NULL;
948 goto cleanup;
949 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000950 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000951 if (fp != NULL)
952 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000953 goto cleanup;
954
955 error:
956 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000958 Py_XDECREF(path);
959 Py_XDECREF(file);
960 return m;
961}
962
963
964/* Helper to test for built-in module */
965
966static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000967is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000968{
969 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000970 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
971 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
972 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000973 return -1;
974 else
975 return 1;
976 }
977 }
978 return 0;
979}
980
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000981
Just van Rossum52e14d62002-12-30 22:08:05 +0000982/* Return an importer object for a sys.path/pkg.__path__ item 'p',
983 possibly by fetching it from the path_importer_cache dict. If it
984 wasn't yet cached, traverse path_hooks until it a hook is found
985 that can handle the path item. Return None if no hook could;
986 this tells our caller it should fall back to the builtin
987 import mechanism. Cache the result in path_importer_cache.
988 Returns a borrowed reference. */
989
990static PyObject *
991get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
992 PyObject *p)
993{
994 PyObject *importer;
995 int j, nhooks;
996
997 /* These conditions are the caller's responsibility: */
998 assert(PyList_Check(path_hooks));
999 assert(PyDict_Check(path_importer_cache));
1000
1001 nhooks = PyList_Size(path_hooks);
1002 if (nhooks < 0)
1003 return NULL; /* Shouldn't happen */
1004
1005 importer = PyDict_GetItem(path_importer_cache, p);
1006 if (importer != NULL)
1007 return importer;
1008
1009 /* set path_importer_cache[p] to None to avoid recursion */
1010 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1011 return NULL;
1012
1013 for (j = 0; j < nhooks; j++) {
1014 PyObject *hook = PyList_GetItem(path_hooks, j);
1015 if (hook == NULL)
1016 return NULL;
1017 importer = PyObject_CallFunction(hook, "O", p);
1018 if (importer != NULL)
1019 break;
1020
1021 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1022 return NULL;
1023 }
1024 PyErr_Clear();
1025 }
1026 if (importer == NULL)
1027 importer = Py_None;
1028 else if (importer != Py_None) {
1029 int err = PyDict_SetItem(path_importer_cache, p, importer);
1030 Py_DECREF(importer);
1031 if (err != 0)
1032 return NULL;
1033 }
1034 return importer;
1035}
1036
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001037/* Search the path (default sys.path) for a module. Return the
1038 corresponding filedescr struct, and (via return arguments) the
1039 pathname and an open file. Return NULL if the module is not found. */
1040
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001041#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001042extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1043 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001044#endif
1045
Tim Peters50d8d372001-02-28 05:34:27 +00001046static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001047static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001048static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001049
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001050static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001051find_module(char *fullname, char *subname, PyObject *path, char *buf,
1052 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053{
Fred Drake4c82b232000-06-30 16:18:57 +00001054 int i, npath;
1055 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001056 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001057 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001058 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001059 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001060#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001061 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001062#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001063 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1064 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1065 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001066 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001067#if defined(PYOS_OS2)
1068 size_t saved_len;
1069 size_t saved_namelen;
1070 char *saved_buf = NULL;
1071#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001072 if (p_loader != NULL)
1073 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001074
Just van Rossum52e14d62002-12-30 22:08:05 +00001075 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001076 PyErr_SetString(PyExc_OverflowError,
1077 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001078 return NULL;
1079 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001080 strcpy(name, subname);
1081
1082 /* sys.meta_path import hook */
1083 if (p_loader != NULL) {
1084 PyObject *meta_path;
1085
1086 meta_path = PySys_GetObject("meta_path");
1087 if (meta_path == NULL || !PyList_Check(meta_path)) {
1088 PyErr_SetString(PyExc_ImportError,
1089 "sys.meta_path must be a list of "
1090 "import hooks");
1091 return NULL;
1092 }
1093 Py_INCREF(meta_path); /* zap guard */
1094 npath = PyList_Size(meta_path);
1095 for (i = 0; i < npath; i++) {
1096 PyObject *loader;
1097 PyObject *hook = PyList_GetItem(meta_path, i);
1098 loader = PyObject_CallMethod(hook, "find_module",
1099 "sO", fullname,
1100 path != NULL ?
1101 path : Py_None);
1102 if (loader == NULL) {
1103 Py_DECREF(meta_path);
1104 return NULL; /* true error */
1105 }
1106 if (loader != Py_None) {
1107 /* a loader was found */
1108 *p_loader = loader;
1109 Py_DECREF(meta_path);
1110 return &importhookdescr;
1111 }
1112 Py_DECREF(loader);
1113 }
1114 Py_DECREF(meta_path);
1115 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001116
1117 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001118 /* The only type of submodule allowed inside a "frozen"
1119 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001120 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1121 PyErr_SetString(PyExc_ImportError,
1122 "full frozen module name too long");
1123 return NULL;
1124 }
1125 strcpy(buf, PyString_AsString(path));
1126 strcat(buf, ".");
1127 strcat(buf, name);
1128 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001129 if (find_frozen(name) != NULL) {
1130 strcpy(buf, name);
1131 return &fd_frozen;
1132 }
1133 PyErr_Format(PyExc_ImportError,
1134 "No frozen submodule named %.200s", name);
1135 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001136 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001137 if (path == NULL) {
1138 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001139 strcpy(buf, name);
1140 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001141 }
Greg Ward201baee2001-10-04 14:52:06 +00001142 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001143 strcpy(buf, name);
1144 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001145 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001146
Guido van Rossumac279101996-08-22 23:10:58 +00001147#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001148 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1149 if (fp != NULL) {
1150 *p_fp = fp;
1151 return fdp;
1152 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001153#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001154 path = PySys_GetObject("path");
1155 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 if (path == NULL || !PyList_Check(path)) {
1157 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001158 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001159 return NULL;
1160 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001161
1162 path_hooks = PySys_GetObject("path_hooks");
1163 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1164 PyErr_SetString(PyExc_ImportError,
1165 "sys.path_hooks must be a list of "
1166 "import hooks");
1167 return NULL;
1168 }
1169 path_importer_cache = PySys_GetObject("path_importer_cache");
1170 if (path_importer_cache == NULL ||
1171 !PyDict_Check(path_importer_cache)) {
1172 PyErr_SetString(PyExc_ImportError,
1173 "sys.path_importer_cache must be a dict");
1174 return NULL;
1175 }
1176
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 namelen = strlen(name);
1179 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001180 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001182#ifdef Py_USING_UNICODE
1183 if (PyUnicode_Check(v)) {
1184 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1185 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1186 if (copy == NULL)
1187 return NULL;
1188 v = copy;
1189 }
1190 else
1191#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001193 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001195 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1196 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001198 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001200 if (strlen(buf) != len) {
1201 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001202 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001203 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001204
1205 /* sys.path_hooks import hook */
1206 if (p_loader != NULL) {
1207 PyObject *importer;
1208
1209 importer = get_path_importer(path_importer_cache,
1210 path_hooks, v);
1211 if (importer == NULL)
1212 return NULL;
1213 /* Note: importer is a borrowed reference */
1214 if (importer != Py_None) {
1215 PyObject *loader;
1216 loader = PyObject_CallMethod(importer,
1217 "find_module",
1218 "s", fullname);
1219 if (loader == NULL)
1220 return NULL; /* error */
1221 if (loader != Py_None) {
1222 /* a loader was found */
1223 *p_loader = loader;
1224 return &importhookdescr;
1225 }
1226 Py_DECREF(loader);
1227 }
1228 /* no hook was successful, use builtin import */
1229 }
1230
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001231 if (len > 0 && buf[len-1] != SEP
1232#ifdef ALTSEP
1233 && buf[len-1] != ALTSEP
1234#endif
1235 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001236 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001237 strcpy(buf+len, name);
1238 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001239
1240 /* Check for package import (buf holds a directory name,
1241 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001242#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001243 if (stat(buf, &statbuf) == 0 && /* it exists */
1244 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1245 find_init_module(buf) && /* it has __init__.py */
1246 case_ok(buf, len, namelen, name)) { /* and case matches */
1247 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001248 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001249 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001250#else
1251 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001252#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001253 if (isdir(buf) &&
1254 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001255 case_ok(buf, len, namelen, name)) {
1256 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001257 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001258 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001259#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001260#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001261#if defined(PYOS_OS2)
1262 /* take a snapshot of the module spec for restoration
1263 * after the 8 character DLL hackery
1264 */
1265 saved_buf = strdup(buf);
1266 saved_len = len;
1267 saved_namelen = namelen;
1268#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001270#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001271 /* OS/2 limits DLLs to 8 character names (w/o
1272 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001273 * so if the name is longer than that and its a
1274 * dynamically loaded module we're going to try,
1275 * truncate the name before trying
1276 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001277 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001278 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001279 const struct filedescr *scan;
1280 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001281 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001282 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001283 break;
1284 else
1285 scan++;
1286 }
1287 if (scan->suffix != NULL) {
1288 /* yes, so truncate the name */
1289 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001290 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001291 buf[len] = '\0';
1292 }
1293 }
1294#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001295 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001296 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001297 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001298 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001299 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001300 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001301 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001302 if (fp != NULL) {
1303 if (case_ok(buf, len, namelen, name))
1304 break;
1305 else { /* continue search */
1306 fclose(fp);
1307 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001308 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001309 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001310#if defined(PYOS_OS2)
1311 /* restore the saved snapshot */
1312 strcpy(buf, saved_buf);
1313 len = saved_len;
1314 namelen = saved_namelen;
1315#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001316 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001317#if defined(PYOS_OS2)
1318 /* don't need/want the module name snapshot anymore */
1319 if (saved_buf)
1320 {
1321 free(saved_buf);
1322 saved_buf = NULL;
1323 }
1324#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001325 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001326 if (fp != NULL)
1327 break;
1328 }
1329 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001330 PyErr_Format(PyExc_ImportError,
1331 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001332 return NULL;
1333 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001334 *p_fp = fp;
1335 return fdp;
1336}
1337
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001338/* Helpers for main.c
1339 * Find the source file corresponding to a named module
1340 */
1341struct filedescr *
1342_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1343 size_t buflen, FILE **p_fp, PyObject **p_loader)
1344{
1345 return find_module((char *) name, (char *) name, path,
1346 buf, buflen, p_fp, p_loader);
1347}
1348
1349PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1350{
1351 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1352}
1353
Tim Petersd1e87a82001-03-01 18:12:00 +00001354/* case_ok(char* buf, int len, int namelen, char* name)
1355 * The arguments here are tricky, best shown by example:
1356 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1357 * ^ ^ ^ ^
1358 * |--------------------- buf ---------------------|
1359 * |------------------- len ------------------|
1360 * |------ name -------|
1361 * |----- namelen -----|
1362 * buf is the full path, but len only counts up to (& exclusive of) the
1363 * extension. name is the module name, also exclusive of extension.
1364 *
1365 * We've already done a successful stat() or fopen() on buf, so know that
1366 * there's some match, possibly case-insensitive.
1367 *
Tim Peters50d8d372001-02-28 05:34:27 +00001368 * case_ok() is to return 1 if there's a case-sensitive match for
1369 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1370 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001371 *
Tim Peters50d8d372001-02-28 05:34:27 +00001372 * case_ok() is used to implement case-sensitive import semantics even
1373 * on platforms with case-insensitive filesystems. It's trivial to implement
1374 * for case-sensitive filesystems. It's pretty much a cross-platform
1375 * nightmare for systems with case-insensitive filesystems.
1376 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001377
Tim Peters50d8d372001-02-28 05:34:27 +00001378/* First we may need a pile of platform-specific header files; the sequence
1379 * of #if's here should match the sequence in the body of case_ok().
1380 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001381#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001382#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001383
Tim Peters50d8d372001-02-28 05:34:27 +00001384#elif defined(DJGPP)
1385#include <dir.h>
1386
Jason Tishler7961aa62005-05-20 00:56:54 +00001387#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001388#include <sys/types.h>
1389#include <dirent.h>
1390
Andrew MacIntyred9400542002-02-26 11:41:34 +00001391#elif defined(PYOS_OS2)
1392#define INCL_DOS
1393#define INCL_DOSERRORS
1394#define INCL_NOPMAPI
1395#include <os2.h>
1396
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001397#elif defined(RISCOS)
1398#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001399#endif
1400
Guido van Rossum0980bd91998-02-13 17:18:36 +00001401static int
Tim Peters50d8d372001-02-28 05:34:27 +00001402case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001403{
Tim Peters50d8d372001-02-28 05:34:27 +00001404/* Pick a platform-specific implementation; the sequence of #if's here should
1405 * match the sequence just above.
1406 */
1407
Jason Tishler7961aa62005-05-20 00:56:54 +00001408/* MS_WINDOWS */
1409#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001410 WIN32_FIND_DATA data;
1411 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001412
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001413 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001414 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001415
Guido van Rossum0980bd91998-02-13 17:18:36 +00001416 h = FindFirstFile(buf, &data);
1417 if (h == INVALID_HANDLE_VALUE) {
1418 PyErr_Format(PyExc_NameError,
1419 "Can't find file for module %.100s\n(filename %.300s)",
1420 name, buf);
1421 return 0;
1422 }
1423 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001424 return strncmp(data.cFileName, name, namelen) == 0;
1425
1426/* DJGPP */
1427#elif defined(DJGPP)
1428 struct ffblk ffblk;
1429 int done;
1430
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001431 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001432 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001433
1434 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1435 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001436 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001437 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001438 name, buf);
1439 return 0;
1440 }
Tim Peters50d8d372001-02-28 05:34:27 +00001441 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001442
Jason Tishler7961aa62005-05-20 00:56:54 +00001443/* new-fangled macintosh (macosx) or Cygwin */
1444#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001445 DIR *dirp;
1446 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001447 char dirname[MAXPATHLEN + 1];
1448 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001449
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001450 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001451 return 1;
1452
Tim Petersd1e87a82001-03-01 18:12:00 +00001453 /* Copy the dir component into dirname; substitute "." if empty */
1454 if (dirlen <= 0) {
1455 dirname[0] = '.';
1456 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001457 }
1458 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001459 assert(dirlen <= MAXPATHLEN);
1460 memcpy(dirname, buf, dirlen);
1461 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001462 }
1463 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001464 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001465 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001466 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001467 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001468 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001469#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001470 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001471#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001472 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001473#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001474 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001475 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001476 (void)closedir(dirp);
1477 return 1; /* Found */
1478 }
1479 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001480 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001481 }
Tim Peters430f5d42001-03-01 01:30:56 +00001482 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001483
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001484/* RISC OS */
1485#elif defined(RISCOS)
1486 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1487 char buf2[MAXPATHLEN+2];
1488 char *nameWithExt = buf+len-namelen;
1489 int canonlen;
1490 os_error *e;
1491
1492 if (Py_GETENV("PYTHONCASEOK") != NULL)
1493 return 1;
1494
1495 /* workaround:
1496 append wildcard, otherwise case of filename wouldn't be touched */
1497 strcpy(buf2, buf);
1498 strcat(buf2, "*");
1499
1500 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1501 canonlen = MAXPATHLEN+1-canonlen;
1502 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1503 return 0;
1504 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1505 return 1; /* match */
1506
1507 return 0;
1508
Andrew MacIntyred9400542002-02-26 11:41:34 +00001509/* OS/2 */
1510#elif defined(PYOS_OS2)
1511 HDIR hdir = 1;
1512 ULONG srchcnt = 1;
1513 FILEFINDBUF3 ffbuf;
1514 APIRET rc;
1515
1516 if (getenv("PYTHONCASEOK") != NULL)
1517 return 1;
1518
1519 rc = DosFindFirst(buf,
1520 &hdir,
1521 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1522 &ffbuf, sizeof(ffbuf),
1523 &srchcnt,
1524 FIL_STANDARD);
1525 if (rc != NO_ERROR)
1526 return 0;
1527 return strncmp(ffbuf.achName, name, namelen) == 0;
1528
Tim Peters50d8d372001-02-28 05:34:27 +00001529/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1530#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001531 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001532
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001533#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001534}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001535
Guido van Rossum0980bd91998-02-13 17:18:36 +00001536
Guido van Rossum197346f1997-10-31 18:38:52 +00001537#ifdef HAVE_STAT
1538/* Helper to look for __init__.py or __init__.py[co] in potential package */
1539static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001540find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001541{
Tim Peters0f9431f2001-07-05 03:47:53 +00001542 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001543 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001544 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001545 struct stat statbuf;
1546
Tim Peters0f9431f2001-07-05 03:47:53 +00001547/* For calling case_ok(buf, len, namelen, name):
1548 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1549 * ^ ^ ^ ^
1550 * |--------------------- buf ---------------------|
1551 * |------------------- len ------------------|
1552 * |------ name -------|
1553 * |----- namelen -----|
1554 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001555 if (save_len + 13 >= MAXPATHLEN)
1556 return 0;
1557 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001558 pname = buf + i;
1559 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001560 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001561 if (case_ok(buf,
1562 save_len + 9, /* len("/__init__") */
1563 8, /* len("__init__") */
1564 pname)) {
1565 buf[save_len] = '\0';
1566 return 1;
1567 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001568 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001569 i += strlen(pname);
1570 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001571 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001572 if (case_ok(buf,
1573 save_len + 9, /* len("/__init__") */
1574 8, /* len("__init__") */
1575 pname)) {
1576 buf[save_len] = '\0';
1577 return 1;
1578 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001579 }
1580 buf[save_len] = '\0';
1581 return 0;
1582}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001583
1584#else
1585
1586#ifdef RISCOS
1587static int
1588find_init_module(buf)
1589 char *buf;
1590{
1591 int save_len = strlen(buf);
1592 int i = save_len;
1593
1594 if (save_len + 13 >= MAXPATHLEN)
1595 return 0;
1596 buf[i++] = SEP;
1597 strcpy(buf+i, "__init__/py");
1598 if (isfile(buf)) {
1599 buf[save_len] = '\0';
1600 return 1;
1601 }
1602
1603 if (Py_OptimizeFlag)
1604 strcpy(buf+i, "o");
1605 else
1606 strcpy(buf+i, "c");
1607 if (isfile(buf)) {
1608 buf[save_len] = '\0';
1609 return 1;
1610 }
1611 buf[save_len] = '\0';
1612 return 0;
1613}
1614#endif /*RISCOS*/
1615
Guido van Rossum197346f1997-10-31 18:38:52 +00001616#endif /* HAVE_STAT */
1617
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001618
Tim Petersdbd9ba62000-07-09 03:09:57 +00001619static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001620
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001621/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001622 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001623
Guido van Rossum79f25d91997-04-29 20:08:16 +00001624static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001625load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001626{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001627 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001628 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001629 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001630
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001631 /* First check that there's an open file (if we need one) */
1632 switch (type) {
1633 case PY_SOURCE:
1634 case PY_COMPILED:
1635 if (fp == NULL) {
1636 PyErr_Format(PyExc_ValueError,
1637 "file object required for import (type code %d)",
1638 type);
1639 return NULL;
1640 }
1641 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001642
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001643 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001644
1645 case PY_SOURCE:
1646 m = load_source_module(name, buf, fp);
1647 break;
1648
1649 case PY_COMPILED:
1650 m = load_compiled_module(name, buf, fp);
1651 break;
1652
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001653#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001654 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001655 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001656 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001657#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001658
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001659 case PKG_DIRECTORY:
1660 m = load_package(name, buf);
1661 break;
1662
1663 case C_BUILTIN:
1664 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001665 if (buf != NULL && buf[0] != '\0')
1666 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001667 if (type == C_BUILTIN)
1668 err = init_builtin(name);
1669 else
1670 err = PyImport_ImportFrozenModule(name);
1671 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001672 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001673 if (err == 0) {
1674 PyErr_Format(PyExc_ImportError,
1675 "Purported %s module %.200s not found",
1676 type == C_BUILTIN ?
1677 "builtin" : "frozen",
1678 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001679 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001680 }
1681 modules = PyImport_GetModuleDict();
1682 m = PyDict_GetItemString(modules, name);
1683 if (m == NULL) {
1684 PyErr_Format(
1685 PyExc_ImportError,
1686 "%s module %.200s not properly initialized",
1687 type == C_BUILTIN ?
1688 "builtin" : "frozen",
1689 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001690 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001691 }
1692 Py_INCREF(m);
1693 break;
1694
Just van Rossum52e14d62002-12-30 22:08:05 +00001695 case IMP_HOOK: {
1696 if (loader == NULL) {
1697 PyErr_SetString(PyExc_ImportError,
1698 "import hook without loader");
1699 return NULL;
1700 }
1701 m = PyObject_CallMethod(loader, "load_module", "s", name);
1702 break;
1703 }
1704
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001705 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001706 PyErr_Format(PyExc_ImportError,
1707 "Don't know how to import %.200s (type code %d)",
1708 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001709 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001710
1711 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001712
1713 return m;
1714}
1715
1716
1717/* Initialize a built-in module.
1718 Return 1 for succes, 0 if the module is not found, and -1 with
1719 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001720
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001721static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001722init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001723{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001724 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001725
Greg Ward201baee2001-10-04 14:52:06 +00001726 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001727 return 1;
1728
Guido van Rossum771c6c81997-10-31 18:37:24 +00001729 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001730 if (strcmp(name, p->name) == 0) {
1731 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001732 PyErr_Format(PyExc_ImportError,
1733 "Cannot re-init internal module %.200s",
1734 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001735 return -1;
1736 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001737 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001738 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001739 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001741 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001742 if (_PyImport_FixupExtension(name, name) == NULL)
1743 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001744 return 1;
1745 }
1746 }
1747 return 0;
1748}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001749
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001751/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001752
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001753static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001754find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001755{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001756 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001757
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001759 if (p->name == NULL)
1760 return NULL;
1761 if (strcmp(p->name, name) == 0)
1762 break;
1763 }
1764 return p;
1765}
1766
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001768get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001769{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001770 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001771 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001772
1773 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001774 PyErr_Format(PyExc_ImportError,
1775 "No such frozen object named %.200s",
1776 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001777 return NULL;
1778 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001779 if (p->code == NULL) {
1780 PyErr_Format(PyExc_ImportError,
1781 "Excluded frozen object named %.200s",
1782 name);
1783 return NULL;
1784 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001785 size = p->size;
1786 if (size < 0)
1787 size = -size;
1788 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001789}
1790
1791/* Initialize a frozen module.
1792 Return 1 for succes, 0 if the module is not found, and -1 with
1793 an exception set if the initialization failed.
1794 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001795
1796int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001797PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001798{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001799 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 PyObject *co;
1801 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001802 int ispackage;
1803 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001804
1805 if (p == NULL)
1806 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001807 if (p->code == NULL) {
1808 PyErr_Format(PyExc_ImportError,
1809 "Excluded frozen object named %.200s",
1810 name);
1811 return -1;
1812 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001813 size = p->size;
1814 ispackage = (size < 0);
1815 if (ispackage)
1816 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001818 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001819 name, ispackage ? " package" : "");
1820 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001821 if (co == NULL)
1822 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 if (!PyCode_Check(co)) {
1824 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001825 PyErr_Format(PyExc_TypeError,
1826 "frozen object %.200s is not a code object",
1827 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001828 return -1;
1829 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001830 if (ispackage) {
1831 /* Set __path__ to the package name */
1832 PyObject *d, *s;
1833 int err;
1834 m = PyImport_AddModule(name);
1835 if (m == NULL)
1836 return -1;
1837 d = PyModule_GetDict(m);
1838 s = PyString_InternFromString(name);
1839 if (s == NULL)
1840 return -1;
1841 err = PyDict_SetItemString(d, "__path__", s);
1842 Py_DECREF(s);
1843 if (err != 0)
1844 return err;
1845 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001846 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001847 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001848 if (m == NULL)
1849 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001850 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001851 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001852}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001853
1854
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001855/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001856 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001857
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001859PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001860{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001861 PyObject *pname;
1862 PyObject *result;
1863
1864 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001865 if (pname == NULL)
1866 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001867 result = PyImport_Import(pname);
1868 Py_DECREF(pname);
1869 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001870}
1871
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001872/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001873static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1874static PyObject *load_next(PyObject *mod, PyObject *altmod,
1875 char **p_name, char *buf, int *p_buflen);
1876static int mark_miss(char *name);
1877static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1878 char *buf, int buflen, int recursive);
1879static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001880
1881/* The Magnum Opus of dotted-name import :-) */
1882
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001883static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001884import_module_ex(char *name, PyObject *globals, PyObject *locals,
1885 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001886{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001887 char buf[MAXPATHLEN+1];
1888 int buflen = 0;
1889 PyObject *parent, *head, *next, *tail;
1890
1891 parent = get_parent(globals, buf, &buflen);
1892 if (parent == NULL)
1893 return NULL;
1894
1895 head = load_next(parent, Py_None, &name, buf, &buflen);
1896 if (head == NULL)
1897 return NULL;
1898
1899 tail = head;
1900 Py_INCREF(tail);
1901 while (name) {
1902 next = load_next(tail, tail, &name, buf, &buflen);
1903 Py_DECREF(tail);
1904 if (next == NULL) {
1905 Py_DECREF(head);
1906 return NULL;
1907 }
1908 tail = next;
1909 }
1910
1911 if (fromlist != NULL) {
1912 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1913 fromlist = NULL;
1914 }
1915
1916 if (fromlist == NULL) {
1917 Py_DECREF(tail);
1918 return head;
1919 }
1920
1921 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001922 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001923 Py_DECREF(tail);
1924 return NULL;
1925 }
1926
1927 return tail;
1928}
1929
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001930PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001931PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1932 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001933{
1934 PyObject *result;
1935 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001936 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001937 if (unlock_import() < 0) {
1938 Py_XDECREF(result);
1939 PyErr_SetString(PyExc_RuntimeError,
1940 "not holding the import lock");
1941 return NULL;
1942 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001943 return result;
1944}
1945
Fred Drake87590902004-05-28 20:21:36 +00001946/* Return the package that an import is being performed in. If globals comes
1947 from the module foo.bar.bat (not itself a package), this returns the
1948 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1949 the package's entry in sys.modules is returned.
1950
1951 The *name* of the returned package is returned in buf, with the length of
1952 the name in *p_buflen.
1953
1954 If globals doesn't come from a package or a module in a package, or a
1955 corresponding entry is not found in sys.modules, Py_None is returned.
1956*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001957static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001958get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001959{
1960 static PyObject *namestr = NULL;
1961 static PyObject *pathstr = NULL;
1962 PyObject *modname, *modpath, *modules, *parent;
1963
1964 if (globals == NULL || !PyDict_Check(globals))
1965 return Py_None;
1966
1967 if (namestr == NULL) {
1968 namestr = PyString_InternFromString("__name__");
1969 if (namestr == NULL)
1970 return NULL;
1971 }
1972 if (pathstr == NULL) {
1973 pathstr = PyString_InternFromString("__path__");
1974 if (pathstr == NULL)
1975 return NULL;
1976 }
1977
1978 *buf = '\0';
1979 *p_buflen = 0;
1980 modname = PyDict_GetItem(globals, namestr);
1981 if (modname == NULL || !PyString_Check(modname))
1982 return Py_None;
1983
1984 modpath = PyDict_GetItem(globals, pathstr);
1985 if (modpath != NULL) {
1986 int len = PyString_GET_SIZE(modname);
1987 if (len > MAXPATHLEN) {
1988 PyErr_SetString(PyExc_ValueError,
1989 "Module name too long");
1990 return NULL;
1991 }
1992 strcpy(buf, PyString_AS_STRING(modname));
1993 *p_buflen = len;
1994 }
1995 else {
1996 char *start = PyString_AS_STRING(modname);
1997 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001998 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001999 if (lastdot == NULL)
2000 return Py_None;
2001 len = lastdot - start;
2002 if (len >= MAXPATHLEN) {
2003 PyErr_SetString(PyExc_ValueError,
2004 "Module name too long");
2005 return NULL;
2006 }
2007 strncpy(buf, start, len);
2008 buf[len] = '\0';
2009 *p_buflen = len;
2010 }
2011
2012 modules = PyImport_GetModuleDict();
2013 parent = PyDict_GetItemString(modules, buf);
2014 if (parent == NULL)
2015 parent = Py_None;
2016 return parent;
2017 /* We expect, but can't guarantee, if parent != None, that:
2018 - parent.__name__ == buf
2019 - parent.__dict__ is globals
2020 If this is violated... Who cares? */
2021}
2022
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002023/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002024static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002025load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2026 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002027{
2028 char *name = *p_name;
2029 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002030 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002031 char *p;
2032 PyObject *result;
2033
2034 if (dot == NULL) {
2035 *p_name = NULL;
2036 len = strlen(name);
2037 }
2038 else {
2039 *p_name = dot+1;
2040 len = dot-name;
2041 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002042 if (len == 0) {
2043 PyErr_SetString(PyExc_ValueError,
2044 "Empty module name");
2045 return NULL;
2046 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002047
2048 p = buf + *p_buflen;
2049 if (p != buf)
2050 *p++ = '.';
2051 if (p+len-buf >= MAXPATHLEN) {
2052 PyErr_SetString(PyExc_ValueError,
2053 "Module name too long");
2054 return NULL;
2055 }
2056 strncpy(p, name, len);
2057 p[len] = '\0';
2058 *p_buflen = p+len-buf;
2059
2060 result = import_submodule(mod, p, buf);
2061 if (result == Py_None && altmod != mod) {
2062 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002063 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002064 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002065 if (result != NULL && result != Py_None) {
2066 if (mark_miss(buf) != 0) {
2067 Py_DECREF(result);
2068 return NULL;
2069 }
2070 strncpy(buf, name, len);
2071 buf[len] = '\0';
2072 *p_buflen = len;
2073 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002074 }
2075 if (result == NULL)
2076 return NULL;
2077
2078 if (result == Py_None) {
2079 Py_DECREF(result);
2080 PyErr_Format(PyExc_ImportError,
2081 "No module named %.200s", name);
2082 return NULL;
2083 }
2084
2085 return result;
2086}
2087
2088static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002089mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002090{
2091 PyObject *modules = PyImport_GetModuleDict();
2092 return PyDict_SetItemString(modules, name, Py_None);
2093}
2094
2095static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002096ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2097 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002098{
2099 int i;
2100
2101 if (!PyObject_HasAttrString(mod, "__path__"))
2102 return 1;
2103
2104 for (i = 0; ; i++) {
2105 PyObject *item = PySequence_GetItem(fromlist, i);
2106 int hasit;
2107 if (item == NULL) {
2108 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2109 PyErr_Clear();
2110 return 1;
2111 }
2112 return 0;
2113 }
2114 if (!PyString_Check(item)) {
2115 PyErr_SetString(PyExc_TypeError,
2116 "Item in ``from list'' not a string");
2117 Py_DECREF(item);
2118 return 0;
2119 }
2120 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002121 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002122 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002123 /* See if the package defines __all__ */
2124 if (recursive)
2125 continue; /* Avoid endless recursion */
2126 all = PyObject_GetAttrString(mod, "__all__");
2127 if (all == NULL)
2128 PyErr_Clear();
2129 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002130 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002131 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002132 if (!ret)
2133 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002134 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002135 continue;
2136 }
2137 hasit = PyObject_HasAttr(mod, item);
2138 if (!hasit) {
2139 char *subname = PyString_AS_STRING(item);
2140 PyObject *submod;
2141 char *p;
2142 if (buflen + strlen(subname) >= MAXPATHLEN) {
2143 PyErr_SetString(PyExc_ValueError,
2144 "Module name too long");
2145 Py_DECREF(item);
2146 return 0;
2147 }
2148 p = buf + buflen;
2149 *p++ = '.';
2150 strcpy(p, subname);
2151 submod = import_submodule(mod, subname, buf);
2152 Py_XDECREF(submod);
2153 if (submod == NULL) {
2154 Py_DECREF(item);
2155 return 0;
2156 }
2157 }
2158 Py_DECREF(item);
2159 }
2160
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002161 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002162}
2163
Neil Schemenauer00b09662003-06-16 21:03:07 +00002164static int
2165add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2166 PyObject *modules)
2167{
2168 if (mod == Py_None)
2169 return 1;
2170 /* Irrespective of the success of this load, make a
2171 reference to it in the parent package module. A copy gets
2172 saved in the modules dictionary under the full name, so get a
2173 reference from there, if need be. (The exception is when the
2174 load failed with a SyntaxError -- then there's no trace in
2175 sys.modules. In that case, of course, do nothing extra.) */
2176 if (submod == NULL) {
2177 submod = PyDict_GetItemString(modules, fullname);
2178 if (submod == NULL)
2179 return 1;
2180 }
2181 if (PyModule_Check(mod)) {
2182 /* We can't use setattr here since it can give a
2183 * spurious warning if the submodule name shadows a
2184 * builtin name */
2185 PyObject *dict = PyModule_GetDict(mod);
2186 if (!dict)
2187 return 0;
2188 if (PyDict_SetItemString(dict, subname, submod) < 0)
2189 return 0;
2190 }
2191 else {
2192 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2193 return 0;
2194 }
2195 return 1;
2196}
2197
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002198static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002199import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002200{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002201 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002202 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002203
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002204 /* Require:
2205 if mod == None: subname == fullname
2206 else: mod.__name__ + "." + subname == fullname
2207 */
2208
Tim Peters50d8d372001-02-28 05:34:27 +00002209 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002210 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002211 }
2212 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002213 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002214 char buf[MAXPATHLEN+1];
2215 struct filedescr *fdp;
2216 FILE *fp = NULL;
2217
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002218 if (mod == Py_None)
2219 path = NULL;
2220 else {
2221 path = PyObject_GetAttrString(mod, "__path__");
2222 if (path == NULL) {
2223 PyErr_Clear();
2224 Py_INCREF(Py_None);
2225 return Py_None;
2226 }
2227 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002228
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002229 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002230 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2231 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002232 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002233 if (fdp == NULL) {
2234 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2235 return NULL;
2236 PyErr_Clear();
2237 Py_INCREF(Py_None);
2238 return Py_None;
2239 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002240 m = load_module(fullname, fp, buf, fdp->type, loader);
2241 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002242 if (fp)
2243 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002244 if (!add_submodule(mod, m, fullname, subname, modules)) {
2245 Py_XDECREF(m);
2246 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002247 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002248 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002249
2250 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002251}
2252
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002253
2254/* Re-import a module of any kind and return its module object, WITH
2255 INCREMENTED REFERENCE COUNT */
2256
Guido van Rossum79f25d91997-04-29 20:08:16 +00002257PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002258PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002259{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002260 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002261 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002262 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002263 char buf[MAXPATHLEN+1];
2264 struct filedescr *fdp;
2265 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002266 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002267
Guido van Rossum79f25d91997-04-29 20:08:16 +00002268 if (m == NULL || !PyModule_Check(m)) {
2269 PyErr_SetString(PyExc_TypeError,
2270 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002271 return NULL;
2272 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002273 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002274 if (name == NULL)
2275 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002276 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002277 PyErr_Format(PyExc_ImportError,
2278 "reload(): module %.200s not in sys.modules",
2279 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002280 return NULL;
2281 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002282 subname = strrchr(name, '.');
2283 if (subname == NULL)
2284 subname = name;
2285 else {
2286 PyObject *parentname, *parent;
2287 parentname = PyString_FromStringAndSize(name, (subname-name));
2288 if (parentname == NULL)
2289 return NULL;
2290 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002291 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002292 if (parent == NULL) {
2293 PyErr_Format(PyExc_ImportError,
2294 "reload(): parent %.200s not in sys.modules",
2295 name);
2296 return NULL;
2297 }
2298 subname++;
2299 path = PyObject_GetAttrString(parent, "__path__");
2300 if (path == NULL)
2301 PyErr_Clear();
2302 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002303 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002304 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002305 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002306
2307 if (fdp == NULL) {
2308 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002309 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002310 }
2311
2312 newm = load_module(name, fp, buf, fdp->type, loader);
2313 Py_XDECREF(loader);
2314
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002315 if (fp)
2316 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002317 if (newm == NULL) {
2318 /* load_module probably removed name from modules because of
2319 * the error. Put back the original module object. We're
2320 * going to return NULL in this case regardless of whether
2321 * replacing name succeeds, so the return value is ignored.
2322 */
2323 PyDict_SetItemString(modules, name, m);
2324 }
2325 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002326}
2327
2328
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002329/* Higher-level import emulator which emulates the "import" statement
2330 more accurately -- it invokes the __import__() function from the
2331 builtins of the current globals. This means that the import is
2332 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002333 environment, e.g. by "rexec".
2334 A dummy list ["__doc__"] is passed as the 4th argument so that
2335 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2336 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002337
2338PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002339PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002340{
2341 static PyObject *silly_list = NULL;
2342 static PyObject *builtins_str = NULL;
2343 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002344 PyObject *globals = NULL;
2345 PyObject *import = NULL;
2346 PyObject *builtins = NULL;
2347 PyObject *r = NULL;
2348
2349 /* Initialize constant string objects */
2350 if (silly_list == NULL) {
2351 import_str = PyString_InternFromString("__import__");
2352 if (import_str == NULL)
2353 return NULL;
2354 builtins_str = PyString_InternFromString("__builtins__");
2355 if (builtins_str == NULL)
2356 return NULL;
2357 silly_list = Py_BuildValue("[s]", "__doc__");
2358 if (silly_list == NULL)
2359 return NULL;
2360 }
2361
2362 /* Get the builtins from current globals */
2363 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002364 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002365 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002366 builtins = PyObject_GetItem(globals, builtins_str);
2367 if (builtins == NULL)
2368 goto err;
2369 }
2370 else {
2371 /* No globals -- use standard builtins, and fake globals */
2372 PyErr_Clear();
2373
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002374 builtins = PyImport_ImportModuleEx("__builtin__",
2375 NULL, NULL, NULL);
2376 if (builtins == NULL)
2377 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002378 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2379 if (globals == NULL)
2380 goto err;
2381 }
2382
2383 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002384 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002385 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002386 if (import == NULL)
2387 PyErr_SetObject(PyExc_KeyError, import_str);
2388 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002389 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002390 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002391 if (import == NULL)
2392 goto err;
2393
2394 /* Call the _import__ function with the proper argument list */
2395 r = PyObject_CallFunction(import, "OOOO",
2396 module_name, globals, globals, silly_list);
2397
2398 err:
2399 Py_XDECREF(globals);
2400 Py_XDECREF(builtins);
2401 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002402
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002403 return r;
2404}
2405
2406
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407/* Module 'imp' provides Python access to the primitives used for
2408 importing modules.
2409*/
2410
Guido van Rossum79f25d91997-04-29 20:08:16 +00002411static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002412imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002413{
2414 char buf[4];
2415
Guido van Rossum96774c12000-05-01 20:19:08 +00002416 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2417 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2418 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2419 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002420
Guido van Rossum79f25d91997-04-29 20:08:16 +00002421 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002422}
2423
Guido van Rossum79f25d91997-04-29 20:08:16 +00002424static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002425imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002426{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002427 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002428 struct filedescr *fdp;
2429
Guido van Rossum79f25d91997-04-29 20:08:16 +00002430 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002431 if (list == NULL)
2432 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002433 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2434 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002435 fdp->suffix, fdp->mode, fdp->type);
2436 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438 return NULL;
2439 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002440 if (PyList_Append(list, item) < 0) {
2441 Py_DECREF(list);
2442 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002443 return NULL;
2444 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002445 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002446 }
2447 return list;
2448}
2449
Guido van Rossum79f25d91997-04-29 20:08:16 +00002450static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002451call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002452{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002453 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002454 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002455 struct filedescr *fdp;
2456 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002457 FILE *fp = NULL;
2458
2459 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002460 if (path == Py_None)
2461 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002462 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002463 if (fdp == NULL)
2464 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002465 if (fp != NULL) {
2466 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2467 if (fob == NULL) {
2468 fclose(fp);
2469 return NULL;
2470 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002471 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002472 else {
2473 fob = Py_None;
2474 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002475 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002476 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002477 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002478 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002479 return ret;
2480}
2481
Guido van Rossum79f25d91997-04-29 20:08:16 +00002482static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002483imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002484{
2485 char *name;
2486 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002487 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002488 return NULL;
2489 return call_find_module(name, path);
2490}
2491
2492static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002493imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002494{
2495 char *name;
2496 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002497 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002498 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002499 return NULL;
2500 ret = init_builtin(name);
2501 if (ret < 0)
2502 return NULL;
2503 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002504 Py_INCREF(Py_None);
2505 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002506 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002507 m = PyImport_AddModule(name);
2508 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002509 return m;
2510}
2511
Guido van Rossum79f25d91997-04-29 20:08:16 +00002512static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002513imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002514{
2515 char *name;
2516 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002517 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002518 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002520 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002521 if (ret < 0)
2522 return NULL;
2523 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002524 Py_INCREF(Py_None);
2525 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002526 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527 m = PyImport_AddModule(name);
2528 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002529 return m;
2530}
2531
Guido van Rossum79f25d91997-04-29 20:08:16 +00002532static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002533imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002534{
2535 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002536
Guido van Rossum43713e52000-02-29 13:59:29 +00002537 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002538 return NULL;
2539 return get_frozen_object(name);
2540}
2541
Guido van Rossum79f25d91997-04-29 20:08:16 +00002542static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002543imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002544{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002545 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002546 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002547 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002548 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002549}
2550
Guido van Rossum79f25d91997-04-29 20:08:16 +00002551static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002552imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002553{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002554 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002555 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002556 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002557 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002558 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002559 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002560}
2561
2562static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002563get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002564{
2565 FILE *fp;
2566 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002567 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002568 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002569 fp = fopen(pathname, mode);
2570 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572 }
2573 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002575 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002576 PyErr_SetString(PyExc_ValueError,
2577 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002578 }
2579 return fp;
2580}
2581
Guido van Rossum79f25d91997-04-29 20:08:16 +00002582static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002583imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002584{
2585 char *name;
2586 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 PyObject *fob = NULL;
2588 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002589 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002590 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002592 return NULL;
2593 fp = get_file(pathname, fob, "rb");
2594 if (fp == NULL)
2595 return NULL;
2596 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002597 if (fob == NULL)
2598 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002599 return m;
2600}
2601
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002602#ifdef HAVE_DYNAMIC_LOADING
2603
Guido van Rossum79f25d91997-04-29 20:08:16 +00002604static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002605imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002606{
2607 char *name;
2608 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002609 PyObject *fob = NULL;
2610 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002611 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002612 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002613 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002614 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002615 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002616 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002617 if (fp == NULL)
2618 return NULL;
2619 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002620 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002621 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002622}
2623
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002624#endif /* HAVE_DYNAMIC_LOADING */
2625
Guido van Rossum79f25d91997-04-29 20:08:16 +00002626static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002627imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002628{
2629 char *name;
2630 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002631 PyObject *fob = NULL;
2632 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002633 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002634 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002635 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002636 return NULL;
2637 fp = get_file(pathname, fob, "r");
2638 if (fp == NULL)
2639 return NULL;
2640 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002641 if (fob == NULL)
2642 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002643 return m;
2644}
2645
Guido van Rossum79f25d91997-04-29 20:08:16 +00002646static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002647imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002648{
2649 char *name;
2650 PyObject *fob;
2651 char *pathname;
2652 char *suffix; /* Unused */
2653 char *mode;
2654 int type;
2655 FILE *fp;
2656
Guido van Rossum43713e52000-02-29 13:59:29 +00002657 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002658 &name, &fob, &pathname,
2659 &suffix, &mode, &type))
2660 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002661 if (*mode) {
2662 /* Mode must start with 'r' or 'U' and must not contain '+'.
2663 Implicit in this test is the assumption that the mode
2664 may contain other modifiers like 'b' or 't'. */
2665
2666 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002667 PyErr_Format(PyExc_ValueError,
2668 "invalid file open mode %.200s", mode);
2669 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002670 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002671 }
2672 if (fob == Py_None)
2673 fp = NULL;
2674 else {
2675 if (!PyFile_Check(fob)) {
2676 PyErr_SetString(PyExc_ValueError,
2677 "load_module arg#2 should be a file or None");
2678 return NULL;
2679 }
2680 fp = get_file(pathname, fob, mode);
2681 if (fp == NULL)
2682 return NULL;
2683 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002684 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002685}
2686
2687static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002688imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002689{
2690 char *name;
2691 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002692 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002693 return NULL;
2694 return load_package(name, pathname);
2695}
2696
2697static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002698imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002699{
2700 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002701 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002702 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002703 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002704}
2705
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002706/* Doc strings */
2707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002708PyDoc_STRVAR(doc_imp,
2709"This module provides the components needed to build your own\n\
2710__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002712PyDoc_STRVAR(doc_find_module,
2713"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002714Search for a module. If path is omitted or None, search for a\n\
2715built-in, frozen or special module and continue search in sys.path.\n\
2716The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719PyDoc_STRVAR(doc_load_module,
2720"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002721Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002722The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002724PyDoc_STRVAR(doc_get_magic,
2725"get_magic() -> string\n\
2726Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728PyDoc_STRVAR(doc_get_suffixes,
2729"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002730Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002731that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002732
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002733PyDoc_STRVAR(doc_new_module,
2734"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002735Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002736The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002738PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002739"lock_held() -> boolean\n\
2740Return True if the import lock is currently held, else False.\n\
2741On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002742
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002743PyDoc_STRVAR(doc_acquire_lock,
2744"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002745Acquires the interpreter's import lock for the current thread.\n\
2746This lock should be used by import hooks to ensure thread-safety\n\
2747when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002748On platforms without threads, this function does nothing.");
2749
2750PyDoc_STRVAR(doc_release_lock,
2751"release_lock() -> None\n\
2752Release the interpreter's import lock.\n\
2753On platforms without threads, this function does nothing.");
2754
Guido van Rossum79f25d91997-04-29 20:08:16 +00002755static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002756 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2757 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2758 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2759 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2760 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2761 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2762 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2763 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002764 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002765 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2766 {"init_builtin", imp_init_builtin, METH_VARARGS},
2767 {"init_frozen", imp_init_frozen, METH_VARARGS},
2768 {"is_builtin", imp_is_builtin, METH_VARARGS},
2769 {"is_frozen", imp_is_frozen, METH_VARARGS},
2770 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002771#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002772 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002773#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002774 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002775 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002776 {NULL, NULL} /* sentinel */
2777};
2778
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002779static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002780setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002781{
2782 PyObject *v;
2783 int err;
2784
2785 v = PyInt_FromLong((long)value);
2786 err = PyDict_SetItemString(d, name, v);
2787 Py_XDECREF(v);
2788 return err;
2789}
2790
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002791PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002792initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002793{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002794 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002795
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002796 m = Py_InitModule4("imp", imp_methods, doc_imp,
2797 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002798 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002799
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002800 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2801 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2802 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2803 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2804 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2805 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2806 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2807 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002808 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002809 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002810
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002811 failure:
2812 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002813}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002814
2815
Guido van Rossumb18618d2000-05-03 23:44:39 +00002816/* API for embedding applications that want to add their own entries
2817 to the table of built-in modules. This should normally be called
2818 *before* Py_Initialize(). When the table resize fails, -1 is
2819 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002820
2821 After a similar function by Just van Rossum. */
2822
2823int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002824PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002825{
2826 static struct _inittab *our_copy = NULL;
2827 struct _inittab *p;
2828 int i, n;
2829
2830 /* Count the number of entries in both tables */
2831 for (n = 0; newtab[n].name != NULL; n++)
2832 ;
2833 if (n == 0)
2834 return 0; /* Nothing to do */
2835 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2836 ;
2837
2838 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002839 p = our_copy;
2840 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002841 if (p == NULL)
2842 return -1;
2843
2844 /* Copy the tables into the new memory */
2845 if (our_copy != PyImport_Inittab)
2846 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2847 PyImport_Inittab = our_copy = p;
2848 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2849
2850 return 0;
2851}
2852
2853/* Shorthand to add a single entry given a name and a function */
2854
2855int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002856PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002857{
2858 struct _inittab newtab[2];
2859
2860 memset(newtab, '\0', sizeof newtab);
2861
2862 newtab[0].name = name;
2863 newtab[0].initfunc = initfunc;
2864
2865 return PyImport_ExtendInittab(newtab);
2866}