blob: 9b624a4cb614223aa84f6cefbaf04d92557608f1 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
10#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000011#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000014
Guido van Rossum55a83382000-09-20 20:31:38 +000015#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
18
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000019extern time_t PyOS_GetLastModificationTime(char *, FILE *);
20 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000021
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000022/* Magic word to reject .pyc files generated by other Python versions.
23 It should change for each incompatible change to the bytecode.
24
25 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000026 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000027 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000028
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000029 The magic numbers must be spaced apart atleast 2 values, as the
30 -U interpeter flag will cause MAGIC+1 being used. They have been
31 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000032
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000033 There were a variety of old schemes for setting the magic number.
34 The current working scheme is to increment the previous value by
35 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000036
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037 Known values:
38 Python 1.5: 20121
39 Python 1.5.1: 20121
40 Python 1.5.2: 20121
41 Python 2.0: 50823
42 Python 2.0.1: 50823
43 Python 2.1: 60202
44 Python 2.1.1: 60202
45 Python 2.1.2: 60202
46 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000047 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000048 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000049 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000050 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000051 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000052 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000053 Python 2.5a0: 62071
Tim Peters36515e22001-11-18 04:06:29 +000054*/
Michael W. Hudsondf888462005-06-03 14:41:55 +000055#define MAGIC (62071 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000056
Guido van Rossum96774c12000-05-01 20:19:08 +000057/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000058 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000059 compiler works which are enabled by command line switches. */
60static long pyc_magic = MAGIC;
61
Guido van Rossum25ce5661997-08-02 03:10:38 +000062/* See _PyImport_FixupExtension() below */
63static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000064
Guido van Rossum771c6c81997-10-31 18:37:24 +000065/* This table is defined in config.c: */
66extern struct _inittab _PyImport_Inittab[];
67
68struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000069
Guido van Rossumed1170e1999-12-20 21:23:41 +000070/* these tables define the module suffixes that Python recognizes */
71struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000072
73#ifdef RISCOS
74static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000075 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000076 {"/pyc", "rb", PY_COMPILED},
77 {0, 0}
78};
79#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000080static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000081 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000082#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000083 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000084#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000085 {".pyc", "rb", PY_COMPILED},
86 {0, 0}
87};
Guido van Rossum48a680c2001-03-02 06:34:14 +000088#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000089
Guido van Rossum1ae940a1995-01-02 19:04:15 +000090/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091
92void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000093_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Guido van Rossumed1170e1999-12-20 21:23:41 +000095 const struct filedescr *scan;
96 struct filedescr *filetab;
97 int countD = 0;
98 int countS = 0;
99
100 /* prepare _PyImport_Filetab: copy entries from
101 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
102 */
103 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
104 ++countD;
105 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
106 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000108 memcpy(filetab, _PyImport_DynLoadFiletab,
109 countD * sizeof(struct filedescr));
110 memcpy(filetab + countD, _PyImport_StandardFiletab,
111 countS * sizeof(struct filedescr));
112 filetab[countD + countS].suffix = NULL;
113
114 _PyImport_Filetab = filetab;
115
Guido van Rossum0824f631997-03-11 18:37:35 +0000116 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000117 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
118 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000119#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000120 if (strcmp(filetab->suffix, ".pyc") == 0)
121 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000122#else
123 if (strcmp(filetab->suffix, "/pyc") == 0)
124 filetab->suffix = "/pyo";
125#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000126 }
127 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000128
129 if (Py_UnicodeFlag) {
130 /* Fix the pyc_magic so that byte compiled code created
131 using the all-Unicode method doesn't interfere with
132 code created in normal operation mode. */
133 pyc_magic = MAGIC + 1;
134 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137void
Just van Rossum52e14d62002-12-30 22:08:05 +0000138_PyImportHooks_Init(void)
139{
140 PyObject *v, *path_hooks = NULL, *zimpimport;
141 int err = 0;
142
143 /* adding sys.path_hooks and sys.path_importer_cache, setting up
144 zipimport */
145
146 if (Py_VerboseFlag)
147 PySys_WriteStderr("# installing zipimport hook\n");
148
149 v = PyList_New(0);
150 if (v == NULL)
151 goto error;
152 err = PySys_SetObject("meta_path", v);
153 Py_DECREF(v);
154 if (err)
155 goto error;
156 v = PyDict_New();
157 if (v == NULL)
158 goto error;
159 err = PySys_SetObject("path_importer_cache", v);
160 Py_DECREF(v);
161 if (err)
162 goto error;
163 path_hooks = PyList_New(0);
164 if (path_hooks == NULL)
165 goto error;
166 err = PySys_SetObject("path_hooks", path_hooks);
167 if (err) {
168 error:
169 PyErr_Print();
170 Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
171 "path_importer_cache failed");
172 }
173 zimpimport = PyImport_ImportModule("zipimport");
174 if (zimpimport == NULL) {
175 PyErr_Clear(); /* No zip import module -- okay */
176 if (Py_VerboseFlag)
177 PySys_WriteStderr("# can't import zipimport\n");
178 }
179 else {
180 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
181 "zipimporter");
182 Py_DECREF(zimpimport);
183 if (zipimporter == NULL) {
184 PyErr_Clear(); /* No zipimporter object -- okay */
185 if (Py_VerboseFlag)
186 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000187 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000188 }
189 else {
190 /* sys.path_hooks.append(zipimporter) */
191 err = PyList_Append(path_hooks, zipimporter);
192 Py_DECREF(zipimporter);
193 if (err)
194 goto error;
195 if (Py_VerboseFlag)
196 PySys_WriteStderr(
197 "# installed zipimport hook\n");
198 }
199 }
200 Py_DECREF(path_hooks);
201}
202
203void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000204_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205{
206 Py_XDECREF(extensions);
207 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000208 PyMem_DEL(_PyImport_Filetab);
209 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210}
211
212
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000213/* Locking primitives to prevent parallel imports of the same module
214 in different threads to return with a partially loaded module.
215 These calls are serialized by the global interpreter lock. */
216
217#ifdef WITH_THREAD
218
Guido van Rossum49b56061998-10-01 20:42:43 +0000219#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000220
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000222static long import_lock_thread = -1;
223static int import_lock_level = 0;
224
225static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000227{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000228 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000229 if (me == -1)
230 return; /* Too bad */
231 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000233 if (import_lock_thread == me) {
234 import_lock_level++;
235 return;
236 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000237 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
238 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000239 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000241 PyEval_RestoreThread(tstate);
242 }
243 import_lock_thread = me;
244 import_lock_level = 1;
245}
246
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000247static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251 if (me == -1)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000252 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000253 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000254 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000255 import_lock_level--;
256 if (import_lock_level == 0) {
257 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000260 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000261}
262
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes do not share locks with the parent. */
265
266void
267_PyImport_ReInitLock(void)
268{
269#ifdef _AIX
270 if (import_lock != NULL)
271 import_lock = PyThread_allocate_lock();
272#endif
273}
274
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000275#else
276
277#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000279
280#endif
281
Tim Peters69232342001-08-30 05:16:13 +0000282static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000283imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000284{
Tim Peters69232342001-08-30 05:16:13 +0000285#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000286 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000287#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000288 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000289#endif
290}
291
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000293imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000294{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295#ifdef WITH_THREAD
296 lock_import();
297#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000298 Py_INCREF(Py_None);
299 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000300}
301
302static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000303imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000304{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000305#ifdef WITH_THREAD
306 if (unlock_import() < 0) {
307 PyErr_SetString(PyExc_RuntimeError,
308 "not holding the import lock");
309 return NULL;
310 }
311#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000312 Py_INCREF(Py_None);
313 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000314}
315
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316/* Helper for sys */
317
318PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000319PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000320{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000321 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322 if (interp->modules == NULL)
323 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
324 return interp->modules;
325}
326
Guido van Rossum3f5da241990-12-20 15:06:42 +0000327
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000328/* List of names to clear in sys */
329static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000330 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000331 "exc_type", "exc_value", "exc_traceback",
332 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000333 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000334 NULL
335};
336
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000337static char* sys_files[] = {
338 "stdin", "__stdin__",
339 "stdout", "__stdout__",
340 "stderr", "__stderr__",
341 NULL
342};
343
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000344
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000345/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000346
Guido van Rossum3f5da241990-12-20 15:06:42 +0000347void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000349{
Guido van Rossum758eec01998-01-19 21:58:26 +0000350 int pos, ndone;
351 char *name;
352 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000353 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000354 PyObject *modules = interp->modules;
355
356 if (modules == NULL)
357 return; /* Already done */
358
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000359 /* Delete some special variables first. These are common
360 places where user values hide and people complain when their
361 destructors fail. Since the modules containing them are
362 deleted *last* of all, they would come too late in the normal
363 destruction order. Sigh. */
364
365 value = PyDict_GetItemString(modules, "__builtin__");
366 if (value != NULL && PyModule_Check(value)) {
367 dict = PyModule_GetDict(value);
368 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000369 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000370 PyDict_SetItemString(dict, "_", Py_None);
371 }
372 value = PyDict_GetItemString(modules, "sys");
373 if (value != NULL && PyModule_Check(value)) {
374 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000375 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000376 dict = PyModule_GetDict(value);
377 for (p = sys_deletes; *p != NULL; p++) {
378 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000379 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000380 PyDict_SetItemString(dict, *p, Py_None);
381 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000382 for (p = sys_files; *p != NULL; p+=2) {
383 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000384 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000385 v = PyDict_GetItemString(dict, *(p+1));
386 if (v == NULL)
387 v = Py_None;
388 PyDict_SetItemString(dict, *p, v);
389 }
390 }
391
392 /* First, delete __main__ */
393 value = PyDict_GetItemString(modules, "__main__");
394 if (value != NULL && PyModule_Check(value)) {
395 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000396 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000397 _PyModule_Clear(value);
398 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000399 }
400
Guido van Rossum758eec01998-01-19 21:58:26 +0000401 /* The special treatment of __builtin__ here is because even
402 when it's not referenced as a module, its dictionary is
403 referenced by almost every module's __builtins__. Since
404 deleting a module clears its dictionary (even if there are
405 references left to it), we need to delete the __builtin__
406 module last. Likewise, we don't delete sys until the very
407 end because it is implicitly referenced (e.g. by print).
408
409 Also note that we 'delete' modules by replacing their entry
410 in the modules dict with None, rather than really deleting
411 them; this avoids a rehash of the modules dictionary and
412 also marks them as "non existent" so they won't be
413 re-imported. */
414
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000415 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000416 one (skipping __builtin__ and sys) and delete them */
417 do {
418 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000419 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000420 while (PyDict_Next(modules, &pos, &key, &value)) {
421 if (value->ob_refcnt != 1)
422 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000423 if (PyString_Check(key) && PyModule_Check(value)) {
424 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000425 if (strcmp(name, "__builtin__") == 0)
426 continue;
427 if (strcmp(name, "sys") == 0)
428 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000429 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000430 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000431 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000432 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000433 PyDict_SetItem(modules, key, Py_None);
434 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000435 }
436 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000437 } while (ndone > 0);
438
Guido van Rossum758eec01998-01-19 21:58:26 +0000439 /* Next, delete all modules (still skipping __builtin__ and sys) */
440 pos = 0;
441 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000442 if (PyString_Check(key) && PyModule_Check(value)) {
443 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000444 if (strcmp(name, "__builtin__") == 0)
445 continue;
446 if (strcmp(name, "sys") == 0)
447 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000448 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000449 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000450 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000451 PyDict_SetItem(modules, key, Py_None);
452 }
453 }
454
455 /* Next, delete sys and __builtin__ (in that order) */
456 value = PyDict_GetItemString(modules, "sys");
457 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000458 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000459 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000460 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000461 PyDict_SetItemString(modules, "sys", Py_None);
462 }
463 value = PyDict_GetItemString(modules, "__builtin__");
464 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000465 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000466 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000467 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000468 PyDict_SetItemString(modules, "__builtin__", Py_None);
469 }
470
471 /* Finally, clear and delete the modules directory */
472 PyDict_Clear(modules);
473 interp->modules = NULL;
474 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000475}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000476
477
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000478/* Helper for pythonrun.c -- return magic number */
479
480long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000481PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482{
Guido van Rossum96774c12000-05-01 20:19:08 +0000483 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484}
485
486
Guido van Rossum25ce5661997-08-02 03:10:38 +0000487/* Magic for extension modules (built-in as well as dynamically
488 loaded). To prevent initializing an extension module more than
489 once, we keep a static dictionary 'extensions' keyed by module name
490 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000491 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000492 dictionary is stored by calling _PyImport_FixupExtension()
493 immediately after the module initialization function succeeds. A
494 copy can be retrieved from there by calling
495 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496
Guido van Rossum79f25d91997-04-29 20:08:16 +0000497PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000498_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000500 PyObject *modules, *mod, *dict, *copy;
501 if (extensions == NULL) {
502 extensions = PyDict_New();
503 if (extensions == NULL)
504 return NULL;
505 }
506 modules = PyImport_GetModuleDict();
507 mod = PyDict_GetItemString(modules, name);
508 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000509 PyErr_Format(PyExc_SystemError,
510 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000511 return NULL;
512 }
513 dict = PyModule_GetDict(mod);
514 if (dict == NULL)
515 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000516 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000517 if (copy == NULL)
518 return NULL;
519 PyDict_SetItemString(extensions, filename, copy);
520 Py_DECREF(copy);
521 return copy;
522}
523
524PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000525_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000527 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 if (extensions == NULL)
529 return NULL;
530 dict = PyDict_GetItemString(extensions, filename);
531 if (dict == NULL)
532 return NULL;
533 mod = PyImport_AddModule(name);
534 if (mod == NULL)
535 return NULL;
536 mdict = PyModule_GetDict(mod);
537 if (mdict == NULL)
538 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000539 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000542 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 name, filename);
544 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545}
546
547
548/* Get the module object corresponding to a module name.
549 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000550 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000551 Because the former action is most common, THIS DOES NOT RETURN A
552 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000555PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000557 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000563 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000564 if (m == NULL)
565 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568 return NULL;
569 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
572 return m;
573}
574
Tim Peters1cd70172004-08-02 03:52:12 +0000575/* Remove name from sys.modules, if it's there. */
576static void
577_RemoveModule(const char *name)
578{
579 PyObject *modules = PyImport_GetModuleDict();
580 if (PyDict_GetItemString(modules, name) == NULL)
581 return;
582 if (PyDict_DelItemString(modules, name) < 0)
583 Py_FatalError("import: deleting existing key in"
584 "sys.modules failed");
585}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000587/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000588 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
589 * removed from sys.modules, to avoid leaving damaged module objects
590 * in sys.modules. The caller may wish to restore the original
591 * module object (if any) in this case; PyImport_ReloadModule is an
592 * example.
593 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000595PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000597 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
598}
599
600PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000601PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000602{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607 if (m == NULL)
608 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000609 /* If the module is being reloaded, we get the old module back
610 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 d = PyModule_GetDict(m);
612 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
613 if (PyDict_SetItemString(d, "__builtins__",
614 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000615 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000616 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000617 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000618 v = NULL;
619 if (pathname != NULL) {
620 v = PyString_FromString(pathname);
621 if (v == NULL)
622 PyErr_Clear();
623 }
624 if (v == NULL) {
625 v = ((PyCodeObject *)co)->co_filename;
626 Py_INCREF(v);
627 }
628 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000630 Py_DECREF(v);
631
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000632 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000634 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000636
Guido van Rossum25ce5661997-08-02 03:10:38 +0000637 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000638 PyErr_Format(PyExc_ImportError,
639 "Loaded module %.200s not found in sys.modules",
640 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000641 return NULL;
642 }
643
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645
646 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000647
648 error:
649 _RemoveModule(name);
650 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651}
652
653
654/* Given a pathname for a Python source file, fill a buffer with the
655 pathname for the corresponding compiled file. Return the pathname
656 for the compiled file, or NULL if there's no space in the buffer.
657 Doesn't set an exception. */
658
659static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661{
Tim Petersc1731372001-08-04 08:12:36 +0000662 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663 if (len+2 > buflen)
664 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000665
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000666#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000667 /* Treat .pyw as if it were .py. The case of ".pyw" must match
668 that used in _PyImport_StandardFiletab. */
669 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
670 --len; /* pretend 'w' isn't there */
671#endif
672 memcpy(buf, pathname, len);
673 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
674 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000675
676 return buf;
677}
678
679
680/* Given a pathname for a Python source file, its time of last
681 modification, and a pathname for a compiled file, check whether the
682 compiled file represents the same version of the source. If so,
683 return a FILE pointer for the compiled file, positioned just after
684 the header; if not, return NULL.
685 Doesn't set an exception. */
686
687static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689{
690 FILE *fp;
691 long magic;
692 long pyc_mtime;
693
694 fp = fopen(cpathname, "rb");
695 if (fp == NULL)
696 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000698 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000700 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701 fclose(fp);
702 return NULL;
703 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000707 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708 fclose(fp);
709 return NULL;
710 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000712 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713 return fp;
714}
715
716
717/* Read a code object from a file and check it for validity */
718
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000720read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723
Tim Petersd9b9ac82001-01-28 00:27:39 +0000724 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000725 if (co == NULL)
726 return NULL;
727 if (!PyCode_Check(co)) {
728 PyErr_Format(PyExc_ImportError,
729 "Non-code object in %.200s", cpathname);
730 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731 return NULL;
732 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734}
735
736
737/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000738 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000741load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742{
743 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 PyCodeObject *co;
745 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000748 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000749 PyErr_Format(PyExc_ImportError,
750 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 return NULL;
752 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000754 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755 if (co == NULL)
756 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000758 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000760 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762
763 return m;
764}
765
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766/* Parse a source file and return the corresponding code object */
767
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000769parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772 node *n;
773
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000774 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775 if (n == NULL)
776 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 co = PyNode_Compile(n, pathname);
778 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000779
780 return co;
781}
782
783
Guido van Rossum55a83382000-09-20 20:31:38 +0000784/* Helper to open a bytecode file for writing in exclusive mode */
785
786static FILE *
787open_exclusive(char *filename)
788{
789#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
790 /* Use O_EXCL to avoid a race condition when another process tries to
791 write the same file. When that happens, our open() call fails,
792 which is just fine (since it's only a cache).
793 XXX If the file exists and is writable but the directory is not
794 writable, the file will never be written. Oh well.
795 */
796 int fd;
797 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000798 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
799#ifdef O_BINARY
800 |O_BINARY /* necessary for Windows */
801#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000802#ifdef __VMS
803 , 0666, "ctxt=bin", "shr=nil");
804#else
805 , 0666);
806#endif
Guido van Rossum55a83382000-09-20 20:31:38 +0000807 if (fd < 0)
808 return NULL;
809 return fdopen(fd, "wb");
810#else
811 /* Best we can do -- on Windows this can't happen anyway */
812 return fopen(filename, "wb");
813#endif
814}
815
816
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817/* Write a compiled module to a file, placing the time of last
818 modification of its source into the header.
819 Errors are ignored, if a write error occurs an attempt is made to
820 remove the file. */
821
822static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000823write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824{
825 FILE *fp;
826
Guido van Rossum55a83382000-09-20 20:31:38 +0000827 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000830 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 "# can't create %s\n", cpathname);
832 return;
833 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000834 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000836 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
837 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000838 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000840 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000841 /* Don't keep partial file */
842 fclose(fp);
843 (void) unlink(cpathname);
844 return;
845 }
846 /* Now write the true mtime */
847 fseek(fp, 4L, 0);
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000848 PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849 fflush(fp);
850 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000852 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853}
854
855
856/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000857 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
858 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000859
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000861load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862{
Fred Drake4c82b232000-06-30 16:18:57 +0000863 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864 FILE *fpc;
865 char buf[MAXPATHLEN+1];
866 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyCodeObject *co;
868 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000870 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000871 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000872 return NULL;
873#if SIZEOF_TIME_T > 4
874 /* Python's .pyc timestamp handling presumes that the timestamp fits
875 in 4 bytes. This will be fine until sometime in the year 2038,
876 when a 4-byte signed time_t will overflow.
877 */
878 if (mtime >> 32) {
879 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000880 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000881 return NULL;
882 }
883#endif
Tim Peters36515e22001-11-18 04:06:29 +0000884 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000885 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886 if (cpathname != NULL &&
887 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000888 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000889 fclose(fpc);
890 if (co == NULL)
891 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000893 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000895 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000896 }
897 else {
898 co = parse_source_module(pathname, fp);
899 if (co == NULL)
900 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000901 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000902 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000903 name, pathname);
904 write_compiled_module(co, cpathname, mtime);
905 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000906 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000908
909 return m;
910}
911
912
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000913/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000914static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
915static struct filedescr *find_module(char *, char *, PyObject *,
916 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000917static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000918
919/* Load a package and return its module object WITH INCREMENTED
920 REFERENCE COUNT */
921
922static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000923load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000924{
Tim Peters1cd70172004-08-02 03:52:12 +0000925 PyObject *m, *d;
926 PyObject *file = NULL;
927 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000928 int err;
929 char buf[MAXPATHLEN+1];
930 FILE *fp = NULL;
931 struct filedescr *fdp;
932
933 m = PyImport_AddModule(name);
934 if (m == NULL)
935 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000936 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000937 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000938 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000939 d = PyModule_GetDict(m);
940 file = PyString_FromString(pathname);
941 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000942 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000943 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000944 if (path == NULL)
945 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000946 err = PyDict_SetItemString(d, "__file__", file);
947 if (err == 0)
948 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000949 if (err != 0)
950 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000951 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000952 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000953 if (fdp == NULL) {
954 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
955 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000956 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000957 }
958 else
959 m = NULL;
960 goto cleanup;
961 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000962 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000963 if (fp != NULL)
964 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +0000965 goto cleanup;
966
967 error:
968 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000969 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000970 Py_XDECREF(path);
971 Py_XDECREF(file);
972 return m;
973}
974
975
976/* Helper to test for built-in module */
977
978static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000979is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000980{
981 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000982 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
983 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
984 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000985 return -1;
986 else
987 return 1;
988 }
989 }
990 return 0;
991}
992
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000993
Just van Rossum52e14d62002-12-30 22:08:05 +0000994/* Return an importer object for a sys.path/pkg.__path__ item 'p',
995 possibly by fetching it from the path_importer_cache dict. If it
996 wasn't yet cached, traverse path_hooks until it a hook is found
997 that can handle the path item. Return None if no hook could;
998 this tells our caller it should fall back to the builtin
999 import mechanism. Cache the result in path_importer_cache.
1000 Returns a borrowed reference. */
1001
1002static PyObject *
1003get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1004 PyObject *p)
1005{
1006 PyObject *importer;
1007 int j, nhooks;
1008
1009 /* These conditions are the caller's responsibility: */
1010 assert(PyList_Check(path_hooks));
1011 assert(PyDict_Check(path_importer_cache));
1012
1013 nhooks = PyList_Size(path_hooks);
1014 if (nhooks < 0)
1015 return NULL; /* Shouldn't happen */
1016
1017 importer = PyDict_GetItem(path_importer_cache, p);
1018 if (importer != NULL)
1019 return importer;
1020
1021 /* set path_importer_cache[p] to None to avoid recursion */
1022 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1023 return NULL;
1024
1025 for (j = 0; j < nhooks; j++) {
1026 PyObject *hook = PyList_GetItem(path_hooks, j);
1027 if (hook == NULL)
1028 return NULL;
1029 importer = PyObject_CallFunction(hook, "O", p);
1030 if (importer != NULL)
1031 break;
1032
1033 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1034 return NULL;
1035 }
1036 PyErr_Clear();
1037 }
1038 if (importer == NULL)
1039 importer = Py_None;
1040 else if (importer != Py_None) {
1041 int err = PyDict_SetItem(path_importer_cache, p, importer);
1042 Py_DECREF(importer);
1043 if (err != 0)
1044 return NULL;
1045 }
1046 return importer;
1047}
1048
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049/* Search the path (default sys.path) for a module. Return the
1050 corresponding filedescr struct, and (via return arguments) the
1051 pathname and an open file. Return NULL if the module is not found. */
1052
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001053#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001054extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
1055 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001056#endif
1057
Tim Peters50d8d372001-02-28 05:34:27 +00001058static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001059static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001060static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001061
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001062static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001063find_module(char *fullname, char *subname, PyObject *path, char *buf,
1064 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065{
Fred Drake4c82b232000-06-30 16:18:57 +00001066 int i, npath;
1067 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001068 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001069 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001070 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001071 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001072#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001073 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001074#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001075 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1076 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1077 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001078 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001079#if defined(PYOS_OS2)
1080 size_t saved_len;
1081 size_t saved_namelen;
1082 char *saved_buf = NULL;
1083#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001084 if (p_loader != NULL)
1085 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001086
Just van Rossum52e14d62002-12-30 22:08:05 +00001087 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001088 PyErr_SetString(PyExc_OverflowError,
1089 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001090 return NULL;
1091 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001092 strcpy(name, subname);
1093
1094 /* sys.meta_path import hook */
1095 if (p_loader != NULL) {
1096 PyObject *meta_path;
1097
1098 meta_path = PySys_GetObject("meta_path");
1099 if (meta_path == NULL || !PyList_Check(meta_path)) {
1100 PyErr_SetString(PyExc_ImportError,
1101 "sys.meta_path must be a list of "
1102 "import hooks");
1103 return NULL;
1104 }
1105 Py_INCREF(meta_path); /* zap guard */
1106 npath = PyList_Size(meta_path);
1107 for (i = 0; i < npath; i++) {
1108 PyObject *loader;
1109 PyObject *hook = PyList_GetItem(meta_path, i);
1110 loader = PyObject_CallMethod(hook, "find_module",
1111 "sO", fullname,
1112 path != NULL ?
1113 path : Py_None);
1114 if (loader == NULL) {
1115 Py_DECREF(meta_path);
1116 return NULL; /* true error */
1117 }
1118 if (loader != Py_None) {
1119 /* a loader was found */
1120 *p_loader = loader;
1121 Py_DECREF(meta_path);
1122 return &importhookdescr;
1123 }
1124 Py_DECREF(loader);
1125 }
1126 Py_DECREF(meta_path);
1127 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001128
1129 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001130 /* The only type of submodule allowed inside a "frozen"
1131 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001132 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1133 PyErr_SetString(PyExc_ImportError,
1134 "full frozen module name too long");
1135 return NULL;
1136 }
1137 strcpy(buf, PyString_AsString(path));
1138 strcat(buf, ".");
1139 strcat(buf, name);
1140 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001141 if (find_frozen(name) != NULL) {
1142 strcpy(buf, name);
1143 return &fd_frozen;
1144 }
1145 PyErr_Format(PyExc_ImportError,
1146 "No frozen submodule named %.200s", name);
1147 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001148 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001149 if (path == NULL) {
1150 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001151 strcpy(buf, name);
1152 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001153 }
Greg Ward201baee2001-10-04 14:52:06 +00001154 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001155 strcpy(buf, name);
1156 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001157 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001158
Guido van Rossumac279101996-08-22 23:10:58 +00001159#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001160 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1161 if (fp != NULL) {
1162 *p_fp = fp;
1163 return fdp;
1164 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001165#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001166 path = PySys_GetObject("path");
1167 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168 if (path == NULL || !PyList_Check(path)) {
1169 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001170 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001171 return NULL;
1172 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001173
1174 path_hooks = PySys_GetObject("path_hooks");
1175 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1176 PyErr_SetString(PyExc_ImportError,
1177 "sys.path_hooks must be a list of "
1178 "import hooks");
1179 return NULL;
1180 }
1181 path_importer_cache = PySys_GetObject("path_importer_cache");
1182 if (path_importer_cache == NULL ||
1183 !PyDict_Check(path_importer_cache)) {
1184 PyErr_SetString(PyExc_ImportError,
1185 "sys.path_importer_cache must be a dict");
1186 return NULL;
1187 }
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001190 namelen = strlen(name);
1191 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001192 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 PyObject *v = PyList_GetItem(path, i);
Walter Dörwald3430d702002-06-17 10:43:59 +00001194#ifdef Py_USING_UNICODE
1195 if (PyUnicode_Check(v)) {
1196 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1197 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1198 if (copy == NULL)
1199 return NULL;
1200 v = copy;
1201 }
1202 else
1203#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001205 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001206 len = PyString_Size(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001207 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1208 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001209 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001210 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001211 strcpy(buf, PyString_AsString(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001212 if (strlen(buf) != len) {
1213 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001215 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001216
1217 /* sys.path_hooks import hook */
1218 if (p_loader != NULL) {
1219 PyObject *importer;
1220
1221 importer = get_path_importer(path_importer_cache,
1222 path_hooks, v);
1223 if (importer == NULL)
1224 return NULL;
1225 /* Note: importer is a borrowed reference */
1226 if (importer != Py_None) {
1227 PyObject *loader;
1228 loader = PyObject_CallMethod(importer,
1229 "find_module",
1230 "s", fullname);
1231 if (loader == NULL)
1232 return NULL; /* error */
1233 if (loader != Py_None) {
1234 /* a loader was found */
1235 *p_loader = loader;
1236 return &importhookdescr;
1237 }
1238 Py_DECREF(loader);
1239 }
1240 /* no hook was successful, use builtin import */
1241 }
1242
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001243 if (len > 0 && buf[len-1] != SEP
1244#ifdef ALTSEP
1245 && buf[len-1] != ALTSEP
1246#endif
1247 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001248 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001249 strcpy(buf+len, name);
1250 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001251
1252 /* Check for package import (buf holds a directory name,
1253 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001254#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001255 if (stat(buf, &statbuf) == 0 && /* it exists */
1256 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1257 find_init_module(buf) && /* it has __init__.py */
1258 case_ok(buf, len, namelen, name)) { /* and case matches */
1259 Py_XDECREF(copy);
Tim Peterscab3f682001-04-29 22:21:25 +00001260 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001261 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001262#else
1263 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001264#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001265 if (isdir(buf) &&
1266 find_init_module(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001267 case_ok(buf, len, namelen, name)) {
1268 Py_XDECREF(copy);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001269 return &fd_package;
Walter Dörwald3430d702002-06-17 10:43:59 +00001270 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001271#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001272#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001273#if defined(PYOS_OS2)
1274 /* take a snapshot of the module spec for restoration
1275 * after the 8 character DLL hackery
1276 */
1277 saved_buf = strdup(buf);
1278 saved_len = len;
1279 saved_namelen = namelen;
1280#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001281 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001282#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001283 /* OS/2 limits DLLs to 8 character names (w/o
1284 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001285 * so if the name is longer than that and its a
1286 * dynamically loaded module we're going to try,
1287 * truncate the name before trying
1288 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001289 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001290 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001291 const struct filedescr *scan;
1292 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001293 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001294 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001295 break;
1296 else
1297 scan++;
1298 }
1299 if (scan->suffix != NULL) {
1300 /* yes, so truncate the name */
1301 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001302 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001303 buf[len] = '\0';
1304 }
1305 }
1306#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001309 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001310 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001311 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001312 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001313 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001314 if (fp != NULL) {
1315 if (case_ok(buf, len, namelen, name))
1316 break;
1317 else { /* continue search */
1318 fclose(fp);
1319 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001320 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001321 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001322#if defined(PYOS_OS2)
1323 /* restore the saved snapshot */
1324 strcpy(buf, saved_buf);
1325 len = saved_len;
1326 namelen = saved_namelen;
1327#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001328 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001329#if defined(PYOS_OS2)
1330 /* don't need/want the module name snapshot anymore */
1331 if (saved_buf)
1332 {
1333 free(saved_buf);
1334 saved_buf = NULL;
1335 }
1336#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001337 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001338 if (fp != NULL)
1339 break;
1340 }
1341 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001342 PyErr_Format(PyExc_ImportError,
1343 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001344 return NULL;
1345 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001346 *p_fp = fp;
1347 return fdp;
1348}
1349
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001350/* Helpers for main.c
1351 * Find the source file corresponding to a named module
1352 */
1353struct filedescr *
1354_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1355 size_t buflen, FILE **p_fp, PyObject **p_loader)
1356{
1357 return find_module((char *) name, (char *) name, path,
1358 buf, buflen, p_fp, p_loader);
1359}
1360
1361PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1362{
1363 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1364}
1365
Tim Petersd1e87a82001-03-01 18:12:00 +00001366/* case_ok(char* buf, int len, int namelen, char* name)
1367 * The arguments here are tricky, best shown by example:
1368 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1369 * ^ ^ ^ ^
1370 * |--------------------- buf ---------------------|
1371 * |------------------- len ------------------|
1372 * |------ name -------|
1373 * |----- namelen -----|
1374 * buf is the full path, but len only counts up to (& exclusive of) the
1375 * extension. name is the module name, also exclusive of extension.
1376 *
1377 * We've already done a successful stat() or fopen() on buf, so know that
1378 * there's some match, possibly case-insensitive.
1379 *
Tim Peters50d8d372001-02-28 05:34:27 +00001380 * case_ok() is to return 1 if there's a case-sensitive match for
1381 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1382 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001383 *
Tim Peters50d8d372001-02-28 05:34:27 +00001384 * case_ok() is used to implement case-sensitive import semantics even
1385 * on platforms with case-insensitive filesystems. It's trivial to implement
1386 * for case-sensitive filesystems. It's pretty much a cross-platform
1387 * nightmare for systems with case-insensitive filesystems.
1388 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001389
Tim Peters50d8d372001-02-28 05:34:27 +00001390/* First we may need a pile of platform-specific header files; the sequence
1391 * of #if's here should match the sequence in the body of case_ok().
1392 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001393#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001394#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001395
Tim Peters50d8d372001-02-28 05:34:27 +00001396#elif defined(DJGPP)
1397#include <dir.h>
1398
Jason Tishler7961aa62005-05-20 00:56:54 +00001399#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001400#include <sys/types.h>
1401#include <dirent.h>
1402
Andrew MacIntyred9400542002-02-26 11:41:34 +00001403#elif defined(PYOS_OS2)
1404#define INCL_DOS
1405#define INCL_DOSERRORS
1406#define INCL_NOPMAPI
1407#include <os2.h>
1408
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001409#elif defined(RISCOS)
1410#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001411#endif
1412
Guido van Rossum0980bd91998-02-13 17:18:36 +00001413static int
Tim Peters50d8d372001-02-28 05:34:27 +00001414case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001415{
Tim Peters50d8d372001-02-28 05:34:27 +00001416/* Pick a platform-specific implementation; the sequence of #if's here should
1417 * match the sequence just above.
1418 */
1419
Jason Tishler7961aa62005-05-20 00:56:54 +00001420/* MS_WINDOWS */
1421#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001422 WIN32_FIND_DATA data;
1423 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001424
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001425 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001426 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001427
Guido van Rossum0980bd91998-02-13 17:18:36 +00001428 h = FindFirstFile(buf, &data);
1429 if (h == INVALID_HANDLE_VALUE) {
1430 PyErr_Format(PyExc_NameError,
1431 "Can't find file for module %.100s\n(filename %.300s)",
1432 name, buf);
1433 return 0;
1434 }
1435 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001436 return strncmp(data.cFileName, name, namelen) == 0;
1437
1438/* DJGPP */
1439#elif defined(DJGPP)
1440 struct ffblk ffblk;
1441 int done;
1442
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001443 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001444 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001445
1446 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1447 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001448 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001449 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001450 name, buf);
1451 return 0;
1452 }
Tim Peters50d8d372001-02-28 05:34:27 +00001453 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001454
Jason Tishler7961aa62005-05-20 00:56:54 +00001455/* new-fangled macintosh (macosx) or Cygwin */
1456#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001457 DIR *dirp;
1458 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001459 char dirname[MAXPATHLEN + 1];
1460 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001461
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001462 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001463 return 1;
1464
Tim Petersd1e87a82001-03-01 18:12:00 +00001465 /* Copy the dir component into dirname; substitute "." if empty */
1466 if (dirlen <= 0) {
1467 dirname[0] = '.';
1468 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001469 }
1470 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001471 assert(dirlen <= MAXPATHLEN);
1472 memcpy(dirname, buf, dirlen);
1473 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001474 }
1475 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001476 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001477 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001478 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001479 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001480 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001481#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001482 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001483#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001484 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001485#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001486 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001487 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001488 (void)closedir(dirp);
1489 return 1; /* Found */
1490 }
1491 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001492 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001493 }
Tim Peters430f5d42001-03-01 01:30:56 +00001494 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001495
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001496/* RISC OS */
1497#elif defined(RISCOS)
1498 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1499 char buf2[MAXPATHLEN+2];
1500 char *nameWithExt = buf+len-namelen;
1501 int canonlen;
1502 os_error *e;
1503
1504 if (Py_GETENV("PYTHONCASEOK") != NULL)
1505 return 1;
1506
1507 /* workaround:
1508 append wildcard, otherwise case of filename wouldn't be touched */
1509 strcpy(buf2, buf);
1510 strcat(buf2, "*");
1511
1512 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1513 canonlen = MAXPATHLEN+1-canonlen;
1514 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1515 return 0;
1516 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1517 return 1; /* match */
1518
1519 return 0;
1520
Andrew MacIntyred9400542002-02-26 11:41:34 +00001521/* OS/2 */
1522#elif defined(PYOS_OS2)
1523 HDIR hdir = 1;
1524 ULONG srchcnt = 1;
1525 FILEFINDBUF3 ffbuf;
1526 APIRET rc;
1527
1528 if (getenv("PYTHONCASEOK") != NULL)
1529 return 1;
1530
1531 rc = DosFindFirst(buf,
1532 &hdir,
1533 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1534 &ffbuf, sizeof(ffbuf),
1535 &srchcnt,
1536 FIL_STANDARD);
1537 if (rc != NO_ERROR)
1538 return 0;
1539 return strncmp(ffbuf.achName, name, namelen) == 0;
1540
Tim Peters50d8d372001-02-28 05:34:27 +00001541/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1542#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001543 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001544
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001545#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001546}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001547
Guido van Rossum0980bd91998-02-13 17:18:36 +00001548
Guido van Rossum197346f1997-10-31 18:38:52 +00001549#ifdef HAVE_STAT
1550/* Helper to look for __init__.py or __init__.py[co] in potential package */
1551static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001552find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001553{
Tim Peters0f9431f2001-07-05 03:47:53 +00001554 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001555 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001556 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001557 struct stat statbuf;
1558
Tim Peters0f9431f2001-07-05 03:47:53 +00001559/* For calling case_ok(buf, len, namelen, name):
1560 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1561 * ^ ^ ^ ^
1562 * |--------------------- buf ---------------------|
1563 * |------------------- len ------------------|
1564 * |------ name -------|
1565 * |----- namelen -----|
1566 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001567 if (save_len + 13 >= MAXPATHLEN)
1568 return 0;
1569 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001570 pname = buf + i;
1571 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001572 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001573 if (case_ok(buf,
1574 save_len + 9, /* len("/__init__") */
1575 8, /* len("__init__") */
1576 pname)) {
1577 buf[save_len] = '\0';
1578 return 1;
1579 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001580 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001581 i += strlen(pname);
1582 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001583 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001584 if (case_ok(buf,
1585 save_len + 9, /* len("/__init__") */
1586 8, /* len("__init__") */
1587 pname)) {
1588 buf[save_len] = '\0';
1589 return 1;
1590 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001591 }
1592 buf[save_len] = '\0';
1593 return 0;
1594}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001595
1596#else
1597
1598#ifdef RISCOS
1599static int
1600find_init_module(buf)
1601 char *buf;
1602{
1603 int save_len = strlen(buf);
1604 int i = save_len;
1605
1606 if (save_len + 13 >= MAXPATHLEN)
1607 return 0;
1608 buf[i++] = SEP;
1609 strcpy(buf+i, "__init__/py");
1610 if (isfile(buf)) {
1611 buf[save_len] = '\0';
1612 return 1;
1613 }
1614
1615 if (Py_OptimizeFlag)
1616 strcpy(buf+i, "o");
1617 else
1618 strcpy(buf+i, "c");
1619 if (isfile(buf)) {
1620 buf[save_len] = '\0';
1621 return 1;
1622 }
1623 buf[save_len] = '\0';
1624 return 0;
1625}
1626#endif /*RISCOS*/
1627
Guido van Rossum197346f1997-10-31 18:38:52 +00001628#endif /* HAVE_STAT */
1629
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001630
Tim Petersdbd9ba62000-07-09 03:09:57 +00001631static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001632
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001633/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001634 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001635
Guido van Rossum79f25d91997-04-29 20:08:16 +00001636static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001637load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001638{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001639 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001641 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001642
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001643 /* First check that there's an open file (if we need one) */
1644 switch (type) {
1645 case PY_SOURCE:
1646 case PY_COMPILED:
1647 if (fp == NULL) {
1648 PyErr_Format(PyExc_ValueError,
1649 "file object required for import (type code %d)",
1650 type);
1651 return NULL;
1652 }
1653 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001654
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001655 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001656
1657 case PY_SOURCE:
1658 m = load_source_module(name, buf, fp);
1659 break;
1660
1661 case PY_COMPILED:
1662 m = load_compiled_module(name, buf, fp);
1663 break;
1664
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001665#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001666 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001667 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001668 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001669#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001670
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001671 case PKG_DIRECTORY:
1672 m = load_package(name, buf);
1673 break;
1674
1675 case C_BUILTIN:
1676 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001677 if (buf != NULL && buf[0] != '\0')
1678 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001679 if (type == C_BUILTIN)
1680 err = init_builtin(name);
1681 else
1682 err = PyImport_ImportFrozenModule(name);
1683 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001684 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001685 if (err == 0) {
1686 PyErr_Format(PyExc_ImportError,
1687 "Purported %s module %.200s not found",
1688 type == C_BUILTIN ?
1689 "builtin" : "frozen",
1690 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001691 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001692 }
1693 modules = PyImport_GetModuleDict();
1694 m = PyDict_GetItemString(modules, name);
1695 if (m == NULL) {
1696 PyErr_Format(
1697 PyExc_ImportError,
1698 "%s module %.200s not properly initialized",
1699 type == C_BUILTIN ?
1700 "builtin" : "frozen",
1701 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001702 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001703 }
1704 Py_INCREF(m);
1705 break;
1706
Just van Rossum52e14d62002-12-30 22:08:05 +00001707 case IMP_HOOK: {
1708 if (loader == NULL) {
1709 PyErr_SetString(PyExc_ImportError,
1710 "import hook without loader");
1711 return NULL;
1712 }
1713 m = PyObject_CallMethod(loader, "load_module", "s", name);
1714 break;
1715 }
1716
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001717 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001718 PyErr_Format(PyExc_ImportError,
1719 "Don't know how to import %.200s (type code %d)",
1720 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001721 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722
1723 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001724
1725 return m;
1726}
1727
1728
1729/* Initialize a built-in module.
1730 Return 1 for succes, 0 if the module is not found, and -1 with
1731 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001732
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001733static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001734init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001735{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001736 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001737
Greg Ward201baee2001-10-04 14:52:06 +00001738 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001739 return 1;
1740
Guido van Rossum771c6c81997-10-31 18:37:24 +00001741 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001742 if (strcmp(name, p->name) == 0) {
1743 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001744 PyErr_Format(PyExc_ImportError,
1745 "Cannot re-init internal module %.200s",
1746 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001747 return -1;
1748 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001750 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001751 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001753 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001754 if (_PyImport_FixupExtension(name, name) == NULL)
1755 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001756 return 1;
1757 }
1758 }
1759 return 0;
1760}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001761
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001762
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001763/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001764
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001765static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001766find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001767{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001768 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001769
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001771 if (p->name == NULL)
1772 return NULL;
1773 if (strcmp(p->name, name) == 0)
1774 break;
1775 }
1776 return p;
1777}
1778
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001780get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001781{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001782 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001783 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001784
1785 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001786 PyErr_Format(PyExc_ImportError,
1787 "No such frozen object named %.200s",
1788 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001789 return NULL;
1790 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001791 if (p->code == NULL) {
1792 PyErr_Format(PyExc_ImportError,
1793 "Excluded frozen object named %.200s",
1794 name);
1795 return NULL;
1796 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001797 size = p->size;
1798 if (size < 0)
1799 size = -size;
1800 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001801}
1802
1803/* Initialize a frozen module.
1804 Return 1 for succes, 0 if the module is not found, and -1 with
1805 an exception set if the initialization failed.
1806 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001807
1808int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001809PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001810{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001811 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812 PyObject *co;
1813 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001814 int ispackage;
1815 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001816
1817 if (p == NULL)
1818 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001819 if (p->code == NULL) {
1820 PyErr_Format(PyExc_ImportError,
1821 "Excluded frozen object named %.200s",
1822 name);
1823 return -1;
1824 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001825 size = p->size;
1826 ispackage = (size < 0);
1827 if (ispackage)
1828 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001830 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001831 name, ispackage ? " package" : "");
1832 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001833 if (co == NULL)
1834 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 if (!PyCode_Check(co)) {
1836 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001837 PyErr_Format(PyExc_TypeError,
1838 "frozen object %.200s is not a code object",
1839 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001840 return -1;
1841 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001842 if (ispackage) {
1843 /* Set __path__ to the package name */
1844 PyObject *d, *s;
1845 int err;
1846 m = PyImport_AddModule(name);
1847 if (m == NULL)
1848 return -1;
1849 d = PyModule_GetDict(m);
1850 s = PyString_InternFromString(name);
1851 if (s == NULL)
1852 return -1;
1853 err = PyDict_SetItemString(d, "__path__", s);
1854 Py_DECREF(s);
1855 if (err != 0)
1856 return err;
1857 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001858 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001860 if (m == NULL)
1861 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001862 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001863 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001864}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001865
1866
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001867/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001868 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001869
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001871PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001872{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001873 PyObject *pname;
1874 PyObject *result;
1875
1876 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001877 if (pname == NULL)
1878 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001879 result = PyImport_Import(pname);
1880 Py_DECREF(pname);
1881 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001882}
1883
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001884/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001885static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1886static PyObject *load_next(PyObject *mod, PyObject *altmod,
1887 char **p_name, char *buf, int *p_buflen);
1888static int mark_miss(char *name);
1889static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1890 char *buf, int buflen, int recursive);
1891static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001892
1893/* The Magnum Opus of dotted-name import :-) */
1894
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001895static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001896import_module_ex(char *name, PyObject *globals, PyObject *locals,
1897 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001898{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001899 char buf[MAXPATHLEN+1];
1900 int buflen = 0;
1901 PyObject *parent, *head, *next, *tail;
1902
1903 parent = get_parent(globals, buf, &buflen);
1904 if (parent == NULL)
1905 return NULL;
1906
1907 head = load_next(parent, Py_None, &name, buf, &buflen);
1908 if (head == NULL)
1909 return NULL;
1910
1911 tail = head;
1912 Py_INCREF(tail);
1913 while (name) {
1914 next = load_next(tail, tail, &name, buf, &buflen);
1915 Py_DECREF(tail);
1916 if (next == NULL) {
1917 Py_DECREF(head);
1918 return NULL;
1919 }
1920 tail = next;
1921 }
1922
1923 if (fromlist != NULL) {
1924 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1925 fromlist = NULL;
1926 }
1927
1928 if (fromlist == NULL) {
1929 Py_DECREF(tail);
1930 return head;
1931 }
1932
1933 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001934 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001935 Py_DECREF(tail);
1936 return NULL;
1937 }
1938
1939 return tail;
1940}
1941
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001942PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001943PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1944 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001945{
1946 PyObject *result;
1947 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001948 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001949 if (unlock_import() < 0) {
1950 Py_XDECREF(result);
1951 PyErr_SetString(PyExc_RuntimeError,
1952 "not holding the import lock");
1953 return NULL;
1954 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001955 return result;
1956}
1957
Fred Drake87590902004-05-28 20:21:36 +00001958/* Return the package that an import is being performed in. If globals comes
1959 from the module foo.bar.bat (not itself a package), this returns the
1960 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
1961 the package's entry in sys.modules is returned.
1962
1963 The *name* of the returned package is returned in buf, with the length of
1964 the name in *p_buflen.
1965
1966 If globals doesn't come from a package or a module in a package, or a
1967 corresponding entry is not found in sys.modules, Py_None is returned.
1968*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001969static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001970get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001971{
1972 static PyObject *namestr = NULL;
1973 static PyObject *pathstr = NULL;
1974 PyObject *modname, *modpath, *modules, *parent;
1975
1976 if (globals == NULL || !PyDict_Check(globals))
1977 return Py_None;
1978
1979 if (namestr == NULL) {
1980 namestr = PyString_InternFromString("__name__");
1981 if (namestr == NULL)
1982 return NULL;
1983 }
1984 if (pathstr == NULL) {
1985 pathstr = PyString_InternFromString("__path__");
1986 if (pathstr == NULL)
1987 return NULL;
1988 }
1989
1990 *buf = '\0';
1991 *p_buflen = 0;
1992 modname = PyDict_GetItem(globals, namestr);
1993 if (modname == NULL || !PyString_Check(modname))
1994 return Py_None;
1995
1996 modpath = PyDict_GetItem(globals, pathstr);
1997 if (modpath != NULL) {
1998 int len = PyString_GET_SIZE(modname);
1999 if (len > MAXPATHLEN) {
2000 PyErr_SetString(PyExc_ValueError,
2001 "Module name too long");
2002 return NULL;
2003 }
2004 strcpy(buf, PyString_AS_STRING(modname));
2005 *p_buflen = len;
2006 }
2007 else {
2008 char *start = PyString_AS_STRING(modname);
2009 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002010 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002011 if (lastdot == NULL)
2012 return Py_None;
2013 len = lastdot - start;
2014 if (len >= MAXPATHLEN) {
2015 PyErr_SetString(PyExc_ValueError,
2016 "Module name too long");
2017 return NULL;
2018 }
2019 strncpy(buf, start, len);
2020 buf[len] = '\0';
2021 *p_buflen = len;
2022 }
2023
2024 modules = PyImport_GetModuleDict();
2025 parent = PyDict_GetItemString(modules, buf);
2026 if (parent == NULL)
2027 parent = Py_None;
2028 return parent;
2029 /* We expect, but can't guarantee, if parent != None, that:
2030 - parent.__name__ == buf
2031 - parent.__dict__ is globals
2032 If this is violated... Who cares? */
2033}
2034
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002035/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002036static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002037load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
2038 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002039{
2040 char *name = *p_name;
2041 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002042 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002043 char *p;
2044 PyObject *result;
2045
2046 if (dot == NULL) {
2047 *p_name = NULL;
2048 len = strlen(name);
2049 }
2050 else {
2051 *p_name = dot+1;
2052 len = dot-name;
2053 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002054 if (len == 0) {
2055 PyErr_SetString(PyExc_ValueError,
2056 "Empty module name");
2057 return NULL;
2058 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002059
2060 p = buf + *p_buflen;
2061 if (p != buf)
2062 *p++ = '.';
2063 if (p+len-buf >= MAXPATHLEN) {
2064 PyErr_SetString(PyExc_ValueError,
2065 "Module name too long");
2066 return NULL;
2067 }
2068 strncpy(p, name, len);
2069 p[len] = '\0';
2070 *p_buflen = p+len-buf;
2071
2072 result = import_submodule(mod, p, buf);
2073 if (result == Py_None && altmod != mod) {
2074 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002075 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002076 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002077 if (result != NULL && result != Py_None) {
2078 if (mark_miss(buf) != 0) {
2079 Py_DECREF(result);
2080 return NULL;
2081 }
2082 strncpy(buf, name, len);
2083 buf[len] = '\0';
2084 *p_buflen = len;
2085 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002086 }
2087 if (result == NULL)
2088 return NULL;
2089
2090 if (result == Py_None) {
2091 Py_DECREF(result);
2092 PyErr_Format(PyExc_ImportError,
2093 "No module named %.200s", name);
2094 return NULL;
2095 }
2096
2097 return result;
2098}
2099
2100static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002101mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002102{
2103 PyObject *modules = PyImport_GetModuleDict();
2104 return PyDict_SetItemString(modules, name, Py_None);
2105}
2106
2107static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002108ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
2109 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002110{
2111 int i;
2112
2113 if (!PyObject_HasAttrString(mod, "__path__"))
2114 return 1;
2115
2116 for (i = 0; ; i++) {
2117 PyObject *item = PySequence_GetItem(fromlist, i);
2118 int hasit;
2119 if (item == NULL) {
2120 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2121 PyErr_Clear();
2122 return 1;
2123 }
2124 return 0;
2125 }
2126 if (!PyString_Check(item)) {
2127 PyErr_SetString(PyExc_TypeError,
2128 "Item in ``from list'' not a string");
2129 Py_DECREF(item);
2130 return 0;
2131 }
2132 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002133 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002134 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002135 /* See if the package defines __all__ */
2136 if (recursive)
2137 continue; /* Avoid endless recursion */
2138 all = PyObject_GetAttrString(mod, "__all__");
2139 if (all == NULL)
2140 PyErr_Clear();
2141 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002142 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002143 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002144 if (!ret)
2145 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002146 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002147 continue;
2148 }
2149 hasit = PyObject_HasAttr(mod, item);
2150 if (!hasit) {
2151 char *subname = PyString_AS_STRING(item);
2152 PyObject *submod;
2153 char *p;
2154 if (buflen + strlen(subname) >= MAXPATHLEN) {
2155 PyErr_SetString(PyExc_ValueError,
2156 "Module name too long");
2157 Py_DECREF(item);
2158 return 0;
2159 }
2160 p = buf + buflen;
2161 *p++ = '.';
2162 strcpy(p, subname);
2163 submod = import_submodule(mod, subname, buf);
2164 Py_XDECREF(submod);
2165 if (submod == NULL) {
2166 Py_DECREF(item);
2167 return 0;
2168 }
2169 }
2170 Py_DECREF(item);
2171 }
2172
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002173 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002174}
2175
Neil Schemenauer00b09662003-06-16 21:03:07 +00002176static int
2177add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2178 PyObject *modules)
2179{
2180 if (mod == Py_None)
2181 return 1;
2182 /* Irrespective of the success of this load, make a
2183 reference to it in the parent package module. A copy gets
2184 saved in the modules dictionary under the full name, so get a
2185 reference from there, if need be. (The exception is when the
2186 load failed with a SyntaxError -- then there's no trace in
2187 sys.modules. In that case, of course, do nothing extra.) */
2188 if (submod == NULL) {
2189 submod = PyDict_GetItemString(modules, fullname);
2190 if (submod == NULL)
2191 return 1;
2192 }
2193 if (PyModule_Check(mod)) {
2194 /* We can't use setattr here since it can give a
2195 * spurious warning if the submodule name shadows a
2196 * builtin name */
2197 PyObject *dict = PyModule_GetDict(mod);
2198 if (!dict)
2199 return 0;
2200 if (PyDict_SetItemString(dict, subname, submod) < 0)
2201 return 0;
2202 }
2203 else {
2204 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2205 return 0;
2206 }
2207 return 1;
2208}
2209
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002210static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002211import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002212{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002213 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002214 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002215
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002216 /* Require:
2217 if mod == None: subname == fullname
2218 else: mod.__name__ + "." + subname == fullname
2219 */
2220
Tim Peters50d8d372001-02-28 05:34:27 +00002221 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002222 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002223 }
2224 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002225 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002226 char buf[MAXPATHLEN+1];
2227 struct filedescr *fdp;
2228 FILE *fp = NULL;
2229
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002230 if (mod == Py_None)
2231 path = NULL;
2232 else {
2233 path = PyObject_GetAttrString(mod, "__path__");
2234 if (path == NULL) {
2235 PyErr_Clear();
2236 Py_INCREF(Py_None);
2237 return Py_None;
2238 }
2239 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002240
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002241 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002242 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2243 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002244 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002245 if (fdp == NULL) {
2246 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2247 return NULL;
2248 PyErr_Clear();
2249 Py_INCREF(Py_None);
2250 return Py_None;
2251 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002252 m = load_module(fullname, fp, buf, fdp->type, loader);
2253 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002254 if (fp)
2255 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002256 if (!add_submodule(mod, m, fullname, subname, modules)) {
2257 Py_XDECREF(m);
2258 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002259 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002260 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002261
2262 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002263}
2264
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002265
2266/* Re-import a module of any kind and return its module object, WITH
2267 INCREMENTED REFERENCE COUNT */
2268
Guido van Rossum79f25d91997-04-29 20:08:16 +00002269PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002270PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002271{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002272 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002273 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002274 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002275 char buf[MAXPATHLEN+1];
2276 struct filedescr *fdp;
2277 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002278 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002279
Guido van Rossum79f25d91997-04-29 20:08:16 +00002280 if (m == NULL || !PyModule_Check(m)) {
2281 PyErr_SetString(PyExc_TypeError,
2282 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002283 return NULL;
2284 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002285 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002286 if (name == NULL)
2287 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002288 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002289 PyErr_Format(PyExc_ImportError,
2290 "reload(): module %.200s not in sys.modules",
2291 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002292 return NULL;
2293 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002294 subname = strrchr(name, '.');
2295 if (subname == NULL)
2296 subname = name;
2297 else {
2298 PyObject *parentname, *parent;
2299 parentname = PyString_FromStringAndSize(name, (subname-name));
2300 if (parentname == NULL)
2301 return NULL;
2302 parent = PyDict_GetItem(modules, parentname);
2303 if (parent == NULL) {
2304 PyErr_Format(PyExc_ImportError,
2305 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002306 PyString_AS_STRING(parentname));
2307 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002308 return NULL;
2309 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002310 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002311 subname++;
2312 path = PyObject_GetAttrString(parent, "__path__");
2313 if (path == NULL)
2314 PyErr_Clear();
2315 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002316 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002317 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002318 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002319
2320 if (fdp == NULL) {
2321 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002322 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002323 }
2324
2325 newm = load_module(name, fp, buf, fdp->type, loader);
2326 Py_XDECREF(loader);
2327
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002328 if (fp)
2329 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002330 if (newm == NULL) {
2331 /* load_module probably removed name from modules because of
2332 * the error. Put back the original module object. We're
2333 * going to return NULL in this case regardless of whether
2334 * replacing name succeeds, so the return value is ignored.
2335 */
2336 PyDict_SetItemString(modules, name, m);
2337 }
2338 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002339}
2340
2341
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002342/* Higher-level import emulator which emulates the "import" statement
2343 more accurately -- it invokes the __import__() function from the
2344 builtins of the current globals. This means that the import is
2345 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002346 environment, e.g. by "rexec".
2347 A dummy list ["__doc__"] is passed as the 4th argument so that
2348 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2349 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002350
2351PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002352PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002353{
2354 static PyObject *silly_list = NULL;
2355 static PyObject *builtins_str = NULL;
2356 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002357 PyObject *globals = NULL;
2358 PyObject *import = NULL;
2359 PyObject *builtins = NULL;
2360 PyObject *r = NULL;
2361
2362 /* Initialize constant string objects */
2363 if (silly_list == NULL) {
2364 import_str = PyString_InternFromString("__import__");
2365 if (import_str == NULL)
2366 return NULL;
2367 builtins_str = PyString_InternFromString("__builtins__");
2368 if (builtins_str == NULL)
2369 return NULL;
2370 silly_list = Py_BuildValue("[s]", "__doc__");
2371 if (silly_list == NULL)
2372 return NULL;
2373 }
2374
2375 /* Get the builtins from current globals */
2376 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002377 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002378 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002379 builtins = PyObject_GetItem(globals, builtins_str);
2380 if (builtins == NULL)
2381 goto err;
2382 }
2383 else {
2384 /* No globals -- use standard builtins, and fake globals */
2385 PyErr_Clear();
2386
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002387 builtins = PyImport_ImportModuleEx("__builtin__",
2388 NULL, NULL, NULL);
2389 if (builtins == NULL)
2390 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002391 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2392 if (globals == NULL)
2393 goto err;
2394 }
2395
2396 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002397 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002398 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002399 if (import == NULL)
2400 PyErr_SetObject(PyExc_KeyError, import_str);
2401 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002402 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002403 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002404 if (import == NULL)
2405 goto err;
2406
2407 /* Call the _import__ function with the proper argument list */
2408 r = PyObject_CallFunction(import, "OOOO",
2409 module_name, globals, globals, silly_list);
2410
2411 err:
2412 Py_XDECREF(globals);
2413 Py_XDECREF(builtins);
2414 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002415
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002416 return r;
2417}
2418
2419
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002420/* Module 'imp' provides Python access to the primitives used for
2421 importing modules.
2422*/
2423
Guido van Rossum79f25d91997-04-29 20:08:16 +00002424static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002425imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002426{
2427 char buf[4];
2428
Guido van Rossum96774c12000-05-01 20:19:08 +00002429 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2430 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2431 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2432 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002435}
2436
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002438imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002439{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002440 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441 struct filedescr *fdp;
2442
Guido van Rossum79f25d91997-04-29 20:08:16 +00002443 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002444 if (list == NULL)
2445 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002446 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2447 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002448 fdp->suffix, fdp->mode, fdp->type);
2449 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002450 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002451 return NULL;
2452 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 if (PyList_Append(list, item) < 0) {
2454 Py_DECREF(list);
2455 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002456 return NULL;
2457 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002458 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002459 }
2460 return list;
2461}
2462
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002464call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002465{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002466 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002467 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002468 struct filedescr *fdp;
2469 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002470 FILE *fp = NULL;
2471
2472 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002473 if (path == Py_None)
2474 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002475 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002476 if (fdp == NULL)
2477 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002478 if (fp != NULL) {
2479 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2480 if (fob == NULL) {
2481 fclose(fp);
2482 return NULL;
2483 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002484 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002485 else {
2486 fob = Py_None;
2487 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002488 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002489 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002490 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002491 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002492 return ret;
2493}
2494
Guido van Rossum79f25d91997-04-29 20:08:16 +00002495static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002496imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002497{
2498 char *name;
2499 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002500 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002501 return NULL;
2502 return call_find_module(name, path);
2503}
2504
2505static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002506imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002507{
2508 char *name;
2509 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002510 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002511 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002512 return NULL;
2513 ret = init_builtin(name);
2514 if (ret < 0)
2515 return NULL;
2516 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002517 Py_INCREF(Py_None);
2518 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002519 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002520 m = PyImport_AddModule(name);
2521 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002522 return m;
2523}
2524
Guido van Rossum79f25d91997-04-29 20:08:16 +00002525static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002526imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002527{
2528 char *name;
2529 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002531 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002532 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002533 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534 if (ret < 0)
2535 return NULL;
2536 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002537 Py_INCREF(Py_None);
2538 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002539 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002540 m = PyImport_AddModule(name);
2541 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002542 return m;
2543}
2544
Guido van Rossum79f25d91997-04-29 20:08:16 +00002545static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002546imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002547{
2548 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002549
Guido van Rossum43713e52000-02-29 13:59:29 +00002550 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002551 return NULL;
2552 return get_frozen_object(name);
2553}
2554
Guido van Rossum79f25d91997-04-29 20:08:16 +00002555static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002556imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002557{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002559 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002560 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002561 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002562}
2563
Guido van Rossum79f25d91997-04-29 20:08:16 +00002564static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002565imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002566{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002567 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002568 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002569 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002571 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002572 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002573}
2574
2575static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002576get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577{
2578 FILE *fp;
2579 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002580 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002581 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002582 fp = fopen(pathname, mode);
2583 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002585 }
2586 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002588 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002589 PyErr_SetString(PyExc_ValueError,
2590 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002591 }
2592 return fp;
2593}
2594
Guido van Rossum79f25d91997-04-29 20:08:16 +00002595static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002596imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002597{
2598 char *name;
2599 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600 PyObject *fob = NULL;
2601 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002603 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002604 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002605 return NULL;
2606 fp = get_file(pathname, fob, "rb");
2607 if (fp == NULL)
2608 return NULL;
2609 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002610 if (fob == NULL)
2611 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002612 return m;
2613}
2614
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002615#ifdef HAVE_DYNAMIC_LOADING
2616
Guido van Rossum79f25d91997-04-29 20:08:16 +00002617static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002618imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002619{
2620 char *name;
2621 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002622 PyObject *fob = NULL;
2623 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002624 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002625 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002626 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002627 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002628 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002629 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002630 if (fp == NULL)
2631 return NULL;
2632 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002634 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002635}
2636
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002637#endif /* HAVE_DYNAMIC_LOADING */
2638
Guido van Rossum79f25d91997-04-29 20:08:16 +00002639static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002640imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002641{
2642 char *name;
2643 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002644 PyObject *fob = NULL;
2645 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002646 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002647 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002649 return NULL;
2650 fp = get_file(pathname, fob, "r");
2651 if (fp == NULL)
2652 return NULL;
2653 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002654 if (fob == NULL)
2655 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656 return m;
2657}
2658
Guido van Rossum79f25d91997-04-29 20:08:16 +00002659static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002660imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002661{
2662 char *name;
2663 PyObject *fob;
2664 char *pathname;
2665 char *suffix; /* Unused */
2666 char *mode;
2667 int type;
2668 FILE *fp;
2669
Guido van Rossum43713e52000-02-29 13:59:29 +00002670 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002671 &name, &fob, &pathname,
2672 &suffix, &mode, &type))
2673 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002674 if (*mode) {
2675 /* Mode must start with 'r' or 'U' and must not contain '+'.
2676 Implicit in this test is the assumption that the mode
2677 may contain other modifiers like 'b' or 't'. */
2678
2679 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002680 PyErr_Format(PyExc_ValueError,
2681 "invalid file open mode %.200s", mode);
2682 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002683 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002684 }
2685 if (fob == Py_None)
2686 fp = NULL;
2687 else {
2688 if (!PyFile_Check(fob)) {
2689 PyErr_SetString(PyExc_ValueError,
2690 "load_module arg#2 should be a file or None");
2691 return NULL;
2692 }
2693 fp = get_file(pathname, fob, mode);
2694 if (fp == NULL)
2695 return NULL;
2696 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002697 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002698}
2699
2700static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002701imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002702{
2703 char *name;
2704 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002705 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002706 return NULL;
2707 return load_package(name, pathname);
2708}
2709
2710static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002711imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002712{
2713 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002714 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002715 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002716 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002717}
2718
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002719/* Doc strings */
2720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002721PyDoc_STRVAR(doc_imp,
2722"This module provides the components needed to build your own\n\
2723__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725PyDoc_STRVAR(doc_find_module,
2726"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002727Search for a module. If path is omitted or None, search for a\n\
2728built-in, frozen or special module and continue search in sys.path.\n\
2729The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002730package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732PyDoc_STRVAR(doc_load_module,
2733"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002734Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002735The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002737PyDoc_STRVAR(doc_get_magic,
2738"get_magic() -> string\n\
2739Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741PyDoc_STRVAR(doc_get_suffixes,
2742"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002743Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002744that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(doc_new_module,
2747"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002748Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002749The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002751PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002752"lock_held() -> boolean\n\
2753Return True if the import lock is currently held, else False.\n\
2754On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002755
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002756PyDoc_STRVAR(doc_acquire_lock,
2757"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002758Acquires the interpreter's import lock for the current thread.\n\
2759This lock should be used by import hooks to ensure thread-safety\n\
2760when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002761On platforms without threads, this function does nothing.");
2762
2763PyDoc_STRVAR(doc_release_lock,
2764"release_lock() -> None\n\
2765Release the interpreter's import lock.\n\
2766On platforms without threads, this function does nothing.");
2767
Guido van Rossum79f25d91997-04-29 20:08:16 +00002768static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002769 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2770 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2771 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2772 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2773 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2774 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2775 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2776 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002777 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002778 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2779 {"init_builtin", imp_init_builtin, METH_VARARGS},
2780 {"init_frozen", imp_init_frozen, METH_VARARGS},
2781 {"is_builtin", imp_is_builtin, METH_VARARGS},
2782 {"is_frozen", imp_is_frozen, METH_VARARGS},
2783 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002784#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002785 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002786#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002787 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002788 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002789 {NULL, NULL} /* sentinel */
2790};
2791
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002792static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002793setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002794{
2795 PyObject *v;
2796 int err;
2797
2798 v = PyInt_FromLong((long)value);
2799 err = PyDict_SetItemString(d, name, v);
2800 Py_XDECREF(v);
2801 return err;
2802}
2803
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002804PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002805initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002806{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002807 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002808
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002809 m = Py_InitModule4("imp", imp_methods, doc_imp,
2810 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002811 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002813 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2814 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2815 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2816 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2817 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2818 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2819 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2820 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002821 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00002822 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002823
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002824 failure:
2825 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002826}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002827
2828
Guido van Rossumb18618d2000-05-03 23:44:39 +00002829/* API for embedding applications that want to add their own entries
2830 to the table of built-in modules. This should normally be called
2831 *before* Py_Initialize(). When the table resize fails, -1 is
2832 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002833
2834 After a similar function by Just van Rossum. */
2835
2836int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002837PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002838{
2839 static struct _inittab *our_copy = NULL;
2840 struct _inittab *p;
2841 int i, n;
2842
2843 /* Count the number of entries in both tables */
2844 for (n = 0; newtab[n].name != NULL; n++)
2845 ;
2846 if (n == 0)
2847 return 0; /* Nothing to do */
2848 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2849 ;
2850
2851 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002852 p = our_copy;
2853 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002854 if (p == NULL)
2855 return -1;
2856
2857 /* Copy the tables into the new memory */
2858 if (our_copy != PyImport_Inittab)
2859 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2860 PyImport_Inittab = our_copy = p;
2861 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2862
2863 return 0;
2864}
2865
2866/* Shorthand to add a single entry given a name and a function */
2867
2868int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002869PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002870{
2871 struct _inittab newtab[2];
2872
2873 memset(newtab, '\0', sizeof newtab);
2874
2875 newtab[0].name = name;
2876 newtab[0].initfunc = initfunc;
2877
2878 return PyImport_ExtendInittab(newtab);
2879}