blob: fae60cf1b2fdafd607a6273a85132de1ee72c7f4 [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)
Guido van Rossum452bf512007-02-09 05:32:43 +000074 3050 (print becomes a function)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000075.
Tim Peters36515e22001-11-18 04:06:29 +000076*/
Guido van Rossum452bf512007-02-09 05:32:43 +000077#define MAGIC (3050 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000078
Guido van Rossum96774c12000-05-01 20:19:08 +000079/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000080 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000081 compiler works which are enabled by command line switches. */
82static long pyc_magic = MAGIC;
83
Guido van Rossum25ce5661997-08-02 03:10:38 +000084/* See _PyImport_FixupExtension() below */
85static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000086
Guido van Rossum771c6c81997-10-31 18:37:24 +000087/* This table is defined in config.c: */
88extern struct _inittab _PyImport_Inittab[];
89
90struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000091
Guido van Rossumed1170e1999-12-20 21:23:41 +000092/* these tables define the module suffixes that Python recognizes */
93struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000094
95#ifdef RISCOS
96static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000097 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000098 {"/pyc", "rb", PY_COMPILED},
99 {0, 0}
100};
101#else
Guido van Rossumed1170e1999-12-20 21:23:41 +0000102static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000103 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000104#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000105 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000106#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000107 {".pyc", "rb", PY_COMPILED},
108 {0, 0}
109};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000110#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000111
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112static PyTypeObject NullImporterType; /* Forward reference */
113
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115
116void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000117_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000119 const struct filedescr *scan;
120 struct filedescr *filetab;
121 int countD = 0;
122 int countS = 0;
123
124 /* prepare _PyImport_Filetab: copy entries from
125 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
126 */
127 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
128 ++countD;
129 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
130 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000131 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000132 if (filetab == NULL)
133 Py_FatalError("Can't initialize import file table.");
Guido van Rossumed1170e1999-12-20 21:23:41 +0000134 memcpy(filetab, _PyImport_DynLoadFiletab,
135 countD * sizeof(struct filedescr));
136 memcpy(filetab + countD, _PyImport_StandardFiletab,
137 countS * sizeof(struct filedescr));
138 filetab[countD + countS].suffix = NULL;
139
140 _PyImport_Filetab = filetab;
141
Guido van Rossum0824f631997-03-11 18:37:35 +0000142 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000143 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
144 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000145#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000146 if (strcmp(filetab->suffix, ".pyc") == 0)
147 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000148#else
149 if (strcmp(filetab->suffix, "/pyc") == 0)
150 filetab->suffix = "/pyo";
151#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000152 }
153 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000154
155 if (Py_UnicodeFlag) {
156 /* Fix the pyc_magic so that byte compiled code created
157 using the all-Unicode method doesn't interfere with
158 code created in normal operation mode. */
159 pyc_magic = MAGIC + 1;
160 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161}
162
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163void
Just van Rossum52e14d62002-12-30 22:08:05 +0000164_PyImportHooks_Init(void)
165{
166 PyObject *v, *path_hooks = NULL, *zimpimport;
167 int err = 0;
168
169 /* adding sys.path_hooks and sys.path_importer_cache, setting up
170 zipimport */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000171 if (PyType_Ready(&NullImporterType) < 0)
172 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000173
174 if (Py_VerboseFlag)
175 PySys_WriteStderr("# installing zipimport hook\n");
176
177 v = PyList_New(0);
178 if (v == NULL)
179 goto error;
180 err = PySys_SetObject("meta_path", v);
181 Py_DECREF(v);
182 if (err)
183 goto error;
184 v = PyDict_New();
185 if (v == NULL)
186 goto error;
187 err = PySys_SetObject("path_importer_cache", v);
188 Py_DECREF(v);
189 if (err)
190 goto error;
191 path_hooks = PyList_New(0);
192 if (path_hooks == NULL)
193 goto error;
194 err = PySys_SetObject("path_hooks", path_hooks);
195 if (err) {
196 error:
197 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000198 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
199 "path_importer_cache, or NullImporter failed"
200 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000201 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202
Just van Rossum52e14d62002-12-30 22:08:05 +0000203 zimpimport = PyImport_ImportModule("zipimport");
204 if (zimpimport == NULL) {
205 PyErr_Clear(); /* No zip import module -- okay */
206 if (Py_VerboseFlag)
207 PySys_WriteStderr("# can't import zipimport\n");
208 }
209 else {
210 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
211 "zipimporter");
212 Py_DECREF(zimpimport);
213 if (zipimporter == NULL) {
214 PyErr_Clear(); /* No zipimporter object -- okay */
215 if (Py_VerboseFlag)
216 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000217 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000218 }
219 else {
220 /* sys.path_hooks.append(zipimporter) */
221 err = PyList_Append(path_hooks, zipimporter);
222 Py_DECREF(zipimporter);
223 if (err)
224 goto error;
225 if (Py_VerboseFlag)
226 PySys_WriteStderr(
227 "# installed zipimport hook\n");
228 }
229 }
230 Py_DECREF(path_hooks);
231}
232
233void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235{
236 Py_XDECREF(extensions);
237 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000238 PyMem_DEL(_PyImport_Filetab);
239 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240}
241
242
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000243/* Locking primitives to prevent parallel imports of the same module
244 in different threads to return with a partially loaded module.
245 These calls are serialized by the global interpreter lock. */
246
247#ifdef WITH_THREAD
248
Guido van Rossum49b56061998-10-01 20:42:43 +0000249#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252static long import_lock_thread = -1;
253static int import_lock_level = 0;
254
255static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000257{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259 if (me == -1)
260 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000262 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263 if (import_lock == NULL)
264 return; /* Nothing much we can do. */
265 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266 if (import_lock_thread == me) {
267 import_lock_level++;
268 return;
269 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000270 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
271 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000272 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000273 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000274 PyEval_RestoreThread(tstate);
275 }
276 import_lock_thread = me;
277 import_lock_level = 1;
278}
279
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000280static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000282{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000283 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000285 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000286 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000288 import_lock_level--;
289 if (import_lock_level == 0) {
290 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000291 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000292 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000293 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000294}
295
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000296/* This function is called from PyOS_AfterFork to ensure that newly
297 created child processes do not share locks with the parent. */
298
299void
300_PyImport_ReInitLock(void)
301{
302#ifdef _AIX
303 if (import_lock != NULL)
304 import_lock = PyThread_allocate_lock();
305#endif
306}
307
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000308#else
309
310#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000311#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000312
313#endif
314
Tim Peters69232342001-08-30 05:16:13 +0000315static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000316imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000317{
Tim Peters69232342001-08-30 05:16:13 +0000318#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000319 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000320#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000321 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000322#endif
323}
324
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000325static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000326imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000328#ifdef WITH_THREAD
329 lock_import();
330#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000331 Py_INCREF(Py_None);
332 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000333}
334
335static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000336imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000337{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000338#ifdef WITH_THREAD
339 if (unlock_import() < 0) {
340 PyErr_SetString(PyExc_RuntimeError,
341 "not holding the import lock");
342 return NULL;
343 }
344#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000345 Py_INCREF(Py_None);
346 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000347}
348
Guido van Rossum25ce5661997-08-02 03:10:38 +0000349/* Helper for sys */
350
351PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000353{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000354 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000355 if (interp->modules == NULL)
356 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
357 return interp->modules;
358}
359
Guido van Rossum3f5da241990-12-20 15:06:42 +0000360
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000361/* List of names to clear in sys */
362static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000363 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000364 "exc_type", "exc_value", "exc_traceback",
365 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000366 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000367 NULL
368};
369
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000370static char* sys_files[] = {
371 "stdin", "__stdin__",
372 "stdout", "__stdout__",
373 "stderr", "__stderr__",
374 NULL
375};
376
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000377
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000378/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379
Guido van Rossum3f5da241990-12-20 15:06:42 +0000380void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000384 char *name;
385 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000386 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000387 PyObject *modules = interp->modules;
388
389 if (modules == NULL)
390 return; /* Already done */
391
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000392 /* Delete some special variables first. These are common
393 places where user values hide and people complain when their
394 destructors fail. Since the modules containing them are
395 deleted *last* of all, they would come too late in the normal
396 destruction order. Sigh. */
397
398 value = PyDict_GetItemString(modules, "__builtin__");
399 if (value != NULL && PyModule_Check(value)) {
400 dict = PyModule_GetDict(value);
401 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000402 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000403 PyDict_SetItemString(dict, "_", Py_None);
404 }
405 value = PyDict_GetItemString(modules, "sys");
406 if (value != NULL && PyModule_Check(value)) {
407 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000408 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000409 dict = PyModule_GetDict(value);
410 for (p = sys_deletes; *p != NULL; p++) {
411 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000412 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000413 PyDict_SetItemString(dict, *p, Py_None);
414 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000415 for (p = sys_files; *p != NULL; p+=2) {
416 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000417 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000418 v = PyDict_GetItemString(dict, *(p+1));
419 if (v == NULL)
420 v = Py_None;
421 PyDict_SetItemString(dict, *p, v);
422 }
423 }
424
425 /* First, delete __main__ */
426 value = PyDict_GetItemString(modules, "__main__");
427 if (value != NULL && PyModule_Check(value)) {
428 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000429 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000430 _PyModule_Clear(value);
431 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000432 }
433
Guido van Rossum758eec01998-01-19 21:58:26 +0000434 /* The special treatment of __builtin__ here is because even
435 when it's not referenced as a module, its dictionary is
436 referenced by almost every module's __builtins__. Since
437 deleting a module clears its dictionary (even if there are
438 references left to it), we need to delete the __builtin__
439 module last. Likewise, we don't delete sys until the very
440 end because it is implicitly referenced (e.g. by print).
441
442 Also note that we 'delete' modules by replacing their entry
443 in the modules dict with None, rather than really deleting
444 them; this avoids a rehash of the modules dictionary and
445 also marks them as "non existent" so they won't be
446 re-imported. */
447
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000448 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000449 one (skipping __builtin__ and sys) and delete them */
450 do {
451 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000452 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000453 while (PyDict_Next(modules, &pos, &key, &value)) {
454 if (value->ob_refcnt != 1)
455 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000456 if (PyString_Check(key) && PyModule_Check(value)) {
457 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000458 if (strcmp(name, "__builtin__") == 0)
459 continue;
460 if (strcmp(name, "sys") == 0)
461 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000462 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000463 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000464 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000465 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000466 PyDict_SetItem(modules, key, Py_None);
467 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000468 }
469 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000470 } while (ndone > 0);
471
Guido van Rossum758eec01998-01-19 21:58:26 +0000472 /* Next, delete all modules (still skipping __builtin__ and sys) */
473 pos = 0;
474 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000475 if (PyString_Check(key) && PyModule_Check(value)) {
476 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000477 if (strcmp(name, "__builtin__") == 0)
478 continue;
479 if (strcmp(name, "sys") == 0)
480 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000481 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000482 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000483 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000484 PyDict_SetItem(modules, key, Py_None);
485 }
486 }
487
488 /* Next, delete sys and __builtin__ (in that order) */
489 value = PyDict_GetItemString(modules, "sys");
490 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000491 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000492 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000493 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000494 PyDict_SetItemString(modules, "sys", Py_None);
495 }
496 value = PyDict_GetItemString(modules, "__builtin__");
497 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000498 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000499 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000500 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000501 PyDict_SetItemString(modules, "__builtin__", Py_None);
502 }
503
504 /* Finally, clear and delete the modules directory */
505 PyDict_Clear(modules);
506 interp->modules = NULL;
507 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000508}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000509
510
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511/* Helper for pythonrun.c -- return magic number */
512
513long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000514PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000515{
Guido van Rossum96774c12000-05-01 20:19:08 +0000516 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517}
518
519
Guido van Rossum25ce5661997-08-02 03:10:38 +0000520/* Magic for extension modules (built-in as well as dynamically
521 loaded). To prevent initializing an extension module more than
522 once, we keep a static dictionary 'extensions' keyed by module name
523 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000524 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000525 dictionary is stored by calling _PyImport_FixupExtension()
526 immediately after the module initialization function succeeds. A
527 copy can be retrieved from there by calling
528 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000533 PyObject *modules, *mod, *dict, *copy;
534 if (extensions == NULL) {
535 extensions = PyDict_New();
536 if (extensions == NULL)
537 return NULL;
538 }
539 modules = PyImport_GetModuleDict();
540 mod = PyDict_GetItemString(modules, name);
541 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000542 PyErr_Format(PyExc_SystemError,
543 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 return NULL;
545 }
546 dict = PyModule_GetDict(mod);
547 if (dict == NULL)
548 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000549 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550 if (copy == NULL)
551 return NULL;
552 PyDict_SetItemString(extensions, filename, copy);
553 Py_DECREF(copy);
554 return copy;
555}
556
557PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000558_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000559{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000560 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000561 if (extensions == NULL)
562 return NULL;
563 dict = PyDict_GetItemString(extensions, filename);
564 if (dict == NULL)
565 return NULL;
566 mod = PyImport_AddModule(name);
567 if (mod == NULL)
568 return NULL;
569 mdict = PyModule_GetDict(mod);
570 if (mdict == NULL)
571 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000572 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000573 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000574 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000575 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000576 name, filename);
577 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000578}
579
580
581/* Get the module object corresponding to a module name.
582 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000583 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000584 Because the former action is most common, THIS DOES NOT RETURN A
585 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
Guido van Rossum79f25d91997-04-29 20:08:16 +0000587PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000588PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000596 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000597 if (m == NULL)
598 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601 return NULL;
602 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604
605 return m;
606}
607
Tim Peters1cd70172004-08-02 03:52:12 +0000608/* Remove name from sys.modules, if it's there. */
609static void
610_RemoveModule(const char *name)
611{
612 PyObject *modules = PyImport_GetModuleDict();
613 if (PyDict_GetItemString(modules, name) == NULL)
614 return;
615 if (PyDict_DelItemString(modules, name) < 0)
616 Py_FatalError("import: deleting existing key in"
617 "sys.modules failed");
618}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000620/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000621 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
622 * removed from sys.modules, to avoid leaving damaged module objects
623 * in sys.modules. The caller may wish to restore the original
624 * module object (if any) in this case; PyImport_ReloadModule is an
625 * example.
626 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000628PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000629{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000630 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
631}
632
633PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000635{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000636 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640 if (m == NULL)
641 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000642 /* If the module is being reloaded, we get the old module back
643 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 d = PyModule_GetDict(m);
645 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
646 if (PyDict_SetItemString(d, "__builtins__",
647 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000648 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000649 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000650 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000651 v = NULL;
652 if (pathname != NULL) {
653 v = PyString_FromString(pathname);
654 if (v == NULL)
655 PyErr_Clear();
656 }
657 if (v == NULL) {
658 v = ((PyCodeObject *)co)->co_filename;
659 Py_INCREF(v);
660 }
661 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000663 Py_DECREF(v);
664
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000665 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000667 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000669
Guido van Rossum25ce5661997-08-02 03:10:38 +0000670 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000671 PyErr_Format(PyExc_ImportError,
672 "Loaded module %.200s not found in sys.modules",
673 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000674 return NULL;
675 }
676
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000678
679 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000680
681 error:
682 _RemoveModule(name);
683 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684}
685
686
687/* Given a pathname for a Python source file, fill a buffer with the
688 pathname for the corresponding compiled file. Return the pathname
689 for the compiled file, or NULL if there's no space in the buffer.
690 Doesn't set an exception. */
691
692static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000693make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694{
Tim Petersc1731372001-08-04 08:12:36 +0000695 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696 if (len+2 > buflen)
697 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000698
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000699#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000700 /* Treat .pyw as if it were .py. The case of ".pyw" must match
701 that used in _PyImport_StandardFiletab. */
702 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
703 --len; /* pretend 'w' isn't there */
704#endif
705 memcpy(buf, pathname, len);
706 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
707 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708
709 return buf;
710}
711
712
713/* Given a pathname for a Python source file, its time of last
714 modification, and a pathname for a compiled file, check whether the
715 compiled file represents the same version of the source. If so,
716 return a FILE pointer for the compiled file, positioned just after
717 the header; if not, return NULL.
718 Doesn't set an exception. */
719
720static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000721check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722{
723 FILE *fp;
724 long magic;
725 long pyc_mtime;
726
727 fp = fopen(cpathname, "rb");
728 if (fp == NULL)
729 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000731 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000733 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734 fclose(fp);
735 return NULL;
736 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000740 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 fclose(fp);
742 return NULL;
743 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000745 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 return fp;
747}
748
749
750/* Read a code object from a file and check it for validity */
751
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000753read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756
Tim Petersd9b9ac82001-01-28 00:27:39 +0000757 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000758 if (co == NULL)
759 return NULL;
760 if (!PyCode_Check(co)) {
761 PyErr_Format(PyExc_ImportError,
762 "Non-code object in %.200s", cpathname);
763 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000764 return NULL;
765 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000767}
768
769
770/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000771 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000774load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775{
776 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 PyCodeObject *co;
778 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000779
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000781 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000782 PyErr_Format(PyExc_ImportError,
783 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000784 return NULL;
785 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000787 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788 if (co == NULL)
789 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000791 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000793 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795
796 return m;
797}
798
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000799/* Parse a source file and return the corresponding code object */
800
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000803{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 PyCodeObject *co = NULL;
805 mod_ty mod;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000806 PyArena *arena = PyArena_New();
807 if (arena == NULL)
808 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000811 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000813 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000815 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816 return co;
817}
818
819
Guido van Rossum55a83382000-09-20 20:31:38 +0000820/* Helper to open a bytecode file for writing in exclusive mode */
821
822static FILE *
823open_exclusive(char *filename)
824{
825#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
826 /* Use O_EXCL to avoid a race condition when another process tries to
827 write the same file. When that happens, our open() call fails,
828 which is just fine (since it's only a cache).
829 XXX If the file exists and is writable but the directory is not
830 writable, the file will never be written. Oh well.
831 */
832 int fd;
833 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000834 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
835#ifdef O_BINARY
836 |O_BINARY /* necessary for Windows */
837#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000838#ifdef __VMS
Martin v. Löwis18e16552006-02-15 17:27:45 +0000839 , 0666, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000840#else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 , 0666
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000842#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000843 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000844 if (fd < 0)
845 return NULL;
846 return fdopen(fd, "wb");
847#else
848 /* Best we can do -- on Windows this can't happen anyway */
849 return fopen(filename, "wb");
850#endif
851}
852
853
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854/* Write a compiled module to a file, placing the time of last
855 modification of its source into the header.
856 Errors are ignored, if a write error occurs an attempt is made to
857 remove the file. */
858
859static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000860write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000861{
862 FILE *fp;
863
Guido van Rossum55a83382000-09-20 20:31:38 +0000864 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000865 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000866 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000867 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868 "# can't create %s\n", cpathname);
869 return;
870 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000871 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000873 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
874 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000875 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000876 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000877 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000878 /* Don't keep partial file */
879 fclose(fp);
880 (void) unlink(cpathname);
881 return;
882 }
883 /* Now write the true mtime */
884 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000886 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000887 fflush(fp);
888 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000890 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891}
892
893
894/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000895 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
896 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000897
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000899load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000900{
Fred Drake4c82b232000-06-30 16:18:57 +0000901 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000902 FILE *fpc;
903 char buf[MAXPATHLEN+1];
904 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 PyCodeObject *co;
906 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000907
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000908 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000909 if (mtime == (time_t)(-1)) {
910 PyErr_Format(PyExc_RuntimeError,
911 "unable to get modification time from '%s'",
912 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000913 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000914 }
Fred Drake4c82b232000-06-30 16:18:57 +0000915#if SIZEOF_TIME_T > 4
916 /* Python's .pyc timestamp handling presumes that the timestamp fits
917 in 4 bytes. This will be fine until sometime in the year 2038,
918 when a 4-byte signed time_t will overflow.
919 */
920 if (mtime >> 32) {
921 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000922 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000923 return NULL;
924 }
925#endif
Tim Peters36515e22001-11-18 04:06:29 +0000926 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000927 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000928 if (cpathname != NULL &&
929 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000930 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931 fclose(fpc);
932 if (co == NULL)
933 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000935 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000937 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000938 }
939 else {
940 co = parse_source_module(pathname, fp);
941 if (co == NULL)
942 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000944 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945 name, pathname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000946 if (cpathname)
947 write_compiled_module(co, cpathname, mtime);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000948 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000949 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000951
952 return m;
953}
954
955
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000957static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
958static struct filedescr *find_module(char *, char *, PyObject *,
959 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000960static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000961
962/* Load a package and return its module object WITH INCREMENTED
963 REFERENCE COUNT */
964
965static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000966load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000967{
Tim Peters1cd70172004-08-02 03:52:12 +0000968 PyObject *m, *d;
969 PyObject *file = NULL;
970 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000971 int err;
972 char buf[MAXPATHLEN+1];
973 FILE *fp = NULL;
974 struct filedescr *fdp;
975
976 m = PyImport_AddModule(name);
977 if (m == NULL)
978 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000979 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000980 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000981 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000982 d = PyModule_GetDict(m);
983 file = PyString_FromString(pathname);
984 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000985 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000986 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000987 if (path == NULL)
988 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000989 err = PyDict_SetItemString(d, "__file__", file);
990 if (err == 0)
991 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +0000992 if (err != 0)
993 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000994 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000995 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000996 if (fdp == NULL) {
997 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
998 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +0000999 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001000 }
1001 else
1002 m = NULL;
1003 goto cleanup;
1004 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001005 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001006 if (fp != NULL)
1007 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001008 goto cleanup;
1009
1010 error:
1011 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001012 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001013 Py_XDECREF(path);
1014 Py_XDECREF(file);
1015 return m;
1016}
1017
1018
1019/* Helper to test for built-in module */
1020
1021static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001022is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001023{
1024 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001025 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1026 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1027 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001028 return -1;
1029 else
1030 return 1;
1031 }
1032 }
1033 return 0;
1034}
1035
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001036
Just van Rossum52e14d62002-12-30 22:08:05 +00001037/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1038 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001039 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001040 that can handle the path item. Return None if no hook could;
1041 this tells our caller it should fall back to the builtin
1042 import mechanism. Cache the result in path_importer_cache.
1043 Returns a borrowed reference. */
1044
1045static PyObject *
1046get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1047 PyObject *p)
1048{
1049 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001050 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001051
1052 /* These conditions are the caller's responsibility: */
1053 assert(PyList_Check(path_hooks));
1054 assert(PyDict_Check(path_importer_cache));
1055
1056 nhooks = PyList_Size(path_hooks);
1057 if (nhooks < 0)
1058 return NULL; /* Shouldn't happen */
1059
1060 importer = PyDict_GetItem(path_importer_cache, p);
1061 if (importer != NULL)
1062 return importer;
1063
1064 /* set path_importer_cache[p] to None to avoid recursion */
1065 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1066 return NULL;
1067
1068 for (j = 0; j < nhooks; j++) {
1069 PyObject *hook = PyList_GetItem(path_hooks, j);
1070 if (hook == NULL)
1071 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001073 if (importer != NULL)
1074 break;
1075
1076 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1077 return NULL;
1078 }
1079 PyErr_Clear();
1080 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001081 if (importer == NULL) {
1082 importer = PyObject_CallFunctionObjArgs(
1083 (PyObject *)&NullImporterType, p, NULL
1084 );
1085 if (importer == NULL) {
1086 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1087 PyErr_Clear();
1088 return Py_None;
1089 }
1090 }
1091 }
1092 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001093 int err = PyDict_SetItem(path_importer_cache, p, importer);
1094 Py_DECREF(importer);
1095 if (err != 0)
1096 return NULL;
1097 }
1098 return importer;
1099}
1100
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101/* Search the path (default sys.path) for a module. Return the
1102 corresponding filedescr struct, and (via return arguments) the
1103 pathname and an open file. Return NULL if the module is not found. */
1104
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001105#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001106extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001107 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001108#endif
1109
Martin v. Löwis18e16552006-02-15 17:27:45 +00001110static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001111static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001112static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001113
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001114static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001115find_module(char *fullname, char *subname, PyObject *path, char *buf,
1116 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001118 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001119 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001120 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001121 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001122 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001123 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001124#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001125 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001126#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001127 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1128 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1129 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001130 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001131#if defined(PYOS_OS2)
1132 size_t saved_len;
1133 size_t saved_namelen;
1134 char *saved_buf = NULL;
1135#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001136 if (p_loader != NULL)
1137 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001138
Just van Rossum52e14d62002-12-30 22:08:05 +00001139 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001140 PyErr_SetString(PyExc_OverflowError,
1141 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001142 return NULL;
1143 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001144 strcpy(name, subname);
1145
1146 /* sys.meta_path import hook */
1147 if (p_loader != NULL) {
1148 PyObject *meta_path;
1149
1150 meta_path = PySys_GetObject("meta_path");
1151 if (meta_path == NULL || !PyList_Check(meta_path)) {
1152 PyErr_SetString(PyExc_ImportError,
1153 "sys.meta_path must be a list of "
1154 "import hooks");
1155 return NULL;
1156 }
1157 Py_INCREF(meta_path); /* zap guard */
1158 npath = PyList_Size(meta_path);
1159 for (i = 0; i < npath; i++) {
1160 PyObject *loader;
1161 PyObject *hook = PyList_GetItem(meta_path, i);
1162 loader = PyObject_CallMethod(hook, "find_module",
1163 "sO", fullname,
1164 path != NULL ?
1165 path : Py_None);
1166 if (loader == NULL) {
1167 Py_DECREF(meta_path);
1168 return NULL; /* true error */
1169 }
1170 if (loader != Py_None) {
1171 /* a loader was found */
1172 *p_loader = loader;
1173 Py_DECREF(meta_path);
1174 return &importhookdescr;
1175 }
1176 Py_DECREF(loader);
1177 }
1178 Py_DECREF(meta_path);
1179 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001180
1181 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001182 /* The only type of submodule allowed inside a "frozen"
1183 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001184 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1185 PyErr_SetString(PyExc_ImportError,
1186 "full frozen module name too long");
1187 return NULL;
1188 }
1189 strcpy(buf, PyString_AsString(path));
1190 strcat(buf, ".");
1191 strcat(buf, name);
1192 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001193 if (find_frozen(name) != NULL) {
1194 strcpy(buf, name);
1195 return &fd_frozen;
1196 }
1197 PyErr_Format(PyExc_ImportError,
1198 "No frozen submodule named %.200s", name);
1199 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001200 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001201 if (path == NULL) {
1202 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001203 strcpy(buf, name);
1204 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001205 }
Greg Ward201baee2001-10-04 14:52:06 +00001206 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001207 strcpy(buf, name);
1208 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001209 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210
Guido van Rossumac279101996-08-22 23:10:58 +00001211#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001212 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1213 if (fp != NULL) {
1214 *p_fp = fp;
1215 return fdp;
1216 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001217#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001218 path = PySys_GetObject("path");
1219 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001220 if (path == NULL || !PyList_Check(path)) {
1221 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001222 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001223 return NULL;
1224 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001225
1226 path_hooks = PySys_GetObject("path_hooks");
1227 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1228 PyErr_SetString(PyExc_ImportError,
1229 "sys.path_hooks must be a list of "
1230 "import hooks");
1231 return NULL;
1232 }
1233 path_importer_cache = PySys_GetObject("path_importer_cache");
1234 if (path_importer_cache == NULL ||
1235 !PyDict_Check(path_importer_cache)) {
1236 PyErr_SetString(PyExc_ImportError,
1237 "sys.path_importer_cache must be a dict");
1238 return NULL;
1239 }
1240
Guido van Rossum79f25d91997-04-29 20:08:16 +00001241 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001242 namelen = strlen(name);
1243 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001244 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 PyObject *v = PyList_GetItem(path, i);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001246 if (!v)
1247 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001248#ifdef Py_USING_UNICODE
1249 if (PyUnicode_Check(v)) {
1250 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1251 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1252 if (copy == NULL)
1253 return NULL;
1254 v = copy;
1255 }
1256 else
1257#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001258 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001259 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260 len = PyString_GET_SIZE(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001261 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1262 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001263 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001264 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 strcpy(buf, PyString_AS_STRING(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001266 if (strlen(buf) != len) {
1267 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001268 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001269 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001270
1271 /* sys.path_hooks import hook */
1272 if (p_loader != NULL) {
1273 PyObject *importer;
1274
1275 importer = get_path_importer(path_importer_cache,
1276 path_hooks, v);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001277 if (importer == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001278 Py_XDECREF(copy);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001279 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001280 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001281 /* Note: importer is a borrowed reference */
1282 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001283 PyObject *loader;
1284 loader = PyObject_CallMethod(importer,
1285 "find_module",
1286 "s", fullname);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001287 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001288 if (loader == NULL)
1289 return NULL; /* error */
1290 if (loader != Py_None) {
1291 /* a loader was found */
1292 *p_loader = loader;
1293 return &importhookdescr;
1294 }
1295 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001296 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001297 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001298 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001299 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001300
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001301 if (len > 0 && buf[len-1] != SEP
1302#ifdef ALTSEP
1303 && buf[len-1] != ALTSEP
1304#endif
1305 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001306 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001307 strcpy(buf+len, name);
1308 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001309
1310 /* Check for package import (buf holds a directory name,
1311 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001312#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001313 if (stat(buf, &statbuf) == 0 && /* it exists */
1314 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001315 case_ok(buf, len, namelen, name)) { /* case matches */
1316 if (find_init_module(buf)) { /* and has __init__.py */
1317 Py_XDECREF(copy);
1318 return &fd_package;
1319 }
1320 else {
1321 char warnstr[MAXPATHLEN+80];
1322 sprintf(warnstr, "Not importing directory "
1323 "'%.*s': missing __init__.py",
1324 MAXPATHLEN, buf);
1325 if (PyErr_Warn(PyExc_ImportWarning,
1326 warnstr)) {
1327 Py_XDECREF(copy);
1328 return NULL;
1329 }
1330 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001331 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001332#else
1333 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001334#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001335 if (isdir(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001336 case_ok(buf, len, namelen, name)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001337 if (find_init_module(buf)) {
1338 Py_XDECREF(copy);
1339 return &fd_package;
1340 }
1341 else {
1342 char warnstr[MAXPATHLEN+80];
1343 sprintf(warnstr, "Not importing directory "
1344 "'%.*s': missing __init__.py",
1345 MAXPATHLEN, buf);
1346 if (PyErr_Warn(PyExc_ImportWarning,
1347 warnstr)) {
1348 Py_XDECREF(copy);
1349 return NULL;
1350 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001351 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001352#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001353#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001354#if defined(PYOS_OS2)
1355 /* take a snapshot of the module spec for restoration
1356 * after the 8 character DLL hackery
1357 */
1358 saved_buf = strdup(buf);
1359 saved_len = len;
1360 saved_namelen = namelen;
1361#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001363#if defined(PYOS_OS2)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001364 /* OS/2 limits DLLs to 8 character names (w/o
1365 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001366 * so if the name is longer than that and its a
1367 * dynamically loaded module we're going to try,
1368 * truncate the name before trying
1369 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001370 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001371 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001372 const struct filedescr *scan;
1373 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001374 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001375 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001376 break;
1377 else
1378 scan++;
1379 }
1380 if (scan->suffix != NULL) {
1381 /* yes, so truncate the name */
1382 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001383 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001384 buf[len] = '\0';
1385 }
1386 }
1387#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001388 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001389 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001390 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001391 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001392 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001393 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001394 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001395 if (fp != NULL) {
1396 if (case_ok(buf, len, namelen, name))
1397 break;
1398 else { /* continue search */
1399 fclose(fp);
1400 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001401 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001402 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001403#if defined(PYOS_OS2)
1404 /* restore the saved snapshot */
1405 strcpy(buf, saved_buf);
1406 len = saved_len;
1407 namelen = saved_namelen;
1408#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001409 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001410#if defined(PYOS_OS2)
1411 /* don't need/want the module name snapshot anymore */
1412 if (saved_buf)
1413 {
1414 free(saved_buf);
1415 saved_buf = NULL;
1416 }
1417#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001418 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001419 if (fp != NULL)
1420 break;
1421 }
1422 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001423 PyErr_Format(PyExc_ImportError,
1424 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001425 return NULL;
1426 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001427 *p_fp = fp;
1428 return fdp;
1429}
1430
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001431/* Helpers for main.c
1432 * Find the source file corresponding to a named module
1433 */
1434struct filedescr *
1435_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1436 size_t buflen, FILE **p_fp, PyObject **p_loader)
1437{
1438 return find_module((char *) name, (char *) name, path,
1439 buf, buflen, p_fp, p_loader);
1440}
1441
1442PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1443{
1444 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1445}
1446
Martin v. Löwis18e16552006-02-15 17:27:45 +00001447/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001448 * The arguments here are tricky, best shown by example:
1449 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1450 * ^ ^ ^ ^
1451 * |--------------------- buf ---------------------|
1452 * |------------------- len ------------------|
1453 * |------ name -------|
1454 * |----- namelen -----|
1455 * buf is the full path, but len only counts up to (& exclusive of) the
1456 * extension. name is the module name, also exclusive of extension.
1457 *
1458 * We've already done a successful stat() or fopen() on buf, so know that
1459 * there's some match, possibly case-insensitive.
1460 *
Tim Peters50d8d372001-02-28 05:34:27 +00001461 * case_ok() is to return 1 if there's a case-sensitive match for
1462 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1463 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001464 *
Tim Peters50d8d372001-02-28 05:34:27 +00001465 * case_ok() is used to implement case-sensitive import semantics even
1466 * on platforms with case-insensitive filesystems. It's trivial to implement
1467 * for case-sensitive filesystems. It's pretty much a cross-platform
1468 * nightmare for systems with case-insensitive filesystems.
1469 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001470
Tim Peters50d8d372001-02-28 05:34:27 +00001471/* First we may need a pile of platform-specific header files; the sequence
1472 * of #if's here should match the sequence in the body of case_ok().
1473 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001474#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001475#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001476
Tim Peters50d8d372001-02-28 05:34:27 +00001477#elif defined(DJGPP)
1478#include <dir.h>
1479
Jason Tishler7961aa62005-05-20 00:56:54 +00001480#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001481#include <sys/types.h>
1482#include <dirent.h>
1483
Andrew MacIntyred9400542002-02-26 11:41:34 +00001484#elif defined(PYOS_OS2)
1485#define INCL_DOS
1486#define INCL_DOSERRORS
1487#define INCL_NOPMAPI
1488#include <os2.h>
1489
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001490#elif defined(RISCOS)
1491#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001492#endif
1493
Guido van Rossum0980bd91998-02-13 17:18:36 +00001494static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001495case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001496{
Tim Peters50d8d372001-02-28 05:34:27 +00001497/* Pick a platform-specific implementation; the sequence of #if's here should
1498 * match the sequence just above.
1499 */
1500
Jason Tishler7961aa62005-05-20 00:56:54 +00001501/* MS_WINDOWS */
1502#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001503 WIN32_FIND_DATA data;
1504 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001505
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001506 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001507 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001508
Guido van Rossum0980bd91998-02-13 17:18:36 +00001509 h = FindFirstFile(buf, &data);
1510 if (h == INVALID_HANDLE_VALUE) {
1511 PyErr_Format(PyExc_NameError,
1512 "Can't find file for module %.100s\n(filename %.300s)",
1513 name, buf);
1514 return 0;
1515 }
1516 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001517 return strncmp(data.cFileName, name, namelen) == 0;
1518
1519/* DJGPP */
1520#elif defined(DJGPP)
1521 struct ffblk ffblk;
1522 int done;
1523
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001524 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001525 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001526
1527 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1528 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001529 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001530 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001531 name, buf);
1532 return 0;
1533 }
Tim Peters50d8d372001-02-28 05:34:27 +00001534 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001535
Jason Tishler7961aa62005-05-20 00:56:54 +00001536/* new-fangled macintosh (macosx) or Cygwin */
1537#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001538 DIR *dirp;
1539 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001540 char dirname[MAXPATHLEN + 1];
1541 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001542
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001543 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001544 return 1;
1545
Tim Petersd1e87a82001-03-01 18:12:00 +00001546 /* Copy the dir component into dirname; substitute "." if empty */
1547 if (dirlen <= 0) {
1548 dirname[0] = '.';
1549 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001550 }
1551 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001552 assert(dirlen <= MAXPATHLEN);
1553 memcpy(dirname, buf, dirlen);
1554 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001555 }
1556 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001557 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001558 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001559 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001560 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001561 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001562#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001563 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001564#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001565 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001566#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001567 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001568 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001569 (void)closedir(dirp);
1570 return 1; /* Found */
1571 }
1572 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001573 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001574 }
Tim Peters430f5d42001-03-01 01:30:56 +00001575 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001576
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001577/* RISC OS */
1578#elif defined(RISCOS)
1579 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1580 char buf2[MAXPATHLEN+2];
1581 char *nameWithExt = buf+len-namelen;
1582 int canonlen;
1583 os_error *e;
1584
1585 if (Py_GETENV("PYTHONCASEOK") != NULL)
1586 return 1;
1587
1588 /* workaround:
1589 append wildcard, otherwise case of filename wouldn't be touched */
1590 strcpy(buf2, buf);
1591 strcat(buf2, "*");
1592
1593 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1594 canonlen = MAXPATHLEN+1-canonlen;
1595 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1596 return 0;
1597 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1598 return 1; /* match */
1599
1600 return 0;
1601
Andrew MacIntyred9400542002-02-26 11:41:34 +00001602/* OS/2 */
1603#elif defined(PYOS_OS2)
1604 HDIR hdir = 1;
1605 ULONG srchcnt = 1;
1606 FILEFINDBUF3 ffbuf;
1607 APIRET rc;
1608
1609 if (getenv("PYTHONCASEOK") != NULL)
1610 return 1;
1611
1612 rc = DosFindFirst(buf,
1613 &hdir,
1614 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1615 &ffbuf, sizeof(ffbuf),
1616 &srchcnt,
1617 FIL_STANDARD);
1618 if (rc != NO_ERROR)
1619 return 0;
1620 return strncmp(ffbuf.achName, name, namelen) == 0;
1621
Tim Peters50d8d372001-02-28 05:34:27 +00001622/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1623#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001624 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001625
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001626#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001627}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001628
Guido van Rossum0980bd91998-02-13 17:18:36 +00001629
Guido van Rossum197346f1997-10-31 18:38:52 +00001630#ifdef HAVE_STAT
1631/* Helper to look for __init__.py or __init__.py[co] in potential package */
1632static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001633find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001634{
Tim Peters0f9431f2001-07-05 03:47:53 +00001635 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001636 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001637 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001638 struct stat statbuf;
1639
Tim Peters0f9431f2001-07-05 03:47:53 +00001640/* For calling case_ok(buf, len, namelen, name):
1641 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1642 * ^ ^ ^ ^
1643 * |--------------------- buf ---------------------|
1644 * |------------------- len ------------------|
1645 * |------ name -------|
1646 * |----- namelen -----|
1647 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001648 if (save_len + 13 >= MAXPATHLEN)
1649 return 0;
1650 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001651 pname = buf + i;
1652 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001653 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001654 if (case_ok(buf,
1655 save_len + 9, /* len("/__init__") */
1656 8, /* len("__init__") */
1657 pname)) {
1658 buf[save_len] = '\0';
1659 return 1;
1660 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001661 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001662 i += strlen(pname);
1663 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001664 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001665 if (case_ok(buf,
1666 save_len + 9, /* len("/__init__") */
1667 8, /* len("__init__") */
1668 pname)) {
1669 buf[save_len] = '\0';
1670 return 1;
1671 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001672 }
1673 buf[save_len] = '\0';
1674 return 0;
1675}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001676
1677#else
1678
1679#ifdef RISCOS
1680static int
1681find_init_module(buf)
1682 char *buf;
1683{
1684 int save_len = strlen(buf);
1685 int i = save_len;
1686
1687 if (save_len + 13 >= MAXPATHLEN)
1688 return 0;
1689 buf[i++] = SEP;
1690 strcpy(buf+i, "__init__/py");
1691 if (isfile(buf)) {
1692 buf[save_len] = '\0';
1693 return 1;
1694 }
1695
1696 if (Py_OptimizeFlag)
1697 strcpy(buf+i, "o");
1698 else
1699 strcpy(buf+i, "c");
1700 if (isfile(buf)) {
1701 buf[save_len] = '\0';
1702 return 1;
1703 }
1704 buf[save_len] = '\0';
1705 return 0;
1706}
1707#endif /*RISCOS*/
1708
Guido van Rossum197346f1997-10-31 18:38:52 +00001709#endif /* HAVE_STAT */
1710
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001711
Tim Petersdbd9ba62000-07-09 03:09:57 +00001712static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001713
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001714/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001715 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001716
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001718load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001719{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001720 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001722 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001723
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001724 /* First check that there's an open file (if we need one) */
1725 switch (type) {
1726 case PY_SOURCE:
1727 case PY_COMPILED:
1728 if (fp == NULL) {
1729 PyErr_Format(PyExc_ValueError,
1730 "file object required for import (type code %d)",
1731 type);
1732 return NULL;
1733 }
1734 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001736 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001737
1738 case PY_SOURCE:
1739 m = load_source_module(name, buf, fp);
1740 break;
1741
1742 case PY_COMPILED:
1743 m = load_compiled_module(name, buf, fp);
1744 break;
1745
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001746#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001747 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001748 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001749 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001750#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001751
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001752 case PKG_DIRECTORY:
1753 m = load_package(name, buf);
1754 break;
1755
1756 case C_BUILTIN:
1757 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001758 if (buf != NULL && buf[0] != '\0')
1759 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001760 if (type == C_BUILTIN)
1761 err = init_builtin(name);
1762 else
1763 err = PyImport_ImportFrozenModule(name);
1764 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001765 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001766 if (err == 0) {
1767 PyErr_Format(PyExc_ImportError,
1768 "Purported %s module %.200s not found",
1769 type == C_BUILTIN ?
1770 "builtin" : "frozen",
1771 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001772 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001773 }
1774 modules = PyImport_GetModuleDict();
1775 m = PyDict_GetItemString(modules, name);
1776 if (m == NULL) {
1777 PyErr_Format(
1778 PyExc_ImportError,
1779 "%s module %.200s not properly initialized",
1780 type == C_BUILTIN ?
1781 "builtin" : "frozen",
1782 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001783 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001784 }
1785 Py_INCREF(m);
1786 break;
1787
Just van Rossum52e14d62002-12-30 22:08:05 +00001788 case IMP_HOOK: {
1789 if (loader == NULL) {
1790 PyErr_SetString(PyExc_ImportError,
1791 "import hook without loader");
1792 return NULL;
1793 }
1794 m = PyObject_CallMethod(loader, "load_module", "s", name);
1795 break;
1796 }
1797
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001799 PyErr_Format(PyExc_ImportError,
1800 "Don't know how to import %.200s (type code %d)",
1801 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001802 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803
1804 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001805
1806 return m;
1807}
1808
1809
1810/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001811 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001812 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001813
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001814static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001815init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001816{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001817 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001818
Greg Ward201baee2001-10-04 14:52:06 +00001819 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001820 return 1;
1821
Guido van Rossum771c6c81997-10-31 18:37:24 +00001822 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001823 if (strcmp(name, p->name) == 0) {
1824 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001825 PyErr_Format(PyExc_ImportError,
1826 "Cannot re-init internal module %.200s",
1827 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001828 return -1;
1829 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001831 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001832 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001834 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001835 if (_PyImport_FixupExtension(name, name) == NULL)
1836 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001837 return 1;
1838 }
1839 }
1840 return 0;
1841}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001842
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001844/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001845
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001846static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001847find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001848{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001849 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001850
Guido van Rossum79f25d91997-04-29 20:08:16 +00001851 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001852 if (p->name == NULL)
1853 return NULL;
1854 if (strcmp(p->name, name) == 0)
1855 break;
1856 }
1857 return p;
1858}
1859
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001861get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001862{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001863 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001864 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001865
1866 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001867 PyErr_Format(PyExc_ImportError,
1868 "No such frozen object named %.200s",
1869 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001870 return NULL;
1871 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001872 if (p->code == NULL) {
1873 PyErr_Format(PyExc_ImportError,
1874 "Excluded frozen object named %.200s",
1875 name);
1876 return NULL;
1877 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001878 size = p->size;
1879 if (size < 0)
1880 size = -size;
1881 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001882}
1883
1884/* Initialize a frozen module.
1885 Return 1 for succes, 0 if the module is not found, and -1 with
1886 an exception set if the initialization failed.
1887 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001888
1889int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001890PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001891{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001892 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 PyObject *co;
1894 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001895 int ispackage;
1896 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001897
1898 if (p == NULL)
1899 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001900 if (p->code == NULL) {
1901 PyErr_Format(PyExc_ImportError,
1902 "Excluded frozen object named %.200s",
1903 name);
1904 return -1;
1905 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001906 size = p->size;
1907 ispackage = (size < 0);
1908 if (ispackage)
1909 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001911 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001912 name, ispackage ? " package" : "");
1913 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001914 if (co == NULL)
1915 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001917 PyErr_Format(PyExc_TypeError,
1918 "frozen object %.200s is not a code object",
1919 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001920 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001921 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001922 if (ispackage) {
1923 /* Set __path__ to the package name */
1924 PyObject *d, *s;
1925 int err;
1926 m = PyImport_AddModule(name);
1927 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001928 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001929 d = PyModule_GetDict(m);
1930 s = PyString_InternFromString(name);
1931 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001932 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001933 err = PyDict_SetItemString(d, "__path__", s);
1934 Py_DECREF(s);
1935 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001936 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001937 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001938 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001939 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001940 goto err_return;
1941 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001943 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001944err_return:
1945 Py_DECREF(co);
1946 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001947}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001948
1949
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001950/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001951 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001952
Guido van Rossum79f25d91997-04-29 20:08:16 +00001953PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001954PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001955{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001956 PyObject *pname;
1957 PyObject *result;
1958
1959 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001960 if (pname == NULL)
1961 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001962 result = PyImport_Import(pname);
1963 Py_DECREF(pname);
1964 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001965}
1966
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001967/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001968static PyObject *get_parent(PyObject *globals, char *buf,
1969 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001970static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001971 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001972static int mark_miss(char *name);
1973static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001974 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001975static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001976
1977/* The Magnum Opus of dotted-name import :-) */
1978
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001979static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001980import_module_level(char *name, PyObject *globals, PyObject *locals,
1981 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001982{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001983 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00001984 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001985 PyObject *parent, *head, *next, *tail;
1986
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001987 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001988 if (parent == NULL)
1989 return NULL;
1990
1991 head = load_next(parent, Py_None, &name, buf, &buflen);
1992 if (head == NULL)
1993 return NULL;
1994
1995 tail = head;
1996 Py_INCREF(tail);
1997 while (name) {
1998 next = load_next(tail, tail, &name, buf, &buflen);
1999 Py_DECREF(tail);
2000 if (next == NULL) {
2001 Py_DECREF(head);
2002 return NULL;
2003 }
2004 tail = next;
2005 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002006 if (tail == Py_None) {
2007 /* If tail is Py_None, both get_parent and load_next found
2008 an empty module name: someone called __import__("") or
2009 doctored faulty bytecode */
2010 Py_DECREF(tail);
2011 Py_DECREF(head);
2012 PyErr_SetString(PyExc_ValueError,
2013 "Empty module name");
2014 return NULL;
2015 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002016
2017 if (fromlist != NULL) {
2018 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2019 fromlist = NULL;
2020 }
2021
2022 if (fromlist == NULL) {
2023 Py_DECREF(tail);
2024 return head;
2025 }
2026
2027 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002028 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002029 Py_DECREF(tail);
2030 return NULL;
2031 }
2032
2033 return tail;
2034}
2035
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002036/* For DLL compatibility */
2037#undef PyImport_ImportModuleEx
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002038PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002039PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
2040 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002041{
2042 PyObject *result;
2043 lock_import();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002044 result = import_module_level(name, globals, locals, fromlist, -1);
2045 if (unlock_import() < 0) {
2046 Py_XDECREF(result);
2047 PyErr_SetString(PyExc_RuntimeError,
2048 "not holding the import lock");
2049 return NULL;
2050 }
2051 return result;
2052}
2053#define PyImport_ImportModuleEx(n, g, l, f) \
2054 PyImport_ImportModuleLevel(n, g, l, f, -1);
2055
2056PyObject *
2057PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2058 PyObject *fromlist, int level)
2059{
2060 PyObject *result;
2061 lock_import();
2062 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002063 if (unlock_import() < 0) {
2064 Py_XDECREF(result);
2065 PyErr_SetString(PyExc_RuntimeError,
2066 "not holding the import lock");
2067 return NULL;
2068 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002069 return result;
2070}
2071
Fred Drake87590902004-05-28 20:21:36 +00002072/* Return the package that an import is being performed in. If globals comes
2073 from the module foo.bar.bat (not itself a package), this returns the
2074 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002075 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002076
2077 The *name* of the returned package is returned in buf, with the length of
2078 the name in *p_buflen.
2079
2080 If globals doesn't come from a package or a module in a package, or a
2081 corresponding entry is not found in sys.modules, Py_None is returned.
2082*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002083static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002084get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002085{
2086 static PyObject *namestr = NULL;
2087 static PyObject *pathstr = NULL;
2088 PyObject *modname, *modpath, *modules, *parent;
2089
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002090 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002091 return Py_None;
2092
2093 if (namestr == NULL) {
2094 namestr = PyString_InternFromString("__name__");
2095 if (namestr == NULL)
2096 return NULL;
2097 }
2098 if (pathstr == NULL) {
2099 pathstr = PyString_InternFromString("__path__");
2100 if (pathstr == NULL)
2101 return NULL;
2102 }
2103
2104 *buf = '\0';
2105 *p_buflen = 0;
2106 modname = PyDict_GetItem(globals, namestr);
2107 if (modname == NULL || !PyString_Check(modname))
2108 return Py_None;
2109
2110 modpath = PyDict_GetItem(globals, pathstr);
2111 if (modpath != NULL) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00002112 Py_ssize_t len = PyString_GET_SIZE(modname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002113 if (len > MAXPATHLEN) {
2114 PyErr_SetString(PyExc_ValueError,
2115 "Module name too long");
2116 return NULL;
2117 }
2118 strcpy(buf, PyString_AS_STRING(modname));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002119 }
2120 else {
2121 char *start = PyString_AS_STRING(modname);
2122 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002123 size_t len;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002124 if (lastdot == NULL && level > 0) {
2125 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002126 "Attempted relative import in non-package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002127 return NULL;
2128 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002129 if (lastdot == NULL)
2130 return Py_None;
2131 len = lastdot - start;
2132 if (len >= MAXPATHLEN) {
2133 PyErr_SetString(PyExc_ValueError,
2134 "Module name too long");
2135 return NULL;
2136 }
2137 strncpy(buf, start, len);
2138 buf[len] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002139 }
2140
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002141 while (--level > 0) {
2142 char *dot = strrchr(buf, '.');
2143 if (dot == NULL) {
2144 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 "Attempted relative import beyond "
2146 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002147 return NULL;
2148 }
2149 *dot = '\0';
2150 }
2151 *p_buflen = strlen(buf);
2152
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002153 modules = PyImport_GetModuleDict();
2154 parent = PyDict_GetItemString(modules, buf);
2155 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002156 PyErr_Format(PyExc_SystemError,
2157 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002158 return parent;
2159 /* We expect, but can't guarantee, if parent != None, that:
2160 - parent.__name__ == buf
2161 - parent.__dict__ is globals
2162 If this is violated... Who cares? */
2163}
2164
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002165/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002166static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002167load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002168 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002169{
2170 char *name = *p_name;
2171 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002172 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002173 char *p;
2174 PyObject *result;
2175
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002176 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002177 /* completely empty module name should only happen in
2178 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002179 Py_INCREF(mod);
2180 *p_name = NULL;
2181 return mod;
2182 }
2183
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002184 if (dot == NULL) {
2185 *p_name = NULL;
2186 len = strlen(name);
2187 }
2188 else {
2189 *p_name = dot+1;
2190 len = dot-name;
2191 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002192 if (len == 0) {
2193 PyErr_SetString(PyExc_ValueError,
2194 "Empty module name");
2195 return NULL;
2196 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002197
2198 p = buf + *p_buflen;
2199 if (p != buf)
2200 *p++ = '.';
2201 if (p+len-buf >= MAXPATHLEN) {
2202 PyErr_SetString(PyExc_ValueError,
2203 "Module name too long");
2204 return NULL;
2205 }
2206 strncpy(p, name, len);
2207 p[len] = '\0';
2208 *p_buflen = p+len-buf;
2209
2210 result = import_submodule(mod, p, buf);
2211 if (result == Py_None && altmod != mod) {
2212 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002213 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002214 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002215 if (result != NULL && result != Py_None) {
2216 if (mark_miss(buf) != 0) {
2217 Py_DECREF(result);
2218 return NULL;
2219 }
2220 strncpy(buf, name, len);
2221 buf[len] = '\0';
2222 *p_buflen = len;
2223 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002224 }
2225 if (result == NULL)
2226 return NULL;
2227
2228 if (result == Py_None) {
2229 Py_DECREF(result);
2230 PyErr_Format(PyExc_ImportError,
2231 "No module named %.200s", name);
2232 return NULL;
2233 }
2234
2235 return result;
2236}
2237
2238static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002239mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002240{
2241 PyObject *modules = PyImport_GetModuleDict();
2242 return PyDict_SetItemString(modules, name, Py_None);
2243}
2244
2245static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002246ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002247 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002248{
2249 int i;
2250
2251 if (!PyObject_HasAttrString(mod, "__path__"))
2252 return 1;
2253
2254 for (i = 0; ; i++) {
2255 PyObject *item = PySequence_GetItem(fromlist, i);
2256 int hasit;
2257 if (item == NULL) {
2258 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2259 PyErr_Clear();
2260 return 1;
2261 }
2262 return 0;
2263 }
2264 if (!PyString_Check(item)) {
2265 PyErr_SetString(PyExc_TypeError,
2266 "Item in ``from list'' not a string");
2267 Py_DECREF(item);
2268 return 0;
2269 }
2270 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002271 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002272 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002273 /* See if the package defines __all__ */
2274 if (recursive)
2275 continue; /* Avoid endless recursion */
2276 all = PyObject_GetAttrString(mod, "__all__");
2277 if (all == NULL)
2278 PyErr_Clear();
2279 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002280 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002281 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002282 if (!ret)
2283 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002284 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002285 continue;
2286 }
2287 hasit = PyObject_HasAttr(mod, item);
2288 if (!hasit) {
2289 char *subname = PyString_AS_STRING(item);
2290 PyObject *submod;
2291 char *p;
2292 if (buflen + strlen(subname) >= MAXPATHLEN) {
2293 PyErr_SetString(PyExc_ValueError,
2294 "Module name too long");
2295 Py_DECREF(item);
2296 return 0;
2297 }
2298 p = buf + buflen;
2299 *p++ = '.';
2300 strcpy(p, subname);
2301 submod = import_submodule(mod, subname, buf);
2302 Py_XDECREF(submod);
2303 if (submod == NULL) {
2304 Py_DECREF(item);
2305 return 0;
2306 }
2307 }
2308 Py_DECREF(item);
2309 }
2310
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002311 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002312}
2313
Neil Schemenauer00b09662003-06-16 21:03:07 +00002314static int
2315add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2316 PyObject *modules)
2317{
2318 if (mod == Py_None)
2319 return 1;
2320 /* Irrespective of the success of this load, make a
2321 reference to it in the parent package module. A copy gets
2322 saved in the modules dictionary under the full name, so get a
2323 reference from there, if need be. (The exception is when the
2324 load failed with a SyntaxError -- then there's no trace in
2325 sys.modules. In that case, of course, do nothing extra.) */
2326 if (submod == NULL) {
2327 submod = PyDict_GetItemString(modules, fullname);
2328 if (submod == NULL)
2329 return 1;
2330 }
2331 if (PyModule_Check(mod)) {
2332 /* We can't use setattr here since it can give a
2333 * spurious warning if the submodule name shadows a
2334 * builtin name */
2335 PyObject *dict = PyModule_GetDict(mod);
2336 if (!dict)
2337 return 0;
2338 if (PyDict_SetItemString(dict, subname, submod) < 0)
2339 return 0;
2340 }
2341 else {
2342 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2343 return 0;
2344 }
2345 return 1;
2346}
2347
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002348static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002349import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002350{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002351 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002352 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002353
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002354 /* Require:
2355 if mod == None: subname == fullname
2356 else: mod.__name__ + "." + subname == fullname
2357 */
2358
Tim Peters50d8d372001-02-28 05:34:27 +00002359 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002360 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002361 }
2362 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002363 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002364 char buf[MAXPATHLEN+1];
2365 struct filedescr *fdp;
2366 FILE *fp = NULL;
2367
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002368 if (mod == Py_None)
2369 path = NULL;
2370 else {
2371 path = PyObject_GetAttrString(mod, "__path__");
2372 if (path == NULL) {
2373 PyErr_Clear();
2374 Py_INCREF(Py_None);
2375 return Py_None;
2376 }
2377 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002378
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002379 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002380 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2381 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002382 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002383 if (fdp == NULL) {
2384 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2385 return NULL;
2386 PyErr_Clear();
2387 Py_INCREF(Py_None);
2388 return Py_None;
2389 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002390 m = load_module(fullname, fp, buf, fdp->type, loader);
2391 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002392 if (fp)
2393 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002394 if (!add_submodule(mod, m, fullname, subname, modules)) {
2395 Py_XDECREF(m);
2396 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002397 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002398 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002399
2400 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002401}
2402
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002403
2404/* Re-import a module of any kind and return its module object, WITH
2405 INCREMENTED REFERENCE COUNT */
2406
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002408PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002409{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002410 PyObject *modules = PyImport_GetModuleDict();
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002411 PyObject *path = NULL, *loader = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002412 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002413 char buf[MAXPATHLEN+1];
2414 struct filedescr *fdp;
2415 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002416 PyObject *newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418 if (m == NULL || !PyModule_Check(m)) {
2419 PyErr_SetString(PyExc_TypeError,
2420 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002421 return NULL;
2422 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002423 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002424 if (name == NULL)
2425 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002426 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002427 PyErr_Format(PyExc_ImportError,
2428 "reload(): module %.200s not in sys.modules",
2429 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002430 return NULL;
2431 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002432 subname = strrchr(name, '.');
2433 if (subname == NULL)
2434 subname = name;
2435 else {
2436 PyObject *parentname, *parent;
2437 parentname = PyString_FromStringAndSize(name, (subname-name));
2438 if (parentname == NULL)
2439 return NULL;
2440 parent = PyDict_GetItem(modules, parentname);
2441 if (parent == NULL) {
2442 PyErr_Format(PyExc_ImportError,
2443 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002444 PyString_AS_STRING(parentname));
2445 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002446 return NULL;
2447 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002448 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002449 subname++;
2450 path = PyObject_GetAttrString(parent, "__path__");
2451 if (path == NULL)
2452 PyErr_Clear();
2453 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002454 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002455 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002456 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002457
2458 if (fdp == NULL) {
2459 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002460 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002461 }
2462
2463 newm = load_module(name, fp, buf, fdp->type, loader);
2464 Py_XDECREF(loader);
2465
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002466 if (fp)
2467 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002468 if (newm == NULL) {
2469 /* load_module probably removed name from modules because of
2470 * the error. Put back the original module object. We're
2471 * going to return NULL in this case regardless of whether
2472 * replacing name succeeds, so the return value is ignored.
2473 */
2474 PyDict_SetItemString(modules, name, m);
2475 }
2476 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002477}
2478
2479
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002480/* Higher-level import emulator which emulates the "import" statement
2481 more accurately -- it invokes the __import__() function from the
2482 builtins of the current globals. This means that the import is
2483 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002484 environment, e.g. by "rexec".
2485 A dummy list ["__doc__"] is passed as the 4th argument so that
2486 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2487 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002488
2489PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002490PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002491{
2492 static PyObject *silly_list = NULL;
2493 static PyObject *builtins_str = NULL;
2494 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002495 PyObject *globals = NULL;
2496 PyObject *import = NULL;
2497 PyObject *builtins = NULL;
2498 PyObject *r = NULL;
2499
2500 /* Initialize constant string objects */
2501 if (silly_list == NULL) {
2502 import_str = PyString_InternFromString("__import__");
2503 if (import_str == NULL)
2504 return NULL;
2505 builtins_str = PyString_InternFromString("__builtins__");
2506 if (builtins_str == NULL)
2507 return NULL;
2508 silly_list = Py_BuildValue("[s]", "__doc__");
2509 if (silly_list == NULL)
2510 return NULL;
2511 }
2512
2513 /* Get the builtins from current globals */
2514 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002515 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002516 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002517 builtins = PyObject_GetItem(globals, builtins_str);
2518 if (builtins == NULL)
2519 goto err;
2520 }
2521 else {
2522 /* No globals -- use standard builtins, and fake globals */
2523 PyErr_Clear();
2524
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002525 builtins = PyImport_ImportModuleLevel("__builtin__",
2526 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002527 if (builtins == NULL)
2528 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002529 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2530 if (globals == NULL)
2531 goto err;
2532 }
2533
2534 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002535 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002536 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002537 if (import == NULL)
2538 PyErr_SetObject(PyExc_KeyError, import_str);
2539 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002540 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002541 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002542 if (import == NULL)
2543 goto err;
2544
2545 /* Call the _import__ function with the proper argument list */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002546 r = PyObject_CallFunctionObjArgs(import, module_name, globals,
2547 globals, silly_list, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002548
2549 err:
2550 Py_XDECREF(globals);
2551 Py_XDECREF(builtins);
2552 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002553
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002554 return r;
2555}
2556
2557
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558/* Module 'imp' provides Python access to the primitives used for
2559 importing modules.
2560*/
2561
Guido van Rossum79f25d91997-04-29 20:08:16 +00002562static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002563imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002564{
2565 char buf[4];
2566
Guido van Rossum96774c12000-05-01 20:19:08 +00002567 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2568 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2569 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2570 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002571
Guido van Rossum79f25d91997-04-29 20:08:16 +00002572 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002573}
2574
Guido van Rossum79f25d91997-04-29 20:08:16 +00002575static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002576imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002577{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002578 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002579 struct filedescr *fdp;
2580
Guido van Rossum79f25d91997-04-29 20:08:16 +00002581 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002582 if (list == NULL)
2583 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2585 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002586 fdp->suffix, fdp->mode, fdp->type);
2587 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002588 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002589 return NULL;
2590 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 if (PyList_Append(list, item) < 0) {
2592 Py_DECREF(list);
2593 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002594 return NULL;
2595 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002596 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002597 }
2598 return list;
2599}
2600
Guido van Rossum79f25d91997-04-29 20:08:16 +00002601static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002602call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002603{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002604 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002606 struct filedescr *fdp;
2607 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002608 FILE *fp = NULL;
2609
2610 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002611 if (path == Py_None)
2612 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002613 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002614 if (fdp == NULL)
2615 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002616 if (fp != NULL) {
2617 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2618 if (fob == NULL) {
2619 fclose(fp);
2620 return NULL;
2621 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002622 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002623 else {
2624 fob = Py_None;
2625 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002626 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002628 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002629 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002630 return ret;
2631}
2632
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002634imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002635{
2636 char *name;
2637 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002638 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002639 return NULL;
2640 return call_find_module(name, path);
2641}
2642
2643static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002644imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002645{
2646 char *name;
2647 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002649 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002650 return NULL;
2651 ret = init_builtin(name);
2652 if (ret < 0)
2653 return NULL;
2654 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002655 Py_INCREF(Py_None);
2656 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002657 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002658 m = PyImport_AddModule(name);
2659 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002660 return m;
2661}
2662
Guido van Rossum79f25d91997-04-29 20:08:16 +00002663static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002664imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002665{
2666 char *name;
2667 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002668 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002669 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002670 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002671 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002672 if (ret < 0)
2673 return NULL;
2674 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002675 Py_INCREF(Py_None);
2676 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002677 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002678 m = PyImport_AddModule(name);
2679 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002680 return m;
2681}
2682
Guido van Rossum79f25d91997-04-29 20:08:16 +00002683static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002684imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002685{
2686 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002687
Guido van Rossum43713e52000-02-29 13:59:29 +00002688 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002689 return NULL;
2690 return get_frozen_object(name);
2691}
2692
Guido van Rossum79f25d91997-04-29 20:08:16 +00002693static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002694imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002695{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002696 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002697 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002698 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002699 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002700}
2701
Guido van Rossum79f25d91997-04-29 20:08:16 +00002702static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002703imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002704{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002705 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002706 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002707 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002708 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002709 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002710 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002711}
2712
2713static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002714get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002715{
2716 FILE *fp;
2717 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002718 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002719 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002720 fp = fopen(pathname, mode);
2721 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002722 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002723 }
2724 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002725 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002726 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002727 PyErr_SetString(PyExc_ValueError,
2728 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002729 }
2730 return fp;
2731}
2732
Guido van Rossum79f25d91997-04-29 20:08:16 +00002733static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002734imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002735{
2736 char *name;
2737 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002738 PyObject *fob = NULL;
2739 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002740 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002741 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002742 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002743 return NULL;
2744 fp = get_file(pathname, fob, "rb");
2745 if (fp == NULL)
2746 return NULL;
2747 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002748 if (fob == NULL)
2749 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002750 return m;
2751}
2752
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002753#ifdef HAVE_DYNAMIC_LOADING
2754
Guido van Rossum79f25d91997-04-29 20:08:16 +00002755static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002756imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002757{
2758 char *name;
2759 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002760 PyObject *fob = NULL;
2761 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002762 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002763 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002764 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002765 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002766 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002767 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002768 if (fp == NULL)
2769 return NULL;
2770 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002771 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002772 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002773}
2774
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002775#endif /* HAVE_DYNAMIC_LOADING */
2776
Guido van Rossum79f25d91997-04-29 20:08:16 +00002777static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002778imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002779{
2780 char *name;
2781 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002782 PyObject *fob = NULL;
2783 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002784 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002785 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002786 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002787 return NULL;
2788 fp = get_file(pathname, fob, "r");
2789 if (fp == NULL)
2790 return NULL;
2791 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002792 if (fob == NULL)
2793 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002794 return m;
2795}
2796
Guido van Rossum79f25d91997-04-29 20:08:16 +00002797static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002798imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002799{
2800 char *name;
2801 PyObject *fob;
2802 char *pathname;
2803 char *suffix; /* Unused */
2804 char *mode;
2805 int type;
2806 FILE *fp;
2807
Guido van Rossum43713e52000-02-29 13:59:29 +00002808 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002809 &name, &fob, &pathname,
2810 &suffix, &mode, &type))
2811 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002812 if (*mode) {
2813 /* Mode must start with 'r' or 'U' and must not contain '+'.
2814 Implicit in this test is the assumption that the mode
2815 may contain other modifiers like 'b' or 't'. */
2816
2817 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002818 PyErr_Format(PyExc_ValueError,
2819 "invalid file open mode %.200s", mode);
2820 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002821 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002822 }
2823 if (fob == Py_None)
2824 fp = NULL;
2825 else {
2826 if (!PyFile_Check(fob)) {
2827 PyErr_SetString(PyExc_ValueError,
2828 "load_module arg#2 should be a file or None");
2829 return NULL;
2830 }
2831 fp = get_file(pathname, fob, mode);
2832 if (fp == NULL)
2833 return NULL;
2834 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002835 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002836}
2837
2838static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002839imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002840{
2841 char *name;
2842 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002843 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002844 return NULL;
2845 return load_package(name, pathname);
2846}
2847
2848static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002849imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002850{
2851 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002852 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002853 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002854 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002855}
2856
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002857/* Doc strings */
2858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002859PyDoc_STRVAR(doc_imp,
2860"This module provides the components needed to build your own\n\
2861__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002863PyDoc_STRVAR(doc_find_module,
2864"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002865Search for a module. If path is omitted or None, search for a\n\
2866built-in, frozen or special module and continue search in sys.path.\n\
2867The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002868package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002870PyDoc_STRVAR(doc_load_module,
2871"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002872Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002874
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002875PyDoc_STRVAR(doc_get_magic,
2876"get_magic() -> string\n\
2877Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002879PyDoc_STRVAR(doc_get_suffixes,
2880"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002881Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002882that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002884PyDoc_STRVAR(doc_new_module,
2885"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002886Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002887The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002889PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002890"lock_held() -> boolean\n\
2891Return True if the import lock is currently held, else False.\n\
2892On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002893
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002894PyDoc_STRVAR(doc_acquire_lock,
2895"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002896Acquires the interpreter's import lock for the current thread.\n\
2897This lock should be used by import hooks to ensure thread-safety\n\
2898when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002899On platforms without threads, this function does nothing.");
2900
2901PyDoc_STRVAR(doc_release_lock,
2902"release_lock() -> None\n\
2903Release the interpreter's import lock.\n\
2904On platforms without threads, this function does nothing.");
2905
Guido van Rossum79f25d91997-04-29 20:08:16 +00002906static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002907 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2908 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2909 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2910 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2911 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2912 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2913 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2914 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002915 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002916 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2917 {"init_builtin", imp_init_builtin, METH_VARARGS},
2918 {"init_frozen", imp_init_frozen, METH_VARARGS},
2919 {"is_builtin", imp_is_builtin, METH_VARARGS},
2920 {"is_frozen", imp_is_frozen, METH_VARARGS},
2921 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002922#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002923 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002924#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002925 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002926 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002927 {NULL, NULL} /* sentinel */
2928};
2929
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002930static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002931setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002932{
2933 PyObject *v;
2934 int err;
2935
2936 v = PyInt_FromLong((long)value);
2937 err = PyDict_SetItemString(d, name, v);
2938 Py_XDECREF(v);
2939 return err;
2940}
2941
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002942typedef struct {
2943 PyObject_HEAD
2944} NullImporter;
2945
2946static int
2947NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
2948{
2949 char *path;
2950
2951 if (!_PyArg_NoKeywords("NullImporter()", kwds))
2952 return -1;
2953
2954 if (!PyArg_ParseTuple(args, "s:NullImporter",
2955 &path))
2956 return -1;
2957
2958 if (strlen(path) == 0) {
2959 PyErr_SetString(PyExc_ImportError, "empty pathname");
2960 return -1;
2961 } else {
2962#ifndef RISCOS
2963 struct stat statbuf;
2964 int rv;
2965
2966 rv = stat(path, &statbuf);
2967 if (rv == 0) {
2968 /* it exists */
2969 if (S_ISDIR(statbuf.st_mode)) {
2970 /* it's a directory */
2971 PyErr_SetString(PyExc_ImportError,
2972 "existing directory");
2973 return -1;
2974 }
2975 }
2976#else
2977 if (object_exists(path)) {
2978 /* it exists */
2979 if (isdir(path)) {
2980 /* it's a directory */
2981 PyErr_SetString(PyExc_ImportError,
2982 "existing directory");
2983 return -1;
2984 }
2985 }
2986#endif
2987 }
2988 return 0;
2989}
2990
2991static PyObject *
2992NullImporter_find_module(NullImporter *self, PyObject *args)
2993{
2994 Py_RETURN_NONE;
2995}
2996
2997static PyMethodDef NullImporter_methods[] = {
2998 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
2999 "Always return None"
3000 },
3001 {NULL} /* Sentinel */
3002};
3003
3004
3005static PyTypeObject NullImporterType = {
3006 PyObject_HEAD_INIT(NULL)
3007 0, /*ob_size*/
3008 "imp.NullImporter", /*tp_name*/
3009 sizeof(NullImporter), /*tp_basicsize*/
3010 0, /*tp_itemsize*/
3011 0, /*tp_dealloc*/
3012 0, /*tp_print*/
3013 0, /*tp_getattr*/
3014 0, /*tp_setattr*/
3015 0, /*tp_compare*/
3016 0, /*tp_repr*/
3017 0, /*tp_as_number*/
3018 0, /*tp_as_sequence*/
3019 0, /*tp_as_mapping*/
3020 0, /*tp_hash */
3021 0, /*tp_call*/
3022 0, /*tp_str*/
3023 0, /*tp_getattro*/
3024 0, /*tp_setattro*/
3025 0, /*tp_as_buffer*/
3026 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3027 "Null importer object", /* tp_doc */
3028 0, /* tp_traverse */
3029 0, /* tp_clear */
3030 0, /* tp_richcompare */
3031 0, /* tp_weaklistoffset */
3032 0, /* tp_iter */
3033 0, /* tp_iternext */
3034 NullImporter_methods, /* tp_methods */
3035 0, /* tp_members */
3036 0, /* tp_getset */
3037 0, /* tp_base */
3038 0, /* tp_dict */
3039 0, /* tp_descr_get */
3040 0, /* tp_descr_set */
3041 0, /* tp_dictoffset */
3042 (initproc)NullImporter_init, /* tp_init */
3043 0, /* tp_alloc */
3044 PyType_GenericNew /* tp_new */
3045};
3046
3047
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003048PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003049initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003050{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003051 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003052
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003053 if (PyType_Ready(&NullImporterType) < 0)
3054 goto failure;
3055
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003056 m = Py_InitModule4("imp", imp_methods, doc_imp,
3057 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003058 if (m == NULL)
3059 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003060 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003061 if (d == NULL)
3062 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003063
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003064 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3065 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3066 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3067 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3068 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3069 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3070 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3071 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003072 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003073 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003074
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003075 Py_INCREF(&NullImporterType);
3076 PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003077 failure:
3078 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003079}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003080
3081
Guido van Rossumb18618d2000-05-03 23:44:39 +00003082/* API for embedding applications that want to add their own entries
3083 to the table of built-in modules. This should normally be called
3084 *before* Py_Initialize(). When the table resize fails, -1 is
3085 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003086
3087 After a similar function by Just van Rossum. */
3088
3089int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003090PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003091{
3092 static struct _inittab *our_copy = NULL;
3093 struct _inittab *p;
3094 int i, n;
3095
3096 /* Count the number of entries in both tables */
3097 for (n = 0; newtab[n].name != NULL; n++)
3098 ;
3099 if (n == 0)
3100 return 0; /* Nothing to do */
3101 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3102 ;
3103
3104 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003105 p = our_copy;
3106 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003107 if (p == NULL)
3108 return -1;
3109
3110 /* Copy the tables into the new memory */
3111 if (our_copy != PyImport_Inittab)
3112 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3113 PyImport_Inittab = our_copy = p;
3114 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3115
3116 return 0;
3117}
3118
3119/* Shorthand to add a single entry given a name and a function */
3120
3121int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003122PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003123{
3124 struct _inittab newtab[2];
3125
3126 memset(newtab, '\0', sizeof newtab);
3127
3128 newtab[0].name = name;
3129 newtab[0].initfunc = initfunc;
3130
3131 return PyImport_ExtendInittab(newtab);
3132}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003133
3134#ifdef __cplusplus
3135}
3136#endif