blob: 291e63dc794bf22d75098afda13f428f3d48bea1 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +00007#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "pythonrun.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000010#include "marshal.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "code.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000012#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000014#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000015#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000016
Guido van Rossum55a83382000-09-20 20:31:38 +000017#ifdef HAVE_FCNTL_H
18#include <fcntl.h>
19#endif
Anthony Baxterac6bd462006-04-13 02:06:09 +000020#ifdef __cplusplus
21extern "C" {
22#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000023
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000024extern time_t PyOS_GetLastModificationTime(char *, FILE *);
25 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000026
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000027/* Magic word to reject .pyc files generated by other Python versions.
28 It should change for each incompatible change to the bytecode.
29
30 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000031 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000032 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000033
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000034 The magic numbers must be spaced apart atleast 2 values, as the
35 -U interpeter flag will cause MAGIC+1 being used. They have been
36 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000038 There were a variety of old schemes for setting the magic number.
39 The current working scheme is to increment the previous value by
40 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000041
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000042 Known values:
43 Python 1.5: 20121
44 Python 1.5.1: 20121
45 Python 1.5.2: 20121
Skip Montanaro4ec3c262006-03-25 14:12:03 +000046 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000047 Python 2.0: 50823
48 Python 2.0.1: 50823
49 Python 2.1: 60202
50 Python 2.1.1: 60202
51 Python 2.1.2: 60202
52 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000053 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000054 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000055 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000056 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000057 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000058 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000059 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000060 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000061 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000062 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Neal Norwitz0d62a062006-07-30 06:53:31 +000063 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
64 Python 2.5b3: 62111 (fix wrong code: x += yield)
Neal Norwitz9a70f952006-08-04 05:12:19 +000065 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
66 storing constants that should have been removed)
Neal Norwitz3b3aae02006-09-05 03:56:01 +000067 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000068.
Tim Peters36515e22001-11-18 04:06:29 +000069*/
Neal Norwitz3b3aae02006-09-05 03:56:01 +000070#define MAGIC (62131 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000071
Guido van Rossum96774c12000-05-01 20:19:08 +000072/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000073 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000074 compiler works which are enabled by command line switches. */
75static long pyc_magic = MAGIC;
76
Guido van Rossum25ce5661997-08-02 03:10:38 +000077/* See _PyImport_FixupExtension() below */
78static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000079
Guido van Rossum771c6c81997-10-31 18:37:24 +000080/* This table is defined in config.c: */
81extern struct _inittab _PyImport_Inittab[];
82
83struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000084
Guido van Rossumed1170e1999-12-20 21:23:41 +000085/* these tables define the module suffixes that Python recognizes */
86struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000087
88#ifdef RISCOS
89static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000090 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000091 {"/pyc", "rb", PY_COMPILED},
92 {0, 0}
93};
94#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000095static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000096 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000097#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000098 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000099#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000100 {".pyc", "rb", PY_COMPILED},
101 {0, 0}
102};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000103#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000104
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000105static PyTypeObject NullImporterType; /* Forward reference */
106
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000107/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108
109void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000112 const struct filedescr *scan;
113 struct filedescr *filetab;
114 int countD = 0;
115 int countS = 0;
116
117 /* prepare _PyImport_Filetab: copy entries from
118 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
119 */
120 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
121 ++countD;
122 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
123 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000124 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Neal Norwitze1fdb322006-07-21 05:32:28 +0000125 if (filetab == NULL)
Neal Norwitz33722ae2006-07-21 07:59:02 +0000126 Py_FatalError("Can't initialize import file table.");
Guido van Rossumed1170e1999-12-20 21:23:41 +0000127 memcpy(filetab, _PyImport_DynLoadFiletab,
128 countD * sizeof(struct filedescr));
129 memcpy(filetab + countD, _PyImport_StandardFiletab,
130 countS * sizeof(struct filedescr));
131 filetab[countD + countS].suffix = NULL;
132
133 _PyImport_Filetab = filetab;
134
Guido van Rossum0824f631997-03-11 18:37:35 +0000135 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000136 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
137 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000138#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000139 if (strcmp(filetab->suffix, ".pyc") == 0)
140 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000141#else
142 if (strcmp(filetab->suffix, "/pyc") == 0)
143 filetab->suffix = "/pyo";
144#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000145 }
146 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000147
148 if (Py_UnicodeFlag) {
149 /* Fix the pyc_magic so that byte compiled code created
150 using the all-Unicode method doesn't interfere with
151 code created in normal operation mode. */
152 pyc_magic = MAGIC + 1;
153 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154}
155
Guido van Rossum25ce5661997-08-02 03:10:38 +0000156void
Just van Rossum52e14d62002-12-30 22:08:05 +0000157_PyImportHooks_Init(void)
158{
159 PyObject *v, *path_hooks = NULL, *zimpimport;
160 int err = 0;
161
162 /* adding sys.path_hooks and sys.path_importer_cache, setting up
163 zipimport */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000164 if (PyType_Ready(&NullImporterType) < 0)
165 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000166
167 if (Py_VerboseFlag)
168 PySys_WriteStderr("# installing zipimport hook\n");
169
170 v = PyList_New(0);
171 if (v == NULL)
172 goto error;
173 err = PySys_SetObject("meta_path", v);
174 Py_DECREF(v);
175 if (err)
176 goto error;
177 v = PyDict_New();
178 if (v == NULL)
179 goto error;
180 err = PySys_SetObject("path_importer_cache", v);
181 Py_DECREF(v);
182 if (err)
183 goto error;
184 path_hooks = PyList_New(0);
185 if (path_hooks == NULL)
186 goto error;
187 err = PySys_SetObject("path_hooks", path_hooks);
188 if (err) {
189 error:
190 PyErr_Print();
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000191 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
192 "path_importer_cache, or NullImporter failed"
193 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000194 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000195
Just van Rossum52e14d62002-12-30 22:08:05 +0000196 zimpimport = PyImport_ImportModule("zipimport");
197 if (zimpimport == NULL) {
198 PyErr_Clear(); /* No zip import module -- okay */
199 if (Py_VerboseFlag)
200 PySys_WriteStderr("# can't import zipimport\n");
201 }
202 else {
203 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
204 "zipimporter");
205 Py_DECREF(zimpimport);
206 if (zipimporter == NULL) {
207 PyErr_Clear(); /* No zipimporter object -- okay */
208 if (Py_VerboseFlag)
209 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000210 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000211 }
212 else {
213 /* sys.path_hooks.append(zipimporter) */
214 err = PyList_Append(path_hooks, zipimporter);
215 Py_DECREF(zipimporter);
216 if (err)
217 goto error;
218 if (Py_VerboseFlag)
219 PySys_WriteStderr(
220 "# installed zipimport hook\n");
221 }
222 }
223 Py_DECREF(path_hooks);
224}
225
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228{
229 Py_XDECREF(extensions);
230 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000231 PyMem_DEL(_PyImport_Filetab);
232 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233}
234
235
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000236/* Locking primitives to prevent parallel imports of the same module
237 in different threads to return with a partially loaded module.
238 These calls are serialized by the global interpreter lock. */
239
240#ifdef WITH_THREAD
241
Guido van Rossum49b56061998-10-01 20:42:43 +0000242#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000243
Guido van Rossum65d5b571998-12-21 19:32:43 +0000244static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000245static long import_lock_thread = -1;
246static int import_lock_level = 0;
247
248static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252 if (me == -1)
253 return; /* Too bad */
Neal Norwitze1fdb322006-07-21 05:32:28 +0000254 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 import_lock = PyThread_allocate_lock();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000256 if (import_lock == NULL)
257 return; /* Nothing much we can do. */
258 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259 if (import_lock_thread == me) {
260 import_lock_level++;
261 return;
262 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000263 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
264 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000265 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000266 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000267 PyEval_RestoreThread(tstate);
268 }
269 import_lock_thread = me;
270 import_lock_level = 1;
271}
272
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000273static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000275{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000276 long me = PyThread_get_thread_ident();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000277 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000279 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000280 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000281 import_lock_level--;
282 if (import_lock_level == 0) {
283 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000284 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000285 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000287}
288
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000289/* This function is called from PyOS_AfterFork to ensure that newly
290 created child processes do not share locks with the parent. */
291
292void
293_PyImport_ReInitLock(void)
294{
295#ifdef _AIX
296 if (import_lock != NULL)
297 import_lock = PyThread_allocate_lock();
298#endif
299}
300
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000301#else
302
303#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000304#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000305
306#endif
307
Tim Peters69232342001-08-30 05:16:13 +0000308static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000309imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000310{
Tim Peters69232342001-08-30 05:16:13 +0000311#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000312 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000313#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000314 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000315#endif
316}
317
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000318static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000319imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000320{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000321#ifdef WITH_THREAD
322 lock_import();
323#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000324 Py_INCREF(Py_None);
325 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000326}
327
328static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000329imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000330{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000331#ifdef WITH_THREAD
332 if (unlock_import() < 0) {
333 PyErr_SetString(PyExc_RuntimeError,
334 "not holding the import lock");
335 return NULL;
336 }
337#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000338 Py_INCREF(Py_None);
339 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000340}
341
Collin Wintere19d7a32007-03-12 16:49:23 +0000342static void
343imp_modules_reloading_clear (void)
344{
345 PyInterpreterState *interp = PyThreadState_Get()->interp;
346 if (interp->modules_reloading == NULL)
347 return;
348 PyDict_Clear(interp->modules_reloading);
349 return;
350}
351
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352/* Helper for sys */
353
354PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000355PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000357 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000358 if (interp->modules == NULL)
359 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
360 return interp->modules;
361}
362
Guido van Rossum3f5da241990-12-20 15:06:42 +0000363
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000364/* List of names to clear in sys */
365static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000366 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000367 "exc_type", "exc_value", "exc_traceback",
368 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000369 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000370 NULL
371};
372
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000373static char* sys_files[] = {
374 "stdin", "__stdin__",
375 "stdout", "__stdout__",
376 "stderr", "__stderr__",
377 NULL
378};
379
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000380
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000381/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000385{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000386 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000387 char *name;
388 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000389 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000390 PyObject *modules = interp->modules;
391
392 if (modules == NULL)
393 return; /* Already done */
394
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000395 /* Delete some special variables first. These are common
396 places where user values hide and people complain when their
397 destructors fail. Since the modules containing them are
398 deleted *last* of all, they would come too late in the normal
399 destruction order. Sigh. */
400
401 value = PyDict_GetItemString(modules, "__builtin__");
402 if (value != NULL && PyModule_Check(value)) {
403 dict = PyModule_GetDict(value);
404 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000405 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000406 PyDict_SetItemString(dict, "_", Py_None);
407 }
408 value = PyDict_GetItemString(modules, "sys");
409 if (value != NULL && PyModule_Check(value)) {
410 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000411 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000412 dict = PyModule_GetDict(value);
413 for (p = sys_deletes; *p != NULL; p++) {
414 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000415 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000416 PyDict_SetItemString(dict, *p, Py_None);
417 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000418 for (p = sys_files; *p != NULL; p+=2) {
419 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000420 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000421 v = PyDict_GetItemString(dict, *(p+1));
422 if (v == NULL)
423 v = Py_None;
424 PyDict_SetItemString(dict, *p, v);
425 }
426 }
427
428 /* First, delete __main__ */
429 value = PyDict_GetItemString(modules, "__main__");
430 if (value != NULL && PyModule_Check(value)) {
431 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000432 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000433 _PyModule_Clear(value);
434 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000435 }
436
Guido van Rossum758eec01998-01-19 21:58:26 +0000437 /* The special treatment of __builtin__ here is because even
438 when it's not referenced as a module, its dictionary is
439 referenced by almost every module's __builtins__. Since
440 deleting a module clears its dictionary (even if there are
441 references left to it), we need to delete the __builtin__
442 module last. Likewise, we don't delete sys until the very
443 end because it is implicitly referenced (e.g. by print).
444
445 Also note that we 'delete' modules by replacing their entry
446 in the modules dict with None, rather than really deleting
447 them; this avoids a rehash of the modules dictionary and
448 also marks them as "non existent" so they won't be
449 re-imported. */
450
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000451 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000452 one (skipping __builtin__ and sys) and delete them */
453 do {
454 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000455 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000456 while (PyDict_Next(modules, &pos, &key, &value)) {
457 if (value->ob_refcnt != 1)
458 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000459 if (PyString_Check(key) && PyModule_Check(value)) {
460 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000461 if (strcmp(name, "__builtin__") == 0)
462 continue;
463 if (strcmp(name, "sys") == 0)
464 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000465 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000466 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000467 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000468 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000469 PyDict_SetItem(modules, key, Py_None);
470 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000471 }
472 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000473 } while (ndone > 0);
474
Guido van Rossum758eec01998-01-19 21:58:26 +0000475 /* Next, delete all modules (still skipping __builtin__ and sys) */
476 pos = 0;
477 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000478 if (PyString_Check(key) && PyModule_Check(value)) {
479 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000480 if (strcmp(name, "__builtin__") == 0)
481 continue;
482 if (strcmp(name, "sys") == 0)
483 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000484 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000485 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000486 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000487 PyDict_SetItem(modules, key, Py_None);
488 }
489 }
490
491 /* Next, delete sys and __builtin__ (in that order) */
492 value = PyDict_GetItemString(modules, "sys");
493 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000494 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000495 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000496 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000497 PyDict_SetItemString(modules, "sys", Py_None);
498 }
499 value = PyDict_GetItemString(modules, "__builtin__");
500 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000501 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000502 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000503 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000504 PyDict_SetItemString(modules, "__builtin__", Py_None);
505 }
506
507 /* Finally, clear and delete the modules directory */
508 PyDict_Clear(modules);
509 interp->modules = NULL;
510 Py_DECREF(modules);
Collin Wintere19d7a32007-03-12 16:49:23 +0000511 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000512}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000513
514
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000515/* Helper for pythonrun.c -- return magic number */
516
517long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000518PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000519{
Guido van Rossum96774c12000-05-01 20:19:08 +0000520 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000521}
522
523
Guido van Rossum25ce5661997-08-02 03:10:38 +0000524/* Magic for extension modules (built-in as well as dynamically
525 loaded). To prevent initializing an extension module more than
526 once, we keep a static dictionary 'extensions' keyed by module name
527 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000528 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529 dictionary is stored by calling _PyImport_FixupExtension()
530 immediately after the module initialization function succeeds. A
531 copy can be retrieved from there by calling
532 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000537 PyObject *modules, *mod, *dict, *copy;
538 if (extensions == NULL) {
539 extensions = PyDict_New();
540 if (extensions == NULL)
541 return NULL;
542 }
543 modules = PyImport_GetModuleDict();
544 mod = PyDict_GetItemString(modules, name);
545 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000546 PyErr_Format(PyExc_SystemError,
547 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000548 return NULL;
549 }
550 dict = PyModule_GetDict(mod);
551 if (dict == NULL)
552 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000553 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554 if (copy == NULL)
555 return NULL;
556 PyDict_SetItemString(extensions, filename, copy);
557 Py_DECREF(copy);
558 return copy;
559}
560
561PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000562_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000564 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000565 if (extensions == NULL)
566 return NULL;
567 dict = PyDict_GetItemString(extensions, filename);
568 if (dict == NULL)
569 return NULL;
570 mod = PyImport_AddModule(name);
571 if (mod == NULL)
572 return NULL;
573 mdict = PyModule_GetDict(mod);
574 if (mdict == NULL)
575 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000576 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000577 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000579 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000580 name, filename);
581 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582}
583
584
585/* Get the module object corresponding to a module name.
586 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000587 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000588 Because the former action is most common, THIS DOES NOT RETURN A
589 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000592PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000594 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596
Guido van Rossum25ce5661997-08-02 03:10:38 +0000597 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000599 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601 if (m == NULL)
602 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 return NULL;
606 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608
609 return m;
610}
611
Tim Peters1cd70172004-08-02 03:52:12 +0000612/* Remove name from sys.modules, if it's there. */
613static void
614_RemoveModule(const char *name)
615{
616 PyObject *modules = PyImport_GetModuleDict();
617 if (PyDict_GetItemString(modules, name) == NULL)
618 return;
619 if (PyDict_DelItemString(modules, name) < 0)
620 Py_FatalError("import: deleting existing key in"
621 "sys.modules failed");
622}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000624/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000625 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
626 * removed from sys.modules, to avoid leaving damaged module objects
627 * in sys.modules. The caller may wish to restore the original
628 * module object (if any) in this case; PyImport_ReloadModule is an
629 * example.
630 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000632PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000634 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
635}
636
637PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000638PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000639{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000640 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000642
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644 if (m == NULL)
645 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000646 /* If the module is being reloaded, we get the old module back
647 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 d = PyModule_GetDict(m);
649 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
650 if (PyDict_SetItemString(d, "__builtins__",
651 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000652 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000653 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000654 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000655 v = NULL;
656 if (pathname != NULL) {
657 v = PyString_FromString(pathname);
658 if (v == NULL)
659 PyErr_Clear();
660 }
661 if (v == NULL) {
662 v = ((PyCodeObject *)co)->co_filename;
663 Py_INCREF(v);
664 }
665 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000666 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000667 Py_DECREF(v);
668
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000669 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000670 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000671 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000673
Guido van Rossum25ce5661997-08-02 03:10:38 +0000674 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000675 PyErr_Format(PyExc_ImportError,
676 "Loaded module %.200s not found in sys.modules",
677 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000678 return NULL;
679 }
680
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000682
683 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000684
685 error:
686 _RemoveModule(name);
687 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688}
689
690
691/* Given a pathname for a Python source file, fill a buffer with the
692 pathname for the corresponding compiled file. Return the pathname
693 for the compiled file, or NULL if there's no space in the buffer.
694 Doesn't set an exception. */
695
696static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000697make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698{
Tim Petersc1731372001-08-04 08:12:36 +0000699 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700 if (len+2 > buflen)
701 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000702
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000703#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000704 /* Treat .pyw as if it were .py. The case of ".pyw" must match
705 that used in _PyImport_StandardFiletab. */
706 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
707 --len; /* pretend 'w' isn't there */
708#endif
709 memcpy(buf, pathname, len);
710 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
711 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712
713 return buf;
714}
715
716
717/* Given a pathname for a Python source file, its time of last
718 modification, and a pathname for a compiled file, check whether the
719 compiled file represents the same version of the source. If so,
720 return a FILE pointer for the compiled file, positioned just after
721 the header; if not, return NULL.
722 Doesn't set an exception. */
723
724static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000725check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726{
727 FILE *fp;
728 long magic;
729 long pyc_mtime;
730
731 fp = fopen(cpathname, "rb");
732 if (fp == NULL)
733 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000735 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000737 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 fclose(fp);
739 return NULL;
740 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000744 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745 fclose(fp);
746 return NULL;
747 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000749 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000750 return fp;
751}
752
753
754/* Read a code object from a file and check it for validity */
755
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000757read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760
Tim Petersd9b9ac82001-01-28 00:27:39 +0000761 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000762 if (co == NULL)
763 return NULL;
764 if (!PyCode_Check(co)) {
765 PyErr_Format(PyExc_ImportError,
766 "Non-code object in %.200s", cpathname);
767 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000768 return NULL;
769 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771}
772
773
774/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000775 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000776
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000778load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000779{
780 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781 PyCodeObject *co;
782 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000785 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000786 PyErr_Format(PyExc_ImportError,
787 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788 return NULL;
789 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000791 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792 if (co == NULL)
793 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000795 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000796 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000797 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000799
800 return m;
801}
802
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000803/* Parse a source file and return the corresponding code object */
804
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000807{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 PyCodeObject *co = NULL;
809 mod_ty mod;
Neal Norwitzece448e2006-09-11 04:06:23 +0000810 PyArena *arena = PyArena_New();
811 if (arena == NULL)
812 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000815 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000817 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
Neal Norwitzece448e2006-09-11 04:06:23 +0000819 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820 return co;
821}
822
823
Guido van Rossum55a83382000-09-20 20:31:38 +0000824/* Helper to open a bytecode file for writing in exclusive mode */
825
826static FILE *
827open_exclusive(char *filename)
828{
829#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
830 /* Use O_EXCL to avoid a race condition when another process tries to
831 write the same file. When that happens, our open() call fails,
832 which is just fine (since it's only a cache).
833 XXX If the file exists and is writable but the directory is not
834 writable, the file will never be written. Oh well.
835 */
836 int fd;
837 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000838 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
839#ifdef O_BINARY
840 |O_BINARY /* necessary for Windows */
841#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000842#ifdef __VMS
Martin v. Löwis18e16552006-02-15 17:27:45 +0000843 , 0666, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000844#else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000845 , 0666
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000846#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000847 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000848 if (fd < 0)
849 return NULL;
850 return fdopen(fd, "wb");
851#else
852 /* Best we can do -- on Windows this can't happen anyway */
853 return fopen(filename, "wb");
854#endif
855}
856
857
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858/* Write a compiled module to a file, placing the time of last
859 modification of its source into the header.
860 Errors are ignored, if a write error occurs an attempt is made to
861 remove the file. */
862
863static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000864write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000865{
866 FILE *fp;
867
Guido van Rossum55a83382000-09-20 20:31:38 +0000868 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000871 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872 "# can't create %s\n", cpathname);
873 return;
874 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000875 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000877 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
878 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000879 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000881 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882 /* Don't keep partial file */
883 fclose(fp);
884 (void) unlink(cpathname);
885 return;
886 }
887 /* Now write the true mtime */
888 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000889 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000890 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 fflush(fp);
892 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000893 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000894 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000895}
896
897
898/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000899 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
900 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901
Guido van Rossum79f25d91997-04-29 20:08:16 +0000902static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000903load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000904{
Fred Drake4c82b232000-06-30 16:18:57 +0000905 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000906 FILE *fpc;
907 char buf[MAXPATHLEN+1];
908 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 PyCodeObject *co;
910 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000911
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000912 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000913 if (mtime == (time_t)(-1)) {
914 PyErr_Format(PyExc_RuntimeError,
915 "unable to get modification time from '%s'",
916 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000917 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000918 }
Fred Drake4c82b232000-06-30 16:18:57 +0000919#if SIZEOF_TIME_T > 4
920 /* Python's .pyc timestamp handling presumes that the timestamp fits
921 in 4 bytes. This will be fine until sometime in the year 2038,
922 when a 4-byte signed time_t will overflow.
923 */
924 if (mtime >> 32) {
925 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000926 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000927 return NULL;
928 }
929#endif
Tim Peters36515e22001-11-18 04:06:29 +0000930 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000931 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000932 if (cpathname != NULL &&
933 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000934 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000935 fclose(fpc);
936 if (co == NULL)
937 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000939 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000941 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000942 }
943 else {
944 co = parse_source_module(pathname, fp);
945 if (co == NULL)
946 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000948 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949 name, pathname);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +0000950 if (cpathname)
951 write_compiled_module(co, cpathname, mtime);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000953 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000955
956 return m;
957}
958
959
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000960/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000961static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
962static struct filedescr *find_module(char *, char *, PyObject *,
963 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000964static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000965
966/* Load a package and return its module object WITH INCREMENTED
967 REFERENCE COUNT */
968
969static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000970load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000971{
Tim Peters1cd70172004-08-02 03:52:12 +0000972 PyObject *m, *d;
973 PyObject *file = NULL;
974 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000975 int err;
976 char buf[MAXPATHLEN+1];
977 FILE *fp = NULL;
978 struct filedescr *fdp;
979
980 m = PyImport_AddModule(name);
981 if (m == NULL)
982 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000983 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000984 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000985 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000986 d = PyModule_GetDict(m);
987 file = PyString_FromString(pathname);
988 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000989 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000990 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000991 if (path == NULL)
992 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000993 err = PyDict_SetItemString(d, "__file__", file);
994 if (err == 0)
995 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000996 if (err != 0)
997 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000998 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000999 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001000 if (fdp == NULL) {
1001 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1002 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001003 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001004 }
1005 else
1006 m = NULL;
1007 goto cleanup;
1008 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001009 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001010 if (fp != NULL)
1011 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001012 goto cleanup;
1013
1014 error:
1015 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001016 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001017 Py_XDECREF(path);
1018 Py_XDECREF(file);
1019 return m;
1020}
1021
1022
1023/* Helper to test for built-in module */
1024
1025static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001026is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001027{
1028 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001029 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1030 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1031 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001032 return -1;
1033 else
1034 return 1;
1035 }
1036 }
1037 return 0;
1038}
1039
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001040
Just van Rossum52e14d62002-12-30 22:08:05 +00001041/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1042 possibly by fetching it from the path_importer_cache dict. If it
1043 wasn't yet cached, traverse path_hooks until it a hook is found
1044 that can handle the path item. Return None if no hook could;
1045 this tells our caller it should fall back to the builtin
1046 import mechanism. Cache the result in path_importer_cache.
1047 Returns a borrowed reference. */
1048
1049static PyObject *
1050get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1051 PyObject *p)
1052{
1053 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001054 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001055
1056 /* These conditions are the caller's responsibility: */
1057 assert(PyList_Check(path_hooks));
1058 assert(PyDict_Check(path_importer_cache));
1059
1060 nhooks = PyList_Size(path_hooks);
1061 if (nhooks < 0)
1062 return NULL; /* Shouldn't happen */
1063
1064 importer = PyDict_GetItem(path_importer_cache, p);
1065 if (importer != NULL)
1066 return importer;
1067
1068 /* set path_importer_cache[p] to None to avoid recursion */
1069 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1070 return NULL;
1071
1072 for (j = 0; j < nhooks; j++) {
1073 PyObject *hook = PyList_GetItem(path_hooks, j);
1074 if (hook == NULL)
1075 return NULL;
Georg Brandl684fd0c2006-05-25 19:15:31 +00001076 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001077 if (importer != NULL)
1078 break;
1079
1080 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1081 return NULL;
1082 }
1083 PyErr_Clear();
1084 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001085 if (importer == NULL) {
1086 importer = PyObject_CallFunctionObjArgs(
1087 (PyObject *)&NullImporterType, p, NULL
1088 );
1089 if (importer == NULL) {
1090 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1091 PyErr_Clear();
1092 return Py_None;
1093 }
1094 }
1095 }
1096 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001097 int err = PyDict_SetItem(path_importer_cache, p, importer);
1098 Py_DECREF(importer);
1099 if (err != 0)
1100 return NULL;
1101 }
1102 return importer;
1103}
1104
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001105/* Search the path (default sys.path) for a module. Return the
1106 corresponding filedescr struct, and (via return arguments) the
1107 pathname and an open file. Return NULL if the module is not found. */
1108
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001109#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001110extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001111 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001112#endif
1113
Martin v. Löwis18e16552006-02-15 17:27:45 +00001114static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001115static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001116static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001117
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001118static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001119find_module(char *fullname, char *subname, PyObject *path, char *buf,
1120 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001121{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001122 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001123 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001124 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001125 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001126 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001127 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001128#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001129 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001130#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001131 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1132 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1133 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001134 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001135#if defined(PYOS_OS2)
1136 size_t saved_len;
1137 size_t saved_namelen;
1138 char *saved_buf = NULL;
1139#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001140 if (p_loader != NULL)
1141 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001142
Just van Rossum52e14d62002-12-30 22:08:05 +00001143 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001144 PyErr_SetString(PyExc_OverflowError,
1145 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001146 return NULL;
1147 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001148 strcpy(name, subname);
1149
1150 /* sys.meta_path import hook */
1151 if (p_loader != NULL) {
1152 PyObject *meta_path;
1153
1154 meta_path = PySys_GetObject("meta_path");
1155 if (meta_path == NULL || !PyList_Check(meta_path)) {
1156 PyErr_SetString(PyExc_ImportError,
1157 "sys.meta_path must be a list of "
1158 "import hooks");
1159 return NULL;
1160 }
1161 Py_INCREF(meta_path); /* zap guard */
1162 npath = PyList_Size(meta_path);
1163 for (i = 0; i < npath; i++) {
1164 PyObject *loader;
1165 PyObject *hook = PyList_GetItem(meta_path, i);
1166 loader = PyObject_CallMethod(hook, "find_module",
1167 "sO", fullname,
1168 path != NULL ?
1169 path : Py_None);
1170 if (loader == NULL) {
1171 Py_DECREF(meta_path);
1172 return NULL; /* true error */
1173 }
1174 if (loader != Py_None) {
1175 /* a loader was found */
1176 *p_loader = loader;
1177 Py_DECREF(meta_path);
1178 return &importhookdescr;
1179 }
1180 Py_DECREF(loader);
1181 }
1182 Py_DECREF(meta_path);
1183 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001184
1185 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001186 /* The only type of submodule allowed inside a "frozen"
1187 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001188 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1189 PyErr_SetString(PyExc_ImportError,
1190 "full frozen module name too long");
1191 return NULL;
1192 }
1193 strcpy(buf, PyString_AsString(path));
1194 strcat(buf, ".");
1195 strcat(buf, name);
1196 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001197 if (find_frozen(name) != NULL) {
1198 strcpy(buf, name);
1199 return &fd_frozen;
1200 }
1201 PyErr_Format(PyExc_ImportError,
1202 "No frozen submodule named %.200s", name);
1203 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001204 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001205 if (path == NULL) {
1206 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001207 strcpy(buf, name);
1208 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001209 }
Greg Ward201baee2001-10-04 14:52:06 +00001210 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001211 strcpy(buf, name);
1212 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001213 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214
Guido van Rossumac279101996-08-22 23:10:58 +00001215#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001216 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1217 if (fp != NULL) {
1218 *p_fp = fp;
1219 return fdp;
1220 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001221#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001222 path = PySys_GetObject("path");
1223 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001224 if (path == NULL || !PyList_Check(path)) {
1225 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001226 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001227 return NULL;
1228 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001229
1230 path_hooks = PySys_GetObject("path_hooks");
1231 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1232 PyErr_SetString(PyExc_ImportError,
1233 "sys.path_hooks must be a list of "
1234 "import hooks");
1235 return NULL;
1236 }
1237 path_importer_cache = PySys_GetObject("path_importer_cache");
1238 if (path_importer_cache == NULL ||
1239 !PyDict_Check(path_importer_cache)) {
1240 PyErr_SetString(PyExc_ImportError,
1241 "sys.path_importer_cache must be a dict");
1242 return NULL;
1243 }
1244
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001246 namelen = strlen(name);
1247 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001248 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001249 PyObject *v = PyList_GetItem(path, i);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00001250 if (!v)
1251 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001252#ifdef Py_USING_UNICODE
1253 if (PyUnicode_Check(v)) {
1254 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1255 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1256 if (copy == NULL)
1257 return NULL;
1258 v = copy;
1259 }
1260 else
1261#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001263 continue;
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +00001264 len = PyString_GET_SIZE(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001265 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1266 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001267 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001268 }
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +00001269 strcpy(buf, PyString_AS_STRING(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001270 if (strlen(buf) != len) {
1271 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001272 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001273 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001274
1275 /* sys.path_hooks import hook */
1276 if (p_loader != NULL) {
1277 PyObject *importer;
1278
1279 importer = get_path_importer(path_importer_cache,
1280 path_hooks, v);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001281 if (importer == NULL) {
1282 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001283 return NULL;
Neal Norwitza4df11d2006-07-06 04:28:59 +00001284 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001285 /* Note: importer is a borrowed reference */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001286 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001287 PyObject *loader;
1288 loader = PyObject_CallMethod(importer,
1289 "find_module",
1290 "s", fullname);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001291 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001292 if (loader == NULL)
1293 return NULL; /* error */
1294 if (loader != Py_None) {
1295 /* a loader was found */
1296 *p_loader = loader;
1297 return &importhookdescr;
1298 }
1299 Py_DECREF(loader);
Georg Brandlf4ef1162006-05-26 18:03:31 +00001300 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001301 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001302 }
Georg Brandlf4ef1162006-05-26 18:03:31 +00001303 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001304
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001305 if (len > 0 && buf[len-1] != SEP
1306#ifdef ALTSEP
1307 && buf[len-1] != ALTSEP
1308#endif
1309 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001310 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001311 strcpy(buf+len, name);
1312 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001313
1314 /* Check for package import (buf holds a directory name,
1315 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001316#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001317 if (stat(buf, &statbuf) == 0 && /* it exists */
1318 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001319 case_ok(buf, len, namelen, name)) { /* case matches */
1320 if (find_init_module(buf)) { /* and has __init__.py */
1321 Py_XDECREF(copy);
1322 return &fd_package;
1323 }
1324 else {
1325 char warnstr[MAXPATHLEN+80];
1326 sprintf(warnstr, "Not importing directory "
1327 "'%.*s': missing __init__.py",
1328 MAXPATHLEN, buf);
1329 if (PyErr_Warn(PyExc_ImportWarning,
1330 warnstr)) {
1331 Py_XDECREF(copy);
1332 return NULL;
1333 }
1334 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001335 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001336#else
1337 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001338#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001339 if (isdir(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001340 case_ok(buf, len, namelen, name)) {
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001341 if (find_init_module(buf)) {
1342 Py_XDECREF(copy);
1343 return &fd_package;
1344 }
1345 else {
1346 char warnstr[MAXPATHLEN+80];
1347 sprintf(warnstr, "Not importing directory "
1348 "'%.*s': missing __init__.py",
1349 MAXPATHLEN, buf);
1350 if (PyErr_Warn(PyExc_ImportWarning,
1351 warnstr)) {
1352 Py_XDECREF(copy);
1353 return NULL;
1354 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001355 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001356#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001357#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001358#if defined(PYOS_OS2)
1359 /* take a snapshot of the module spec for restoration
1360 * after the 8 character DLL hackery
1361 */
1362 saved_buf = strdup(buf);
1363 saved_len = len;
1364 saved_namelen = namelen;
1365#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001366 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001367#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001368 /* OS/2 limits DLLs to 8 character names (w/o
1369 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001370 * so if the name is longer than that and its a
1371 * dynamically loaded module we're going to try,
1372 * truncate the name before trying
1373 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001374 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001375 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001376 const struct filedescr *scan;
1377 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001378 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001379 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001380 break;
1381 else
1382 scan++;
1383 }
1384 if (scan->suffix != NULL) {
1385 /* yes, so truncate the name */
1386 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001387 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001388 buf[len] = '\0';
1389 }
1390 }
1391#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001392 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001394 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001395 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001396 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001397 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001398 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001399 if (fp != NULL) {
1400 if (case_ok(buf, len, namelen, name))
1401 break;
1402 else { /* continue search */
1403 fclose(fp);
1404 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001405 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001406 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001407#if defined(PYOS_OS2)
1408 /* restore the saved snapshot */
1409 strcpy(buf, saved_buf);
1410 len = saved_len;
1411 namelen = saved_namelen;
1412#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001413 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001414#if defined(PYOS_OS2)
1415 /* don't need/want the module name snapshot anymore */
1416 if (saved_buf)
1417 {
1418 free(saved_buf);
1419 saved_buf = NULL;
1420 }
1421#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001422 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001423 if (fp != NULL)
1424 break;
1425 }
1426 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001427 PyErr_Format(PyExc_ImportError,
1428 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001429 return NULL;
1430 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001431 *p_fp = fp;
1432 return fdp;
1433}
1434
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001435/* Helpers for main.c
1436 * Find the source file corresponding to a named module
1437 */
1438struct filedescr *
1439_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1440 size_t buflen, FILE **p_fp, PyObject **p_loader)
1441{
1442 return find_module((char *) name, (char *) name, path,
1443 buf, buflen, p_fp, p_loader);
1444}
1445
1446PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1447{
1448 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1449}
1450
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001452 * The arguments here are tricky, best shown by example:
1453 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1454 * ^ ^ ^ ^
1455 * |--------------------- buf ---------------------|
1456 * |------------------- len ------------------|
1457 * |------ name -------|
1458 * |----- namelen -----|
1459 * buf is the full path, but len only counts up to (& exclusive of) the
1460 * extension. name is the module name, also exclusive of extension.
1461 *
1462 * We've already done a successful stat() or fopen() on buf, so know that
1463 * there's some match, possibly case-insensitive.
1464 *
Tim Peters50d8d372001-02-28 05:34:27 +00001465 * case_ok() is to return 1 if there's a case-sensitive match for
1466 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1467 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001468 *
Tim Peters50d8d372001-02-28 05:34:27 +00001469 * case_ok() is used to implement case-sensitive import semantics even
1470 * on platforms with case-insensitive filesystems. It's trivial to implement
1471 * for case-sensitive filesystems. It's pretty much a cross-platform
1472 * nightmare for systems with case-insensitive filesystems.
1473 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001474
Tim Peters50d8d372001-02-28 05:34:27 +00001475/* First we may need a pile of platform-specific header files; the sequence
1476 * of #if's here should match the sequence in the body of case_ok().
1477 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001478#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001479#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001480
Tim Peters50d8d372001-02-28 05:34:27 +00001481#elif defined(DJGPP)
1482#include <dir.h>
1483
Jason Tishler7961aa62005-05-20 00:56:54 +00001484#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001485#include <sys/types.h>
1486#include <dirent.h>
1487
Andrew MacIntyred9400542002-02-26 11:41:34 +00001488#elif defined(PYOS_OS2)
1489#define INCL_DOS
1490#define INCL_DOSERRORS
1491#define INCL_NOPMAPI
1492#include <os2.h>
1493
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001494#elif defined(RISCOS)
1495#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001496#endif
1497
Guido van Rossum0980bd91998-02-13 17:18:36 +00001498static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001499case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001500{
Tim Peters50d8d372001-02-28 05:34:27 +00001501/* Pick a platform-specific implementation; the sequence of #if's here should
1502 * match the sequence just above.
1503 */
1504
Jason Tishler7961aa62005-05-20 00:56:54 +00001505/* MS_WINDOWS */
1506#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001507 WIN32_FIND_DATA data;
1508 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001509
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001510 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001511 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001512
Guido van Rossum0980bd91998-02-13 17:18:36 +00001513 h = FindFirstFile(buf, &data);
1514 if (h == INVALID_HANDLE_VALUE) {
1515 PyErr_Format(PyExc_NameError,
1516 "Can't find file for module %.100s\n(filename %.300s)",
1517 name, buf);
1518 return 0;
1519 }
1520 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001521 return strncmp(data.cFileName, name, namelen) == 0;
1522
1523/* DJGPP */
1524#elif defined(DJGPP)
1525 struct ffblk ffblk;
1526 int done;
1527
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001528 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001529 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001530
1531 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1532 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001533 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001534 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001535 name, buf);
1536 return 0;
1537 }
Tim Peters50d8d372001-02-28 05:34:27 +00001538 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001539
Jason Tishler7961aa62005-05-20 00:56:54 +00001540/* new-fangled macintosh (macosx) or Cygwin */
1541#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001542 DIR *dirp;
1543 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001544 char dirname[MAXPATHLEN + 1];
1545 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001546
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001547 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001548 return 1;
1549
Tim Petersd1e87a82001-03-01 18:12:00 +00001550 /* Copy the dir component into dirname; substitute "." if empty */
1551 if (dirlen <= 0) {
1552 dirname[0] = '.';
1553 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001554 }
1555 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001556 assert(dirlen <= MAXPATHLEN);
1557 memcpy(dirname, buf, dirlen);
1558 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001559 }
1560 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001561 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001562 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001563 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001564 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001565 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001566#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001567 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001568#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001569 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001570#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001571 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001572 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001573 (void)closedir(dirp);
1574 return 1; /* Found */
1575 }
1576 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001577 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001578 }
Tim Peters430f5d42001-03-01 01:30:56 +00001579 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001580
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001581/* RISC OS */
1582#elif defined(RISCOS)
1583 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1584 char buf2[MAXPATHLEN+2];
1585 char *nameWithExt = buf+len-namelen;
1586 int canonlen;
1587 os_error *e;
1588
1589 if (Py_GETENV("PYTHONCASEOK") != NULL)
1590 return 1;
1591
1592 /* workaround:
1593 append wildcard, otherwise case of filename wouldn't be touched */
1594 strcpy(buf2, buf);
1595 strcat(buf2, "*");
1596
1597 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1598 canonlen = MAXPATHLEN+1-canonlen;
1599 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1600 return 0;
1601 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1602 return 1; /* match */
1603
1604 return 0;
1605
Andrew MacIntyred9400542002-02-26 11:41:34 +00001606/* OS/2 */
1607#elif defined(PYOS_OS2)
1608 HDIR hdir = 1;
1609 ULONG srchcnt = 1;
1610 FILEFINDBUF3 ffbuf;
1611 APIRET rc;
1612
1613 if (getenv("PYTHONCASEOK") != NULL)
1614 return 1;
1615
1616 rc = DosFindFirst(buf,
1617 &hdir,
1618 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1619 &ffbuf, sizeof(ffbuf),
1620 &srchcnt,
1621 FIL_STANDARD);
1622 if (rc != NO_ERROR)
1623 return 0;
1624 return strncmp(ffbuf.achName, name, namelen) == 0;
1625
Tim Peters50d8d372001-02-28 05:34:27 +00001626/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1627#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001628 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001629
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001630#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001631}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001632
Guido van Rossum0980bd91998-02-13 17:18:36 +00001633
Guido van Rossum197346f1997-10-31 18:38:52 +00001634#ifdef HAVE_STAT
1635/* Helper to look for __init__.py or __init__.py[co] in potential package */
1636static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001637find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001638{
Tim Peters0f9431f2001-07-05 03:47:53 +00001639 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001640 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001641 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001642 struct stat statbuf;
1643
Tim Peters0f9431f2001-07-05 03:47:53 +00001644/* For calling case_ok(buf, len, namelen, name):
1645 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1646 * ^ ^ ^ ^
1647 * |--------------------- buf ---------------------|
1648 * |------------------- len ------------------|
1649 * |------ name -------|
1650 * |----- namelen -----|
1651 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001652 if (save_len + 13 >= MAXPATHLEN)
1653 return 0;
1654 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001655 pname = buf + i;
1656 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001657 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001658 if (case_ok(buf,
1659 save_len + 9, /* len("/__init__") */
1660 8, /* len("__init__") */
1661 pname)) {
1662 buf[save_len] = '\0';
1663 return 1;
1664 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001665 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001666 i += strlen(pname);
1667 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001668 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001669 if (case_ok(buf,
1670 save_len + 9, /* len("/__init__") */
1671 8, /* len("__init__") */
1672 pname)) {
1673 buf[save_len] = '\0';
1674 return 1;
1675 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001676 }
1677 buf[save_len] = '\0';
1678 return 0;
1679}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001680
1681#else
1682
1683#ifdef RISCOS
1684static int
1685find_init_module(buf)
1686 char *buf;
1687{
1688 int save_len = strlen(buf);
1689 int i = save_len;
1690
1691 if (save_len + 13 >= MAXPATHLEN)
1692 return 0;
1693 buf[i++] = SEP;
1694 strcpy(buf+i, "__init__/py");
1695 if (isfile(buf)) {
1696 buf[save_len] = '\0';
1697 return 1;
1698 }
1699
1700 if (Py_OptimizeFlag)
1701 strcpy(buf+i, "o");
1702 else
1703 strcpy(buf+i, "c");
1704 if (isfile(buf)) {
1705 buf[save_len] = '\0';
1706 return 1;
1707 }
1708 buf[save_len] = '\0';
1709 return 0;
1710}
1711#endif /*RISCOS*/
1712
Guido van Rossum197346f1997-10-31 18:38:52 +00001713#endif /* HAVE_STAT */
1714
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715
Tim Petersdbd9ba62000-07-09 03:09:57 +00001716static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001717
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001719 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001722load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001723{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001724 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001725 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001726 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001727
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001728 /* First check that there's an open file (if we need one) */
1729 switch (type) {
1730 case PY_SOURCE:
1731 case PY_COMPILED:
1732 if (fp == NULL) {
1733 PyErr_Format(PyExc_ValueError,
1734 "file object required for import (type code %d)",
1735 type);
1736 return NULL;
1737 }
1738 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001739
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001740 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001741
1742 case PY_SOURCE:
1743 m = load_source_module(name, buf, fp);
1744 break;
1745
1746 case PY_COMPILED:
1747 m = load_compiled_module(name, buf, fp);
1748 break;
1749
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001750#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001751 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001753 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001754#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001755
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001756 case PKG_DIRECTORY:
1757 m = load_package(name, buf);
1758 break;
1759
1760 case C_BUILTIN:
1761 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001762 if (buf != NULL && buf[0] != '\0')
1763 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001764 if (type == C_BUILTIN)
1765 err = init_builtin(name);
1766 else
1767 err = PyImport_ImportFrozenModule(name);
1768 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001769 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001770 if (err == 0) {
1771 PyErr_Format(PyExc_ImportError,
1772 "Purported %s module %.200s not found",
1773 type == C_BUILTIN ?
1774 "builtin" : "frozen",
1775 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001776 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001777 }
1778 modules = PyImport_GetModuleDict();
1779 m = PyDict_GetItemString(modules, name);
1780 if (m == NULL) {
1781 PyErr_Format(
1782 PyExc_ImportError,
1783 "%s module %.200s not properly initialized",
1784 type == C_BUILTIN ?
1785 "builtin" : "frozen",
1786 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001787 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001788 }
1789 Py_INCREF(m);
1790 break;
1791
Just van Rossum52e14d62002-12-30 22:08:05 +00001792 case IMP_HOOK: {
1793 if (loader == NULL) {
1794 PyErr_SetString(PyExc_ImportError,
1795 "import hook without loader");
1796 return NULL;
1797 }
1798 m = PyObject_CallMethod(loader, "load_module", "s", name);
1799 break;
1800 }
1801
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001803 PyErr_Format(PyExc_ImportError,
1804 "Don't know how to import %.200s (type code %d)",
1805 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001806 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807
1808 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809
1810 return m;
1811}
1812
1813
1814/* Initialize a built-in module.
1815 Return 1 for succes, 0 if the module is not found, and -1 with
1816 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001817
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001818static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001819init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001820{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001821 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001822
Greg Ward201baee2001-10-04 14:52:06 +00001823 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001824 return 1;
1825
Guido van Rossum771c6c81997-10-31 18:37:24 +00001826 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001827 if (strcmp(name, p->name) == 0) {
1828 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001829 PyErr_Format(PyExc_ImportError,
1830 "Cannot re-init internal module %.200s",
1831 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001832 return -1;
1833 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001834 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001835 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001836 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001837 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001838 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001839 if (_PyImport_FixupExtension(name, name) == NULL)
1840 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001841 return 1;
1842 }
1843 }
1844 return 0;
1845}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001846
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001847
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001848/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001849
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001850static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001851find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001852{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001853 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001854
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001856 if (p->name == NULL)
1857 return NULL;
1858 if (strcmp(p->name, name) == 0)
1859 break;
1860 }
1861 return p;
1862}
1863
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001865get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001866{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001867 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001868 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001869
1870 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001871 PyErr_Format(PyExc_ImportError,
1872 "No such frozen object named %.200s",
1873 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001874 return NULL;
1875 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001876 if (p->code == NULL) {
1877 PyErr_Format(PyExc_ImportError,
1878 "Excluded frozen object named %.200s",
1879 name);
1880 return NULL;
1881 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001882 size = p->size;
1883 if (size < 0)
1884 size = -size;
1885 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001886}
1887
1888/* Initialize a frozen module.
1889 Return 1 for succes, 0 if the module is not found, and -1 with
1890 an exception set if the initialization failed.
1891 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001892
1893int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001894PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001895{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001896 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897 PyObject *co;
1898 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001899 int ispackage;
1900 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001901
1902 if (p == NULL)
1903 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001904 if (p->code == NULL) {
1905 PyErr_Format(PyExc_ImportError,
1906 "Excluded frozen object named %.200s",
1907 name);
1908 return -1;
1909 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001910 size = p->size;
1911 ispackage = (size < 0);
1912 if (ispackage)
1913 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001914 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001915 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001916 name, ispackage ? " package" : "");
1917 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001918 if (co == NULL)
1919 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001921 PyErr_Format(PyExc_TypeError,
1922 "frozen object %.200s is not a code object",
1923 name);
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001924 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001925 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001926 if (ispackage) {
1927 /* Set __path__ to the package name */
1928 PyObject *d, *s;
1929 int err;
1930 m = PyImport_AddModule(name);
1931 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001932 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001933 d = PyModule_GetDict(m);
1934 s = PyString_InternFromString(name);
1935 if (s == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001936 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001937 err = PyDict_SetItemString(d, "__path__", s);
1938 Py_DECREF(s);
1939 if (err != 0)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001940 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001941 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001942 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001943 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001944 goto err_return;
1945 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001946 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001947 return 1;
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001948err_return:
1949 Py_DECREF(co);
1950 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001951}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001952
1953
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001954/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001955 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001956
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001958PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001959{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001960 PyObject *pname;
1961 PyObject *result;
1962
1963 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001964 if (pname == NULL)
1965 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001966 result = PyImport_Import(pname);
1967 Py_DECREF(pname);
1968 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001969}
1970
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001971/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001972static PyObject *get_parent(PyObject *globals, char *buf,
1973 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001974static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001975 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001976static int mark_miss(char *name);
1977static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001978 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001979static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001980
1981/* The Magnum Opus of dotted-name import :-) */
1982
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001983static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001984import_module_level(char *name, PyObject *globals, PyObject *locals,
1985 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001986{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001987 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00001988 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001989 PyObject *parent, *head, *next, *tail;
1990
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001991 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001992 if (parent == NULL)
1993 return NULL;
1994
1995 head = load_next(parent, Py_None, &name, buf, &buflen);
1996 if (head == NULL)
1997 return NULL;
1998
1999 tail = head;
2000 Py_INCREF(tail);
2001 while (name) {
2002 next = load_next(tail, tail, &name, buf, &buflen);
2003 Py_DECREF(tail);
2004 if (next == NULL) {
2005 Py_DECREF(head);
2006 return NULL;
2007 }
2008 tail = next;
2009 }
Thomas Wouters8ddab272006-04-04 16:17:02 +00002010 if (tail == Py_None) {
2011 /* If tail is Py_None, both get_parent and load_next found
2012 an empty module name: someone called __import__("") or
2013 doctored faulty bytecode */
Thomas Wouters4bdaa272006-04-05 13:39:37 +00002014 Py_DECREF(tail);
2015 Py_DECREF(head);
Thomas Wouters8ddab272006-04-04 16:17:02 +00002016 PyErr_SetString(PyExc_ValueError,
2017 "Empty module name");
2018 return NULL;
2019 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002020
2021 if (fromlist != NULL) {
2022 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2023 fromlist = NULL;
2024 }
2025
2026 if (fromlist == NULL) {
2027 Py_DECREF(tail);
2028 return head;
2029 }
2030
2031 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002032 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002033 Py_DECREF(tail);
2034 return NULL;
2035 }
2036
2037 return tail;
2038}
2039
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002040/* For DLL compatibility */
2041#undef PyImport_ImportModuleEx
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002042PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002043PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
2044 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002045{
2046 PyObject *result;
2047 lock_import();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002048 result = import_module_level(name, globals, locals, fromlist, -1);
2049 if (unlock_import() < 0) {
2050 Py_XDECREF(result);
2051 PyErr_SetString(PyExc_RuntimeError,
2052 "not holding the import lock");
2053 return NULL;
2054 }
2055 return result;
2056}
2057#define PyImport_ImportModuleEx(n, g, l, f) \
2058 PyImport_ImportModuleLevel(n, g, l, f, -1);
2059
2060PyObject *
2061PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2062 PyObject *fromlist, int level)
2063{
2064 PyObject *result;
2065 lock_import();
2066 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002067 if (unlock_import() < 0) {
2068 Py_XDECREF(result);
2069 PyErr_SetString(PyExc_RuntimeError,
2070 "not holding the import lock");
2071 return NULL;
2072 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002073 return result;
2074}
2075
Fred Drake87590902004-05-28 20:21:36 +00002076/* Return the package that an import is being performed in. If globals comes
2077 from the module foo.bar.bat (not itself a package), this returns the
2078 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Georg Brandl5f6861d2006-05-28 21:57:35 +00002079 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002080
2081 The *name* of the returned package is returned in buf, with the length of
2082 the name in *p_buflen.
2083
2084 If globals doesn't come from a package or a module in a package, or a
2085 corresponding entry is not found in sys.modules, Py_None is returned.
2086*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002087static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002088get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002089{
2090 static PyObject *namestr = NULL;
2091 static PyObject *pathstr = NULL;
2092 PyObject *modname, *modpath, *modules, *parent;
2093
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002094 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002095 return Py_None;
2096
2097 if (namestr == NULL) {
2098 namestr = PyString_InternFromString("__name__");
2099 if (namestr == NULL)
2100 return NULL;
2101 }
2102 if (pathstr == NULL) {
2103 pathstr = PyString_InternFromString("__path__");
2104 if (pathstr == NULL)
2105 return NULL;
2106 }
2107
2108 *buf = '\0';
2109 *p_buflen = 0;
2110 modname = PyDict_GetItem(globals, namestr);
2111 if (modname == NULL || !PyString_Check(modname))
2112 return Py_None;
2113
2114 modpath = PyDict_GetItem(globals, pathstr);
2115 if (modpath != NULL) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00002116 Py_ssize_t len = PyString_GET_SIZE(modname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002117 if (len > MAXPATHLEN) {
2118 PyErr_SetString(PyExc_ValueError,
2119 "Module name too long");
2120 return NULL;
2121 }
2122 strcpy(buf, PyString_AS_STRING(modname));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002123 }
2124 else {
2125 char *start = PyString_AS_STRING(modname);
2126 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002127 size_t len;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002128 if (lastdot == NULL && level > 0) {
2129 PyErr_SetString(PyExc_ValueError,
Georg Brandl37a9e572006-09-06 06:09:34 +00002130 "Attempted relative import in non-package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002131 return NULL;
2132 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002133 if (lastdot == NULL)
2134 return Py_None;
2135 len = lastdot - start;
2136 if (len >= MAXPATHLEN) {
2137 PyErr_SetString(PyExc_ValueError,
2138 "Module name too long");
2139 return NULL;
2140 }
2141 strncpy(buf, start, len);
2142 buf[len] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002143 }
2144
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002145 while (--level > 0) {
2146 char *dot = strrchr(buf, '.');
2147 if (dot == NULL) {
2148 PyErr_SetString(PyExc_ValueError,
Georg Brandl37a9e572006-09-06 06:09:34 +00002149 "Attempted relative import beyond "
2150 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002151 return NULL;
2152 }
2153 *dot = '\0';
2154 }
2155 *p_buflen = strlen(buf);
2156
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002157 modules = PyImport_GetModuleDict();
2158 parent = PyDict_GetItemString(modules, buf);
2159 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002160 PyErr_Format(PyExc_SystemError,
2161 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002162 return parent;
2163 /* We expect, but can't guarantee, if parent != None, that:
2164 - parent.__name__ == buf
2165 - parent.__dict__ is globals
2166 If this is violated... Who cares? */
2167}
2168
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002169/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002170static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002171load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002172 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002173{
2174 char *name = *p_name;
2175 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002176 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002177 char *p;
2178 PyObject *result;
2179
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002180 if (strlen(name) == 0) {
Thomas Wouters8ddab272006-04-04 16:17:02 +00002181 /* completely empty module name should only happen in
2182 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002183 Py_INCREF(mod);
2184 *p_name = NULL;
2185 return mod;
2186 }
2187
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002188 if (dot == NULL) {
2189 *p_name = NULL;
2190 len = strlen(name);
2191 }
2192 else {
2193 *p_name = dot+1;
2194 len = dot-name;
2195 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002196 if (len == 0) {
2197 PyErr_SetString(PyExc_ValueError,
2198 "Empty module name");
2199 return NULL;
2200 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002201
2202 p = buf + *p_buflen;
2203 if (p != buf)
2204 *p++ = '.';
2205 if (p+len-buf >= MAXPATHLEN) {
2206 PyErr_SetString(PyExc_ValueError,
2207 "Module name too long");
2208 return NULL;
2209 }
2210 strncpy(p, name, len);
2211 p[len] = '\0';
2212 *p_buflen = p+len-buf;
2213
2214 result = import_submodule(mod, p, buf);
2215 if (result == Py_None && altmod != mod) {
2216 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002217 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002218 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002219 if (result != NULL && result != Py_None) {
2220 if (mark_miss(buf) != 0) {
2221 Py_DECREF(result);
2222 return NULL;
2223 }
2224 strncpy(buf, name, len);
2225 buf[len] = '\0';
2226 *p_buflen = len;
2227 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002228 }
2229 if (result == NULL)
2230 return NULL;
2231
2232 if (result == Py_None) {
2233 Py_DECREF(result);
2234 PyErr_Format(PyExc_ImportError,
2235 "No module named %.200s", name);
2236 return NULL;
2237 }
2238
2239 return result;
2240}
2241
2242static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002243mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002244{
2245 PyObject *modules = PyImport_GetModuleDict();
2246 return PyDict_SetItemString(modules, name, Py_None);
2247}
2248
2249static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002250ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002251 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002252{
2253 int i;
2254
2255 if (!PyObject_HasAttrString(mod, "__path__"))
2256 return 1;
2257
2258 for (i = 0; ; i++) {
2259 PyObject *item = PySequence_GetItem(fromlist, i);
2260 int hasit;
2261 if (item == NULL) {
2262 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2263 PyErr_Clear();
2264 return 1;
2265 }
2266 return 0;
2267 }
2268 if (!PyString_Check(item)) {
2269 PyErr_SetString(PyExc_TypeError,
2270 "Item in ``from list'' not a string");
2271 Py_DECREF(item);
2272 return 0;
2273 }
2274 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002275 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002276 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002277 /* See if the package defines __all__ */
2278 if (recursive)
2279 continue; /* Avoid endless recursion */
2280 all = PyObject_GetAttrString(mod, "__all__");
2281 if (all == NULL)
2282 PyErr_Clear();
2283 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002284 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002285 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002286 if (!ret)
2287 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002288 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002289 continue;
2290 }
2291 hasit = PyObject_HasAttr(mod, item);
2292 if (!hasit) {
2293 char *subname = PyString_AS_STRING(item);
2294 PyObject *submod;
2295 char *p;
2296 if (buflen + strlen(subname) >= MAXPATHLEN) {
2297 PyErr_SetString(PyExc_ValueError,
2298 "Module name too long");
2299 Py_DECREF(item);
2300 return 0;
2301 }
2302 p = buf + buflen;
2303 *p++ = '.';
2304 strcpy(p, subname);
2305 submod = import_submodule(mod, subname, buf);
2306 Py_XDECREF(submod);
2307 if (submod == NULL) {
2308 Py_DECREF(item);
2309 return 0;
2310 }
2311 }
2312 Py_DECREF(item);
2313 }
2314
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002315 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002316}
2317
Neil Schemenauer00b09662003-06-16 21:03:07 +00002318static int
2319add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2320 PyObject *modules)
2321{
2322 if (mod == Py_None)
2323 return 1;
2324 /* Irrespective of the success of this load, make a
2325 reference to it in the parent package module. A copy gets
2326 saved in the modules dictionary under the full name, so get a
2327 reference from there, if need be. (The exception is when the
2328 load failed with a SyntaxError -- then there's no trace in
2329 sys.modules. In that case, of course, do nothing extra.) */
2330 if (submod == NULL) {
2331 submod = PyDict_GetItemString(modules, fullname);
2332 if (submod == NULL)
2333 return 1;
2334 }
2335 if (PyModule_Check(mod)) {
2336 /* We can't use setattr here since it can give a
2337 * spurious warning if the submodule name shadows a
2338 * builtin name */
2339 PyObject *dict = PyModule_GetDict(mod);
2340 if (!dict)
2341 return 0;
2342 if (PyDict_SetItemString(dict, subname, submod) < 0)
2343 return 0;
2344 }
2345 else {
2346 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2347 return 0;
2348 }
2349 return 1;
2350}
2351
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002352static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002353import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002354{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002355 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002356 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002357
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002358 /* Require:
2359 if mod == None: subname == fullname
2360 else: mod.__name__ + "." + subname == fullname
2361 */
2362
Tim Peters50d8d372001-02-28 05:34:27 +00002363 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002364 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002365 }
2366 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002367 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002368 char buf[MAXPATHLEN+1];
2369 struct filedescr *fdp;
2370 FILE *fp = NULL;
2371
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002372 if (mod == Py_None)
2373 path = NULL;
2374 else {
2375 path = PyObject_GetAttrString(mod, "__path__");
2376 if (path == NULL) {
2377 PyErr_Clear();
2378 Py_INCREF(Py_None);
2379 return Py_None;
2380 }
2381 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002382
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002383 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002384 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2385 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002386 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002387 if (fdp == NULL) {
2388 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2389 return NULL;
2390 PyErr_Clear();
2391 Py_INCREF(Py_None);
2392 return Py_None;
2393 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002394 m = load_module(fullname, fp, buf, fdp->type, loader);
2395 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002396 if (fp)
2397 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002398 if (!add_submodule(mod, m, fullname, subname, modules)) {
2399 Py_XDECREF(m);
2400 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002401 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002402 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002403
2404 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002405}
2406
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407
2408/* Re-import a module of any kind and return its module object, WITH
2409 INCREMENTED REFERENCE COUNT */
2410
Guido van Rossum79f25d91997-04-29 20:08:16 +00002411PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002412PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002413{
Collin Winter2a98ff82007-03-13 23:04:29 +00002414 PyInterpreterState *interp = PyThreadState_Get()->interp;
2415 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002416 PyObject *modules = PyImport_GetModuleDict();
Collin Wintere19d7a32007-03-12 16:49:23 +00002417 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002418 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002419 char buf[MAXPATHLEN+1];
2420 struct filedescr *fdp;
2421 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002422 PyObject *newm;
Collin Winter2a98ff82007-03-13 23:04:29 +00002423
2424 if (modules_reloading == NULL) {
2425 Py_FatalError("PyImport_ReloadModule: "
2426 "no modules_reloading dictionary!");
2427 return NULL;
2428 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002429
Guido van Rossum79f25d91997-04-29 20:08:16 +00002430 if (m == NULL || !PyModule_Check(m)) {
2431 PyErr_SetString(PyExc_TypeError,
2432 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433 return NULL;
2434 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002435 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002436 if (name == NULL)
2437 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002438 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002439 PyErr_Format(PyExc_ImportError,
2440 "reload(): module %.200s not in sys.modules",
2441 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002442 return NULL;
2443 }
Collin Wintere19d7a32007-03-12 16:49:23 +00002444 if ((existing_m = PyDict_GetItemString(modules_reloading, name)) != NULL) {
2445 /* Due to a recursive reload, this module is already being reloaded. */
2446 Py_INCREF(existing_m);
2447 return existing_m;
2448 }
2449 PyDict_SetItemString(modules_reloading, name, m);
2450
Guido van Rossum222ef561997-09-06 19:41:09 +00002451 subname = strrchr(name, '.');
2452 if (subname == NULL)
2453 subname = name;
2454 else {
2455 PyObject *parentname, *parent;
2456 parentname = PyString_FromStringAndSize(name, (subname-name));
Collin Wintere19d7a32007-03-12 16:49:23 +00002457 if (parentname == NULL) {
2458 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002459 return NULL;
Collin Wintere19d7a32007-03-12 16:49:23 +00002460 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002461 parent = PyDict_GetItem(modules, parentname);
2462 if (parent == NULL) {
2463 PyErr_Format(PyExc_ImportError,
2464 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002465 PyString_AS_STRING(parentname));
2466 Py_DECREF(parentname);
Collin Wintere19d7a32007-03-12 16:49:23 +00002467 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002468 return NULL;
2469 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002470 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002471 subname++;
2472 path = PyObject_GetAttrString(parent, "__path__");
2473 if (path == NULL)
2474 PyErr_Clear();
2475 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002476 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002477 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002478 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002479
2480 if (fdp == NULL) {
2481 Py_XDECREF(loader);
Collin Wintere19d7a32007-03-12 16:49:23 +00002482 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002483 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002484 }
2485
2486 newm = load_module(name, fp, buf, fdp->type, loader);
2487 Py_XDECREF(loader);
2488
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002489 if (fp)
2490 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002491 if (newm == NULL) {
2492 /* load_module probably removed name from modules because of
2493 * the error. Put back the original module object. We're
2494 * going to return NULL in this case regardless of whether
2495 * replacing name succeeds, so the return value is ignored.
2496 */
2497 PyDict_SetItemString(modules, name, m);
2498 }
Collin Wintere19d7a32007-03-12 16:49:23 +00002499 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002500 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002501}
2502
2503
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002504/* Higher-level import emulator which emulates the "import" statement
2505 more accurately -- it invokes the __import__() function from the
2506 builtins of the current globals. This means that the import is
2507 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002508 environment, e.g. by "rexec".
2509 A dummy list ["__doc__"] is passed as the 4th argument so that
2510 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2511 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002512
2513PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002514PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002515{
2516 static PyObject *silly_list = NULL;
2517 static PyObject *builtins_str = NULL;
2518 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002519 PyObject *globals = NULL;
2520 PyObject *import = NULL;
2521 PyObject *builtins = NULL;
2522 PyObject *r = NULL;
2523
2524 /* Initialize constant string objects */
2525 if (silly_list == NULL) {
2526 import_str = PyString_InternFromString("__import__");
2527 if (import_str == NULL)
2528 return NULL;
2529 builtins_str = PyString_InternFromString("__builtins__");
2530 if (builtins_str == NULL)
2531 return NULL;
2532 silly_list = Py_BuildValue("[s]", "__doc__");
2533 if (silly_list == NULL)
2534 return NULL;
2535 }
2536
2537 /* Get the builtins from current globals */
2538 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002539 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002540 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002541 builtins = PyObject_GetItem(globals, builtins_str);
2542 if (builtins == NULL)
2543 goto err;
2544 }
2545 else {
2546 /* No globals -- use standard builtins, and fake globals */
2547 PyErr_Clear();
2548
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002549 builtins = PyImport_ImportModuleLevel("__builtin__",
2550 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002551 if (builtins == NULL)
2552 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002553 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2554 if (globals == NULL)
2555 goto err;
2556 }
2557
2558 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002559 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002560 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002561 if (import == NULL)
2562 PyErr_SetObject(PyExc_KeyError, import_str);
2563 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002564 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002565 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002566 if (import == NULL)
2567 goto err;
2568
2569 /* Call the _import__ function with the proper argument list */
Georg Brandl684fd0c2006-05-25 19:15:31 +00002570 r = PyObject_CallFunctionObjArgs(import, module_name, globals,
2571 globals, silly_list, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002572
2573 err:
2574 Py_XDECREF(globals);
2575 Py_XDECREF(builtins);
2576 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002577
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002578 return r;
2579}
2580
2581
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002582/* Module 'imp' provides Python access to the primitives used for
2583 importing modules.
2584*/
2585
Guido van Rossum79f25d91997-04-29 20:08:16 +00002586static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002587imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002588{
2589 char buf[4];
2590
Guido van Rossum96774c12000-05-01 20:19:08 +00002591 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2592 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2593 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2594 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595
Guido van Rossum79f25d91997-04-29 20:08:16 +00002596 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002597}
2598
Guido van Rossum79f25d91997-04-29 20:08:16 +00002599static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002600imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002601{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002602 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002603 struct filedescr *fdp;
2604
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002606 if (list == NULL)
2607 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2609 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002610 fdp->suffix, fdp->mode, fdp->type);
2611 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002612 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002613 return NULL;
2614 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002615 if (PyList_Append(list, item) < 0) {
2616 Py_DECREF(list);
2617 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002618 return NULL;
2619 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002620 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002621 }
2622 return list;
2623}
2624
Guido van Rossum79f25d91997-04-29 20:08:16 +00002625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002626call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002627{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002628 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002629 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002630 struct filedescr *fdp;
2631 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002632 FILE *fp = NULL;
2633
2634 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002635 if (path == Py_None)
2636 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002637 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002638 if (fdp == NULL)
2639 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002640 if (fp != NULL) {
2641 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2642 if (fob == NULL) {
2643 fclose(fp);
2644 return NULL;
2645 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002646 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002647 else {
2648 fob = Py_None;
2649 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002650 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002651 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002652 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002654 return ret;
2655}
2656
Guido van Rossum79f25d91997-04-29 20:08:16 +00002657static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002658imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002659{
2660 char *name;
2661 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002662 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002663 return NULL;
2664 return call_find_module(name, path);
2665}
2666
2667static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002668imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002669{
2670 char *name;
2671 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002672 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002673 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002674 return NULL;
2675 ret = init_builtin(name);
2676 if (ret < 0)
2677 return NULL;
2678 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 Py_INCREF(Py_None);
2680 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002681 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002682 m = PyImport_AddModule(name);
2683 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002684 return m;
2685}
2686
Guido van Rossum79f25d91997-04-29 20:08:16 +00002687static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002688imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002689{
2690 char *name;
2691 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002692 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002693 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002694 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002695 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002696 if (ret < 0)
2697 return NULL;
2698 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002699 Py_INCREF(Py_None);
2700 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002701 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002702 m = PyImport_AddModule(name);
2703 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002704 return m;
2705}
2706
Guido van Rossum79f25d91997-04-29 20:08:16 +00002707static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002708imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002709{
2710 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002711
Guido van Rossum43713e52000-02-29 13:59:29 +00002712 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002713 return NULL;
2714 return get_frozen_object(name);
2715}
2716
Guido van Rossum79f25d91997-04-29 20:08:16 +00002717static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002718imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002719{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002720 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002721 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002722 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002723 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002724}
2725
Guido van Rossum79f25d91997-04-29 20:08:16 +00002726static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002727imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002728{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002729 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002730 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002731 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002732 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002733 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002734 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002735}
2736
2737static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002738get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002739{
2740 FILE *fp;
2741 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002742 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002743 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002744 fp = fopen(pathname, mode);
2745 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002746 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002747 }
2748 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002749 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002750 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002751 PyErr_SetString(PyExc_ValueError,
2752 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002753 }
2754 return fp;
2755}
2756
Guido van Rossum79f25d91997-04-29 20:08:16 +00002757static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002758imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002759{
2760 char *name;
2761 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002762 PyObject *fob = NULL;
2763 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002764 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002765 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002766 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002767 return NULL;
2768 fp = get_file(pathname, fob, "rb");
2769 if (fp == NULL)
2770 return NULL;
2771 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002772 if (fob == NULL)
2773 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002774 return m;
2775}
2776
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002777#ifdef HAVE_DYNAMIC_LOADING
2778
Guido van Rossum79f25d91997-04-29 20:08:16 +00002779static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002780imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002781{
2782 char *name;
2783 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002784 PyObject *fob = NULL;
2785 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002786 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002787 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002788 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002789 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002790 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002791 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002792 if (fp == NULL)
2793 return NULL;
2794 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002795 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002796 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002797}
2798
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002799#endif /* HAVE_DYNAMIC_LOADING */
2800
Guido van Rossum79f25d91997-04-29 20:08:16 +00002801static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002802imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002803{
2804 char *name;
2805 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002806 PyObject *fob = NULL;
2807 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002808 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002809 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002810 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002811 return NULL;
2812 fp = get_file(pathname, fob, "r");
2813 if (fp == NULL)
2814 return NULL;
2815 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002816 if (fob == NULL)
2817 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002818 return m;
2819}
2820
Guido van Rossum79f25d91997-04-29 20:08:16 +00002821static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002822imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002823{
2824 char *name;
2825 PyObject *fob;
2826 char *pathname;
2827 char *suffix; /* Unused */
2828 char *mode;
2829 int type;
2830 FILE *fp;
2831
Guido van Rossum43713e52000-02-29 13:59:29 +00002832 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002833 &name, &fob, &pathname,
2834 &suffix, &mode, &type))
2835 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002836 if (*mode) {
2837 /* Mode must start with 'r' or 'U' and must not contain '+'.
2838 Implicit in this test is the assumption that the mode
2839 may contain other modifiers like 'b' or 't'. */
2840
2841 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002842 PyErr_Format(PyExc_ValueError,
2843 "invalid file open mode %.200s", mode);
2844 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002845 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002846 }
2847 if (fob == Py_None)
2848 fp = NULL;
2849 else {
2850 if (!PyFile_Check(fob)) {
2851 PyErr_SetString(PyExc_ValueError,
2852 "load_module arg#2 should be a file or None");
2853 return NULL;
2854 }
2855 fp = get_file(pathname, fob, mode);
2856 if (fp == NULL)
2857 return NULL;
2858 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002859 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002860}
2861
2862static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002863imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002864{
2865 char *name;
2866 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002867 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002868 return NULL;
2869 return load_package(name, pathname);
2870}
2871
2872static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002873imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002874{
2875 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002876 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002877 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002878 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002879}
2880
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002881/* Doc strings */
2882
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002883PyDoc_STRVAR(doc_imp,
2884"This module provides the components needed to build your own\n\
2885__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002887PyDoc_STRVAR(doc_find_module,
2888"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002889Search for a module. If path is omitted or None, search for a\n\
2890built-in, frozen or special module and continue search in sys.path.\n\
2891The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002892package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002894PyDoc_STRVAR(doc_load_module,
2895"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002896Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002897The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002899PyDoc_STRVAR(doc_get_magic,
2900"get_magic() -> string\n\
2901Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002902
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002903PyDoc_STRVAR(doc_get_suffixes,
2904"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002905Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002906that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002908PyDoc_STRVAR(doc_new_module,
2909"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002910Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002914"lock_held() -> boolean\n\
2915Return True if the import lock is currently held, else False.\n\
2916On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002917
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002918PyDoc_STRVAR(doc_acquire_lock,
2919"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002920Acquires the interpreter's import lock for the current thread.\n\
2921This lock should be used by import hooks to ensure thread-safety\n\
2922when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002923On platforms without threads, this function does nothing.");
2924
2925PyDoc_STRVAR(doc_release_lock,
2926"release_lock() -> None\n\
2927Release the interpreter's import lock.\n\
2928On platforms without threads, this function does nothing.");
2929
Guido van Rossum79f25d91997-04-29 20:08:16 +00002930static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002931 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2932 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2933 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2934 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2935 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2936 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2937 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2938 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002939 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002940 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2941 {"init_builtin", imp_init_builtin, METH_VARARGS},
2942 {"init_frozen", imp_init_frozen, METH_VARARGS},
2943 {"is_builtin", imp_is_builtin, METH_VARARGS},
2944 {"is_frozen", imp_is_frozen, METH_VARARGS},
2945 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002946#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002947 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002948#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002949 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002950 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002951 {NULL, NULL} /* sentinel */
2952};
2953
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002954static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002955setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002956{
2957 PyObject *v;
2958 int err;
2959
2960 v = PyInt_FromLong((long)value);
2961 err = PyDict_SetItemString(d, name, v);
2962 Py_XDECREF(v);
2963 return err;
2964}
2965
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00002966typedef struct {
2967 PyObject_HEAD
2968} NullImporter;
2969
2970static int
2971NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
2972{
2973 char *path;
2974
2975 if (!_PyArg_NoKeywords("NullImporter()", kwds))
2976 return -1;
2977
2978 if (!PyArg_ParseTuple(args, "s:NullImporter",
2979 &path))
2980 return -1;
2981
2982 if (strlen(path) == 0) {
2983 PyErr_SetString(PyExc_ImportError, "empty pathname");
2984 return -1;
2985 } else {
2986#ifndef RISCOS
2987 struct stat statbuf;
2988 int rv;
2989
2990 rv = stat(path, &statbuf);
2991 if (rv == 0) {
2992 /* it exists */
2993 if (S_ISDIR(statbuf.st_mode)) {
2994 /* it's a directory */
2995 PyErr_SetString(PyExc_ImportError,
2996 "existing directory");
2997 return -1;
2998 }
2999 }
3000#else
3001 if (object_exists(path)) {
3002 /* it exists */
3003 if (isdir(path)) {
3004 /* it's a directory */
3005 PyErr_SetString(PyExc_ImportError,
3006 "existing directory");
3007 return -1;
3008 }
3009 }
3010#endif
3011 }
3012 return 0;
3013}
3014
3015static PyObject *
3016NullImporter_find_module(NullImporter *self, PyObject *args)
3017{
3018 Py_RETURN_NONE;
3019}
3020
3021static PyMethodDef NullImporter_methods[] = {
3022 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3023 "Always return None"
3024 },
3025 {NULL} /* Sentinel */
3026};
3027
3028
3029static PyTypeObject NullImporterType = {
3030 PyObject_HEAD_INIT(NULL)
3031 0, /*ob_size*/
3032 "imp.NullImporter", /*tp_name*/
3033 sizeof(NullImporter), /*tp_basicsize*/
3034 0, /*tp_itemsize*/
3035 0, /*tp_dealloc*/
3036 0, /*tp_print*/
3037 0, /*tp_getattr*/
3038 0, /*tp_setattr*/
3039 0, /*tp_compare*/
3040 0, /*tp_repr*/
3041 0, /*tp_as_number*/
3042 0, /*tp_as_sequence*/
3043 0, /*tp_as_mapping*/
3044 0, /*tp_hash */
3045 0, /*tp_call*/
3046 0, /*tp_str*/
3047 0, /*tp_getattro*/
3048 0, /*tp_setattro*/
3049 0, /*tp_as_buffer*/
3050 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3051 "Null importer object", /* tp_doc */
3052 0, /* tp_traverse */
3053 0, /* tp_clear */
3054 0, /* tp_richcompare */
3055 0, /* tp_weaklistoffset */
3056 0, /* tp_iter */
3057 0, /* tp_iternext */
3058 NullImporter_methods, /* tp_methods */
3059 0, /* tp_members */
3060 0, /* tp_getset */
3061 0, /* tp_base */
3062 0, /* tp_dict */
3063 0, /* tp_descr_get */
3064 0, /* tp_descr_set */
3065 0, /* tp_dictoffset */
3066 (initproc)NullImporter_init, /* tp_init */
3067 0, /* tp_alloc */
3068 PyType_GenericNew /* tp_new */
3069};
3070
3071
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003072PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003073initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003074{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003075 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003077 if (PyType_Ready(&NullImporterType) < 0)
3078 goto failure;
3079
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003080 m = Py_InitModule4("imp", imp_methods, doc_imp,
3081 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003082 if (m == NULL)
3083 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003084 d = PyModule_GetDict(m);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00003085 if (d == NULL)
3086 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003087
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003088 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3089 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3090 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3091 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3092 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3093 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3094 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3095 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003096 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003097 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003098
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003099 Py_INCREF(&NullImporterType);
3100 PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003101 failure:
3102 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003103}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003104
3105
Guido van Rossumb18618d2000-05-03 23:44:39 +00003106/* API for embedding applications that want to add their own entries
3107 to the table of built-in modules. This should normally be called
3108 *before* Py_Initialize(). When the table resize fails, -1 is
3109 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003110
3111 After a similar function by Just van Rossum. */
3112
3113int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003114PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003115{
3116 static struct _inittab *our_copy = NULL;
3117 struct _inittab *p;
3118 int i, n;
3119
3120 /* Count the number of entries in both tables */
3121 for (n = 0; newtab[n].name != NULL; n++)
3122 ;
3123 if (n == 0)
3124 return 0; /* Nothing to do */
3125 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3126 ;
3127
3128 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003129 p = our_copy;
3130 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003131 if (p == NULL)
3132 return -1;
3133
3134 /* Copy the tables into the new memory */
3135 if (our_copy != PyImport_Inittab)
3136 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3137 PyImport_Inittab = our_copy = p;
3138 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3139
3140 return 0;
3141}
3142
3143/* Shorthand to add a single entry given a name and a function */
3144
3145int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003146PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003147{
3148 struct _inittab newtab[2];
3149
3150 memset(newtab, '\0', sizeof newtab);
3151
3152 newtab[0].name = name;
3153 newtab[0].initfunc = initfunc;
3154
3155 return PyImport_ExtendInittab(newtab);
3156}
Anthony Baxterac6bd462006-04-13 02:06:09 +00003157
3158#ifdef __cplusplus
3159}
3160#endif