blob: 9151762081f40e6a9a60759ed0dfc90e1514ba98 [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 Norwitzdac090d2006-09-05 03:53:08 +000067 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Neal Norwitzcbeb6872006-10-14 21:33:38 +000068 Python 2.6a0: 62141 (peephole optimizations)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000069.
Tim Peters36515e22001-11-18 04:06:29 +000070*/
Neal Norwitzcbeb6872006-10-14 21:33:38 +000071#define MAGIC (62141 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000072
Guido van Rossum96774c12000-05-01 20:19:08 +000073/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000074 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000075 compiler works which are enabled by command line switches. */
76static long pyc_magic = MAGIC;
77
Guido van Rossum25ce5661997-08-02 03:10:38 +000078/* See _PyImport_FixupExtension() below */
79static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000080
Guido van Rossum771c6c81997-10-31 18:37:24 +000081/* This table is defined in config.c: */
82extern struct _inittab _PyImport_Inittab[];
83
84struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000085
Guido van Rossumed1170e1999-12-20 21:23:41 +000086/* these tables define the module suffixes that Python recognizes */
87struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000088
89#ifdef RISCOS
90static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000091 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000092 {"/pyc", "rb", PY_COMPILED},
93 {0, 0}
94};
95#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000096static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000097 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000098#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +000099 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000100#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000101 {".pyc", "rb", PY_COMPILED},
102 {0, 0}
103};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000104#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000105
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000106static PyTypeObject NullImporterType; /* Forward reference */
107
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000108/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
110void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000113 const struct filedescr *scan;
114 struct filedescr *filetab;
115 int countD = 0;
116 int countS = 0;
117
118 /* prepare _PyImport_Filetab: copy entries from
119 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
120 */
121 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
122 ++countD;
123 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
124 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000125 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Neal Norwitze1fdb322006-07-21 05:32:28 +0000126 if (filetab == NULL)
Neal Norwitz33722ae2006-07-21 07:59:02 +0000127 Py_FatalError("Can't initialize import file table.");
Guido van Rossumed1170e1999-12-20 21:23:41 +0000128 memcpy(filetab, _PyImport_DynLoadFiletab,
129 countD * sizeof(struct filedescr));
130 memcpy(filetab + countD, _PyImport_StandardFiletab,
131 countS * sizeof(struct filedescr));
132 filetab[countD + countS].suffix = NULL;
133
134 _PyImport_Filetab = filetab;
135
Guido van Rossum0824f631997-03-11 18:37:35 +0000136 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000137 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
138 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000139#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000140 if (strcmp(filetab->suffix, ".pyc") == 0)
141 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000142#else
143 if (strcmp(filetab->suffix, "/pyc") == 0)
144 filetab->suffix = "/pyo";
145#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000146 }
147 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000148
149 if (Py_UnicodeFlag) {
150 /* Fix the pyc_magic so that byte compiled code created
151 using the all-Unicode method doesn't interfere with
152 code created in normal operation mode. */
153 pyc_magic = MAGIC + 1;
154 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155}
156
Guido van Rossum25ce5661997-08-02 03:10:38 +0000157void
Just van Rossum52e14d62002-12-30 22:08:05 +0000158_PyImportHooks_Init(void)
159{
160 PyObject *v, *path_hooks = NULL, *zimpimport;
161 int err = 0;
162
163 /* adding sys.path_hooks and sys.path_importer_cache, setting up
164 zipimport */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000165 if (PyType_Ready(&NullImporterType) < 0)
166 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000167
168 if (Py_VerboseFlag)
169 PySys_WriteStderr("# installing zipimport hook\n");
170
171 v = PyList_New(0);
172 if (v == NULL)
173 goto error;
174 err = PySys_SetObject("meta_path", v);
175 Py_DECREF(v);
176 if (err)
177 goto error;
178 v = PyDict_New();
179 if (v == NULL)
180 goto error;
181 err = PySys_SetObject("path_importer_cache", v);
182 Py_DECREF(v);
183 if (err)
184 goto error;
185 path_hooks = PyList_New(0);
186 if (path_hooks == NULL)
187 goto error;
188 err = PySys_SetObject("path_hooks", path_hooks);
189 if (err) {
190 error:
191 PyErr_Print();
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000192 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
193 "path_importer_cache, or NullImporter failed"
194 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000195 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000196
Just van Rossum52e14d62002-12-30 22:08:05 +0000197 zimpimport = PyImport_ImportModule("zipimport");
198 if (zimpimport == NULL) {
199 PyErr_Clear(); /* No zip import module -- okay */
200 if (Py_VerboseFlag)
201 PySys_WriteStderr("# can't import zipimport\n");
202 }
203 else {
204 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
205 "zipimporter");
206 Py_DECREF(zimpimport);
207 if (zipimporter == NULL) {
208 PyErr_Clear(); /* No zipimporter object -- okay */
209 if (Py_VerboseFlag)
210 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000211 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000212 }
213 else {
214 /* sys.path_hooks.append(zipimporter) */
215 err = PyList_Append(path_hooks, zipimporter);
216 Py_DECREF(zipimporter);
217 if (err)
218 goto error;
219 if (Py_VerboseFlag)
220 PySys_WriteStderr(
221 "# installed zipimport hook\n");
222 }
223 }
224 Py_DECREF(path_hooks);
225}
226
227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
230 Py_XDECREF(extensions);
231 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000232 PyMem_DEL(_PyImport_Filetab);
233 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234}
235
236
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000237/* Locking primitives to prevent parallel imports of the same module
238 in different threads to return with a partially loaded module.
239 These calls are serialized by the global interpreter lock. */
240
241#ifdef WITH_THREAD
242
Guido van Rossum49b56061998-10-01 20:42:43 +0000243#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000244
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000246static long import_lock_thread = -1;
247static int import_lock_level = 0;
248
249static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000250lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000252 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000253 if (me == -1)
254 return; /* Too bad */
Neal Norwitze1fdb322006-07-21 05:32:28 +0000255 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 import_lock = PyThread_allocate_lock();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000257 if (import_lock == NULL)
258 return; /* Nothing much we can do. */
259 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000260 if (import_lock_thread == me) {
261 import_lock_level++;
262 return;
263 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000264 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
265 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000268 PyEval_RestoreThread(tstate);
269 }
270 import_lock_thread = me;
271 import_lock_level = 1;
272}
273
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000274static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000277 long me = PyThread_get_thread_ident();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000278 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000280 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000281 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000282 import_lock_level--;
283 if (import_lock_level == 0) {
284 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000285 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000286 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000288}
289
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000290/* This function is called from PyOS_AfterFork to ensure that newly
291 created child processes do not share locks with the parent. */
292
293void
294_PyImport_ReInitLock(void)
295{
296#ifdef _AIX
297 if (import_lock != NULL)
298 import_lock = PyThread_allocate_lock();
299#endif
300}
301
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000302#else
303
304#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000305#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000306
307#endif
308
Tim Peters69232342001-08-30 05:16:13 +0000309static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000310imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000311{
Tim Peters69232342001-08-30 05:16:13 +0000312#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000313 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000314#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000315 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000316#endif
317}
318
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000319static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000320imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000321{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000322#ifdef WITH_THREAD
323 lock_import();
324#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000325 Py_INCREF(Py_None);
326 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327}
328
329static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000330imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000331{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332#ifdef WITH_THREAD
333 if (unlock_import() < 0) {
334 PyErr_SetString(PyExc_RuntimeError,
335 "not holding the import lock");
336 return NULL;
337 }
338#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000339 Py_INCREF(Py_None);
340 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000341}
342
Collin Winter276887b2007-03-12 16:11:39 +0000343static void
Neal Norwitz75c7c802007-03-13 05:31:38 +0000344imp_modules_reloading_clear(void)
Collin Winter276887b2007-03-12 16:11:39 +0000345{
346 PyInterpreterState *interp = PyThreadState_Get()->interp;
Neal Norwitz75c7c802007-03-13 05:31:38 +0000347 if (interp->modules_reloading != NULL)
348 PyDict_Clear(interp->modules_reloading);
Collin Winter276887b2007-03-12 16:11:39 +0000349}
350
Guido van Rossum25ce5661997-08-02 03:10:38 +0000351/* Helper for sys */
352
353PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000354PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000355{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000356 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357 if (interp->modules == NULL)
358 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
359 return interp->modules;
360}
361
Guido van Rossum3f5da241990-12-20 15:06:42 +0000362
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000363/* List of names to clear in sys */
364static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000365 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000366 "exc_type", "exc_value", "exc_traceback",
367 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000368 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000369 NULL
370};
371
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000372static char* sys_files[] = {
373 "stdin", "__stdin__",
374 "stdout", "__stdout__",
375 "stderr", "__stderr__",
376 NULL
377};
378
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000379
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000380/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000384{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000385 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000386 char *name;
387 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000388 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000389 PyObject *modules = interp->modules;
390
391 if (modules == NULL)
392 return; /* Already done */
393
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000394 /* Delete some special variables first. These are common
395 places where user values hide and people complain when their
396 destructors fail. Since the modules containing them are
397 deleted *last* of all, they would come too late in the normal
398 destruction order. Sigh. */
399
400 value = PyDict_GetItemString(modules, "__builtin__");
401 if (value != NULL && PyModule_Check(value)) {
402 dict = PyModule_GetDict(value);
403 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000404 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000405 PyDict_SetItemString(dict, "_", Py_None);
406 }
407 value = PyDict_GetItemString(modules, "sys");
408 if (value != NULL && PyModule_Check(value)) {
409 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000410 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000411 dict = PyModule_GetDict(value);
412 for (p = sys_deletes; *p != NULL; p++) {
413 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000414 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000415 PyDict_SetItemString(dict, *p, Py_None);
416 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000417 for (p = sys_files; *p != NULL; p+=2) {
418 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000419 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000420 v = PyDict_GetItemString(dict, *(p+1));
421 if (v == NULL)
422 v = Py_None;
423 PyDict_SetItemString(dict, *p, v);
424 }
425 }
426
427 /* First, delete __main__ */
428 value = PyDict_GetItemString(modules, "__main__");
429 if (value != NULL && PyModule_Check(value)) {
430 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000431 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000432 _PyModule_Clear(value);
433 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000434 }
435
Guido van Rossum758eec01998-01-19 21:58:26 +0000436 /* The special treatment of __builtin__ here is because even
437 when it's not referenced as a module, its dictionary is
438 referenced by almost every module's __builtins__. Since
439 deleting a module clears its dictionary (even if there are
440 references left to it), we need to delete the __builtin__
441 module last. Likewise, we don't delete sys until the very
442 end because it is implicitly referenced (e.g. by print).
443
444 Also note that we 'delete' modules by replacing their entry
445 in the modules dict with None, rather than really deleting
446 them; this avoids a rehash of the modules dictionary and
447 also marks them as "non existent" so they won't be
448 re-imported. */
449
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000450 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000451 one (skipping __builtin__ and sys) and delete them */
452 do {
453 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000454 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000455 while (PyDict_Next(modules, &pos, &key, &value)) {
456 if (value->ob_refcnt != 1)
457 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000458 if (PyString_Check(key) && PyModule_Check(value)) {
459 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000460 if (strcmp(name, "__builtin__") == 0)
461 continue;
462 if (strcmp(name, "sys") == 0)
463 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000464 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000465 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000466 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000467 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000468 PyDict_SetItem(modules, key, Py_None);
469 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000470 }
471 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000472 } while (ndone > 0);
473
Guido van Rossum758eec01998-01-19 21:58:26 +0000474 /* Next, delete all modules (still skipping __builtin__ and sys) */
475 pos = 0;
476 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000477 if (PyString_Check(key) && PyModule_Check(value)) {
478 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000479 if (strcmp(name, "__builtin__") == 0)
480 continue;
481 if (strcmp(name, "sys") == 0)
482 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000483 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000484 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000485 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000486 PyDict_SetItem(modules, key, Py_None);
487 }
488 }
489
490 /* Next, delete sys and __builtin__ (in that order) */
491 value = PyDict_GetItemString(modules, "sys");
492 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000493 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000494 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000495 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000496 PyDict_SetItemString(modules, "sys", Py_None);
497 }
498 value = PyDict_GetItemString(modules, "__builtin__");
499 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000500 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000501 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000502 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000503 PyDict_SetItemString(modules, "__builtin__", Py_None);
504 }
505
506 /* Finally, clear and delete the modules directory */
507 PyDict_Clear(modules);
508 interp->modules = NULL;
509 Py_DECREF(modules);
Collin Winter276887b2007-03-12 16:11:39 +0000510 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000511}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000512
513
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000514/* Helper for pythonrun.c -- return magic number */
515
516long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000517PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000518{
Guido van Rossum96774c12000-05-01 20:19:08 +0000519 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520}
521
522
Guido van Rossum25ce5661997-08-02 03:10:38 +0000523/* Magic for extension modules (built-in as well as dynamically
524 loaded). To prevent initializing an extension module more than
525 once, we keep a static dictionary 'extensions' keyed by module name
526 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000527 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528 dictionary is stored by calling _PyImport_FixupExtension()
529 immediately after the module initialization function succeeds. A
530 copy can be retrieved from there by calling
531 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000535{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000536 PyObject *modules, *mod, *dict, *copy;
537 if (extensions == NULL) {
538 extensions = PyDict_New();
539 if (extensions == NULL)
540 return NULL;
541 }
542 modules = PyImport_GetModuleDict();
543 mod = PyDict_GetItemString(modules, name);
544 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000545 PyErr_Format(PyExc_SystemError,
546 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547 return NULL;
548 }
549 dict = PyModule_GetDict(mod);
550 if (dict == NULL)
551 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000552 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 if (copy == NULL)
554 return NULL;
555 PyDict_SetItemString(extensions, filename, copy);
556 Py_DECREF(copy);
557 return copy;
558}
559
560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000562{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000563 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000564 if (extensions == NULL)
565 return NULL;
566 dict = PyDict_GetItemString(extensions, filename);
567 if (dict == NULL)
568 return NULL;
569 mod = PyImport_AddModule(name);
570 if (mod == NULL)
571 return NULL;
572 mdict = PyModule_GetDict(mod);
573 if (mdict == NULL)
574 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000575 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000576 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000577 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000578 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000579 name, filename);
580 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581}
582
583
584/* Get the module object corresponding to a module name.
585 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000586 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000587 Because the former action is most common, THIS DOES NOT RETURN A
588 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000591PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000598 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600 if (m == NULL)
601 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604 return NULL;
605 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607
608 return m;
609}
610
Tim Peters1cd70172004-08-02 03:52:12 +0000611/* Remove name from sys.modules, if it's there. */
612static void
613_RemoveModule(const char *name)
614{
615 PyObject *modules = PyImport_GetModuleDict();
616 if (PyDict_GetItemString(modules, name) == NULL)
617 return;
618 if (PyDict_DelItemString(modules, name) < 0)
619 Py_FatalError("import: deleting existing key in"
620 "sys.modules failed");
621}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000622
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000623/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000624 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
625 * removed from sys.modules, to avoid leaving damaged module objects
626 * in sys.modules. The caller may wish to restore the original
627 * module object (if any) in this case; PyImport_ReloadModule is an
628 * example.
629 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000631PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000633 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
634}
635
636PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000637PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000638{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000639 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643 if (m == NULL)
644 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000645 /* If the module is being reloaded, we get the old module back
646 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 d = PyModule_GetDict(m);
648 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
649 if (PyDict_SetItemString(d, "__builtins__",
650 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000651 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000652 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000653 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000654 v = NULL;
655 if (pathname != NULL) {
656 v = PyString_FromString(pathname);
657 if (v == NULL)
658 PyErr_Clear();
659 }
660 if (v == NULL) {
661 v = ((PyCodeObject *)co)->co_filename;
662 Py_INCREF(v);
663 }
664 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000666 Py_DECREF(v);
667
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000668 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000670 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000672
Guido van Rossum25ce5661997-08-02 03:10:38 +0000673 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000674 PyErr_Format(PyExc_ImportError,
675 "Loaded module %.200s not found in sys.modules",
676 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000677 return NULL;
678 }
679
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681
682 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000683
684 error:
685 _RemoveModule(name);
686 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687}
688
689
690/* Given a pathname for a Python source file, fill a buffer with the
691 pathname for the corresponding compiled file. Return the pathname
692 for the compiled file, or NULL if there's no space in the buffer.
693 Doesn't set an exception. */
694
695static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000696make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000697{
Tim Petersc1731372001-08-04 08:12:36 +0000698 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 if (len+2 > buflen)
700 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000701
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000702#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000703 /* Treat .pyw as if it were .py. The case of ".pyw" must match
704 that used in _PyImport_StandardFiletab. */
705 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
706 --len; /* pretend 'w' isn't there */
707#endif
708 memcpy(buf, pathname, len);
709 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
710 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711
712 return buf;
713}
714
715
716/* Given a pathname for a Python source file, its time of last
717 modification, and a pathname for a compiled file, check whether the
718 compiled file represents the same version of the source. If so,
719 return a FILE pointer for the compiled file, positioned just after
720 the header; if not, return NULL.
721 Doesn't set an exception. */
722
723static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000724check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725{
726 FILE *fp;
727 long magic;
728 long pyc_mtime;
729
730 fp = fopen(cpathname, "rb");
731 if (fp == NULL)
732 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000734 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000736 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737 fclose(fp);
738 return NULL;
739 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000743 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744 fclose(fp);
745 return NULL;
746 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000748 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749 return fp;
750}
751
752
753/* Read a code object from a file and check it for validity */
754
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759
Tim Petersd9b9ac82001-01-28 00:27:39 +0000760 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000761 if (co == NULL)
762 return NULL;
763 if (!PyCode_Check(co)) {
764 PyErr_Format(PyExc_ImportError,
765 "Non-code object in %.200s", cpathname);
766 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000767 return NULL;
768 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000769 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770}
771
772
773/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000774 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000777load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778{
779 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 PyCodeObject *co;
781 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000782
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000784 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000785 PyErr_Format(PyExc_ImportError,
786 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000787 return NULL;
788 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000790 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000791 if (co == NULL)
792 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000794 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000796 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798
799 return m;
800}
801
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802/* Parse a source file and return the corresponding code object */
803
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 PyCodeObject *co = NULL;
808 mod_ty mod;
Neal Norwitz2a399b02006-09-11 04:28:16 +0000809 PyArena *arena = PyArena_New();
810 if (arena == NULL)
811 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000816 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 }
Neal Norwitz2a399b02006-09-11 04:28:16 +0000818 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819 return co;
820}
821
822
Guido van Rossum55a83382000-09-20 20:31:38 +0000823/* Helper to open a bytecode file for writing in exclusive mode */
824
825static FILE *
826open_exclusive(char *filename)
827{
828#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
829 /* Use O_EXCL to avoid a race condition when another process tries to
830 write the same file. When that happens, our open() call fails,
831 which is just fine (since it's only a cache).
832 XXX If the file exists and is writable but the directory is not
833 writable, the file will never be written. Oh well.
834 */
835 int fd;
836 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000837 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
838#ifdef O_BINARY
839 |O_BINARY /* necessary for Windows */
840#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000841#ifdef __VMS
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842 , 0666, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000843#else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000844 , 0666
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000845#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000846 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000847 if (fd < 0)
848 return NULL;
849 return fdopen(fd, "wb");
850#else
851 /* Best we can do -- on Windows this can't happen anyway */
852 return fopen(filename, "wb");
853#endif
854}
855
856
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000857/* Write a compiled module to a file, placing the time of last
858 modification of its source into the header.
859 Errors are ignored, if a write error occurs an attempt is made to
860 remove the file. */
861
862static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000863write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864{
865 FILE *fp;
866
Guido van Rossum55a83382000-09-20 20:31:38 +0000867 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000869 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000870 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871 "# can't create %s\n", cpathname);
872 return;
873 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000874 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000875 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000876 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
877 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000878 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000880 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881 /* Don't keep partial file */
882 fclose(fp);
883 (void) unlink(cpathname);
884 return;
885 }
886 /* Now write the true mtime */
887 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000888 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000889 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890 fflush(fp);
891 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000893 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894}
895
896
897/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000898 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
899 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000900
Guido van Rossum79f25d91997-04-29 20:08:16 +0000901static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000902load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000903{
Fred Drake4c82b232000-06-30 16:18:57 +0000904 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000905 FILE *fpc;
906 char buf[MAXPATHLEN+1];
907 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 PyCodeObject *co;
909 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000911 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000912 if (mtime == (time_t)(-1)) {
913 PyErr_Format(PyExc_RuntimeError,
914 "unable to get modification time from '%s'",
915 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000916 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000917 }
Fred Drake4c82b232000-06-30 16:18:57 +0000918#if SIZEOF_TIME_T > 4
919 /* Python's .pyc timestamp handling presumes that the timestamp fits
920 in 4 bytes. This will be fine until sometime in the year 2038,
921 when a 4-byte signed time_t will overflow.
922 */
923 if (mtime >> 32) {
924 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000925 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000926 return NULL;
927 }
928#endif
Tim Peters36515e22001-11-18 04:06:29 +0000929 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000930 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931 if (cpathname != NULL &&
932 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000933 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000934 fclose(fpc);
935 if (co == NULL)
936 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000938 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000940 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000941 }
942 else {
943 co = parse_source_module(pathname, fp);
944 if (co == NULL)
945 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000947 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000948 name, pathname);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +0000949 if (cpathname)
950 write_compiled_module(co, cpathname, mtime);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000951 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000952 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000954
955 return m;
956}
957
958
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000959/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000960static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
961static struct filedescr *find_module(char *, char *, PyObject *,
962 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000963static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000964
965/* Load a package and return its module object WITH INCREMENTED
966 REFERENCE COUNT */
967
968static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000969load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000970{
Tim Peters1cd70172004-08-02 03:52:12 +0000971 PyObject *m, *d;
972 PyObject *file = NULL;
973 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000974 int err;
975 char buf[MAXPATHLEN+1];
976 FILE *fp = NULL;
977 struct filedescr *fdp;
978
979 m = PyImport_AddModule(name);
980 if (m == NULL)
981 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000982 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000983 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000984 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000985 d = PyModule_GetDict(m);
986 file = PyString_FromString(pathname);
987 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000988 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000989 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000990 if (path == NULL)
991 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000992 err = PyDict_SetItemString(d, "__file__", file);
993 if (err == 0)
994 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000995 if (err != 0)
996 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000997 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000998 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000999 if (fdp == NULL) {
1000 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1001 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001002 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001003 }
1004 else
1005 m = NULL;
1006 goto cleanup;
1007 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001008 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001009 if (fp != NULL)
1010 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001011 goto cleanup;
1012
1013 error:
1014 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001015 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001016 Py_XDECREF(path);
1017 Py_XDECREF(file);
1018 return m;
1019}
1020
1021
1022/* Helper to test for built-in module */
1023
1024static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001025is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001026{
1027 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001028 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1029 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1030 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001031 return -1;
1032 else
1033 return 1;
1034 }
1035 }
1036 return 0;
1037}
1038
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001039
Just van Rossum52e14d62002-12-30 22:08:05 +00001040/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1041 possibly by fetching it from the path_importer_cache dict. If it
Brett Cannon94b69f62006-09-28 22:10:14 +00001042 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001043 that can handle the path item. Return None if no hook could;
1044 this tells our caller it should fall back to the builtin
1045 import mechanism. Cache the result in path_importer_cache.
1046 Returns a borrowed reference. */
1047
1048static PyObject *
1049get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1050 PyObject *p)
1051{
1052 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001053 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001054
1055 /* These conditions are the caller's responsibility: */
1056 assert(PyList_Check(path_hooks));
1057 assert(PyDict_Check(path_importer_cache));
1058
1059 nhooks = PyList_Size(path_hooks);
1060 if (nhooks < 0)
1061 return NULL; /* Shouldn't happen */
1062
1063 importer = PyDict_GetItem(path_importer_cache, p);
1064 if (importer != NULL)
1065 return importer;
1066
1067 /* set path_importer_cache[p] to None to avoid recursion */
1068 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1069 return NULL;
1070
1071 for (j = 0; j < nhooks; j++) {
1072 PyObject *hook = PyList_GetItem(path_hooks, j);
1073 if (hook == NULL)
1074 return NULL;
Georg Brandl684fd0c2006-05-25 19:15:31 +00001075 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001076 if (importer != NULL)
1077 break;
1078
1079 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1080 return NULL;
1081 }
1082 PyErr_Clear();
1083 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001084 if (importer == NULL) {
1085 importer = PyObject_CallFunctionObjArgs(
1086 (PyObject *)&NullImporterType, p, NULL
1087 );
1088 if (importer == NULL) {
1089 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1090 PyErr_Clear();
1091 return Py_None;
1092 }
1093 }
1094 }
1095 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001096 int err = PyDict_SetItem(path_importer_cache, p, importer);
1097 Py_DECREF(importer);
1098 if (err != 0)
1099 return NULL;
1100 }
1101 return importer;
1102}
1103
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001104/* Search the path (default sys.path) for a module. Return the
1105 corresponding filedescr struct, and (via return arguments) the
1106 pathname and an open file. Return NULL if the module is not found. */
1107
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001108#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001109extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001110 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001111#endif
1112
Martin v. Löwis18e16552006-02-15 17:27:45 +00001113static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001114static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001115static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001116
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001118find_module(char *fullname, char *subname, PyObject *path, char *buf,
1119 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001121 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001122 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001123 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001124 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001125 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001126 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001127#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001128 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001129#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001130 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1131 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1132 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001133 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001134#if defined(PYOS_OS2)
1135 size_t saved_len;
1136 size_t saved_namelen;
1137 char *saved_buf = NULL;
1138#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001139 if (p_loader != NULL)
1140 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001141
Just van Rossum52e14d62002-12-30 22:08:05 +00001142 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001143 PyErr_SetString(PyExc_OverflowError,
1144 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001145 return NULL;
1146 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001147 strcpy(name, subname);
1148
1149 /* sys.meta_path import hook */
1150 if (p_loader != NULL) {
1151 PyObject *meta_path;
1152
1153 meta_path = PySys_GetObject("meta_path");
1154 if (meta_path == NULL || !PyList_Check(meta_path)) {
1155 PyErr_SetString(PyExc_ImportError,
1156 "sys.meta_path must be a list of "
1157 "import hooks");
1158 return NULL;
1159 }
1160 Py_INCREF(meta_path); /* zap guard */
1161 npath = PyList_Size(meta_path);
1162 for (i = 0; i < npath; i++) {
1163 PyObject *loader;
1164 PyObject *hook = PyList_GetItem(meta_path, i);
1165 loader = PyObject_CallMethod(hook, "find_module",
1166 "sO", fullname,
1167 path != NULL ?
1168 path : Py_None);
1169 if (loader == NULL) {
1170 Py_DECREF(meta_path);
1171 return NULL; /* true error */
1172 }
1173 if (loader != Py_None) {
1174 /* a loader was found */
1175 *p_loader = loader;
1176 Py_DECREF(meta_path);
1177 return &importhookdescr;
1178 }
1179 Py_DECREF(loader);
1180 }
1181 Py_DECREF(meta_path);
1182 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001183
1184 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001185 /* The only type of submodule allowed inside a "frozen"
1186 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001187 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1188 PyErr_SetString(PyExc_ImportError,
1189 "full frozen module name too long");
1190 return NULL;
1191 }
1192 strcpy(buf, PyString_AsString(path));
1193 strcat(buf, ".");
1194 strcat(buf, name);
1195 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001196 if (find_frozen(name) != NULL) {
1197 strcpy(buf, name);
1198 return &fd_frozen;
1199 }
1200 PyErr_Format(PyExc_ImportError,
1201 "No frozen submodule named %.200s", name);
1202 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001203 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001204 if (path == NULL) {
1205 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001206 strcpy(buf, name);
1207 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001208 }
Greg Ward201baee2001-10-04 14:52:06 +00001209 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001210 strcpy(buf, name);
1211 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001212 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001213
Guido van Rossumac279101996-08-22 23:10:58 +00001214#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001215 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1216 if (fp != NULL) {
1217 *p_fp = fp;
1218 return fdp;
1219 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001220#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001221 path = PySys_GetObject("path");
1222 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001223 if (path == NULL || !PyList_Check(path)) {
1224 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001225 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226 return NULL;
1227 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001228
1229 path_hooks = PySys_GetObject("path_hooks");
1230 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1231 PyErr_SetString(PyExc_ImportError,
1232 "sys.path_hooks must be a list of "
1233 "import hooks");
1234 return NULL;
1235 }
1236 path_importer_cache = PySys_GetObject("path_importer_cache");
1237 if (path_importer_cache == NULL ||
1238 !PyDict_Check(path_importer_cache)) {
1239 PyErr_SetString(PyExc_ImportError,
1240 "sys.path_importer_cache must be a dict");
1241 return NULL;
1242 }
1243
Guido van Rossum79f25d91997-04-29 20:08:16 +00001244 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001245 namelen = strlen(name);
1246 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001247 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 PyObject *v = PyList_GetItem(path, i);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00001249 if (!v)
1250 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001251#ifdef Py_USING_UNICODE
1252 if (PyUnicode_Check(v)) {
1253 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1254 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1255 if (copy == NULL)
1256 return NULL;
1257 v = copy;
1258 }
1259 else
1260#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001261 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001262 continue;
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +00001263 len = PyString_GET_SIZE(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001264 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1265 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001266 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001267 }
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +00001268 strcpy(buf, PyString_AS_STRING(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001269 if (strlen(buf) != len) {
1270 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001271 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001272 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001273
1274 /* sys.path_hooks import hook */
1275 if (p_loader != NULL) {
1276 PyObject *importer;
1277
1278 importer = get_path_importer(path_importer_cache,
1279 path_hooks, v);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001280 if (importer == NULL) {
1281 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001282 return NULL;
Neal Norwitza4df11d2006-07-06 04:28:59 +00001283 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001284 /* Note: importer is a borrowed reference */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001285 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001286 PyObject *loader;
1287 loader = PyObject_CallMethod(importer,
1288 "find_module",
1289 "s", fullname);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001290 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001291 if (loader == NULL)
1292 return NULL; /* error */
1293 if (loader != Py_None) {
1294 /* a loader was found */
1295 *p_loader = loader;
1296 return &importhookdescr;
1297 }
1298 Py_DECREF(loader);
Georg Brandlf4ef1162006-05-26 18:03:31 +00001299 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001300 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001301 }
Georg Brandlf4ef1162006-05-26 18:03:31 +00001302 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001303
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001304 if (len > 0 && buf[len-1] != SEP
1305#ifdef ALTSEP
1306 && buf[len-1] != ALTSEP
1307#endif
1308 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001309 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001310 strcpy(buf+len, name);
1311 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001312
1313 /* Check for package import (buf holds a directory name,
1314 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001315#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001316 if (stat(buf, &statbuf) == 0 && /* it exists */
1317 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001318 case_ok(buf, len, namelen, name)) { /* case matches */
1319 if (find_init_module(buf)) { /* and has __init__.py */
1320 Py_XDECREF(copy);
1321 return &fd_package;
1322 }
1323 else {
1324 char warnstr[MAXPATHLEN+80];
1325 sprintf(warnstr, "Not importing directory "
1326 "'%.*s': missing __init__.py",
1327 MAXPATHLEN, buf);
1328 if (PyErr_Warn(PyExc_ImportWarning,
1329 warnstr)) {
1330 Py_XDECREF(copy);
1331 return NULL;
1332 }
1333 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001334 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001335#else
1336 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001337#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001338 if (isdir(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001339 case_ok(buf, len, namelen, name)) {
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001340 if (find_init_module(buf)) {
1341 Py_XDECREF(copy);
1342 return &fd_package;
1343 }
1344 else {
1345 char warnstr[MAXPATHLEN+80];
1346 sprintf(warnstr, "Not importing directory "
1347 "'%.*s': missing __init__.py",
1348 MAXPATHLEN, buf);
1349 if (PyErr_Warn(PyExc_ImportWarning,
1350 warnstr)) {
1351 Py_XDECREF(copy);
1352 return NULL;
1353 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001354 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001355#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001356#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001357#if defined(PYOS_OS2)
1358 /* take a snapshot of the module spec for restoration
1359 * after the 8 character DLL hackery
1360 */
1361 saved_buf = strdup(buf);
1362 saved_len = len;
1363 saved_namelen = namelen;
1364#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001365 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001366#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001367 /* OS/2 limits DLLs to 8 character names (w/o
1368 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001369 * so if the name is longer than that and its a
1370 * dynamically loaded module we're going to try,
1371 * truncate the name before trying
1372 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001373 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001374 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001375 const struct filedescr *scan;
1376 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001377 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001378 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001379 break;
1380 else
1381 scan++;
1382 }
1383 if (scan->suffix != NULL) {
1384 /* yes, so truncate the name */
1385 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001386 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001387 buf[len] = '\0';
1388 }
1389 }
1390#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001391 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001392 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001393 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001394 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001395 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001396 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001397 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001398 if (fp != NULL) {
1399 if (case_ok(buf, len, namelen, name))
1400 break;
1401 else { /* continue search */
1402 fclose(fp);
1403 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001404 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001405 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001406#if defined(PYOS_OS2)
1407 /* restore the saved snapshot */
1408 strcpy(buf, saved_buf);
1409 len = saved_len;
1410 namelen = saved_namelen;
1411#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001412 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001413#if defined(PYOS_OS2)
1414 /* don't need/want the module name snapshot anymore */
1415 if (saved_buf)
1416 {
1417 free(saved_buf);
1418 saved_buf = NULL;
1419 }
1420#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001421 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001422 if (fp != NULL)
1423 break;
1424 }
1425 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001426 PyErr_Format(PyExc_ImportError,
1427 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001428 return NULL;
1429 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001430 *p_fp = fp;
1431 return fdp;
1432}
1433
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001434/* Helpers for main.c
1435 * Find the source file corresponding to a named module
1436 */
1437struct filedescr *
1438_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1439 size_t buflen, FILE **p_fp, PyObject **p_loader)
1440{
1441 return find_module((char *) name, (char *) name, path,
1442 buf, buflen, p_fp, p_loader);
1443}
1444
1445PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1446{
1447 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1448}
1449
Martin v. Löwis18e16552006-02-15 17:27:45 +00001450/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001451 * The arguments here are tricky, best shown by example:
1452 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1453 * ^ ^ ^ ^
1454 * |--------------------- buf ---------------------|
1455 * |------------------- len ------------------|
1456 * |------ name -------|
1457 * |----- namelen -----|
1458 * buf is the full path, but len only counts up to (& exclusive of) the
1459 * extension. name is the module name, also exclusive of extension.
1460 *
1461 * We've already done a successful stat() or fopen() on buf, so know that
1462 * there's some match, possibly case-insensitive.
1463 *
Tim Peters50d8d372001-02-28 05:34:27 +00001464 * case_ok() is to return 1 if there's a case-sensitive match for
1465 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1466 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001467 *
Tim Peters50d8d372001-02-28 05:34:27 +00001468 * case_ok() is used to implement case-sensitive import semantics even
1469 * on platforms with case-insensitive filesystems. It's trivial to implement
1470 * for case-sensitive filesystems. It's pretty much a cross-platform
1471 * nightmare for systems with case-insensitive filesystems.
1472 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001473
Tim Peters50d8d372001-02-28 05:34:27 +00001474/* First we may need a pile of platform-specific header files; the sequence
1475 * of #if's here should match the sequence in the body of case_ok().
1476 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001477#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001478#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001479
Tim Peters50d8d372001-02-28 05:34:27 +00001480#elif defined(DJGPP)
1481#include <dir.h>
1482
Jason Tishler7961aa62005-05-20 00:56:54 +00001483#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001484#include <sys/types.h>
1485#include <dirent.h>
1486
Andrew MacIntyred9400542002-02-26 11:41:34 +00001487#elif defined(PYOS_OS2)
1488#define INCL_DOS
1489#define INCL_DOSERRORS
1490#define INCL_NOPMAPI
1491#include <os2.h>
1492
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001493#elif defined(RISCOS)
1494#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001495#endif
1496
Guido van Rossum0980bd91998-02-13 17:18:36 +00001497static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001498case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001499{
Tim Peters50d8d372001-02-28 05:34:27 +00001500/* Pick a platform-specific implementation; the sequence of #if's here should
1501 * match the sequence just above.
1502 */
1503
Jason Tishler7961aa62005-05-20 00:56:54 +00001504/* MS_WINDOWS */
1505#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001506 WIN32_FIND_DATA data;
1507 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001508
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001509 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001510 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001511
Guido van Rossum0980bd91998-02-13 17:18:36 +00001512 h = FindFirstFile(buf, &data);
1513 if (h == INVALID_HANDLE_VALUE) {
1514 PyErr_Format(PyExc_NameError,
1515 "Can't find file for module %.100s\n(filename %.300s)",
1516 name, buf);
1517 return 0;
1518 }
1519 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001520 return strncmp(data.cFileName, name, namelen) == 0;
1521
1522/* DJGPP */
1523#elif defined(DJGPP)
1524 struct ffblk ffblk;
1525 int done;
1526
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001527 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001528 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001529
1530 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1531 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001532 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001533 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001534 name, buf);
1535 return 0;
1536 }
Tim Peters50d8d372001-02-28 05:34:27 +00001537 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001538
Jason Tishler7961aa62005-05-20 00:56:54 +00001539/* new-fangled macintosh (macosx) or Cygwin */
1540#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001541 DIR *dirp;
1542 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001543 char dirname[MAXPATHLEN + 1];
1544 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001545
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001546 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001547 return 1;
1548
Tim Petersd1e87a82001-03-01 18:12:00 +00001549 /* Copy the dir component into dirname; substitute "." if empty */
1550 if (dirlen <= 0) {
1551 dirname[0] = '.';
1552 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001553 }
1554 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001555 assert(dirlen <= MAXPATHLEN);
1556 memcpy(dirname, buf, dirlen);
1557 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001558 }
1559 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001560 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001561 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001562 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001563 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001564 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001565#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001566 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001567#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001568 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001569#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001570 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001571 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001572 (void)closedir(dirp);
1573 return 1; /* Found */
1574 }
1575 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001576 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001577 }
Tim Peters430f5d42001-03-01 01:30:56 +00001578 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001579
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001580/* RISC OS */
1581#elif defined(RISCOS)
1582 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1583 char buf2[MAXPATHLEN+2];
1584 char *nameWithExt = buf+len-namelen;
1585 int canonlen;
1586 os_error *e;
1587
1588 if (Py_GETENV("PYTHONCASEOK") != NULL)
1589 return 1;
1590
1591 /* workaround:
1592 append wildcard, otherwise case of filename wouldn't be touched */
1593 strcpy(buf2, buf);
1594 strcat(buf2, "*");
1595
1596 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1597 canonlen = MAXPATHLEN+1-canonlen;
1598 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1599 return 0;
1600 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1601 return 1; /* match */
1602
1603 return 0;
1604
Andrew MacIntyred9400542002-02-26 11:41:34 +00001605/* OS/2 */
1606#elif defined(PYOS_OS2)
1607 HDIR hdir = 1;
1608 ULONG srchcnt = 1;
1609 FILEFINDBUF3 ffbuf;
1610 APIRET rc;
1611
1612 if (getenv("PYTHONCASEOK") != NULL)
1613 return 1;
1614
1615 rc = DosFindFirst(buf,
1616 &hdir,
1617 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1618 &ffbuf, sizeof(ffbuf),
1619 &srchcnt,
1620 FIL_STANDARD);
1621 if (rc != NO_ERROR)
1622 return 0;
1623 return strncmp(ffbuf.achName, name, namelen) == 0;
1624
Tim Peters50d8d372001-02-28 05:34:27 +00001625/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1626#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001627 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001628
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001629#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001630}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001631
Guido van Rossum0980bd91998-02-13 17:18:36 +00001632
Guido van Rossum197346f1997-10-31 18:38:52 +00001633#ifdef HAVE_STAT
1634/* Helper to look for __init__.py or __init__.py[co] in potential package */
1635static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001636find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001637{
Tim Peters0f9431f2001-07-05 03:47:53 +00001638 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001639 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001640 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001641 struct stat statbuf;
1642
Tim Peters0f9431f2001-07-05 03:47:53 +00001643/* For calling case_ok(buf, len, namelen, name):
1644 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1645 * ^ ^ ^ ^
1646 * |--------------------- buf ---------------------|
1647 * |------------------- len ------------------|
1648 * |------ name -------|
1649 * |----- namelen -----|
1650 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001651 if (save_len + 13 >= MAXPATHLEN)
1652 return 0;
1653 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001654 pname = buf + i;
1655 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001656 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001657 if (case_ok(buf,
1658 save_len + 9, /* len("/__init__") */
1659 8, /* len("__init__") */
1660 pname)) {
1661 buf[save_len] = '\0';
1662 return 1;
1663 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001664 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001665 i += strlen(pname);
1666 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001667 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001668 if (case_ok(buf,
1669 save_len + 9, /* len("/__init__") */
1670 8, /* len("__init__") */
1671 pname)) {
1672 buf[save_len] = '\0';
1673 return 1;
1674 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001675 }
1676 buf[save_len] = '\0';
1677 return 0;
1678}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001679
1680#else
1681
1682#ifdef RISCOS
1683static int
1684find_init_module(buf)
1685 char *buf;
1686{
1687 int save_len = strlen(buf);
1688 int i = save_len;
1689
1690 if (save_len + 13 >= MAXPATHLEN)
1691 return 0;
1692 buf[i++] = SEP;
1693 strcpy(buf+i, "__init__/py");
1694 if (isfile(buf)) {
1695 buf[save_len] = '\0';
1696 return 1;
1697 }
1698
1699 if (Py_OptimizeFlag)
1700 strcpy(buf+i, "o");
1701 else
1702 strcpy(buf+i, "c");
1703 if (isfile(buf)) {
1704 buf[save_len] = '\0';
1705 return 1;
1706 }
1707 buf[save_len] = '\0';
1708 return 0;
1709}
1710#endif /*RISCOS*/
1711
Guido van Rossum197346f1997-10-31 18:38:52 +00001712#endif /* HAVE_STAT */
1713
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001714
Tim Petersdbd9ba62000-07-09 03:09:57 +00001715static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001716
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001717/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001718 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001719
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001721load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001723 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001725 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001727 /* First check that there's an open file (if we need one) */
1728 switch (type) {
1729 case PY_SOURCE:
1730 case PY_COMPILED:
1731 if (fp == NULL) {
1732 PyErr_Format(PyExc_ValueError,
1733 "file object required for import (type code %d)",
1734 type);
1735 return NULL;
1736 }
1737 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001738
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001739 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740
1741 case PY_SOURCE:
1742 m = load_source_module(name, buf, fp);
1743 break;
1744
1745 case PY_COMPILED:
1746 m = load_compiled_module(name, buf, fp);
1747 break;
1748
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001749#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001751 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001752 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001753#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001754
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001755 case PKG_DIRECTORY:
1756 m = load_package(name, buf);
1757 break;
1758
1759 case C_BUILTIN:
1760 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001761 if (buf != NULL && buf[0] != '\0')
1762 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001763 if (type == C_BUILTIN)
1764 err = init_builtin(name);
1765 else
1766 err = PyImport_ImportFrozenModule(name);
1767 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001768 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001769 if (err == 0) {
1770 PyErr_Format(PyExc_ImportError,
1771 "Purported %s module %.200s not found",
1772 type == C_BUILTIN ?
1773 "builtin" : "frozen",
1774 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001775 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001776 }
1777 modules = PyImport_GetModuleDict();
1778 m = PyDict_GetItemString(modules, name);
1779 if (m == NULL) {
1780 PyErr_Format(
1781 PyExc_ImportError,
1782 "%s module %.200s not properly initialized",
1783 type == C_BUILTIN ?
1784 "builtin" : "frozen",
1785 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001786 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001787 }
1788 Py_INCREF(m);
1789 break;
1790
Just van Rossum52e14d62002-12-30 22:08:05 +00001791 case IMP_HOOK: {
1792 if (loader == NULL) {
1793 PyErr_SetString(PyExc_ImportError,
1794 "import hook without loader");
1795 return NULL;
1796 }
1797 m = PyObject_CallMethod(loader, "load_module", "s", name);
1798 break;
1799 }
1800
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001802 PyErr_Format(PyExc_ImportError,
1803 "Don't know how to import %.200s (type code %d)",
1804 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001805 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806
1807 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001808
1809 return m;
1810}
1811
1812
1813/* Initialize a built-in module.
Brett Cannon5a9aa4f2006-10-03 21:58:55 +00001814 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001815 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001816
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001817static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001818init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001819{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001820 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001821
Greg Ward201baee2001-10-04 14:52:06 +00001822 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001823 return 1;
1824
Guido van Rossum771c6c81997-10-31 18:37:24 +00001825 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001826 if (strcmp(name, p->name) == 0) {
1827 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001828 PyErr_Format(PyExc_ImportError,
1829 "Cannot re-init internal module %.200s",
1830 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001831 return -1;
1832 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001834 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001835 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001837 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001838 if (_PyImport_FixupExtension(name, name) == NULL)
1839 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001840 return 1;
1841 }
1842 }
1843 return 0;
1844}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001845
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001846
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001847/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001848
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001849static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001850find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001851{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001852 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001853
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001855 if (p->name == NULL)
1856 return NULL;
1857 if (strcmp(p->name, name) == 0)
1858 break;
1859 }
1860 return p;
1861}
1862
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001864get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001865{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001866 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001867 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001868
1869 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001870 PyErr_Format(PyExc_ImportError,
1871 "No such frozen object named %.200s",
1872 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001873 return NULL;
1874 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001875 if (p->code == NULL) {
1876 PyErr_Format(PyExc_ImportError,
1877 "Excluded frozen object named %.200s",
1878 name);
1879 return NULL;
1880 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001881 size = p->size;
1882 if (size < 0)
1883 size = -size;
1884 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001885}
1886
1887/* Initialize a frozen module.
1888 Return 1 for succes, 0 if the module is not found, and -1 with
1889 an exception set if the initialization failed.
1890 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001891
1892int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001893PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001894{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001895 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001896 PyObject *co;
1897 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001898 int ispackage;
1899 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001900
1901 if (p == NULL)
1902 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001903 if (p->code == NULL) {
1904 PyErr_Format(PyExc_ImportError,
1905 "Excluded frozen object named %.200s",
1906 name);
1907 return -1;
1908 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001909 size = p->size;
1910 ispackage = (size < 0);
1911 if (ispackage)
1912 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001914 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001915 name, ispackage ? " package" : "");
1916 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001917 if (co == NULL)
1918 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001920 PyErr_Format(PyExc_TypeError,
1921 "frozen object %.200s is not a code object",
1922 name);
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001923 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001924 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001925 if (ispackage) {
1926 /* Set __path__ to the package name */
1927 PyObject *d, *s;
1928 int err;
1929 m = PyImport_AddModule(name);
1930 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001931 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001932 d = PyModule_GetDict(m);
1933 s = PyString_InternFromString(name);
1934 if (s == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001935 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001936 err = PyDict_SetItemString(d, "__path__", s);
1937 Py_DECREF(s);
1938 if (err != 0)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001939 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001940 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001941 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001942 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001943 goto err_return;
1944 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001945 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001946 return 1;
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001947err_return:
1948 Py_DECREF(co);
1949 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001950}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001951
1952
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001953/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001954 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001955
Guido van Rossum79f25d91997-04-29 20:08:16 +00001956PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001957PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001958{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001959 PyObject *pname;
1960 PyObject *result;
1961
1962 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001963 if (pname == NULL)
1964 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001965 result = PyImport_Import(pname);
1966 Py_DECREF(pname);
1967 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001968}
1969
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001970/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001971static PyObject *get_parent(PyObject *globals, char *buf,
1972 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001973static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001974 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001975static int mark_miss(char *name);
1976static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001977 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001978static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001979
1980/* The Magnum Opus of dotted-name import :-) */
1981
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001982static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001983import_module_level(char *name, PyObject *globals, PyObject *locals,
1984 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001985{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001986 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00001987 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001988 PyObject *parent, *head, *next, *tail;
1989
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001990 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001991 if (parent == NULL)
1992 return NULL;
1993
1994 head = load_next(parent, Py_None, &name, buf, &buflen);
1995 if (head == NULL)
1996 return NULL;
1997
1998 tail = head;
1999 Py_INCREF(tail);
2000 while (name) {
2001 next = load_next(tail, tail, &name, buf, &buflen);
2002 Py_DECREF(tail);
2003 if (next == NULL) {
2004 Py_DECREF(head);
2005 return NULL;
2006 }
2007 tail = next;
2008 }
Thomas Wouters8ddab272006-04-04 16:17:02 +00002009 if (tail == Py_None) {
2010 /* If tail is Py_None, both get_parent and load_next found
2011 an empty module name: someone called __import__("") or
2012 doctored faulty bytecode */
Thomas Wouters4bdaa272006-04-05 13:39:37 +00002013 Py_DECREF(tail);
2014 Py_DECREF(head);
Thomas Wouters8ddab272006-04-04 16:17:02 +00002015 PyErr_SetString(PyExc_ValueError,
2016 "Empty module name");
2017 return NULL;
2018 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002019
2020 if (fromlist != NULL) {
2021 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2022 fromlist = NULL;
2023 }
2024
2025 if (fromlist == NULL) {
2026 Py_DECREF(tail);
2027 return head;
2028 }
2029
2030 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002031 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002032 Py_DECREF(tail);
2033 return NULL;
2034 }
2035
2036 return tail;
2037}
2038
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002039/* For DLL compatibility */
2040#undef PyImport_ImportModuleEx
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002041PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002042PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
2043 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002044{
2045 PyObject *result;
2046 lock_import();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002047 result = import_module_level(name, globals, locals, fromlist, -1);
2048 if (unlock_import() < 0) {
2049 Py_XDECREF(result);
2050 PyErr_SetString(PyExc_RuntimeError,
2051 "not holding the import lock");
2052 return NULL;
2053 }
2054 return result;
2055}
2056#define PyImport_ImportModuleEx(n, g, l, f) \
2057 PyImport_ImportModuleLevel(n, g, l, f, -1);
2058
2059PyObject *
2060PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2061 PyObject *fromlist, int level)
2062{
2063 PyObject *result;
2064 lock_import();
2065 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002066 if (unlock_import() < 0) {
2067 Py_XDECREF(result);
2068 PyErr_SetString(PyExc_RuntimeError,
2069 "not holding the import lock");
2070 return NULL;
2071 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002072 return result;
2073}
2074
Fred Drake87590902004-05-28 20:21:36 +00002075/* Return the package that an import is being performed in. If globals comes
2076 from the module foo.bar.bat (not itself a package), this returns the
2077 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Georg Brandl5f6861d2006-05-28 21:57:35 +00002078 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002079
2080 The *name* of the returned package is returned in buf, with the length of
2081 the name in *p_buflen.
2082
2083 If globals doesn't come from a package or a module in a package, or a
2084 corresponding entry is not found in sys.modules, Py_None is returned.
2085*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002086static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002087get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002088{
2089 static PyObject *namestr = NULL;
2090 static PyObject *pathstr = NULL;
2091 PyObject *modname, *modpath, *modules, *parent;
2092
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002093 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002094 return Py_None;
2095
2096 if (namestr == NULL) {
2097 namestr = PyString_InternFromString("__name__");
2098 if (namestr == NULL)
2099 return NULL;
2100 }
2101 if (pathstr == NULL) {
2102 pathstr = PyString_InternFromString("__path__");
2103 if (pathstr == NULL)
2104 return NULL;
2105 }
2106
2107 *buf = '\0';
2108 *p_buflen = 0;
2109 modname = PyDict_GetItem(globals, namestr);
2110 if (modname == NULL || !PyString_Check(modname))
2111 return Py_None;
2112
2113 modpath = PyDict_GetItem(globals, pathstr);
2114 if (modpath != NULL) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00002115 Py_ssize_t len = PyString_GET_SIZE(modname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002116 if (len > MAXPATHLEN) {
2117 PyErr_SetString(PyExc_ValueError,
2118 "Module name too long");
2119 return NULL;
2120 }
2121 strcpy(buf, PyString_AS_STRING(modname));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002122 }
2123 else {
2124 char *start = PyString_AS_STRING(modname);
2125 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002126 size_t len;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002127 if (lastdot == NULL && level > 0) {
2128 PyErr_SetString(PyExc_ValueError,
Georg Brandl98775df2006-09-06 06:09:31 +00002129 "Attempted relative import in non-package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002130 return NULL;
2131 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002132 if (lastdot == NULL)
2133 return Py_None;
2134 len = lastdot - start;
2135 if (len >= MAXPATHLEN) {
2136 PyErr_SetString(PyExc_ValueError,
2137 "Module name too long");
2138 return NULL;
2139 }
2140 strncpy(buf, start, len);
2141 buf[len] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002142 }
2143
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002144 while (--level > 0) {
2145 char *dot = strrchr(buf, '.');
2146 if (dot == NULL) {
2147 PyErr_SetString(PyExc_ValueError,
Georg Brandl98775df2006-09-06 06:09:31 +00002148 "Attempted relative import beyond "
2149 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002150 return NULL;
2151 }
2152 *dot = '\0';
2153 }
2154 *p_buflen = strlen(buf);
2155
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002156 modules = PyImport_GetModuleDict();
2157 parent = PyDict_GetItemString(modules, buf);
2158 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002159 PyErr_Format(PyExc_SystemError,
2160 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002161 return parent;
2162 /* We expect, but can't guarantee, if parent != None, that:
2163 - parent.__name__ == buf
2164 - parent.__dict__ is globals
2165 If this is violated... Who cares? */
2166}
2167
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002168/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002169static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002170load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002171 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002172{
2173 char *name = *p_name;
2174 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002175 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002176 char *p;
2177 PyObject *result;
2178
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002179 if (strlen(name) == 0) {
Thomas Wouters8ddab272006-04-04 16:17:02 +00002180 /* completely empty module name should only happen in
2181 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002182 Py_INCREF(mod);
2183 *p_name = NULL;
2184 return mod;
2185 }
2186
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002187 if (dot == NULL) {
2188 *p_name = NULL;
2189 len = strlen(name);
2190 }
2191 else {
2192 *p_name = dot+1;
2193 len = dot-name;
2194 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002195 if (len == 0) {
2196 PyErr_SetString(PyExc_ValueError,
2197 "Empty module name");
2198 return NULL;
2199 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002200
2201 p = buf + *p_buflen;
2202 if (p != buf)
2203 *p++ = '.';
2204 if (p+len-buf >= MAXPATHLEN) {
2205 PyErr_SetString(PyExc_ValueError,
2206 "Module name too long");
2207 return NULL;
2208 }
2209 strncpy(p, name, len);
2210 p[len] = '\0';
2211 *p_buflen = p+len-buf;
2212
2213 result = import_submodule(mod, p, buf);
2214 if (result == Py_None && altmod != mod) {
2215 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002216 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002217 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002218 if (result != NULL && result != Py_None) {
2219 if (mark_miss(buf) != 0) {
2220 Py_DECREF(result);
2221 return NULL;
2222 }
2223 strncpy(buf, name, len);
2224 buf[len] = '\0';
2225 *p_buflen = len;
2226 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002227 }
2228 if (result == NULL)
2229 return NULL;
2230
2231 if (result == Py_None) {
2232 Py_DECREF(result);
2233 PyErr_Format(PyExc_ImportError,
2234 "No module named %.200s", name);
2235 return NULL;
2236 }
2237
2238 return result;
2239}
2240
2241static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002242mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002243{
2244 PyObject *modules = PyImport_GetModuleDict();
2245 return PyDict_SetItemString(modules, name, Py_None);
2246}
2247
2248static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002249ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002250 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002251{
2252 int i;
2253
2254 if (!PyObject_HasAttrString(mod, "__path__"))
2255 return 1;
2256
2257 for (i = 0; ; i++) {
2258 PyObject *item = PySequence_GetItem(fromlist, i);
2259 int hasit;
2260 if (item == NULL) {
2261 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2262 PyErr_Clear();
2263 return 1;
2264 }
2265 return 0;
2266 }
2267 if (!PyString_Check(item)) {
2268 PyErr_SetString(PyExc_TypeError,
2269 "Item in ``from list'' not a string");
2270 Py_DECREF(item);
2271 return 0;
2272 }
2273 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002274 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002275 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002276 /* See if the package defines __all__ */
2277 if (recursive)
2278 continue; /* Avoid endless recursion */
2279 all = PyObject_GetAttrString(mod, "__all__");
2280 if (all == NULL)
2281 PyErr_Clear();
2282 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002283 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002284 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002285 if (!ret)
2286 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002287 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002288 continue;
2289 }
2290 hasit = PyObject_HasAttr(mod, item);
2291 if (!hasit) {
2292 char *subname = PyString_AS_STRING(item);
2293 PyObject *submod;
2294 char *p;
2295 if (buflen + strlen(subname) >= MAXPATHLEN) {
2296 PyErr_SetString(PyExc_ValueError,
2297 "Module name too long");
2298 Py_DECREF(item);
2299 return 0;
2300 }
2301 p = buf + buflen;
2302 *p++ = '.';
2303 strcpy(p, subname);
2304 submod = import_submodule(mod, subname, buf);
2305 Py_XDECREF(submod);
2306 if (submod == NULL) {
2307 Py_DECREF(item);
2308 return 0;
2309 }
2310 }
2311 Py_DECREF(item);
2312 }
2313
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002314 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002315}
2316
Neil Schemenauer00b09662003-06-16 21:03:07 +00002317static int
2318add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2319 PyObject *modules)
2320{
2321 if (mod == Py_None)
2322 return 1;
2323 /* Irrespective of the success of this load, make a
2324 reference to it in the parent package module. A copy gets
2325 saved in the modules dictionary under the full name, so get a
2326 reference from there, if need be. (The exception is when the
2327 load failed with a SyntaxError -- then there's no trace in
2328 sys.modules. In that case, of course, do nothing extra.) */
2329 if (submod == NULL) {
2330 submod = PyDict_GetItemString(modules, fullname);
2331 if (submod == NULL)
2332 return 1;
2333 }
2334 if (PyModule_Check(mod)) {
2335 /* We can't use setattr here since it can give a
2336 * spurious warning if the submodule name shadows a
2337 * builtin name */
2338 PyObject *dict = PyModule_GetDict(mod);
2339 if (!dict)
2340 return 0;
2341 if (PyDict_SetItemString(dict, subname, submod) < 0)
2342 return 0;
2343 }
2344 else {
2345 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2346 return 0;
2347 }
2348 return 1;
2349}
2350
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002351static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002352import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002353{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002354 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002355 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002356
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002357 /* Require:
2358 if mod == None: subname == fullname
2359 else: mod.__name__ + "." + subname == fullname
2360 */
2361
Tim Peters50d8d372001-02-28 05:34:27 +00002362 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002363 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002364 }
2365 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002366 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002367 char buf[MAXPATHLEN+1];
2368 struct filedescr *fdp;
2369 FILE *fp = NULL;
2370
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002371 if (mod == Py_None)
2372 path = NULL;
2373 else {
2374 path = PyObject_GetAttrString(mod, "__path__");
2375 if (path == NULL) {
2376 PyErr_Clear();
2377 Py_INCREF(Py_None);
2378 return Py_None;
2379 }
2380 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002381
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002382 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002383 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2384 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002385 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002386 if (fdp == NULL) {
2387 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2388 return NULL;
2389 PyErr_Clear();
2390 Py_INCREF(Py_None);
2391 return Py_None;
2392 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002393 m = load_module(fullname, fp, buf, fdp->type, loader);
2394 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002395 if (fp)
2396 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002397 if (!add_submodule(mod, m, fullname, subname, modules)) {
2398 Py_XDECREF(m);
2399 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002400 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002401 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002402
2403 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002404}
2405
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002406
2407/* Re-import a module of any kind and return its module object, WITH
2408 INCREMENTED REFERENCE COUNT */
2409
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002411PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002412{
Collin Winter47c52a82007-03-13 23:02:15 +00002413 PyInterpreterState *interp = PyThreadState_Get()->interp;
2414 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002415 PyObject *modules = PyImport_GetModuleDict();
Collin Winter276887b2007-03-12 16:11:39 +00002416 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002417 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002418 char buf[MAXPATHLEN+1];
2419 struct filedescr *fdp;
2420 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002421 PyObject *newm;
Collin Winter47c52a82007-03-13 23:02:15 +00002422
2423 if (modules_reloading == NULL) {
2424 Py_FatalError("PyImport_ReloadModule: "
2425 "no modules_reloading dictionary!");
2426 return NULL;
2427 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002428
Guido van Rossum79f25d91997-04-29 20:08:16 +00002429 if (m == NULL || !PyModule_Check(m)) {
2430 PyErr_SetString(PyExc_TypeError,
2431 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002432 return NULL;
2433 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002435 if (name == NULL)
2436 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002437 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002438 PyErr_Format(PyExc_ImportError,
2439 "reload(): module %.200s not in sys.modules",
2440 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441 return NULL;
2442 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002443 existing_m = PyDict_GetItemString(modules_reloading, name);
2444 if (existing_m != NULL) {
2445 /* Due to a recursive reload, this module is already
2446 being reloaded. */
2447 Py_INCREF(existing_m);
2448 return existing_m;
Collin Winter276887b2007-03-12 16:11:39 +00002449 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002450 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2451 return NULL;
Collin Winter276887b2007-03-12 16:11:39 +00002452
Guido van Rossum222ef561997-09-06 19:41:09 +00002453 subname = strrchr(name, '.');
2454 if (subname == NULL)
2455 subname = name;
2456 else {
2457 PyObject *parentname, *parent;
2458 parentname = PyString_FromStringAndSize(name, (subname-name));
Collin Winter276887b2007-03-12 16:11:39 +00002459 if (parentname == NULL) {
2460 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002461 return NULL;
Neal Norwitz75c7c802007-03-13 05:31:38 +00002462 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002463 parent = PyDict_GetItem(modules, parentname);
2464 if (parent == NULL) {
2465 PyErr_Format(PyExc_ImportError,
2466 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002467 PyString_AS_STRING(parentname));
2468 Py_DECREF(parentname);
Collin Winter276887b2007-03-12 16:11:39 +00002469 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002470 return NULL;
2471 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002472 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002473 subname++;
2474 path = PyObject_GetAttrString(parent, "__path__");
2475 if (path == NULL)
2476 PyErr_Clear();
2477 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002478 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002479 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002480 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002481
2482 if (fdp == NULL) {
2483 Py_XDECREF(loader);
Collin Winter276887b2007-03-12 16:11:39 +00002484 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002485 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002486 }
2487
2488 newm = load_module(name, fp, buf, fdp->type, loader);
2489 Py_XDECREF(loader);
2490
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002491 if (fp)
2492 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002493 if (newm == NULL) {
2494 /* load_module probably removed name from modules because of
2495 * the error. Put back the original module object. We're
2496 * going to return NULL in this case regardless of whether
2497 * replacing name succeeds, so the return value is ignored.
2498 */
2499 PyDict_SetItemString(modules, name, m);
2500 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002501 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002502 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002503}
2504
2505
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002506/* Higher-level import emulator which emulates the "import" statement
2507 more accurately -- it invokes the __import__() function from the
2508 builtins of the current globals. This means that the import is
2509 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002510 environment, e.g. by "rexec".
2511 A dummy list ["__doc__"] is passed as the 4th argument so that
2512 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2513 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002514
2515PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002516PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002517{
2518 static PyObject *silly_list = NULL;
2519 static PyObject *builtins_str = NULL;
2520 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002521 PyObject *globals = NULL;
2522 PyObject *import = NULL;
2523 PyObject *builtins = NULL;
2524 PyObject *r = NULL;
2525
2526 /* Initialize constant string objects */
2527 if (silly_list == NULL) {
2528 import_str = PyString_InternFromString("__import__");
2529 if (import_str == NULL)
2530 return NULL;
2531 builtins_str = PyString_InternFromString("__builtins__");
2532 if (builtins_str == NULL)
2533 return NULL;
2534 silly_list = Py_BuildValue("[s]", "__doc__");
2535 if (silly_list == NULL)
2536 return NULL;
2537 }
2538
2539 /* Get the builtins from current globals */
2540 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002541 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002542 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002543 builtins = PyObject_GetItem(globals, builtins_str);
2544 if (builtins == NULL)
2545 goto err;
2546 }
2547 else {
2548 /* No globals -- use standard builtins, and fake globals */
2549 PyErr_Clear();
2550
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002551 builtins = PyImport_ImportModuleLevel("__builtin__",
2552 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002553 if (builtins == NULL)
2554 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002555 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2556 if (globals == NULL)
2557 goto err;
2558 }
2559
2560 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002561 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002562 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002563 if (import == NULL)
2564 PyErr_SetObject(PyExc_KeyError, import_str);
2565 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002566 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002567 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002568 if (import == NULL)
2569 goto err;
2570
Georg Brandl74780962007-03-10 07:38:14 +00002571 /* Call the __import__ function with the proper argument list */
Georg Brandl684fd0c2006-05-25 19:15:31 +00002572 r = PyObject_CallFunctionObjArgs(import, module_name, globals,
2573 globals, silly_list, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002574
2575 err:
2576 Py_XDECREF(globals);
2577 Py_XDECREF(builtins);
2578 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002579
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002580 return r;
2581}
2582
2583
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002584/* Module 'imp' provides Python access to the primitives used for
2585 importing modules.
2586*/
2587
Guido van Rossum79f25d91997-04-29 20:08:16 +00002588static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002589imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002590{
2591 char buf[4];
2592
Guido van Rossum96774c12000-05-01 20:19:08 +00002593 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2594 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2595 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2596 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002597
Guido van Rossum79f25d91997-04-29 20:08:16 +00002598 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002599}
2600
Guido van Rossum79f25d91997-04-29 20:08:16 +00002601static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002602imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002603{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002604 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002605 struct filedescr *fdp;
2606
Guido van Rossum79f25d91997-04-29 20:08:16 +00002607 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002608 if (list == NULL)
2609 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002610 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2611 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002612 fdp->suffix, fdp->mode, fdp->type);
2613 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002615 return NULL;
2616 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002617 if (PyList_Append(list, item) < 0) {
2618 Py_DECREF(list);
2619 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002620 return NULL;
2621 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002622 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002623 }
2624 return list;
2625}
2626
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002628call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002629{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002630 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002631 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002632 struct filedescr *fdp;
2633 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002634 FILE *fp = NULL;
2635
2636 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002637 if (path == Py_None)
2638 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002639 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002640 if (fdp == NULL)
2641 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002642 if (fp != NULL) {
2643 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2644 if (fob == NULL) {
2645 fclose(fp);
2646 return NULL;
2647 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002648 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002649 else {
2650 fob = Py_None;
2651 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002652 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002654 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002655 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656 return ret;
2657}
2658
Guido van Rossum79f25d91997-04-29 20:08:16 +00002659static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002660imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002661{
2662 char *name;
2663 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002664 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002665 return NULL;
2666 return call_find_module(name, path);
2667}
2668
2669static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002670imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002671{
2672 char *name;
2673 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002674 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002675 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002676 return NULL;
2677 ret = init_builtin(name);
2678 if (ret < 0)
2679 return NULL;
2680 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002681 Py_INCREF(Py_None);
2682 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002683 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002684 m = PyImport_AddModule(name);
2685 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002686 return m;
2687}
2688
Guido van Rossum79f25d91997-04-29 20:08:16 +00002689static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002690imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002691{
2692 char *name;
2693 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002695 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002696 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002697 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002698 if (ret < 0)
2699 return NULL;
2700 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002701 Py_INCREF(Py_None);
2702 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002703 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002704 m = PyImport_AddModule(name);
2705 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002706 return m;
2707}
2708
Guido van Rossum79f25d91997-04-29 20:08:16 +00002709static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002710imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002711{
2712 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002713
Guido van Rossum43713e52000-02-29 13:59:29 +00002714 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002715 return NULL;
2716 return get_frozen_object(name);
2717}
2718
Guido van Rossum79f25d91997-04-29 20:08:16 +00002719static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002720imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002721{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002722 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002723 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002724 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002725 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002726}
2727
Guido van Rossum79f25d91997-04-29 20:08:16 +00002728static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002729imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002730{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002731 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002732 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002733 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002734 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002735 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002736 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002737}
2738
2739static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002740get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002741{
2742 FILE *fp;
2743 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002744 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002745 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002746 fp = fopen(pathname, mode);
2747 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002748 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002749 }
2750 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002751 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002752 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002753 PyErr_SetString(PyExc_ValueError,
2754 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002755 }
2756 return fp;
2757}
2758
Guido van Rossum79f25d91997-04-29 20:08:16 +00002759static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002760imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002761{
2762 char *name;
2763 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002764 PyObject *fob = NULL;
2765 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002766 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002767 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002768 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002769 return NULL;
2770 fp = get_file(pathname, fob, "rb");
2771 if (fp == NULL)
2772 return NULL;
2773 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002774 if (fob == NULL)
2775 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002776 return m;
2777}
2778
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002779#ifdef HAVE_DYNAMIC_LOADING
2780
Guido van Rossum79f25d91997-04-29 20:08:16 +00002781static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002782imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002783{
2784 char *name;
2785 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002786 PyObject *fob = NULL;
2787 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002788 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002789 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002790 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002791 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002792 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002793 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002794 if (fp == NULL)
2795 return NULL;
2796 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002797 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002798 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002799}
2800
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002801#endif /* HAVE_DYNAMIC_LOADING */
2802
Guido van Rossum79f25d91997-04-29 20:08:16 +00002803static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002804imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002805{
2806 char *name;
2807 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002808 PyObject *fob = NULL;
2809 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002810 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002811 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002812 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002813 return NULL;
2814 fp = get_file(pathname, fob, "r");
2815 if (fp == NULL)
2816 return NULL;
2817 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002818 if (fob == NULL)
2819 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002820 return m;
2821}
2822
Guido van Rossum79f25d91997-04-29 20:08:16 +00002823static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002824imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002825{
2826 char *name;
2827 PyObject *fob;
2828 char *pathname;
2829 char *suffix; /* Unused */
2830 char *mode;
2831 int type;
2832 FILE *fp;
2833
Guido van Rossum43713e52000-02-29 13:59:29 +00002834 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002835 &name, &fob, &pathname,
2836 &suffix, &mode, &type))
2837 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002838 if (*mode) {
2839 /* Mode must start with 'r' or 'U' and must not contain '+'.
2840 Implicit in this test is the assumption that the mode
2841 may contain other modifiers like 'b' or 't'. */
2842
2843 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002844 PyErr_Format(PyExc_ValueError,
2845 "invalid file open mode %.200s", mode);
2846 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002847 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002848 }
2849 if (fob == Py_None)
2850 fp = NULL;
2851 else {
2852 if (!PyFile_Check(fob)) {
2853 PyErr_SetString(PyExc_ValueError,
2854 "load_module arg#2 should be a file or None");
2855 return NULL;
2856 }
2857 fp = get_file(pathname, fob, mode);
2858 if (fp == NULL)
2859 return NULL;
2860 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002861 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002862}
2863
2864static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002865imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002866{
2867 char *name;
2868 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002869 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002870 return NULL;
2871 return load_package(name, pathname);
2872}
2873
2874static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002875imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002876{
2877 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002878 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002879 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002880 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002881}
2882
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002883/* Doc strings */
2884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002885PyDoc_STRVAR(doc_imp,
2886"This module provides the components needed to build your own\n\
2887__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002889PyDoc_STRVAR(doc_find_module,
2890"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002891Search for a module. If path is omitted or None, search for a\n\
2892built-in, frozen or special module and continue search in sys.path.\n\
2893The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002894package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002896PyDoc_STRVAR(doc_load_module,
2897"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002898Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002899The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002901PyDoc_STRVAR(doc_get_magic,
2902"get_magic() -> string\n\
2903Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002905PyDoc_STRVAR(doc_get_suffixes,
2906"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002907Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002908that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002910PyDoc_STRVAR(doc_new_module,
2911"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002912Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002916"lock_held() -> boolean\n\
2917Return True if the import lock is currently held, else False.\n\
2918On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002919
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002920PyDoc_STRVAR(doc_acquire_lock,
2921"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002922Acquires the interpreter's import lock for the current thread.\n\
2923This lock should be used by import hooks to ensure thread-safety\n\
2924when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002925On platforms without threads, this function does nothing.");
2926
2927PyDoc_STRVAR(doc_release_lock,
2928"release_lock() -> None\n\
2929Release the interpreter's import lock.\n\
2930On platforms without threads, this function does nothing.");
2931
Guido van Rossum79f25d91997-04-29 20:08:16 +00002932static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002933 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2934 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2935 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2936 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2937 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2938 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2939 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2940 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002941 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002942 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2943 {"init_builtin", imp_init_builtin, METH_VARARGS},
2944 {"init_frozen", imp_init_frozen, METH_VARARGS},
2945 {"is_builtin", imp_is_builtin, METH_VARARGS},
2946 {"is_frozen", imp_is_frozen, METH_VARARGS},
2947 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002948#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002949 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002950#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002951 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002952 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002953 {NULL, NULL} /* sentinel */
2954};
2955
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002956static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002957setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002958{
2959 PyObject *v;
2960 int err;
2961
2962 v = PyInt_FromLong((long)value);
2963 err = PyDict_SetItemString(d, name, v);
2964 Py_XDECREF(v);
2965 return err;
2966}
2967
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00002968typedef struct {
2969 PyObject_HEAD
2970} NullImporter;
2971
2972static int
2973NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
2974{
2975 char *path;
2976
2977 if (!_PyArg_NoKeywords("NullImporter()", kwds))
2978 return -1;
2979
2980 if (!PyArg_ParseTuple(args, "s:NullImporter",
2981 &path))
2982 return -1;
2983
2984 if (strlen(path) == 0) {
2985 PyErr_SetString(PyExc_ImportError, "empty pathname");
2986 return -1;
2987 } else {
2988#ifndef RISCOS
2989 struct stat statbuf;
2990 int rv;
2991
2992 rv = stat(path, &statbuf);
2993 if (rv == 0) {
2994 /* it exists */
2995 if (S_ISDIR(statbuf.st_mode)) {
2996 /* it's a directory */
2997 PyErr_SetString(PyExc_ImportError,
2998 "existing directory");
2999 return -1;
3000 }
3001 }
3002#else
3003 if (object_exists(path)) {
3004 /* it exists */
3005 if (isdir(path)) {
3006 /* it's a directory */
3007 PyErr_SetString(PyExc_ImportError,
3008 "existing directory");
3009 return -1;
3010 }
3011 }
3012#endif
3013 }
3014 return 0;
3015}
3016
3017static PyObject *
3018NullImporter_find_module(NullImporter *self, PyObject *args)
3019{
3020 Py_RETURN_NONE;
3021}
3022
3023static PyMethodDef NullImporter_methods[] = {
3024 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3025 "Always return None"
3026 },
3027 {NULL} /* Sentinel */
3028};
3029
3030
3031static PyTypeObject NullImporterType = {
3032 PyObject_HEAD_INIT(NULL)
3033 0, /*ob_size*/
3034 "imp.NullImporter", /*tp_name*/
3035 sizeof(NullImporter), /*tp_basicsize*/
3036 0, /*tp_itemsize*/
3037 0, /*tp_dealloc*/
3038 0, /*tp_print*/
3039 0, /*tp_getattr*/
3040 0, /*tp_setattr*/
3041 0, /*tp_compare*/
3042 0, /*tp_repr*/
3043 0, /*tp_as_number*/
3044 0, /*tp_as_sequence*/
3045 0, /*tp_as_mapping*/
3046 0, /*tp_hash */
3047 0, /*tp_call*/
3048 0, /*tp_str*/
3049 0, /*tp_getattro*/
3050 0, /*tp_setattro*/
3051 0, /*tp_as_buffer*/
3052 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3053 "Null importer object", /* tp_doc */
3054 0, /* tp_traverse */
3055 0, /* tp_clear */
3056 0, /* tp_richcompare */
3057 0, /* tp_weaklistoffset */
3058 0, /* tp_iter */
3059 0, /* tp_iternext */
3060 NullImporter_methods, /* tp_methods */
3061 0, /* tp_members */
3062 0, /* tp_getset */
3063 0, /* tp_base */
3064 0, /* tp_dict */
3065 0, /* tp_descr_get */
3066 0, /* tp_descr_set */
3067 0, /* tp_dictoffset */
3068 (initproc)NullImporter_init, /* tp_init */
3069 0, /* tp_alloc */
3070 PyType_GenericNew /* tp_new */
3071};
3072
3073
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003074PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003075initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003077 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003078
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003079 if (PyType_Ready(&NullImporterType) < 0)
3080 goto failure;
3081
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003082 m = Py_InitModule4("imp", imp_methods, doc_imp,
3083 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003084 if (m == NULL)
3085 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003086 d = PyModule_GetDict(m);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00003087 if (d == NULL)
3088 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003089
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003090 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3091 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3092 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3093 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3094 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3095 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3096 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3097 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003098 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003099 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003100
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003101 Py_INCREF(&NullImporterType);
3102 PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003103 failure:
3104 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003105}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003106
3107
Guido van Rossumb18618d2000-05-03 23:44:39 +00003108/* API for embedding applications that want to add their own entries
3109 to the table of built-in modules. This should normally be called
3110 *before* Py_Initialize(). When the table resize fails, -1 is
3111 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003112
3113 After a similar function by Just van Rossum. */
3114
3115int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003116PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003117{
3118 static struct _inittab *our_copy = NULL;
3119 struct _inittab *p;
3120 int i, n;
3121
3122 /* Count the number of entries in both tables */
3123 for (n = 0; newtab[n].name != NULL; n++)
3124 ;
3125 if (n == 0)
3126 return 0; /* Nothing to do */
3127 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3128 ;
3129
3130 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003131 p = our_copy;
3132 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003133 if (p == NULL)
3134 return -1;
3135
3136 /* Copy the tables into the new memory */
3137 if (our_copy != PyImport_Inittab)
3138 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3139 PyImport_Inittab = our_copy = p;
3140 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3141
3142 return 0;
3143}
3144
3145/* Shorthand to add a single entry given a name and a function */
3146
3147int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003148PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003149{
3150 struct _inittab newtab[2];
3151
3152 memset(newtab, '\0', sizeof newtab);
3153
3154 newtab[0].name = name;
3155 newtab[0].initfunc = initfunc;
3156
3157 return PyImport_ExtendInittab(newtab);
3158}
Anthony Baxterac6bd462006-04-13 02:06:09 +00003159
3160#ifdef __cplusplus
3161}
3162#endif