blob: 7d274569e7befe4135d567f41483e7514ffd9d9d [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Guido van Rossum45aecf42006-03-15 04:58:47 +000034 The magic numbers must be spaced apart at least 2 values, as the
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000035 -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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
64 Python 2.5b3: 62111 (fix wrong code: x += yield)
65 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
66 storing constants that should have been removed)
Thomas Wouters89f507f2006-12-13 04:49:30 +000067 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
68 Python 2.6a0: 62141 (peephole optimizations)
Guido van Rossum45aecf42006-03-15 04:58:47 +000069 Python 3000: 3000
Brett Cannone2e23ef2006-08-25 05:05:30 +000070 3010 (removed UNARY_CONVERT)
Guido van Rossum86e58e22006-08-28 15:27:34 +000071 3020 (added BUILD_SET)
Guido van Rossum4f72a782006-10-27 23:31:49 +000072 3030 (added keyword-only parameters)
Guido van Rossum3203bdc2006-12-28 21:03:31 +000073 3040 (added signature annotations)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000074.
Tim Peters36515e22001-11-18 04:06:29 +000075*/
Guido van Rossum3203bdc2006-12-28 21:03:31 +000076#define MAGIC (3040 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000077
Guido van Rossum96774c12000-05-01 20:19:08 +000078/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000079 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000080 compiler works which are enabled by command line switches. */
81static long pyc_magic = MAGIC;
82
Guido van Rossum25ce5661997-08-02 03:10:38 +000083/* See _PyImport_FixupExtension() below */
84static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000085
Guido van Rossum771c6c81997-10-31 18:37:24 +000086/* This table is defined in config.c: */
87extern struct _inittab _PyImport_Inittab[];
88
89struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000090
Guido van Rossumed1170e1999-12-20 21:23:41 +000091/* these tables define the module suffixes that Python recognizes */
92struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000093
94#ifdef RISCOS
95static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000096 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000097 {"/pyc", "rb", PY_COMPILED},
98 {0, 0}
99};
100#else
Guido van Rossumed1170e1999-12-20 21:23:41 +0000101static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000102 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000103#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000104 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000105#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000106 {".pyc", "rb", PY_COMPILED},
107 {0, 0}
108};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000109#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000110
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111static PyTypeObject NullImporterType; /* Forward reference */
112
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000113/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
115void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000118 const struct filedescr *scan;
119 struct filedescr *filetab;
120 int countD = 0;
121 int countS = 0;
122
123 /* prepare _PyImport_Filetab: copy entries from
124 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
125 */
126 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
127 ++countD;
128 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
129 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000131 if (filetab == NULL)
132 Py_FatalError("Can't initialize import file table.");
Guido van Rossumed1170e1999-12-20 21:23:41 +0000133 memcpy(filetab, _PyImport_DynLoadFiletab,
134 countD * sizeof(struct filedescr));
135 memcpy(filetab + countD, _PyImport_StandardFiletab,
136 countS * sizeof(struct filedescr));
137 filetab[countD + countS].suffix = NULL;
138
139 _PyImport_Filetab = filetab;
140
Guido van Rossum0824f631997-03-11 18:37:35 +0000141 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000142 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
143 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000144#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000145 if (strcmp(filetab->suffix, ".pyc") == 0)
146 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000147#else
148 if (strcmp(filetab->suffix, "/pyc") == 0)
149 filetab->suffix = "/pyo";
150#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000151 }
152 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000153
154 if (Py_UnicodeFlag) {
155 /* Fix the pyc_magic so that byte compiled code created
156 using the all-Unicode method doesn't interfere with
157 code created in normal operation mode. */
158 pyc_magic = MAGIC + 1;
159 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162void
Just van Rossum52e14d62002-12-30 22:08:05 +0000163_PyImportHooks_Init(void)
164{
165 PyObject *v, *path_hooks = NULL, *zimpimport;
166 int err = 0;
167
168 /* adding sys.path_hooks and sys.path_importer_cache, setting up
169 zipimport */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000170 if (PyType_Ready(&NullImporterType) < 0)
171 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000172
173 if (Py_VerboseFlag)
174 PySys_WriteStderr("# installing zipimport hook\n");
175
176 v = PyList_New(0);
177 if (v == NULL)
178 goto error;
179 err = PySys_SetObject("meta_path", v);
180 Py_DECREF(v);
181 if (err)
182 goto error;
183 v = PyDict_New();
184 if (v == NULL)
185 goto error;
186 err = PySys_SetObject("path_importer_cache", v);
187 Py_DECREF(v);
188 if (err)
189 goto error;
190 path_hooks = PyList_New(0);
191 if (path_hooks == NULL)
192 goto error;
193 err = PySys_SetObject("path_hooks", path_hooks);
194 if (err) {
195 error:
196 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000197 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
198 "path_importer_cache, or NullImporter failed"
199 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000200 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000201
Just van Rossum52e14d62002-12-30 22:08:05 +0000202 zimpimport = PyImport_ImportModule("zipimport");
203 if (zimpimport == NULL) {
204 PyErr_Clear(); /* No zip import module -- okay */
205 if (Py_VerboseFlag)
206 PySys_WriteStderr("# can't import zipimport\n");
207 }
208 else {
209 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
210 "zipimporter");
211 Py_DECREF(zimpimport);
212 if (zipimporter == NULL) {
213 PyErr_Clear(); /* No zipimporter object -- okay */
214 if (Py_VerboseFlag)
215 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000216 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000217 }
218 else {
219 /* sys.path_hooks.append(zipimporter) */
220 err = PyList_Append(path_hooks, zipimporter);
221 Py_DECREF(zipimporter);
222 if (err)
223 goto error;
224 if (Py_VerboseFlag)
225 PySys_WriteStderr(
226 "# installed zipimport hook\n");
227 }
228 }
229 Py_DECREF(path_hooks);
230}
231
232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234{
235 Py_XDECREF(extensions);
236 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000237 PyMem_DEL(_PyImport_Filetab);
238 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239}
240
241
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000242/* Locking primitives to prevent parallel imports of the same module
243 in different threads to return with a partially loaded module.
244 These calls are serialized by the global interpreter lock. */
245
246#ifdef WITH_THREAD
247
Guido van Rossum49b56061998-10-01 20:42:43 +0000248#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251static long import_lock_thread = -1;
252static int import_lock_level = 0;
253
254static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258 if (me == -1)
259 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262 if (import_lock == NULL)
263 return; /* Nothing much we can do. */
264 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000265 if (import_lock_thread == me) {
266 import_lock_level++;
267 return;
268 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000269 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
270 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000271 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000273 PyEval_RestoreThread(tstate);
274 }
275 import_lock_thread = me;
276 import_lock_level = 1;
277}
278
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000281{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000282 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000284 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000285 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000287 import_lock_level--;
288 if (import_lock_level == 0) {
289 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000290 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000291 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000293}
294
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000295/* This function is called from PyOS_AfterFork to ensure that newly
296 created child processes do not share locks with the parent. */
297
298void
299_PyImport_ReInitLock(void)
300{
301#ifdef _AIX
302 if (import_lock != NULL)
303 import_lock = PyThread_allocate_lock();
304#endif
305}
306
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000307#else
308
309#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000310#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000311
312#endif
313
Tim Peters69232342001-08-30 05:16:13 +0000314static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000315imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000316{
Tim Peters69232342001-08-30 05:16:13 +0000317#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000318 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000319#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000320 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000321#endif
322}
323
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000324static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000325imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000326{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327#ifdef WITH_THREAD
328 lock_import();
329#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000330 Py_INCREF(Py_None);
331 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332}
333
334static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000335imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000336{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000337#ifdef WITH_THREAD
338 if (unlock_import() < 0) {
339 PyErr_SetString(PyExc_RuntimeError,
340 "not holding the import lock");
341 return NULL;
342 }
343#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000344 Py_INCREF(Py_None);
345 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000346}
347
Guido van Rossum25ce5661997-08-02 03:10:38 +0000348/* Helper for sys */
349
350PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000353 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000354 if (interp->modules == NULL)
355 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
356 return interp->modules;
357}
358
Guido van Rossum3f5da241990-12-20 15:06:42 +0000359
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000360/* List of names to clear in sys */
361static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000362 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000363 "exc_type", "exc_value", "exc_traceback",
364 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000365 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000366 NULL
367};
368
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000369static char* sys_files[] = {
370 "stdin", "__stdin__",
371 "stdout", "__stdout__",
372 "stderr", "__stderr__",
373 NULL
374};
375
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000376
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000377/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000382 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000383 char *name;
384 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000385 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000386 PyObject *modules = interp->modules;
387
388 if (modules == NULL)
389 return; /* Already done */
390
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000391 /* Delete some special variables first. These are common
392 places where user values hide and people complain when their
393 destructors fail. Since the modules containing them are
394 deleted *last* of all, they would come too late in the normal
395 destruction order. Sigh. */
396
397 value = PyDict_GetItemString(modules, "__builtin__");
398 if (value != NULL && PyModule_Check(value)) {
399 dict = PyModule_GetDict(value);
400 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000401 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000402 PyDict_SetItemString(dict, "_", Py_None);
403 }
404 value = PyDict_GetItemString(modules, "sys");
405 if (value != NULL && PyModule_Check(value)) {
406 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000407 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000408 dict = PyModule_GetDict(value);
409 for (p = sys_deletes; *p != NULL; p++) {
410 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000411 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000412 PyDict_SetItemString(dict, *p, Py_None);
413 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000414 for (p = sys_files; *p != NULL; p+=2) {
415 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000416 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000417 v = PyDict_GetItemString(dict, *(p+1));
418 if (v == NULL)
419 v = Py_None;
420 PyDict_SetItemString(dict, *p, v);
421 }
422 }
423
424 /* First, delete __main__ */
425 value = PyDict_GetItemString(modules, "__main__");
426 if (value != NULL && PyModule_Check(value)) {
427 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000428 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000429 _PyModule_Clear(value);
430 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000431 }
432
Guido van Rossum758eec01998-01-19 21:58:26 +0000433 /* The special treatment of __builtin__ here is because even
434 when it's not referenced as a module, its dictionary is
435 referenced by almost every module's __builtins__. Since
436 deleting a module clears its dictionary (even if there are
437 references left to it), we need to delete the __builtin__
438 module last. Likewise, we don't delete sys until the very
439 end because it is implicitly referenced (e.g. by print).
440
441 Also note that we 'delete' modules by replacing their entry
442 in the modules dict with None, rather than really deleting
443 them; this avoids a rehash of the modules dictionary and
444 also marks them as "non existent" so they won't be
445 re-imported. */
446
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000447 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000448 one (skipping __builtin__ and sys) and delete them */
449 do {
450 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000451 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000452 while (PyDict_Next(modules, &pos, &key, &value)) {
453 if (value->ob_refcnt != 1)
454 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000455 if (PyString_Check(key) && PyModule_Check(value)) {
456 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000457 if (strcmp(name, "__builtin__") == 0)
458 continue;
459 if (strcmp(name, "sys") == 0)
460 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000461 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000462 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000463 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000464 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000465 PyDict_SetItem(modules, key, Py_None);
466 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000467 }
468 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000469 } while (ndone > 0);
470
Guido van Rossum758eec01998-01-19 21:58:26 +0000471 /* Next, delete all modules (still skipping __builtin__ and sys) */
472 pos = 0;
473 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000474 if (PyString_Check(key) && PyModule_Check(value)) {
475 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000476 if (strcmp(name, "__builtin__") == 0)
477 continue;
478 if (strcmp(name, "sys") == 0)
479 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000480 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000481 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000482 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000483 PyDict_SetItem(modules, key, Py_None);
484 }
485 }
486
487 /* Next, delete sys and __builtin__ (in that order) */
488 value = PyDict_GetItemString(modules, "sys");
489 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000490 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000491 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000492 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000493 PyDict_SetItemString(modules, "sys", Py_None);
494 }
495 value = PyDict_GetItemString(modules, "__builtin__");
496 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000497 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000498 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000499 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000500 PyDict_SetItemString(modules, "__builtin__", Py_None);
501 }
502
503 /* Finally, clear and delete the modules directory */
504 PyDict_Clear(modules);
505 interp->modules = NULL;
506 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000507}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000508
509
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000510/* Helper for pythonrun.c -- return magic number */
511
512long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000513PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000514{
Guido van Rossum96774c12000-05-01 20:19:08 +0000515 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516}
517
518
Guido van Rossum25ce5661997-08-02 03:10:38 +0000519/* Magic for extension modules (built-in as well as dynamically
520 loaded). To prevent initializing an extension module more than
521 once, we keep a static dictionary 'extensions' keyed by module name
522 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000523 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000524 dictionary is stored by calling _PyImport_FixupExtension()
525 immediately after the module initialization function succeeds. A
526 copy can be retrieved from there by calling
527 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000528
Guido van Rossum79f25d91997-04-29 20:08:16 +0000529PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000531{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000532 PyObject *modules, *mod, *dict, *copy;
533 if (extensions == NULL) {
534 extensions = PyDict_New();
535 if (extensions == NULL)
536 return NULL;
537 }
538 modules = PyImport_GetModuleDict();
539 mod = PyDict_GetItemString(modules, name);
540 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000541 PyErr_Format(PyExc_SystemError,
542 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 return NULL;
544 }
545 dict = PyModule_GetDict(mod);
546 if (dict == NULL)
547 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000548 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000549 if (copy == NULL)
550 return NULL;
551 PyDict_SetItemString(extensions, filename, copy);
552 Py_DECREF(copy);
553 return copy;
554}
555
556PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000557_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000558{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000559 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 if (extensions == NULL)
561 return NULL;
562 dict = PyDict_GetItemString(extensions, filename);
563 if (dict == NULL)
564 return NULL;
565 mod = PyImport_AddModule(name);
566 if (mod == NULL)
567 return NULL;
568 mdict = PyModule_GetDict(mod);
569 if (mdict == NULL)
570 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000571 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000572 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000573 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000574 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000575 name, filename);
576 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577}
578
579
580/* Get the module object corresponding to a module name.
581 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000582 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000583 Because the former action is most common, THIS DOES NOT RETURN A
584 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000587PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000591
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 if (m == NULL)
597 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600 return NULL;
601 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603
604 return m;
605}
606
Tim Peters1cd70172004-08-02 03:52:12 +0000607/* Remove name from sys.modules, if it's there. */
608static void
609_RemoveModule(const char *name)
610{
611 PyObject *modules = PyImport_GetModuleDict();
612 if (PyDict_GetItemString(modules, name) == NULL)
613 return;
614 if (PyDict_DelItemString(modules, name) < 0)
615 Py_FatalError("import: deleting existing key in"
616 "sys.modules failed");
617}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000619/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000620 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
621 * removed from sys.modules, to avoid leaving damaged module objects
622 * in sys.modules. The caller may wish to restore the original
623 * module object (if any) in this case; PyImport_ReloadModule is an
624 * example.
625 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000627PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000629 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
630}
631
632PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000633PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000634{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000635 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000637
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000639 if (m == NULL)
640 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000641 /* If the module is being reloaded, we get the old module back
642 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 d = PyModule_GetDict(m);
644 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
645 if (PyDict_SetItemString(d, "__builtins__",
646 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000647 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000648 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000649 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000650 v = NULL;
651 if (pathname != NULL) {
652 v = PyString_FromString(pathname);
653 if (v == NULL)
654 PyErr_Clear();
655 }
656 if (v == NULL) {
657 v = ((PyCodeObject *)co)->co_filename;
658 Py_INCREF(v);
659 }
660 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000662 Py_DECREF(v);
663
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000664 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000666 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000668
Guido van Rossum25ce5661997-08-02 03:10:38 +0000669 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000670 PyErr_Format(PyExc_ImportError,
671 "Loaded module %.200s not found in sys.modules",
672 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000673 return NULL;
674 }
675
Guido van Rossum79f25d91997-04-29 20:08:16 +0000676 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000677
678 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000679
680 error:
681 _RemoveModule(name);
682 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683}
684
685
686/* Given a pathname for a Python source file, fill a buffer with the
687 pathname for the corresponding compiled file. Return the pathname
688 for the compiled file, or NULL if there's no space in the buffer.
689 Doesn't set an exception. */
690
691static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000692make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693{
Tim Petersc1731372001-08-04 08:12:36 +0000694 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 if (len+2 > buflen)
696 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000697
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000698#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000699 /* Treat .pyw as if it were .py. The case of ".pyw" must match
700 that used in _PyImport_StandardFiletab. */
701 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
702 --len; /* pretend 'w' isn't there */
703#endif
704 memcpy(buf, pathname, len);
705 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
706 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
708 return buf;
709}
710
711
712/* Given a pathname for a Python source file, its time of last
713 modification, and a pathname for a compiled file, check whether the
714 compiled file represents the same version of the source. If so,
715 return a FILE pointer for the compiled file, positioned just after
716 the header; if not, return NULL.
717 Doesn't set an exception. */
718
719static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000720check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721{
722 FILE *fp;
723 long magic;
724 long pyc_mtime;
725
726 fp = fopen(cpathname, "rb");
727 if (fp == NULL)
728 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000730 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000732 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000733 fclose(fp);
734 return NULL;
735 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000739 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740 fclose(fp);
741 return NULL;
742 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000744 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745 return fp;
746}
747
748
749/* Read a code object from a file and check it for validity */
750
Guido van Rossum79f25d91997-04-29 20:08:16 +0000751static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000752read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000753{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755
Tim Petersd9b9ac82001-01-28 00:27:39 +0000756 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000757 if (co == NULL)
758 return NULL;
759 if (!PyCode_Check(co)) {
760 PyErr_Format(PyExc_ImportError,
761 "Non-code object in %.200s", cpathname);
762 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763 return NULL;
764 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766}
767
768
769/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000770 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000773load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774{
775 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 PyCodeObject *co;
777 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778
Guido van Rossum79f25d91997-04-29 20:08:16 +0000779 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000780 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000781 PyErr_Format(PyExc_ImportError,
782 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783 return NULL;
784 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000786 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000787 if (co == NULL)
788 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000790 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000791 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000792 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000794
795 return m;
796}
797
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798/* Parse a source file and return the corresponding code object */
799
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 PyCodeObject *co = NULL;
804 mod_ty mod;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000805 PyArena *arena = PyArena_New();
806 if (arena == NULL)
807 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000810 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000812 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000814 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815 return co;
816}
817
818
Guido van Rossum55a83382000-09-20 20:31:38 +0000819/* Helper to open a bytecode file for writing in exclusive mode */
820
821static FILE *
822open_exclusive(char *filename)
823{
824#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
825 /* Use O_EXCL to avoid a race condition when another process tries to
826 write the same file. When that happens, our open() call fails,
827 which is just fine (since it's only a cache).
828 XXX If the file exists and is writable but the directory is not
829 writable, the file will never be written. Oh well.
830 */
831 int fd;
832 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000833 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
834#ifdef O_BINARY
835 |O_BINARY /* necessary for Windows */
836#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000837#ifdef __VMS
Martin v. Löwis18e16552006-02-15 17:27:45 +0000838 , 0666, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000839#else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000840 , 0666
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000841#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000843 if (fd < 0)
844 return NULL;
845 return fdopen(fd, "wb");
846#else
847 /* Best we can do -- on Windows this can't happen anyway */
848 return fopen(filename, "wb");
849#endif
850}
851
852
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853/* Write a compiled module to a file, placing the time of last
854 modification of its source into the header.
855 Errors are ignored, if a write error occurs an attempt is made to
856 remove the file. */
857
858static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000859write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860{
861 FILE *fp;
862
Guido van Rossum55a83382000-09-20 20:31:38 +0000863 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000866 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000867 "# can't create %s\n", cpathname);
868 return;
869 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000870 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000872 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
873 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000874 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000875 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000876 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000877 /* Don't keep partial file */
878 fclose(fp);
879 (void) unlink(cpathname);
880 return;
881 }
882 /* Now write the true mtime */
883 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000884 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000885 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886 fflush(fp);
887 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000889 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890}
891
892
893/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000894 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
895 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000896
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000898load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000899{
Fred Drake4c82b232000-06-30 16:18:57 +0000900 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901 FILE *fpc;
902 char buf[MAXPATHLEN+1];
903 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 PyCodeObject *co;
905 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000906
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000907 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000908 if (mtime == (time_t)(-1)) {
909 PyErr_Format(PyExc_RuntimeError,
910 "unable to get modification time from '%s'",
911 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000912 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000913 }
Fred Drake4c82b232000-06-30 16:18:57 +0000914#if SIZEOF_TIME_T > 4
915 /* Python's .pyc timestamp handling presumes that the timestamp fits
916 in 4 bytes. This will be fine until sometime in the year 2038,
917 when a 4-byte signed time_t will overflow.
918 */
919 if (mtime >> 32) {
920 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000921 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000922 return NULL;
923 }
924#endif
Tim Peters36515e22001-11-18 04:06:29 +0000925 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000926 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927 if (cpathname != NULL &&
928 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000929 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 fclose(fpc);
931 if (co == NULL)
932 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000934 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000935 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000936 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000937 }
938 else {
939 co = parse_source_module(pathname, fp);
940 if (co == NULL)
941 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000943 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000944 name, pathname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000945 if (cpathname)
946 write_compiled_module(co, cpathname, mtime);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000947 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000948 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000950
951 return m;
952}
953
954
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000955/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000956static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
957static struct filedescr *find_module(char *, char *, PyObject *,
958 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000959static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000960
961/* Load a package and return its module object WITH INCREMENTED
962 REFERENCE COUNT */
963
964static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000965load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000966{
Tim Peters1cd70172004-08-02 03:52:12 +0000967 PyObject *m, *d;
968 PyObject *file = NULL;
969 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000970 int err;
971 char buf[MAXPATHLEN+1];
972 FILE *fp = NULL;
973 struct filedescr *fdp;
974
975 m = PyImport_AddModule(name);
976 if (m == NULL)
977 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000978 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000979 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000980 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000981 d = PyModule_GetDict(m);
982 file = PyString_FromString(pathname);
983 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000984 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000985 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000986 if (path == NULL)
987 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000988 err = PyDict_SetItemString(d, "__file__", file);
989 if (err == 0)
990 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000991 if (err != 0)
992 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000993 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000994 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000995 if (fdp == NULL) {
996 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
997 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000998 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000999 }
1000 else
1001 m = NULL;
1002 goto cleanup;
1003 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001004 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001005 if (fp != NULL)
1006 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001007 goto cleanup;
1008
1009 error:
1010 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001011 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001012 Py_XDECREF(path);
1013 Py_XDECREF(file);
1014 return m;
1015}
1016
1017
1018/* Helper to test for built-in module */
1019
1020static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001021is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001022{
1023 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001024 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1025 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1026 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001027 return -1;
1028 else
1029 return 1;
1030 }
1031 }
1032 return 0;
1033}
1034
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001035
Just van Rossum52e14d62002-12-30 22:08:05 +00001036/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1037 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001038 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001039 that can handle the path item. Return None if no hook could;
1040 this tells our caller it should fall back to the builtin
1041 import mechanism. Cache the result in path_importer_cache.
1042 Returns a borrowed reference. */
1043
1044static PyObject *
1045get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1046 PyObject *p)
1047{
1048 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001049 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001050
1051 /* These conditions are the caller's responsibility: */
1052 assert(PyList_Check(path_hooks));
1053 assert(PyDict_Check(path_importer_cache));
1054
1055 nhooks = PyList_Size(path_hooks);
1056 if (nhooks < 0)
1057 return NULL; /* Shouldn't happen */
1058
1059 importer = PyDict_GetItem(path_importer_cache, p);
1060 if (importer != NULL)
1061 return importer;
1062
1063 /* set path_importer_cache[p] to None to avoid recursion */
1064 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1065 return NULL;
1066
1067 for (j = 0; j < nhooks; j++) {
1068 PyObject *hook = PyList_GetItem(path_hooks, j);
1069 if (hook == NULL)
1070 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001072 if (importer != NULL)
1073 break;
1074
1075 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1076 return NULL;
1077 }
1078 PyErr_Clear();
1079 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001080 if (importer == NULL) {
1081 importer = PyObject_CallFunctionObjArgs(
1082 (PyObject *)&NullImporterType, p, NULL
1083 );
1084 if (importer == NULL) {
1085 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1086 PyErr_Clear();
1087 return Py_None;
1088 }
1089 }
1090 }
1091 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001092 int err = PyDict_SetItem(path_importer_cache, p, importer);
1093 Py_DECREF(importer);
1094 if (err != 0)
1095 return NULL;
1096 }
1097 return importer;
1098}
1099
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001100/* Search the path (default sys.path) for a module. Return the
1101 corresponding filedescr struct, and (via return arguments) the
1102 pathname and an open file. Return NULL if the module is not found. */
1103
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001104#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001105extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001106 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001107#endif
1108
Martin v. Löwis18e16552006-02-15 17:27:45 +00001109static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001110static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001111static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001112
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001113static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001114find_module(char *fullname, char *subname, PyObject *path, char *buf,
1115 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001116{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001117 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001118 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001119 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001120 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001121 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001122 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001123#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001124 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001125#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001126 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1127 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1128 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001129 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001130#if defined(PYOS_OS2)
1131 size_t saved_len;
1132 size_t saved_namelen;
1133 char *saved_buf = NULL;
1134#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001135 if (p_loader != NULL)
1136 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001137
Just van Rossum52e14d62002-12-30 22:08:05 +00001138 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001139 PyErr_SetString(PyExc_OverflowError,
1140 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001141 return NULL;
1142 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001143 strcpy(name, subname);
1144
1145 /* sys.meta_path import hook */
1146 if (p_loader != NULL) {
1147 PyObject *meta_path;
1148
1149 meta_path = PySys_GetObject("meta_path");
1150 if (meta_path == NULL || !PyList_Check(meta_path)) {
1151 PyErr_SetString(PyExc_ImportError,
1152 "sys.meta_path must be a list of "
1153 "import hooks");
1154 return NULL;
1155 }
1156 Py_INCREF(meta_path); /* zap guard */
1157 npath = PyList_Size(meta_path);
1158 for (i = 0; i < npath; i++) {
1159 PyObject *loader;
1160 PyObject *hook = PyList_GetItem(meta_path, i);
1161 loader = PyObject_CallMethod(hook, "find_module",
1162 "sO", fullname,
1163 path != NULL ?
1164 path : Py_None);
1165 if (loader == NULL) {
1166 Py_DECREF(meta_path);
1167 return NULL; /* true error */
1168 }
1169 if (loader != Py_None) {
1170 /* a loader was found */
1171 *p_loader = loader;
1172 Py_DECREF(meta_path);
1173 return &importhookdescr;
1174 }
1175 Py_DECREF(loader);
1176 }
1177 Py_DECREF(meta_path);
1178 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001179
1180 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001181 /* The only type of submodule allowed inside a "frozen"
1182 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001183 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1184 PyErr_SetString(PyExc_ImportError,
1185 "full frozen module name too long");
1186 return NULL;
1187 }
1188 strcpy(buf, PyString_AsString(path));
1189 strcat(buf, ".");
1190 strcat(buf, name);
1191 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001192 if (find_frozen(name) != NULL) {
1193 strcpy(buf, name);
1194 return &fd_frozen;
1195 }
1196 PyErr_Format(PyExc_ImportError,
1197 "No frozen submodule named %.200s", name);
1198 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001199 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001200 if (path == NULL) {
1201 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001202 strcpy(buf, name);
1203 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001204 }
Greg Ward201baee2001-10-04 14:52:06 +00001205 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001206 strcpy(buf, name);
1207 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001208 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001209
Guido van Rossumac279101996-08-22 23:10:58 +00001210#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001211 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1212 if (fp != NULL) {
1213 *p_fp = fp;
1214 return fdp;
1215 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001216#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001217 path = PySys_GetObject("path");
1218 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001219 if (path == NULL || !PyList_Check(path)) {
1220 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001221 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001222 return NULL;
1223 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001224
1225 path_hooks = PySys_GetObject("path_hooks");
1226 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1227 PyErr_SetString(PyExc_ImportError,
1228 "sys.path_hooks must be a list of "
1229 "import hooks");
1230 return NULL;
1231 }
1232 path_importer_cache = PySys_GetObject("path_importer_cache");
1233 if (path_importer_cache == NULL ||
1234 !PyDict_Check(path_importer_cache)) {
1235 PyErr_SetString(PyExc_ImportError,
1236 "sys.path_importer_cache must be a dict");
1237 return NULL;
1238 }
1239
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001241 namelen = strlen(name);
1242 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001243 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001244 PyObject *v = PyList_GetItem(path, i);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001245 if (!v)
1246 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001247#ifdef Py_USING_UNICODE
1248 if (PyUnicode_Check(v)) {
1249 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1250 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1251 if (copy == NULL)
1252 return NULL;
1253 v = copy;
1254 }
1255 else
1256#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001257 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001258 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 len = PyString_GET_SIZE(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001260 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1261 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001262 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001263 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 strcpy(buf, PyString_AS_STRING(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001265 if (strlen(buf) != len) {
1266 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001267 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001268 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001269
1270 /* sys.path_hooks import hook */
1271 if (p_loader != NULL) {
1272 PyObject *importer;
1273
1274 importer = get_path_importer(path_importer_cache,
1275 path_hooks, v);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001276 if (importer == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001277 Py_XDECREF(copy);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001278 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001279 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001280 /* Note: importer is a borrowed reference */
1281 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001282 PyObject *loader;
1283 loader = PyObject_CallMethod(importer,
1284 "find_module",
1285 "s", fullname);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001286 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001287 if (loader == NULL)
1288 return NULL; /* error */
1289 if (loader != Py_None) {
1290 /* a loader was found */
1291 *p_loader = loader;
1292 return &importhookdescr;
1293 }
1294 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001295 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001296 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001297 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001298 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001299
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001300 if (len > 0 && buf[len-1] != SEP
1301#ifdef ALTSEP
1302 && buf[len-1] != ALTSEP
1303#endif
1304 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001305 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001306 strcpy(buf+len, name);
1307 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001308
1309 /* Check for package import (buf holds a directory name,
1310 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001311#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001312 if (stat(buf, &statbuf) == 0 && /* it exists */
1313 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001314 case_ok(buf, len, namelen, name)) { /* case matches */
1315 if (find_init_module(buf)) { /* and has __init__.py */
1316 Py_XDECREF(copy);
1317 return &fd_package;
1318 }
1319 else {
1320 char warnstr[MAXPATHLEN+80];
1321 sprintf(warnstr, "Not importing directory "
1322 "'%.*s': missing __init__.py",
1323 MAXPATHLEN, buf);
1324 if (PyErr_Warn(PyExc_ImportWarning,
1325 warnstr)) {
1326 Py_XDECREF(copy);
1327 return NULL;
1328 }
1329 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001330 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001331#else
1332 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001333#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001334 if (isdir(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001335 case_ok(buf, len, namelen, name)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001336 if (find_init_module(buf)) {
1337 Py_XDECREF(copy);
1338 return &fd_package;
1339 }
1340 else {
1341 char warnstr[MAXPATHLEN+80];
1342 sprintf(warnstr, "Not importing directory "
1343 "'%.*s': missing __init__.py",
1344 MAXPATHLEN, buf);
1345 if (PyErr_Warn(PyExc_ImportWarning,
1346 warnstr)) {
1347 Py_XDECREF(copy);
1348 return NULL;
1349 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001350 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001351#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001352#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001353#if defined(PYOS_OS2)
1354 /* take a snapshot of the module spec for restoration
1355 * after the 8 character DLL hackery
1356 */
1357 saved_buf = strdup(buf);
1358 saved_len = len;
1359 saved_namelen = namelen;
1360#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001362#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001363 /* OS/2 limits DLLs to 8 character names (w/o
1364 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001365 * so if the name is longer than that and its a
1366 * dynamically loaded module we're going to try,
1367 * truncate the name before trying
1368 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001369 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001370 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001371 const struct filedescr *scan;
1372 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001373 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001374 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001375 break;
1376 else
1377 scan++;
1378 }
1379 if (scan->suffix != NULL) {
1380 /* yes, so truncate the name */
1381 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001382 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001383 buf[len] = '\0';
1384 }
1385 }
1386#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001387 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001388 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001389 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001390 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001391 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001392 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001393 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001394 if (fp != NULL) {
1395 if (case_ok(buf, len, namelen, name))
1396 break;
1397 else { /* continue search */
1398 fclose(fp);
1399 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001400 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001401 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001402#if defined(PYOS_OS2)
1403 /* restore the saved snapshot */
1404 strcpy(buf, saved_buf);
1405 len = saved_len;
1406 namelen = saved_namelen;
1407#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001408 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001409#if defined(PYOS_OS2)
1410 /* don't need/want the module name snapshot anymore */
1411 if (saved_buf)
1412 {
1413 free(saved_buf);
1414 saved_buf = NULL;
1415 }
1416#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001417 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001418 if (fp != NULL)
1419 break;
1420 }
1421 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001422 PyErr_Format(PyExc_ImportError,
1423 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001424 return NULL;
1425 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001426 *p_fp = fp;
1427 return fdp;
1428}
1429
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001430/* Helpers for main.c
1431 * Find the source file corresponding to a named module
1432 */
1433struct filedescr *
1434_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1435 size_t buflen, FILE **p_fp, PyObject **p_loader)
1436{
1437 return find_module((char *) name, (char *) name, path,
1438 buf, buflen, p_fp, p_loader);
1439}
1440
1441PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1442{
1443 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1444}
1445
Martin v. Löwis18e16552006-02-15 17:27:45 +00001446/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001447 * The arguments here are tricky, best shown by example:
1448 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1449 * ^ ^ ^ ^
1450 * |--------------------- buf ---------------------|
1451 * |------------------- len ------------------|
1452 * |------ name -------|
1453 * |----- namelen -----|
1454 * buf is the full path, but len only counts up to (& exclusive of) the
1455 * extension. name is the module name, also exclusive of extension.
1456 *
1457 * We've already done a successful stat() or fopen() on buf, so know that
1458 * there's some match, possibly case-insensitive.
1459 *
Tim Peters50d8d372001-02-28 05:34:27 +00001460 * case_ok() is to return 1 if there's a case-sensitive match for
1461 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1462 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001463 *
Tim Peters50d8d372001-02-28 05:34:27 +00001464 * case_ok() is used to implement case-sensitive import semantics even
1465 * on platforms with case-insensitive filesystems. It's trivial to implement
1466 * for case-sensitive filesystems. It's pretty much a cross-platform
1467 * nightmare for systems with case-insensitive filesystems.
1468 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001469
Tim Peters50d8d372001-02-28 05:34:27 +00001470/* First we may need a pile of platform-specific header files; the sequence
1471 * of #if's here should match the sequence in the body of case_ok().
1472 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001473#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001474#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001475
Tim Peters50d8d372001-02-28 05:34:27 +00001476#elif defined(DJGPP)
1477#include <dir.h>
1478
Jason Tishler7961aa62005-05-20 00:56:54 +00001479#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001480#include <sys/types.h>
1481#include <dirent.h>
1482
Andrew MacIntyred9400542002-02-26 11:41:34 +00001483#elif defined(PYOS_OS2)
1484#define INCL_DOS
1485#define INCL_DOSERRORS
1486#define INCL_NOPMAPI
1487#include <os2.h>
1488
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001489#elif defined(RISCOS)
1490#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001491#endif
1492
Guido van Rossum0980bd91998-02-13 17:18:36 +00001493static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001494case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001495{
Tim Peters50d8d372001-02-28 05:34:27 +00001496/* Pick a platform-specific implementation; the sequence of #if's here should
1497 * match the sequence just above.
1498 */
1499
Jason Tishler7961aa62005-05-20 00:56:54 +00001500/* MS_WINDOWS */
1501#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001502 WIN32_FIND_DATA data;
1503 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001504
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001505 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001506 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001507
Guido van Rossum0980bd91998-02-13 17:18:36 +00001508 h = FindFirstFile(buf, &data);
1509 if (h == INVALID_HANDLE_VALUE) {
1510 PyErr_Format(PyExc_NameError,
1511 "Can't find file for module %.100s\n(filename %.300s)",
1512 name, buf);
1513 return 0;
1514 }
1515 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001516 return strncmp(data.cFileName, name, namelen) == 0;
1517
1518/* DJGPP */
1519#elif defined(DJGPP)
1520 struct ffblk ffblk;
1521 int done;
1522
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001523 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001524 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001525
1526 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1527 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001528 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001529 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001530 name, buf);
1531 return 0;
1532 }
Tim Peters50d8d372001-02-28 05:34:27 +00001533 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001534
Jason Tishler7961aa62005-05-20 00:56:54 +00001535/* new-fangled macintosh (macosx) or Cygwin */
1536#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001537 DIR *dirp;
1538 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001539 char dirname[MAXPATHLEN + 1];
1540 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001541
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001542 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001543 return 1;
1544
Tim Petersd1e87a82001-03-01 18:12:00 +00001545 /* Copy the dir component into dirname; substitute "." if empty */
1546 if (dirlen <= 0) {
1547 dirname[0] = '.';
1548 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001549 }
1550 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001551 assert(dirlen <= MAXPATHLEN);
1552 memcpy(dirname, buf, dirlen);
1553 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001554 }
1555 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001556 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001557 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001558 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001559 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001560 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001561#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001562 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001563#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001564 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001565#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001566 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001567 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001568 (void)closedir(dirp);
1569 return 1; /* Found */
1570 }
1571 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001572 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001573 }
Tim Peters430f5d42001-03-01 01:30:56 +00001574 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001575
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001576/* RISC OS */
1577#elif defined(RISCOS)
1578 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1579 char buf2[MAXPATHLEN+2];
1580 char *nameWithExt = buf+len-namelen;
1581 int canonlen;
1582 os_error *e;
1583
1584 if (Py_GETENV("PYTHONCASEOK") != NULL)
1585 return 1;
1586
1587 /* workaround:
1588 append wildcard, otherwise case of filename wouldn't be touched */
1589 strcpy(buf2, buf);
1590 strcat(buf2, "*");
1591
1592 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1593 canonlen = MAXPATHLEN+1-canonlen;
1594 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1595 return 0;
1596 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1597 return 1; /* match */
1598
1599 return 0;
1600
Andrew MacIntyred9400542002-02-26 11:41:34 +00001601/* OS/2 */
1602#elif defined(PYOS_OS2)
1603 HDIR hdir = 1;
1604 ULONG srchcnt = 1;
1605 FILEFINDBUF3 ffbuf;
1606 APIRET rc;
1607
1608 if (getenv("PYTHONCASEOK") != NULL)
1609 return 1;
1610
1611 rc = DosFindFirst(buf,
1612 &hdir,
1613 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1614 &ffbuf, sizeof(ffbuf),
1615 &srchcnt,
1616 FIL_STANDARD);
1617 if (rc != NO_ERROR)
1618 return 0;
1619 return strncmp(ffbuf.achName, name, namelen) == 0;
1620
Tim Peters50d8d372001-02-28 05:34:27 +00001621/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1622#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001623 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001624
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001625#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001626}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001627
Guido van Rossum0980bd91998-02-13 17:18:36 +00001628
Guido van Rossum197346f1997-10-31 18:38:52 +00001629#ifdef HAVE_STAT
1630/* Helper to look for __init__.py or __init__.py[co] in potential package */
1631static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001632find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001633{
Tim Peters0f9431f2001-07-05 03:47:53 +00001634 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001635 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001636 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001637 struct stat statbuf;
1638
Tim Peters0f9431f2001-07-05 03:47:53 +00001639/* For calling case_ok(buf, len, namelen, name):
1640 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1641 * ^ ^ ^ ^
1642 * |--------------------- buf ---------------------|
1643 * |------------------- len ------------------|
1644 * |------ name -------|
1645 * |----- namelen -----|
1646 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001647 if (save_len + 13 >= MAXPATHLEN)
1648 return 0;
1649 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001650 pname = buf + i;
1651 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001652 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001653 if (case_ok(buf,
1654 save_len + 9, /* len("/__init__") */
1655 8, /* len("__init__") */
1656 pname)) {
1657 buf[save_len] = '\0';
1658 return 1;
1659 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001660 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001661 i += strlen(pname);
1662 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001663 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001664 if (case_ok(buf,
1665 save_len + 9, /* len("/__init__") */
1666 8, /* len("__init__") */
1667 pname)) {
1668 buf[save_len] = '\0';
1669 return 1;
1670 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001671 }
1672 buf[save_len] = '\0';
1673 return 0;
1674}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001675
1676#else
1677
1678#ifdef RISCOS
1679static int
1680find_init_module(buf)
1681 char *buf;
1682{
1683 int save_len = strlen(buf);
1684 int i = save_len;
1685
1686 if (save_len + 13 >= MAXPATHLEN)
1687 return 0;
1688 buf[i++] = SEP;
1689 strcpy(buf+i, "__init__/py");
1690 if (isfile(buf)) {
1691 buf[save_len] = '\0';
1692 return 1;
1693 }
1694
1695 if (Py_OptimizeFlag)
1696 strcpy(buf+i, "o");
1697 else
1698 strcpy(buf+i, "c");
1699 if (isfile(buf)) {
1700 buf[save_len] = '\0';
1701 return 1;
1702 }
1703 buf[save_len] = '\0';
1704 return 0;
1705}
1706#endif /*RISCOS*/
1707
Guido van Rossum197346f1997-10-31 18:38:52 +00001708#endif /* HAVE_STAT */
1709
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001710
Tim Petersdbd9ba62000-07-09 03:09:57 +00001711static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001712
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001714 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001717load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001719 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001721 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001723 /* First check that there's an open file (if we need one) */
1724 switch (type) {
1725 case PY_SOURCE:
1726 case PY_COMPILED:
1727 if (fp == NULL) {
1728 PyErr_Format(PyExc_ValueError,
1729 "file object required for import (type code %d)",
1730 type);
1731 return NULL;
1732 }
1733 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001734
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001735 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001736
1737 case PY_SOURCE:
1738 m = load_source_module(name, buf, fp);
1739 break;
1740
1741 case PY_COMPILED:
1742 m = load_compiled_module(name, buf, fp);
1743 break;
1744
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001745#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001746 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001748 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001749#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001751 case PKG_DIRECTORY:
1752 m = load_package(name, buf);
1753 break;
1754
1755 case C_BUILTIN:
1756 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001757 if (buf != NULL && buf[0] != '\0')
1758 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001759 if (type == C_BUILTIN)
1760 err = init_builtin(name);
1761 else
1762 err = PyImport_ImportFrozenModule(name);
1763 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001764 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001765 if (err == 0) {
1766 PyErr_Format(PyExc_ImportError,
1767 "Purported %s module %.200s not found",
1768 type == C_BUILTIN ?
1769 "builtin" : "frozen",
1770 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001771 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001772 }
1773 modules = PyImport_GetModuleDict();
1774 m = PyDict_GetItemString(modules, name);
1775 if (m == NULL) {
1776 PyErr_Format(
1777 PyExc_ImportError,
1778 "%s module %.200s not properly initialized",
1779 type == C_BUILTIN ?
1780 "builtin" : "frozen",
1781 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001782 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001783 }
1784 Py_INCREF(m);
1785 break;
1786
Just van Rossum52e14d62002-12-30 22:08:05 +00001787 case IMP_HOOK: {
1788 if (loader == NULL) {
1789 PyErr_SetString(PyExc_ImportError,
1790 "import hook without loader");
1791 return NULL;
1792 }
1793 m = PyObject_CallMethod(loader, "load_module", "s", name);
1794 break;
1795 }
1796
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001798 PyErr_Format(PyExc_ImportError,
1799 "Don't know how to import %.200s (type code %d)",
1800 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001801 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802
1803 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804
1805 return m;
1806}
1807
1808
1809/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001810 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001811 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001812
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001813static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001814init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001815{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001816 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001817
Greg Ward201baee2001-10-04 14:52:06 +00001818 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001819 return 1;
1820
Guido van Rossum771c6c81997-10-31 18:37:24 +00001821 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001822 if (strcmp(name, p->name) == 0) {
1823 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001824 PyErr_Format(PyExc_ImportError,
1825 "Cannot re-init internal module %.200s",
1826 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001827 return -1;
1828 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001830 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001831 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001833 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001834 if (_PyImport_FixupExtension(name, name) == NULL)
1835 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001836 return 1;
1837 }
1838 }
1839 return 0;
1840}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001841
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001842
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001843/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001844
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001845static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001846find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001847{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001848 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001849
Guido van Rossum79f25d91997-04-29 20:08:16 +00001850 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001851 if (p->name == NULL)
1852 return NULL;
1853 if (strcmp(p->name, name) == 0)
1854 break;
1855 }
1856 return p;
1857}
1858
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001860get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001861{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001862 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001863 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001864
1865 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001866 PyErr_Format(PyExc_ImportError,
1867 "No such frozen object named %.200s",
1868 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001869 return NULL;
1870 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001871 if (p->code == NULL) {
1872 PyErr_Format(PyExc_ImportError,
1873 "Excluded frozen object named %.200s",
1874 name);
1875 return NULL;
1876 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001877 size = p->size;
1878 if (size < 0)
1879 size = -size;
1880 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001881}
1882
1883/* Initialize a frozen module.
1884 Return 1 for succes, 0 if the module is not found, and -1 with
1885 an exception set if the initialization failed.
1886 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001887
1888int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001889PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001890{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001891 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001892 PyObject *co;
1893 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001894 int ispackage;
1895 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001896
1897 if (p == NULL)
1898 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001899 if (p->code == NULL) {
1900 PyErr_Format(PyExc_ImportError,
1901 "Excluded frozen object named %.200s",
1902 name);
1903 return -1;
1904 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001905 size = p->size;
1906 ispackage = (size < 0);
1907 if (ispackage)
1908 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001909 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001910 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001911 name, ispackage ? " package" : "");
1912 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001913 if (co == NULL)
1914 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001916 PyErr_Format(PyExc_TypeError,
1917 "frozen object %.200s is not a code object",
1918 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001919 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001920 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001921 if (ispackage) {
1922 /* Set __path__ to the package name */
1923 PyObject *d, *s;
1924 int err;
1925 m = PyImport_AddModule(name);
1926 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001927 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001928 d = PyModule_GetDict(m);
1929 s = PyString_InternFromString(name);
1930 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001931 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001932 err = PyDict_SetItemString(d, "__path__", s);
1933 Py_DECREF(s);
1934 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001935 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001936 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001937 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001938 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001939 goto err_return;
1940 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001942 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001943err_return:
1944 Py_DECREF(co);
1945 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001946}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001947
1948
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001949/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001950 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001951
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001953PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001954{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001955 PyObject *pname;
1956 PyObject *result;
1957
1958 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001959 if (pname == NULL)
1960 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001961 result = PyImport_Import(pname);
1962 Py_DECREF(pname);
1963 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001964}
1965
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001966/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001967static PyObject *get_parent(PyObject *globals, char *buf,
1968 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001969static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001970 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001971static int mark_miss(char *name);
1972static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001973 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001974static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001975
1976/* The Magnum Opus of dotted-name import :-) */
1977
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001978static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001979import_module_level(char *name, PyObject *globals, PyObject *locals,
1980 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001981{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001982 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00001983 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001984 PyObject *parent, *head, *next, *tail;
1985
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001986 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001987 if (parent == NULL)
1988 return NULL;
1989
1990 head = load_next(parent, Py_None, &name, buf, &buflen);
1991 if (head == NULL)
1992 return NULL;
1993
1994 tail = head;
1995 Py_INCREF(tail);
1996 while (name) {
1997 next = load_next(tail, tail, &name, buf, &buflen);
1998 Py_DECREF(tail);
1999 if (next == NULL) {
2000 Py_DECREF(head);
2001 return NULL;
2002 }
2003 tail = next;
2004 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002005 if (tail == Py_None) {
2006 /* If tail is Py_None, both get_parent and load_next found
2007 an empty module name: someone called __import__("") or
2008 doctored faulty bytecode */
2009 Py_DECREF(tail);
2010 Py_DECREF(head);
2011 PyErr_SetString(PyExc_ValueError,
2012 "Empty module name");
2013 return NULL;
2014 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002015
2016 if (fromlist != NULL) {
2017 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2018 fromlist = NULL;
2019 }
2020
2021 if (fromlist == NULL) {
2022 Py_DECREF(tail);
2023 return head;
2024 }
2025
2026 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002027 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002028 Py_DECREF(tail);
2029 return NULL;
2030 }
2031
2032 return tail;
2033}
2034
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002035/* For DLL compatibility */
2036#undef PyImport_ImportModuleEx
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002037PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002038PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
2039 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002040{
2041 PyObject *result;
2042 lock_import();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002043 result = import_module_level(name, globals, locals, fromlist, -1);
2044 if (unlock_import() < 0) {
2045 Py_XDECREF(result);
2046 PyErr_SetString(PyExc_RuntimeError,
2047 "not holding the import lock");
2048 return NULL;
2049 }
2050 return result;
2051}
2052#define PyImport_ImportModuleEx(n, g, l, f) \
2053 PyImport_ImportModuleLevel(n, g, l, f, -1);
2054
2055PyObject *
2056PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2057 PyObject *fromlist, int level)
2058{
2059 PyObject *result;
2060 lock_import();
2061 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002062 if (unlock_import() < 0) {
2063 Py_XDECREF(result);
2064 PyErr_SetString(PyExc_RuntimeError,
2065 "not holding the import lock");
2066 return NULL;
2067 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002068 return result;
2069}
2070
Fred Drake87590902004-05-28 20:21:36 +00002071/* Return the package that an import is being performed in. If globals comes
2072 from the module foo.bar.bat (not itself a package), this returns the
2073 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002074 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002075
2076 The *name* of the returned package is returned in buf, with the length of
2077 the name in *p_buflen.
2078
2079 If globals doesn't come from a package or a module in a package, or a
2080 corresponding entry is not found in sys.modules, Py_None is returned.
2081*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002082static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002083get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002084{
2085 static PyObject *namestr = NULL;
2086 static PyObject *pathstr = NULL;
2087 PyObject *modname, *modpath, *modules, *parent;
2088
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002089 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002090 return Py_None;
2091
2092 if (namestr == NULL) {
2093 namestr = PyString_InternFromString("__name__");
2094 if (namestr == NULL)
2095 return NULL;
2096 }
2097 if (pathstr == NULL) {
2098 pathstr = PyString_InternFromString("__path__");
2099 if (pathstr == NULL)
2100 return NULL;
2101 }
2102
2103 *buf = '\0';
2104 *p_buflen = 0;
2105 modname = PyDict_GetItem(globals, namestr);
2106 if (modname == NULL || !PyString_Check(modname))
2107 return Py_None;
2108
2109 modpath = PyDict_GetItem(globals, pathstr);
2110 if (modpath != NULL) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00002111 Py_ssize_t len = PyString_GET_SIZE(modname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002112 if (len > MAXPATHLEN) {
2113 PyErr_SetString(PyExc_ValueError,
2114 "Module name too long");
2115 return NULL;
2116 }
2117 strcpy(buf, PyString_AS_STRING(modname));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002118 }
2119 else {
2120 char *start = PyString_AS_STRING(modname);
2121 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002122 size_t len;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002123 if (lastdot == NULL && level > 0) {
2124 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002125 "Attempted relative import in non-package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002126 return NULL;
2127 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002128 if (lastdot == NULL)
2129 return Py_None;
2130 len = lastdot - start;
2131 if (len >= MAXPATHLEN) {
2132 PyErr_SetString(PyExc_ValueError,
2133 "Module name too long");
2134 return NULL;
2135 }
2136 strncpy(buf, start, len);
2137 buf[len] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002138 }
2139
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002140 while (--level > 0) {
2141 char *dot = strrchr(buf, '.');
2142 if (dot == NULL) {
2143 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002144 "Attempted relative import beyond "
2145 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002146 return NULL;
2147 }
2148 *dot = '\0';
2149 }
2150 *p_buflen = strlen(buf);
2151
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002152 modules = PyImport_GetModuleDict();
2153 parent = PyDict_GetItemString(modules, buf);
2154 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002155 PyErr_Format(PyExc_SystemError,
2156 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002157 return parent;
2158 /* We expect, but can't guarantee, if parent != None, that:
2159 - parent.__name__ == buf
2160 - parent.__dict__ is globals
2161 If this is violated... Who cares? */
2162}
2163
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002164/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002165static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002166load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002167 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002168{
2169 char *name = *p_name;
2170 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002171 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002172 char *p;
2173 PyObject *result;
2174
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002175 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002176 /* completely empty module name should only happen in
2177 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002178 Py_INCREF(mod);
2179 *p_name = NULL;
2180 return mod;
2181 }
2182
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002183 if (dot == NULL) {
2184 *p_name = NULL;
2185 len = strlen(name);
2186 }
2187 else {
2188 *p_name = dot+1;
2189 len = dot-name;
2190 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002191 if (len == 0) {
2192 PyErr_SetString(PyExc_ValueError,
2193 "Empty module name");
2194 return NULL;
2195 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002196
2197 p = buf + *p_buflen;
2198 if (p != buf)
2199 *p++ = '.';
2200 if (p+len-buf >= MAXPATHLEN) {
2201 PyErr_SetString(PyExc_ValueError,
2202 "Module name too long");
2203 return NULL;
2204 }
2205 strncpy(p, name, len);
2206 p[len] = '\0';
2207 *p_buflen = p+len-buf;
2208
2209 result = import_submodule(mod, p, buf);
2210 if (result == Py_None && altmod != mod) {
2211 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002212 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002213 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002214 if (result != NULL && result != Py_None) {
2215 if (mark_miss(buf) != 0) {
2216 Py_DECREF(result);
2217 return NULL;
2218 }
2219 strncpy(buf, name, len);
2220 buf[len] = '\0';
2221 *p_buflen = len;
2222 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002223 }
2224 if (result == NULL)
2225 return NULL;
2226
2227 if (result == Py_None) {
2228 Py_DECREF(result);
2229 PyErr_Format(PyExc_ImportError,
2230 "No module named %.200s", name);
2231 return NULL;
2232 }
2233
2234 return result;
2235}
2236
2237static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002238mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002239{
2240 PyObject *modules = PyImport_GetModuleDict();
2241 return PyDict_SetItemString(modules, name, Py_None);
2242}
2243
2244static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002245ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002246 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002247{
2248 int i;
2249
2250 if (!PyObject_HasAttrString(mod, "__path__"))
2251 return 1;
2252
2253 for (i = 0; ; i++) {
2254 PyObject *item = PySequence_GetItem(fromlist, i);
2255 int hasit;
2256 if (item == NULL) {
2257 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2258 PyErr_Clear();
2259 return 1;
2260 }
2261 return 0;
2262 }
2263 if (!PyString_Check(item)) {
2264 PyErr_SetString(PyExc_TypeError,
2265 "Item in ``from list'' not a string");
2266 Py_DECREF(item);
2267 return 0;
2268 }
2269 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002270 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002271 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002272 /* See if the package defines __all__ */
2273 if (recursive)
2274 continue; /* Avoid endless recursion */
2275 all = PyObject_GetAttrString(mod, "__all__");
2276 if (all == NULL)
2277 PyErr_Clear();
2278 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002279 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002280 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002281 if (!ret)
2282 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002283 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002284 continue;
2285 }
2286 hasit = PyObject_HasAttr(mod, item);
2287 if (!hasit) {
2288 char *subname = PyString_AS_STRING(item);
2289 PyObject *submod;
2290 char *p;
2291 if (buflen + strlen(subname) >= MAXPATHLEN) {
2292 PyErr_SetString(PyExc_ValueError,
2293 "Module name too long");
2294 Py_DECREF(item);
2295 return 0;
2296 }
2297 p = buf + buflen;
2298 *p++ = '.';
2299 strcpy(p, subname);
2300 submod = import_submodule(mod, subname, buf);
2301 Py_XDECREF(submod);
2302 if (submod == NULL) {
2303 Py_DECREF(item);
2304 return 0;
2305 }
2306 }
2307 Py_DECREF(item);
2308 }
2309
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002310 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002311}
2312
Neil Schemenauer00b09662003-06-16 21:03:07 +00002313static int
2314add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2315 PyObject *modules)
2316{
2317 if (mod == Py_None)
2318 return 1;
2319 /* Irrespective of the success of this load, make a
2320 reference to it in the parent package module. A copy gets
2321 saved in the modules dictionary under the full name, so get a
2322 reference from there, if need be. (The exception is when the
2323 load failed with a SyntaxError -- then there's no trace in
2324 sys.modules. In that case, of course, do nothing extra.) */
2325 if (submod == NULL) {
2326 submod = PyDict_GetItemString(modules, fullname);
2327 if (submod == NULL)
2328 return 1;
2329 }
2330 if (PyModule_Check(mod)) {
2331 /* We can't use setattr here since it can give a
2332 * spurious warning if the submodule name shadows a
2333 * builtin name */
2334 PyObject *dict = PyModule_GetDict(mod);
2335 if (!dict)
2336 return 0;
2337 if (PyDict_SetItemString(dict, subname, submod) < 0)
2338 return 0;
2339 }
2340 else {
2341 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2342 return 0;
2343 }
2344 return 1;
2345}
2346
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002347static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002348import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002349{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002350 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002351 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002352
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002353 /* Require:
2354 if mod == None: subname == fullname
2355 else: mod.__name__ + "." + subname == fullname
2356 */
2357
Tim Peters50d8d372001-02-28 05:34:27 +00002358 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002359 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002360 }
2361 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002362 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002363 char buf[MAXPATHLEN+1];
2364 struct filedescr *fdp;
2365 FILE *fp = NULL;
2366
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002367 if (mod == Py_None)
2368 path = NULL;
2369 else {
2370 path = PyObject_GetAttrString(mod, "__path__");
2371 if (path == NULL) {
2372 PyErr_Clear();
2373 Py_INCREF(Py_None);
2374 return Py_None;
2375 }
2376 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002377
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002378 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002379 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2380 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002381 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002382 if (fdp == NULL) {
2383 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2384 return NULL;
2385 PyErr_Clear();
2386 Py_INCREF(Py_None);
2387 return Py_None;
2388 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002389 m = load_module(fullname, fp, buf, fdp->type, loader);
2390 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002391 if (fp)
2392 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002393 if (!add_submodule(mod, m, fullname, subname, modules)) {
2394 Py_XDECREF(m);
2395 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002396 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002397 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002398
2399 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002400}
2401
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002402
2403/* Re-import a module of any kind and return its module object, WITH
2404 INCREMENTED REFERENCE COUNT */
2405
Guido van Rossum79f25d91997-04-29 20:08:16 +00002406PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002407PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002408{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002409 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002410 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002411 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002412 char buf[MAXPATHLEN+1];
2413 struct filedescr *fdp;
2414 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002415 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002416
Guido van Rossum79f25d91997-04-29 20:08:16 +00002417 if (m == NULL || !PyModule_Check(m)) {
2418 PyErr_SetString(PyExc_TypeError,
2419 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002420 return NULL;
2421 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002422 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002423 if (name == NULL)
2424 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002425 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002426 PyErr_Format(PyExc_ImportError,
2427 "reload(): module %.200s not in sys.modules",
2428 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002429 return NULL;
2430 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002431 subname = strrchr(name, '.');
2432 if (subname == NULL)
2433 subname = name;
2434 else {
2435 PyObject *parentname, *parent;
2436 parentname = PyString_FromStringAndSize(name, (subname-name));
2437 if (parentname == NULL)
2438 return NULL;
2439 parent = PyDict_GetItem(modules, parentname);
2440 if (parent == NULL) {
2441 PyErr_Format(PyExc_ImportError,
2442 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002443 PyString_AS_STRING(parentname));
2444 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002445 return NULL;
2446 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002447 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002448 subname++;
2449 path = PyObject_GetAttrString(parent, "__path__");
2450 if (path == NULL)
2451 PyErr_Clear();
2452 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002453 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002454 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002455 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002456
2457 if (fdp == NULL) {
2458 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002459 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002460 }
2461
2462 newm = load_module(name, fp, buf, fdp->type, loader);
2463 Py_XDECREF(loader);
2464
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002465 if (fp)
2466 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002467 if (newm == NULL) {
2468 /* load_module probably removed name from modules because of
2469 * the error. Put back the original module object. We're
2470 * going to return NULL in this case regardless of whether
2471 * replacing name succeeds, so the return value is ignored.
2472 */
2473 PyDict_SetItemString(modules, name, m);
2474 }
2475 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002476}
2477
2478
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002479/* Higher-level import emulator which emulates the "import" statement
2480 more accurately -- it invokes the __import__() function from the
2481 builtins of the current globals. This means that the import is
2482 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002483 environment, e.g. by "rexec".
2484 A dummy list ["__doc__"] is passed as the 4th argument so that
2485 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2486 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002487
2488PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002489PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002490{
2491 static PyObject *silly_list = NULL;
2492 static PyObject *builtins_str = NULL;
2493 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002494 PyObject *globals = NULL;
2495 PyObject *import = NULL;
2496 PyObject *builtins = NULL;
2497 PyObject *r = NULL;
2498
2499 /* Initialize constant string objects */
2500 if (silly_list == NULL) {
2501 import_str = PyString_InternFromString("__import__");
2502 if (import_str == NULL)
2503 return NULL;
2504 builtins_str = PyString_InternFromString("__builtins__");
2505 if (builtins_str == NULL)
2506 return NULL;
2507 silly_list = Py_BuildValue("[s]", "__doc__");
2508 if (silly_list == NULL)
2509 return NULL;
2510 }
2511
2512 /* Get the builtins from current globals */
2513 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002514 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002515 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002516 builtins = PyObject_GetItem(globals, builtins_str);
2517 if (builtins == NULL)
2518 goto err;
2519 }
2520 else {
2521 /* No globals -- use standard builtins, and fake globals */
2522 PyErr_Clear();
2523
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002524 builtins = PyImport_ImportModuleLevel("__builtin__",
2525 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002526 if (builtins == NULL)
2527 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002528 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2529 if (globals == NULL)
2530 goto err;
2531 }
2532
2533 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002534 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002535 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002536 if (import == NULL)
2537 PyErr_SetObject(PyExc_KeyError, import_str);
2538 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002539 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002540 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002541 if (import == NULL)
2542 goto err;
2543
2544 /* Call the _import__ function with the proper argument list */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002545 r = PyObject_CallFunctionObjArgs(import, module_name, globals,
2546 globals, silly_list, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002547
2548 err:
2549 Py_XDECREF(globals);
2550 Py_XDECREF(builtins);
2551 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002552
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002553 return r;
2554}
2555
2556
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002557/* Module 'imp' provides Python access to the primitives used for
2558 importing modules.
2559*/
2560
Guido van Rossum79f25d91997-04-29 20:08:16 +00002561static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002562imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002563{
2564 char buf[4];
2565
Guido van Rossum96774c12000-05-01 20:19:08 +00002566 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2567 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2568 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2569 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002570
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572}
2573
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002575imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002576{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002577 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002578 struct filedescr *fdp;
2579
Guido van Rossum79f25d91997-04-29 20:08:16 +00002580 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002581 if (list == NULL)
2582 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002583 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2584 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002585 fdp->suffix, fdp->mode, fdp->type);
2586 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002588 return NULL;
2589 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590 if (PyList_Append(list, item) < 0) {
2591 Py_DECREF(list);
2592 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002593 return NULL;
2594 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002595 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002596 }
2597 return list;
2598}
2599
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002601call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002603 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002604 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002605 struct filedescr *fdp;
2606 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002607 FILE *fp = NULL;
2608
2609 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002610 if (path == Py_None)
2611 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002612 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002613 if (fdp == NULL)
2614 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002615 if (fp != NULL) {
2616 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2617 if (fob == NULL) {
2618 fclose(fp);
2619 return NULL;
2620 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002621 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002622 else {
2623 fob = Py_None;
2624 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002625 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002626 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002627 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002628 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002629 return ret;
2630}
2631
Guido van Rossum79f25d91997-04-29 20:08:16 +00002632static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002633imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002634{
2635 char *name;
2636 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002637 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002638 return NULL;
2639 return call_find_module(name, path);
2640}
2641
2642static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002643imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002644{
2645 char *name;
2646 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002647 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002648 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002649 return NULL;
2650 ret = init_builtin(name);
2651 if (ret < 0)
2652 return NULL;
2653 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002654 Py_INCREF(Py_None);
2655 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002657 m = PyImport_AddModule(name);
2658 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002659 return m;
2660}
2661
Guido van Rossum79f25d91997-04-29 20:08:16 +00002662static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002663imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002664{
2665 char *name;
2666 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002667 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002668 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002669 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002670 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002671 if (ret < 0)
2672 return NULL;
2673 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002674 Py_INCREF(Py_None);
2675 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002676 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002677 m = PyImport_AddModule(name);
2678 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002679 return m;
2680}
2681
Guido van Rossum79f25d91997-04-29 20:08:16 +00002682static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002683imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002684{
2685 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002686
Guido van Rossum43713e52000-02-29 13:59:29 +00002687 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002688 return NULL;
2689 return get_frozen_object(name);
2690}
2691
Guido van Rossum79f25d91997-04-29 20:08:16 +00002692static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002693imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002694{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002695 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002696 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002697 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002698 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002699}
2700
Guido van Rossum79f25d91997-04-29 20:08:16 +00002701static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002702imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002703{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002704 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002705 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002706 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002707 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002708 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002709 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002710}
2711
2712static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002713get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002714{
2715 FILE *fp;
2716 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002717 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002718 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002719 fp = fopen(pathname, mode);
2720 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002721 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002722 }
2723 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002724 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002725 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002726 PyErr_SetString(PyExc_ValueError,
2727 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002728 }
2729 return fp;
2730}
2731
Guido van Rossum79f25d91997-04-29 20:08:16 +00002732static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002733imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002734{
2735 char *name;
2736 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002737 PyObject *fob = NULL;
2738 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002739 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002740 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002741 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002742 return NULL;
2743 fp = get_file(pathname, fob, "rb");
2744 if (fp == NULL)
2745 return NULL;
2746 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002747 if (fob == NULL)
2748 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002749 return m;
2750}
2751
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002752#ifdef HAVE_DYNAMIC_LOADING
2753
Guido van Rossum79f25d91997-04-29 20:08:16 +00002754static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002755imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002756{
2757 char *name;
2758 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002759 PyObject *fob = NULL;
2760 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002761 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002762 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002763 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002764 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002765 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002766 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002767 if (fp == NULL)
2768 return NULL;
2769 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002770 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002771 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002772}
2773
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002774#endif /* HAVE_DYNAMIC_LOADING */
2775
Guido van Rossum79f25d91997-04-29 20:08:16 +00002776static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002777imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002778{
2779 char *name;
2780 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002781 PyObject *fob = NULL;
2782 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002783 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002784 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002785 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002786 return NULL;
2787 fp = get_file(pathname, fob, "r");
2788 if (fp == NULL)
2789 return NULL;
2790 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002791 if (fob == NULL)
2792 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002793 return m;
2794}
2795
Guido van Rossum79f25d91997-04-29 20:08:16 +00002796static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002797imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002798{
2799 char *name;
2800 PyObject *fob;
2801 char *pathname;
2802 char *suffix; /* Unused */
2803 char *mode;
2804 int type;
2805 FILE *fp;
2806
Guido van Rossum43713e52000-02-29 13:59:29 +00002807 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002808 &name, &fob, &pathname,
2809 &suffix, &mode, &type))
2810 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002811 if (*mode) {
2812 /* Mode must start with 'r' or 'U' and must not contain '+'.
2813 Implicit in this test is the assumption that the mode
2814 may contain other modifiers like 'b' or 't'. */
2815
2816 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002817 PyErr_Format(PyExc_ValueError,
2818 "invalid file open mode %.200s", mode);
2819 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002820 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002821 }
2822 if (fob == Py_None)
2823 fp = NULL;
2824 else {
2825 if (!PyFile_Check(fob)) {
2826 PyErr_SetString(PyExc_ValueError,
2827 "load_module arg#2 should be a file or None");
2828 return NULL;
2829 }
2830 fp = get_file(pathname, fob, mode);
2831 if (fp == NULL)
2832 return NULL;
2833 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002834 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002835}
2836
2837static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002838imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002839{
2840 char *name;
2841 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002842 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002843 return NULL;
2844 return load_package(name, pathname);
2845}
2846
2847static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002848imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002849{
2850 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002851 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002852 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002853 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002854}
2855
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002856/* Doc strings */
2857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002858PyDoc_STRVAR(doc_imp,
2859"This module provides the components needed to build your own\n\
2860__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002862PyDoc_STRVAR(doc_find_module,
2863"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002864Search for a module. If path is omitted or None, search for a\n\
2865built-in, frozen or special module and continue search in sys.path.\n\
2866The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002869PyDoc_STRVAR(doc_load_module,
2870"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002871Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002874PyDoc_STRVAR(doc_get_magic,
2875"get_magic() -> string\n\
2876Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002878PyDoc_STRVAR(doc_get_suffixes,
2879"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002880Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002881that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002882
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002883PyDoc_STRVAR(doc_new_module,
2884"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002885Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002886The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002888PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002889"lock_held() -> boolean\n\
2890Return True if the import lock is currently held, else False.\n\
2891On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002892
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002893PyDoc_STRVAR(doc_acquire_lock,
2894"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002895Acquires the interpreter's import lock for the current thread.\n\
2896This lock should be used by import hooks to ensure thread-safety\n\
2897when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002898On platforms without threads, this function does nothing.");
2899
2900PyDoc_STRVAR(doc_release_lock,
2901"release_lock() -> None\n\
2902Release the interpreter's import lock.\n\
2903On platforms without threads, this function does nothing.");
2904
Guido van Rossum79f25d91997-04-29 20:08:16 +00002905static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002906 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2907 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2908 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2909 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2910 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2911 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2912 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2913 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002914 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002915 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2916 {"init_builtin", imp_init_builtin, METH_VARARGS},
2917 {"init_frozen", imp_init_frozen, METH_VARARGS},
2918 {"is_builtin", imp_is_builtin, METH_VARARGS},
2919 {"is_frozen", imp_is_frozen, METH_VARARGS},
2920 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002921#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002922 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002923#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002924 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002925 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002926 {NULL, NULL} /* sentinel */
2927};
2928
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002929static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002930setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002931{
2932 PyObject *v;
2933 int err;
2934
2935 v = PyInt_FromLong((long)value);
2936 err = PyDict_SetItemString(d, name, v);
2937 Py_XDECREF(v);
2938 return err;
2939}
2940
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002941typedef struct {
2942 PyObject_HEAD
2943} NullImporter;
2944
2945static int
2946NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
2947{
2948 char *path;
2949
2950 if (!_PyArg_NoKeywords("NullImporter()", kwds))
2951 return -1;
2952
2953 if (!PyArg_ParseTuple(args, "s:NullImporter",
2954 &path))
2955 return -1;
2956
2957 if (strlen(path) == 0) {
2958 PyErr_SetString(PyExc_ImportError, "empty pathname");
2959 return -1;
2960 } else {
2961#ifndef RISCOS
2962 struct stat statbuf;
2963 int rv;
2964
2965 rv = stat(path, &statbuf);
2966 if (rv == 0) {
2967 /* it exists */
2968 if (S_ISDIR(statbuf.st_mode)) {
2969 /* it's a directory */
2970 PyErr_SetString(PyExc_ImportError,
2971 "existing directory");
2972 return -1;
2973 }
2974 }
2975#else
2976 if (object_exists(path)) {
2977 /* it exists */
2978 if (isdir(path)) {
2979 /* it's a directory */
2980 PyErr_SetString(PyExc_ImportError,
2981 "existing directory");
2982 return -1;
2983 }
2984 }
2985#endif
2986 }
2987 return 0;
2988}
2989
2990static PyObject *
2991NullImporter_find_module(NullImporter *self, PyObject *args)
2992{
2993 Py_RETURN_NONE;
2994}
2995
2996static PyMethodDef NullImporter_methods[] = {
2997 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
2998 "Always return None"
2999 },
3000 {NULL} /* Sentinel */
3001};
3002
3003
3004static PyTypeObject NullImporterType = {
3005 PyObject_HEAD_INIT(NULL)
3006 0, /*ob_size*/
3007 "imp.NullImporter", /*tp_name*/
3008 sizeof(NullImporter), /*tp_basicsize*/
3009 0, /*tp_itemsize*/
3010 0, /*tp_dealloc*/
3011 0, /*tp_print*/
3012 0, /*tp_getattr*/
3013 0, /*tp_setattr*/
3014 0, /*tp_compare*/
3015 0, /*tp_repr*/
3016 0, /*tp_as_number*/
3017 0, /*tp_as_sequence*/
3018 0, /*tp_as_mapping*/
3019 0, /*tp_hash */
3020 0, /*tp_call*/
3021 0, /*tp_str*/
3022 0, /*tp_getattro*/
3023 0, /*tp_setattro*/
3024 0, /*tp_as_buffer*/
3025 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3026 "Null importer object", /* tp_doc */
3027 0, /* tp_traverse */
3028 0, /* tp_clear */
3029 0, /* tp_richcompare */
3030 0, /* tp_weaklistoffset */
3031 0, /* tp_iter */
3032 0, /* tp_iternext */
3033 NullImporter_methods, /* tp_methods */
3034 0, /* tp_members */
3035 0, /* tp_getset */
3036 0, /* tp_base */
3037 0, /* tp_dict */
3038 0, /* tp_descr_get */
3039 0, /* tp_descr_set */
3040 0, /* tp_dictoffset */
3041 (initproc)NullImporter_init, /* tp_init */
3042 0, /* tp_alloc */
3043 PyType_GenericNew /* tp_new */
3044};
3045
3046
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003047PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003048initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003049{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003050 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003051
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003052 if (PyType_Ready(&NullImporterType) < 0)
3053 goto failure;
3054
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003055 m = Py_InitModule4("imp", imp_methods, doc_imp,
3056 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003057 if (m == NULL)
3058 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003059 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003060 if (d == NULL)
3061 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003062
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003063 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3064 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3065 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3066 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3067 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3068 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3069 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3070 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003071 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003072 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003073
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003074 Py_INCREF(&NullImporterType);
3075 PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003076 failure:
3077 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003078}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003079
3080
Guido van Rossumb18618d2000-05-03 23:44:39 +00003081/* API for embedding applications that want to add their own entries
3082 to the table of built-in modules. This should normally be called
3083 *before* Py_Initialize(). When the table resize fails, -1 is
3084 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003085
3086 After a similar function by Just van Rossum. */
3087
3088int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003089PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003090{
3091 static struct _inittab *our_copy = NULL;
3092 struct _inittab *p;
3093 int i, n;
3094
3095 /* Count the number of entries in both tables */
3096 for (n = 0; newtab[n].name != NULL; n++)
3097 ;
3098 if (n == 0)
3099 return 0; /* Nothing to do */
3100 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3101 ;
3102
3103 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003104 p = our_copy;
3105 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003106 if (p == NULL)
3107 return -1;
3108
3109 /* Copy the tables into the new memory */
3110 if (our_copy != PyImport_Inittab)
3111 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3112 PyImport_Inittab = our_copy = p;
3113 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3114
3115 return 0;
3116}
3117
3118/* Shorthand to add a single entry given a name and a function */
3119
3120int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003121PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003122{
3123 struct _inittab newtab[2];
3124
3125 memset(newtab, '\0', sizeof newtab);
3126
3127 newtab[0].name = name;
3128 newtab[0].initfunc = initfunc;
3129
3130 return PyImport_ExtendInittab(newtab);
3131}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003132
3133#ifdef __cplusplus
3134}
3135#endif