blob: f1d81882bda4b6b2fdcf3abe328ea6b04321ba09 [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"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00008#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009#include "pythonrun.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000011#include "marshal.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012#include "code.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000013#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000014#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000015#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000016#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000017
Guido van Rossum55a83382000-09-20 20:31:38 +000018#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021#ifdef __cplusplus
22extern "C" {
23#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000024
Christian Heimesd3eb5a152008-02-24 00:38:49 +000025#ifdef MS_WINDOWS
26/* for stat.st_mode */
27typedef unsigned short mode_t;
28#endif
29
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030extern time_t PyOS_GetLastModificationTime(char *, FILE *);
31 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000032
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000033/* Magic word to reject .pyc files generated by other Python versions.
34 It should change for each incompatible change to the bytecode.
35
36 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000037 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000038 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000039
Guido van Rossum45aecf42006-03-15 04:58:47 +000040 The magic numbers must be spaced apart at least 2 values, as the
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000041 -U interpeter flag will cause MAGIC+1 being used. They have been
42 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000043
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000044 There were a variety of old schemes for setting the magic number.
45 The current working scheme is to increment the previous value by
46 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000047
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000048 Known values:
49 Python 1.5: 20121
50 Python 1.5.1: 20121
51 Python 1.5.2: 20121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000053 Python 2.0: 50823
54 Python 2.0.1: 50823
55 Python 2.1: 60202
56 Python 2.1.1: 60202
57 Python 2.1.2: 60202
58 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000059 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000060 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000061 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000062 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000063 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000064 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000065 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000066 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000067 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000068 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
70 Python 2.5b3: 62111 (fix wrong code: x += yield)
71 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
72 storing constants that should have been removed)
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Christian Heimes99170a52007-12-19 02:07:34 +000074 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000075 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Guido van Rossum45aecf42006-03-15 04:58:47 +000076 Python 3000: 3000
Brett Cannone2e23ef2006-08-25 05:05:30 +000077 3010 (removed UNARY_CONVERT)
Guido van Rossum86e58e22006-08-28 15:27:34 +000078 3020 (added BUILD_SET)
Guido van Rossum4f72a782006-10-27 23:31:49 +000079 3030 (added keyword-only parameters)
Guido van Rossum3203bdc2006-12-28 21:03:31 +000080 3040 (added signature annotations)
Guido van Rossum452bf512007-02-09 05:32:43 +000081 3050 (print becomes a function)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000082 3060 (PEP 3115 metaclass syntax)
Guido van Rossum00bc0e02007-10-15 02:52:41 +000083 3070 (PEP 3109 raise changes)
84 3080 (PEP 3137 make __file__ and __name__ unicode)
Guido van Rossum98297ee2007-11-06 21:34:58 +000085 3090 (kill str8 interning)
Christian Heimes99170a52007-12-19 02:07:34 +000086 3100 (merge from 2.6a0, see 62151)
Christian Heimes3b06e532008-01-07 20:12:44 +000087 3102 (__file__ points to source file)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000088 Python 3.0a4: 3110 (WITH_CLEANUP optimization).
Tim Peters36515e22001-11-18 04:06:29 +000089*/
Christian Heimesdd15f6c2008-03-16 00:07:10 +000090#define MAGIC (3110 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000091
Guido van Rossum96774c12000-05-01 20:19:08 +000092/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000093 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000094 compiler works which are enabled by command line switches. */
95static long pyc_magic = MAGIC;
96
Guido van Rossum25ce5661997-08-02 03:10:38 +000097/* See _PyImport_FixupExtension() below */
98static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000099
Guido van Rossum771c6c81997-10-31 18:37:24 +0000100/* This table is defined in config.c: */
101extern struct _inittab _PyImport_Inittab[];
102
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000103/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000104extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000105
Guido van Rossum771c6c81997-10-31 18:37:24 +0000106struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000107
Guido van Rossumed1170e1999-12-20 21:23:41 +0000108/* these tables define the module suffixes that Python recognizes */
109struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000110
Guido van Rossumed1170e1999-12-20 21:23:41 +0000111static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000112 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000113#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000114 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000115#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000116 {".pyc", "rb", PY_COMPILED},
117 {0, 0}
118};
119
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000121/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122
123void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000126 const struct filedescr *scan;
127 struct filedescr *filetab;
128 int countD = 0;
129 int countS = 0;
130
131 /* prepare _PyImport_Filetab: copy entries from
132 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
133 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000134#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000135 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
136 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000137#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000138 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
139 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000140 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141 if (filetab == NULL)
142 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000143#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000144 memcpy(filetab, _PyImport_DynLoadFiletab,
145 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000146#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000147 memcpy(filetab + countD, _PyImport_StandardFiletab,
148 countS * sizeof(struct filedescr));
149 filetab[countD + countS].suffix = NULL;
150
151 _PyImport_Filetab = filetab;
152
Guido van Rossum0824f631997-03-11 18:37:35 +0000153 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000154 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
155 for (; filetab->suffix != NULL; filetab++) {
156 if (strcmp(filetab->suffix, ".pyc") == 0)
157 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000158 }
159 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000160
Guido van Rossum572dbf82007-04-27 23:53:51 +0000161 {
Guido van Rossum96774c12000-05-01 20:19:08 +0000162 /* Fix the pyc_magic so that byte compiled code created
163 using the all-Unicode method doesn't interfere with
164 code created in normal operation mode. */
165 pyc_magic = MAGIC + 1;
166 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169void
Just van Rossum52e14d62002-12-30 22:08:05 +0000170_PyImportHooks_Init(void)
171{
172 PyObject *v, *path_hooks = NULL, *zimpimport;
173 int err = 0;
174
175 /* adding sys.path_hooks and sys.path_importer_cache, setting up
176 zipimport */
Christian Heimes9cd17752007-11-18 19:35:23 +0000177 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000179
180 if (Py_VerboseFlag)
181 PySys_WriteStderr("# installing zipimport hook\n");
182
183 v = PyList_New(0);
184 if (v == NULL)
185 goto error;
186 err = PySys_SetObject("meta_path", v);
187 Py_DECREF(v);
188 if (err)
189 goto error;
190 v = PyDict_New();
191 if (v == NULL)
192 goto error;
193 err = PySys_SetObject("path_importer_cache", v);
194 Py_DECREF(v);
195 if (err)
196 goto error;
197 path_hooks = PyList_New(0);
198 if (path_hooks == NULL)
199 goto error;
200 err = PySys_SetObject("path_hooks", path_hooks);
201 if (err) {
202 error:
203 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
205 "path_importer_cache, or NullImporter failed"
206 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000207 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208
Just van Rossum52e14d62002-12-30 22:08:05 +0000209 zimpimport = PyImport_ImportModule("zipimport");
210 if (zimpimport == NULL) {
211 PyErr_Clear(); /* No zip import module -- okay */
212 if (Py_VerboseFlag)
213 PySys_WriteStderr("# can't import zipimport\n");
214 }
215 else {
216 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
217 "zipimporter");
218 Py_DECREF(zimpimport);
219 if (zipimporter == NULL) {
220 PyErr_Clear(); /* No zipimporter object -- okay */
221 if (Py_VerboseFlag)
222 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000223 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000224 }
225 else {
226 /* sys.path_hooks.append(zipimporter) */
227 err = PyList_Append(path_hooks, zipimporter);
228 Py_DECREF(zipimporter);
229 if (err)
230 goto error;
231 if (Py_VerboseFlag)
232 PySys_WriteStderr(
233 "# installed zipimport hook\n");
234 }
235 }
236 Py_DECREF(path_hooks);
237}
238
239void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241{
242 Py_XDECREF(extensions);
243 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000244 PyMem_DEL(_PyImport_Filetab);
245 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246}
247
248
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249/* Locking primitives to prevent parallel imports of the same module
250 in different threads to return with a partially loaded module.
251 These calls are serialized by the global interpreter lock. */
252
253#ifdef WITH_THREAD
254
Guido van Rossum49b56061998-10-01 20:42:43 +0000255#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258static long import_lock_thread = -1;
259static int import_lock_level = 0;
260
261static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000263{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000265 if (me == -1)
266 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269 if (import_lock == NULL)
270 return; /* Nothing much we can do. */
271 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000272 if (import_lock_thread == me) {
273 import_lock_level++;
274 return;
275 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000276 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
277 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000278 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000279 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000280 PyEval_RestoreThread(tstate);
281 }
282 import_lock_thread = me;
283 import_lock_level = 1;
284}
285
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000288{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000289 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000290 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000291 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000292 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000293 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000294 import_lock_level--;
295 if (import_lock_level == 0) {
296 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000298 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000299 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000300}
301
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000302/* This function is called from PyOS_AfterFork to ensure that newly
303 created child processes do not share locks with the parent. */
304
305void
306_PyImport_ReInitLock(void)
307{
308#ifdef _AIX
309 if (import_lock != NULL)
310 import_lock = PyThread_allocate_lock();
311#endif
312}
313
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000314#else
315
316#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000317#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000318
319#endif
320
Tim Peters69232342001-08-30 05:16:13 +0000321static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000322imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000323{
Tim Peters69232342001-08-30 05:16:13 +0000324#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000325 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000326#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000327 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000328#endif
329}
330
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000331static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000332imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000333{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000334#ifdef WITH_THREAD
335 lock_import();
336#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000337 Py_INCREF(Py_None);
338 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000339}
340
341static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000342imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000343{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000344#ifdef WITH_THREAD
345 if (unlock_import() < 0) {
346 PyErr_SetString(PyExc_RuntimeError,
347 "not holding the import lock");
348 return NULL;
349 }
350#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000351 Py_INCREF(Py_None);
352 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000353}
354
Guido van Rossumd8faa362007-04-27 19:54:29 +0000355static void
356imp_modules_reloading_clear(void)
357{
358 PyInterpreterState *interp = PyThreadState_Get()->interp;
359 if (interp->modules_reloading != NULL)
360 PyDict_Clear(interp->modules_reloading);
361}
362
Guido van Rossum25ce5661997-08-02 03:10:38 +0000363/* Helper for sys */
364
365PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000366PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000368 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000369 if (interp->modules == NULL)
370 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
371 return interp->modules;
372}
373
Guido van Rossum3f5da241990-12-20 15:06:42 +0000374
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000375/* List of names to clear in sys */
376static char* sys_deletes[] = {
Collin Winter670e6922007-03-21 02:57:17 +0000377 "path", "argv", "ps1", "ps2",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000378 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000379 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000380 /* misc stuff */
381 "flags", "float_info",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000382 NULL
383};
384
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000385static char* sys_files[] = {
386 "stdin", "__stdin__",
387 "stdout", "__stdout__",
388 "stderr", "__stderr__",
389 NULL
390};
391
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000392
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000393/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000394
Guido van Rossum3f5da241990-12-20 15:06:42 +0000395void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000396PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000397{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000398 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000399 char *name;
400 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000401 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000402 PyObject *modules = interp->modules;
403
404 if (modules == NULL)
405 return; /* Already done */
406
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000407 /* Delete some special variables first. These are common
408 places where user values hide and people complain when their
409 destructors fail. Since the modules containing them are
410 deleted *last* of all, they would come too late in the normal
411 destruction order. Sigh. */
412
Georg Brandl1a3284e2007-12-02 09:40:06 +0000413 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000414 if (value != NULL && PyModule_Check(value)) {
415 dict = PyModule_GetDict(value);
416 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000417 PySys_WriteStderr("# clear builtins._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000418 PyDict_SetItemString(dict, "_", Py_None);
419 }
420 value = PyDict_GetItemString(modules, "sys");
421 if (value != NULL && PyModule_Check(value)) {
422 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000423 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000424 dict = PyModule_GetDict(value);
425 for (p = sys_deletes; *p != NULL; p++) {
426 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000427 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000428 PyDict_SetItemString(dict, *p, Py_None);
429 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000430 for (p = sys_files; *p != NULL; p+=2) {
431 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000432 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000433 v = PyDict_GetItemString(dict, *(p+1));
434 if (v == NULL)
435 v = Py_None;
436 PyDict_SetItemString(dict, *p, v);
437 }
438 }
439
440 /* First, delete __main__ */
441 value = PyDict_GetItemString(modules, "__main__");
442 if (value != NULL && PyModule_Check(value)) {
443 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000444 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000445 _PyModule_Clear(value);
446 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000447 }
448
Georg Brandl1a3284e2007-12-02 09:40:06 +0000449 /* The special treatment of "builtins" here is because even
Guido van Rossum758eec01998-01-19 21:58:26 +0000450 when it's not referenced as a module, its dictionary is
451 referenced by almost every module's __builtins__. Since
452 deleting a module clears its dictionary (even if there are
Georg Brandl1a3284e2007-12-02 09:40:06 +0000453 references left to it), we need to delete the "builtins"
Guido van Rossum758eec01998-01-19 21:58:26 +0000454 module last. Likewise, we don't delete sys until the very
455 end because it is implicitly referenced (e.g. by print).
456
457 Also note that we 'delete' modules by replacing their entry
458 in the modules dict with None, rather than really deleting
459 them; this avoids a rehash of the modules dictionary and
460 also marks them as "non existent" so they won't be
461 re-imported. */
462
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000463 /* Next, repeatedly delete modules with a reference count of
Georg Brandl1a3284e2007-12-02 09:40:06 +0000464 one (skipping builtins and sys) and delete them */
Guido van Rossum758eec01998-01-19 21:58:26 +0000465 do {
466 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000467 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000468 while (PyDict_Next(modules, &pos, &key, &value)) {
469 if (value->ob_refcnt != 1)
470 continue;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000471 if (PyUnicode_Check(key) && PyModule_Check(value)) {
472 name = PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000473 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000474 continue;
475 if (strcmp(name, "sys") == 0)
476 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000477 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000478 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000479 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000480 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000481 PyDict_SetItem(modules, key, Py_None);
482 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000483 }
484 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000485 } while (ndone > 0);
486
Georg Brandl1a3284e2007-12-02 09:40:06 +0000487 /* Next, delete all modules (still skipping builtins and sys) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000488 pos = 0;
489 while (PyDict_Next(modules, &pos, &key, &value)) {
Neal Norwitz80e7f272007-08-26 06:45:23 +0000490 if (PyUnicode_Check(key) && PyModule_Check(value)) {
491 name = PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000492 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000493 continue;
494 if (strcmp(name, "sys") == 0)
495 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000496 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000497 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000498 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000499 PyDict_SetItem(modules, key, Py_None);
500 }
501 }
502
Georg Brandl1a3284e2007-12-02 09:40:06 +0000503 /* Next, delete sys and builtins (in that order) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000504 value = PyDict_GetItemString(modules, "sys");
505 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000506 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000507 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000508 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000509 PyDict_SetItemString(modules, "sys", Py_None);
510 }
Georg Brandl1a3284e2007-12-02 09:40:06 +0000511 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossum758eec01998-01-19 21:58:26 +0000512 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000513 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000514 PySys_WriteStderr("# cleanup builtins\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000515 _PyModule_Clear(value);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000516 PyDict_SetItemString(modules, "builtins", Py_None);
Guido van Rossum758eec01998-01-19 21:58:26 +0000517 }
518
519 /* Finally, clear and delete the modules directory */
520 PyDict_Clear(modules);
521 interp->modules = NULL;
522 Py_DECREF(modules);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000523 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000524}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000525
526
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000527/* Helper for pythonrun.c -- return magic number */
528
529long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000531{
Guido van Rossum96774c12000-05-01 20:19:08 +0000532 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533}
534
535
Guido van Rossum25ce5661997-08-02 03:10:38 +0000536/* Magic for extension modules (built-in as well as dynamically
537 loaded). To prevent initializing an extension module more than
538 once, we keep a static dictionary 'extensions' keyed by module name
539 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000540 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541 dictionary is stored by calling _PyImport_FixupExtension()
542 immediately after the module initialization function succeeds. A
543 copy can be retrieved from there by calling
Martin v. Löwis1a214512008-06-11 05:26:20 +0000544 _PyImport_FindExtension().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545
Martin v. Löwis1a214512008-06-11 05:26:20 +0000546 Modules which do support multiple multiple initialization set
547 their m_size field to a non-negative number (indicating the size
548 of the module-specific state). They are still recorded in the
549 extensions dictionary, to avoid loading shared libraries twice.
550*/
551
552int
553_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000555 PyObject *modules, *dict;
556 struct PyModuleDef *def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000557 if (extensions == NULL) {
558 extensions = PyDict_New();
559 if (extensions == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000560 return -1;
561 }
562 if (mod == NULL || !PyModule_Check(mod)) {
563 PyErr_BadInternalCall();
564 return -1;
565 }
566 def = PyModule_GetDef(mod);
567 if (!def) {
568 PyErr_BadInternalCall();
569 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000570 }
571 modules = PyImport_GetModuleDict();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572 if (PyDict_SetItemString(modules, name, mod) < 0)
573 return -1;
574 if (_PyState_AddModule(mod, def) < 0) {
575 PyDict_DelItemString(modules, name);
576 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000577 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000578 if (def->m_size == -1) {
579 if (def->m_base.m_copy) {
580 /* Somebody already imported the module,
581 likely under a different name.
582 XXX this should really not happen. */
583 Py_DECREF(def->m_base.m_copy);
584 def->m_base.m_copy = NULL;
585 }
586 dict = PyModule_GetDict(mod);
587 if (dict == NULL)
588 return -1;
589 def->m_base.m_copy = PyDict_Copy(dict);
590 if (def->m_base.m_copy == NULL)
591 return -1;
592 }
593 PyDict_SetItemString(extensions, filename, (PyObject*)def);
594 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595}
596
597PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000598_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000600 PyObject *mod, *mdict;
601 PyModuleDef* def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602 if (extensions == NULL)
603 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000604 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
605 if (def == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000607 if (def->m_size == -1) {
608 /* Module does not support repeated initialization */
609 if (def->m_base.m_copy == NULL)
610 return NULL;
611 mod = PyImport_AddModule(name);
612 if (mod == NULL)
613 return NULL;
614 Py_INCREF(mod);
615 mdict = PyModule_GetDict(mod);
616 if (mdict == NULL)
617 return NULL;
618 if (PyDict_Update(mdict, def->m_base.m_copy))
619 return NULL;
620 }
621 else {
622 if (def->m_base.m_init == NULL)
623 return NULL;
624 mod = def->m_base.m_init();
625 if (mod == NULL)
626 return NULL;
627 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
628 }
629 if (_PyState_AddModule(mod, def) < 0) {
630 PyDict_DelItemString(PyImport_GetModuleDict(), name);
631 Py_DECREF(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000632 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000633 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000634 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000635 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Martin v. Löwis1a214512008-06-11 05:26:20 +0000636 name, filename);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000637 return mod;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000638
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000639}
640
641
642/* Get the module object corresponding to a module name.
643 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000644 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000645 Because the former action is most common, THIS DOES NOT RETURN A
646 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000649PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000651 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653
Guido van Rossum25ce5661997-08-02 03:10:38 +0000654 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000655 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000656 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000658 if (m == NULL)
659 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000660 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662 return NULL;
663 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665
666 return m;
667}
668
Tim Peters1cd70172004-08-02 03:52:12 +0000669/* Remove name from sys.modules, if it's there. */
670static void
671_RemoveModule(const char *name)
672{
673 PyObject *modules = PyImport_GetModuleDict();
674 if (PyDict_GetItemString(modules, name) == NULL)
675 return;
676 if (PyDict_DelItemString(modules, name) < 0)
677 Py_FatalError("import: deleting existing key in"
678 "sys.modules failed");
679}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680
Christian Heimes3b06e532008-01-07 20:12:44 +0000681static PyObject * get_sourcefile(const char *file);
682
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000683/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000684 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
685 * removed from sys.modules, to avoid leaving damaged module objects
686 * in sys.modules. The caller may wish to restore the original
687 * module object (if any) in this case; PyImport_ReloadModule is an
688 * example.
689 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000691PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000693 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
694}
695
696PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000697PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000698{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000699 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703 if (m == NULL)
704 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000705 /* If the module is being reloaded, we get the old module back
706 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 d = PyModule_GetDict(m);
708 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
709 if (PyDict_SetItemString(d, "__builtins__",
710 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000711 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000712 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000713 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000714 v = NULL;
715 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000716 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000717 if (v == NULL)
718 PyErr_Clear();
719 }
720 if (v == NULL) {
721 v = ((PyCodeObject *)co)->co_filename;
722 Py_INCREF(v);
723 }
724 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000726 Py_DECREF(v);
727
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000728 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000730 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000732
Guido van Rossum25ce5661997-08-02 03:10:38 +0000733 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000734 PyErr_Format(PyExc_ImportError,
735 "Loaded module %.200s not found in sys.modules",
736 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000737 return NULL;
738 }
739
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741
742 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000743
744 error:
745 _RemoveModule(name);
746 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747}
748
749
750/* Given a pathname for a Python source file, fill a buffer with the
751 pathname for the corresponding compiled file. Return the pathname
752 for the compiled file, or NULL if there's no space in the buffer.
753 Doesn't set an exception. */
754
755static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757{
Tim Petersc1731372001-08-04 08:12:36 +0000758 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 if (len+2 > buflen)
760 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000761
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000762#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000763 /* Treat .pyw as if it were .py. The case of ".pyw" must match
764 that used in _PyImport_StandardFiletab. */
765 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
766 --len; /* pretend 'w' isn't there */
767#endif
768 memcpy(buf, pathname, len);
769 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
770 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771
772 return buf;
773}
774
775
776/* Given a pathname for a Python source file, its time of last
777 modification, and a pathname for a compiled file, check whether the
778 compiled file represents the same version of the source. If so,
779 return a FILE pointer for the compiled file, positioned just after
780 the header; if not, return NULL.
781 Doesn't set an exception. */
782
783static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000784check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785{
786 FILE *fp;
787 long magic;
788 long pyc_mtime;
789
790 fp = fopen(cpathname, "rb");
791 if (fp == NULL)
792 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000794 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000796 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 fclose(fp);
798 return NULL;
799 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000803 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804 fclose(fp);
805 return NULL;
806 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000808 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000809 return fp;
810}
811
812
813/* Read a code object from a file and check it for validity */
814
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000816read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819
Tim Petersd9b9ac82001-01-28 00:27:39 +0000820 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000821 if (co == NULL)
822 return NULL;
823 if (!PyCode_Check(co)) {
824 PyErr_Format(PyExc_ImportError,
825 "Non-code object in %.200s", cpathname);
826 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827 return NULL;
828 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000830}
831
832
833/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000834 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000837load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838{
839 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 PyCodeObject *co;
841 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000842
Guido van Rossum79f25d91997-04-29 20:08:16 +0000843 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000844 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000845 PyErr_Format(PyExc_ImportError,
846 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847 return NULL;
848 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000849 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000850 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851 if (co == NULL)
852 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000854 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000856 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858
859 return m;
860}
861
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862/* Parse a source file and return the corresponding code object */
863
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000866{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 PyCodeObject *co = NULL;
868 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000869 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870 PyArena *arena = PyArena_New();
871 if (arena == NULL)
872 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000874 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000875 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Christian Heimes4d6ec852008-03-26 22:34:47 +0000876 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000877 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000879 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000881 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882 return co;
883}
884
885
Guido van Rossum55a83382000-09-20 20:31:38 +0000886/* Helper to open a bytecode file for writing in exclusive mode */
887
888static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000889open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000890{
891#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
892 /* Use O_EXCL to avoid a race condition when another process tries to
893 write the same file. When that happens, our open() call fails,
894 which is just fine (since it's only a cache).
895 XXX If the file exists and is writable but the directory is not
896 writable, the file will never be written. Oh well.
897 */
898 int fd;
899 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000900 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
901#ifdef O_BINARY
902 |O_BINARY /* necessary for Windows */
903#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000904#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000905 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000906#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000907 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000908#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000909 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000910 if (fd < 0)
911 return NULL;
912 return fdopen(fd, "wb");
913#else
914 /* Best we can do -- on Windows this can't happen anyway */
915 return fopen(filename, "wb");
916#endif
917}
918
919
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000920/* Write a compiled module to a file, placing the time of last
921 modification of its source into the header.
922 Errors are ignored, if a write error occurs an attempt is made to
923 remove the file. */
924
925static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000926write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927{
928 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000929 time_t mtime = srcstat->st_mtime;
930 mode_t mode = srcstat->st_mode;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931
Christian Heimes05e8be12008-02-23 18:30:17 +0000932 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000933 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000935 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 "# can't create %s\n", cpathname);
937 return;
938 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000939 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000941 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
942 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000943 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000945 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000946 /* Don't keep partial file */
947 fclose(fp);
948 (void) unlink(cpathname);
949 return;
950 }
951 /* Now write the true mtime */
952 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000953 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000954 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000955 fflush(fp);
956 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000957 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000958 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959}
960
961
962/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000963 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
964 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000967load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000968{
Christian Heimes05e8be12008-02-23 18:30:17 +0000969 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000970 FILE *fpc;
971 char buf[MAXPATHLEN+1];
972 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 PyCodeObject *co;
974 PyObject *m;
Christian Heimes05e8be12008-02-23 18:30:17 +0000975
976 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +0000977 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +0000978 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +0000979 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000980 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000981 }
Fred Drake4c82b232000-06-30 16:18:57 +0000982#if SIZEOF_TIME_T > 4
983 /* Python's .pyc timestamp handling presumes that the timestamp fits
984 in 4 bytes. This will be fine until sometime in the year 2038,
985 when a 4-byte signed time_t will overflow.
986 */
Christian Heimes05e8be12008-02-23 18:30:17 +0000987 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +0000988 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000989 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000990 return NULL;
991 }
992#endif
Tim Peters36515e22001-11-18 04:06:29 +0000993 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000994 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +0000996 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000997 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000998 fclose(fpc);
999 if (co == NULL)
1000 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001002 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001003 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001004 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001005 }
1006 else {
1007 co = parse_source_module(pathname, fp);
1008 if (co == NULL)
1009 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001010 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001011 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +00001013 if (cpathname) {
1014 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1015 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +00001016 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +00001017 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001019 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001021
1022 return m;
1023}
1024
Christian Heimes3b06e532008-01-07 20:12:44 +00001025/* Get source file -> unicode or None
1026 * Returns the path to the py file if available, else the given path
1027 */
1028static PyObject *
1029get_sourcefile(const char *file)
1030{
1031 char py[MAXPATHLEN + 1];
1032 Py_ssize_t len;
1033 PyObject *u;
1034 struct stat statbuf;
1035
1036 if (!file || !*file) {
1037 Py_RETURN_NONE;
1038 }
1039
1040 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +00001041 /* match '*.py?' */
1042 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +00001043 return PyUnicode_DecodeFSDefault(file);
1044 }
1045
1046 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001047 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001048 if (stat(py, &statbuf) == 0 &&
1049 S_ISREG(statbuf.st_mode)) {
1050 u = PyUnicode_DecodeFSDefault(py);
1051 }
1052 else {
1053 u = PyUnicode_DecodeFSDefault(file);
1054 }
1055 return u;
1056}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001057
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001058/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001059static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1060static struct filedescr *find_module(char *, char *, PyObject *,
1061 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001062static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001063
1064/* Load a package and return its module object WITH INCREMENTED
1065 REFERENCE COUNT */
1066
1067static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001068load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001069{
Tim Peters1cd70172004-08-02 03:52:12 +00001070 PyObject *m, *d;
1071 PyObject *file = NULL;
1072 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001073 int err;
1074 char buf[MAXPATHLEN+1];
1075 FILE *fp = NULL;
1076 struct filedescr *fdp;
1077
1078 m = PyImport_AddModule(name);
1079 if (m == NULL)
1080 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001081 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001082 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001083 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001084 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001085 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001086 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001087 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001088 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001089 if (path == NULL)
1090 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001091 err = PyDict_SetItemString(d, "__file__", file);
1092 if (err == 0)
1093 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001094 if (err != 0)
1095 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001096 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001097 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001098 if (fdp == NULL) {
1099 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1100 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001101 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001102 }
1103 else
1104 m = NULL;
1105 goto cleanup;
1106 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001107 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001108 if (fp != NULL)
1109 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001110 goto cleanup;
1111
1112 error:
1113 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001114 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001115 Py_XDECREF(path);
1116 Py_XDECREF(file);
1117 return m;
1118}
1119
1120
1121/* Helper to test for built-in module */
1122
1123static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001124is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001125{
1126 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001127 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1128 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1129 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001130 return -1;
1131 else
1132 return 1;
1133 }
1134 }
1135 return 0;
1136}
1137
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001138
Just van Rossum52e14d62002-12-30 22:08:05 +00001139/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1140 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001142 that can handle the path item. Return None if no hook could;
1143 this tells our caller it should fall back to the builtin
1144 import mechanism. Cache the result in path_importer_cache.
1145 Returns a borrowed reference. */
1146
1147static PyObject *
1148get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1149 PyObject *p)
1150{
1151 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001152 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001153
1154 /* These conditions are the caller's responsibility: */
1155 assert(PyList_Check(path_hooks));
1156 assert(PyDict_Check(path_importer_cache));
1157
1158 nhooks = PyList_Size(path_hooks);
1159 if (nhooks < 0)
1160 return NULL; /* Shouldn't happen */
1161
1162 importer = PyDict_GetItem(path_importer_cache, p);
1163 if (importer != NULL)
1164 return importer;
1165
1166 /* set path_importer_cache[p] to None to avoid recursion */
1167 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1168 return NULL;
1169
1170 for (j = 0; j < nhooks; j++) {
1171 PyObject *hook = PyList_GetItem(path_hooks, j);
1172 if (hook == NULL)
1173 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001174 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001175 if (importer != NULL)
1176 break;
1177
1178 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1179 return NULL;
1180 }
1181 PyErr_Clear();
1182 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001183 if (importer == NULL) {
1184 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001185 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001186 );
1187 if (importer == NULL) {
1188 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1189 PyErr_Clear();
1190 return Py_None;
1191 }
1192 }
1193 }
1194 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001195 int err = PyDict_SetItem(path_importer_cache, p, importer);
1196 Py_DECREF(importer);
1197 if (err != 0)
1198 return NULL;
1199 }
1200 return importer;
1201}
1202
Christian Heimes9cd17752007-11-18 19:35:23 +00001203PyAPI_FUNC(PyObject *)
1204PyImport_GetImporter(PyObject *path) {
1205 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1206
1207 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1208 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1209 importer = get_path_importer(path_importer_cache,
1210 path_hooks, path);
1211 }
1212 }
1213 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1214 return importer;
1215}
1216
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217/* Search the path (default sys.path) for a module. Return the
1218 corresponding filedescr struct, and (via return arguments) the
1219 pathname and an open file. Return NULL if the module is not found. */
1220
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001221#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001222extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001223 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001224#endif
1225
Martin v. Löwis18e16552006-02-15 17:27:45 +00001226static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001227static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001228static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001229
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001230static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001231find_module(char *fullname, char *subname, PyObject *path, char *buf,
1232 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001234 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001235 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001236 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001237 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001238 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001239 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001240 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001241 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1242 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1243 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001244 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001245#if defined(PYOS_OS2)
1246 size_t saved_len;
1247 size_t saved_namelen;
1248 char *saved_buf = NULL;
1249#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001250 if (p_loader != NULL)
1251 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001252
Just van Rossum52e14d62002-12-30 22:08:05 +00001253 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001254 PyErr_SetString(PyExc_OverflowError,
1255 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001256 return NULL;
1257 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001258 strcpy(name, subname);
1259
1260 /* sys.meta_path import hook */
1261 if (p_loader != NULL) {
1262 PyObject *meta_path;
1263
1264 meta_path = PySys_GetObject("meta_path");
1265 if (meta_path == NULL || !PyList_Check(meta_path)) {
1266 PyErr_SetString(PyExc_ImportError,
1267 "sys.meta_path must be a list of "
1268 "import hooks");
1269 return NULL;
1270 }
1271 Py_INCREF(meta_path); /* zap guard */
1272 npath = PyList_Size(meta_path);
1273 for (i = 0; i < npath; i++) {
1274 PyObject *loader;
1275 PyObject *hook = PyList_GetItem(meta_path, i);
1276 loader = PyObject_CallMethod(hook, "find_module",
1277 "sO", fullname,
1278 path != NULL ?
1279 path : Py_None);
1280 if (loader == NULL) {
1281 Py_DECREF(meta_path);
1282 return NULL; /* true error */
1283 }
1284 if (loader != Py_None) {
1285 /* a loader was found */
1286 *p_loader = loader;
1287 Py_DECREF(meta_path);
1288 return &importhookdescr;
1289 }
1290 Py_DECREF(loader);
1291 }
1292 Py_DECREF(meta_path);
1293 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001294
Guido van Rossum13d77992007-07-23 03:16:50 +00001295 if (path != NULL && PyUnicode_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001296 /* The only type of submodule allowed inside a "frozen"
1297 package are other frozen modules or packages. */
Guido van Rossum13d77992007-07-23 03:16:50 +00001298 char *p = PyUnicode_AsString(path);
1299 if (strlen(p) + 1 + strlen(name) >= (size_t)buflen) {
Guido van Rossum0506a431998-08-11 15:07:39 +00001300 PyErr_SetString(PyExc_ImportError,
1301 "full frozen module name too long");
1302 return NULL;
1303 }
Guido van Rossum13d77992007-07-23 03:16:50 +00001304 strcpy(buf, p);
Guido van Rossum0506a431998-08-11 15:07:39 +00001305 strcat(buf, ".");
1306 strcat(buf, name);
1307 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001308 if (find_frozen(name) != NULL) {
1309 strcpy(buf, name);
1310 return &fd_frozen;
1311 }
1312 PyErr_Format(PyExc_ImportError,
1313 "No frozen submodule named %.200s", name);
1314 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001315 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001316 if (path == NULL) {
1317 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001318 strcpy(buf, name);
1319 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001320 }
Greg Ward201baee2001-10-04 14:52:06 +00001321 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001322 strcpy(buf, name);
1323 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001324 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325
Guido van Rossumac279101996-08-22 23:10:58 +00001326#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001327 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1328 if (fp != NULL) {
1329 *p_fp = fp;
1330 return fdp;
1331 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001332#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001333 path = PySys_GetObject("path");
1334 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001335 if (path == NULL || !PyList_Check(path)) {
1336 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001337 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001338 return NULL;
1339 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001340
1341 path_hooks = PySys_GetObject("path_hooks");
1342 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1343 PyErr_SetString(PyExc_ImportError,
1344 "sys.path_hooks must be a list of "
1345 "import hooks");
1346 return NULL;
1347 }
1348 path_importer_cache = PySys_GetObject("path_importer_cache");
1349 if (path_importer_cache == NULL ||
1350 !PyDict_Check(path_importer_cache)) {
1351 PyErr_SetString(PyExc_ImportError,
1352 "sys.path_importer_cache must be a dict");
1353 return NULL;
1354 }
1355
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001357 namelen = strlen(name);
1358 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001359 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001360 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001361 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001362 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001363 if (!v)
1364 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001365 if (PyUnicode_Check(v)) {
1366 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1367 if (v == NULL)
1368 return NULL;
1369 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001370 if (!PyBytes_Check(v))
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001371 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00001372 base = PyBytes_AS_STRING(v);
1373 size = PyBytes_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001374 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001375 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001376 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001377 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001378 strcpy(buf, base);
Walter Dörwald3430d702002-06-17 10:43:59 +00001379 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001380 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001381 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001382
1383 /* sys.path_hooks import hook */
1384 if (p_loader != NULL) {
1385 PyObject *importer;
1386
1387 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001388 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001389 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001390 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001391 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001392 /* Note: importer is a borrowed reference */
1393 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001394 PyObject *loader;
1395 loader = PyObject_CallMethod(importer,
1396 "find_module",
1397 "s", fullname);
1398 if (loader == NULL)
1399 return NULL; /* error */
1400 if (loader != Py_None) {
1401 /* a loader was found */
1402 *p_loader = loader;
1403 return &importhookdescr;
1404 }
1405 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001406 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001407 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001408 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001410
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001411 if (len > 0 && buf[len-1] != SEP
1412#ifdef ALTSEP
1413 && buf[len-1] != ALTSEP
1414#endif
1415 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001416 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001417 strcpy(buf+len, name);
1418 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001419
1420 /* Check for package import (buf holds a directory name,
1421 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001422#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001423 if (stat(buf, &statbuf) == 0 && /* it exists */
1424 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001425 case_ok(buf, len, namelen, name)) { /* case matches */
1426 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001427 return &fd_package;
1428 }
1429 else {
1430 char warnstr[MAXPATHLEN+80];
1431 sprintf(warnstr, "Not importing directory "
1432 "'%.*s': missing __init__.py",
1433 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001434 if (PyErr_WarnEx(PyExc_ImportWarning,
1435 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001436 return NULL;
1437 }
1438 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001439 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001440#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001441#if defined(PYOS_OS2)
1442 /* take a snapshot of the module spec for restoration
1443 * after the 8 character DLL hackery
1444 */
1445 saved_buf = strdup(buf);
1446 saved_len = len;
1447 saved_namelen = namelen;
1448#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001449 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001450#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001451 /* OS/2 limits DLLs to 8 character names (w/o
1452 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001453 * so if the name is longer than that and its a
1454 * dynamically loaded module we're going to try,
1455 * truncate the name before trying
1456 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001457 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001458 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001459 const struct filedescr *scan;
1460 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001461 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001462 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001463 break;
1464 else
1465 scan++;
1466 }
1467 if (scan->suffix != NULL) {
1468 /* yes, so truncate the name */
1469 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001470 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001471 buf[len] = '\0';
1472 }
1473 }
1474#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001475 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001476 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001477 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001478 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001479 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001480 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001481 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001482 if (fp != NULL) {
1483 if (case_ok(buf, len, namelen, name))
1484 break;
1485 else { /* continue search */
1486 fclose(fp);
1487 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001488 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001489 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001490#if defined(PYOS_OS2)
1491 /* restore the saved snapshot */
1492 strcpy(buf, saved_buf);
1493 len = saved_len;
1494 namelen = saved_namelen;
1495#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001496 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001497#if defined(PYOS_OS2)
1498 /* don't need/want the module name snapshot anymore */
1499 if (saved_buf)
1500 {
1501 free(saved_buf);
1502 saved_buf = NULL;
1503 }
1504#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001505 if (fp != NULL)
1506 break;
1507 }
1508 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001509 PyErr_Format(PyExc_ImportError,
1510 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001511 return NULL;
1512 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001513 *p_fp = fp;
1514 return fdp;
1515}
1516
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001517/* Helpers for main.c
1518 * Find the source file corresponding to a named module
1519 */
1520struct filedescr *
1521_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1522 size_t buflen, FILE **p_fp, PyObject **p_loader)
1523{
1524 return find_module((char *) name, (char *) name, path,
1525 buf, buflen, p_fp, p_loader);
1526}
1527
1528PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1529{
1530 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1531}
1532
Martin v. Löwis18e16552006-02-15 17:27:45 +00001533/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001534 * The arguments here are tricky, best shown by example:
1535 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1536 * ^ ^ ^ ^
1537 * |--------------------- buf ---------------------|
1538 * |------------------- len ------------------|
1539 * |------ name -------|
1540 * |----- namelen -----|
1541 * buf is the full path, but len only counts up to (& exclusive of) the
1542 * extension. name is the module name, also exclusive of extension.
1543 *
1544 * We've already done a successful stat() or fopen() on buf, so know that
1545 * there's some match, possibly case-insensitive.
1546 *
Tim Peters50d8d372001-02-28 05:34:27 +00001547 * case_ok() is to return 1 if there's a case-sensitive match for
1548 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1549 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001550 *
Tim Peters50d8d372001-02-28 05:34:27 +00001551 * case_ok() is used to implement case-sensitive import semantics even
1552 * on platforms with case-insensitive filesystems. It's trivial to implement
1553 * for case-sensitive filesystems. It's pretty much a cross-platform
1554 * nightmare for systems with case-insensitive filesystems.
1555 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001556
Tim Peters50d8d372001-02-28 05:34:27 +00001557/* First we may need a pile of platform-specific header files; the sequence
1558 * of #if's here should match the sequence in the body of case_ok().
1559 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001560#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001561#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001562
Tim Peters50d8d372001-02-28 05:34:27 +00001563#elif defined(DJGPP)
1564#include <dir.h>
1565
Jason Tishler7961aa62005-05-20 00:56:54 +00001566#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001567#include <sys/types.h>
1568#include <dirent.h>
1569
Andrew MacIntyred9400542002-02-26 11:41:34 +00001570#elif defined(PYOS_OS2)
1571#define INCL_DOS
1572#define INCL_DOSERRORS
1573#define INCL_NOPMAPI
1574#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001575#endif
1576
Guido van Rossum0980bd91998-02-13 17:18:36 +00001577static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001578case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001579{
Tim Peters50d8d372001-02-28 05:34:27 +00001580/* Pick a platform-specific implementation; the sequence of #if's here should
1581 * match the sequence just above.
1582 */
1583
Jason Tishler7961aa62005-05-20 00:56:54 +00001584/* MS_WINDOWS */
1585#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001586 WIN32_FIND_DATA data;
1587 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001588
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001589 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001590 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001591
Guido van Rossum0980bd91998-02-13 17:18:36 +00001592 h = FindFirstFile(buf, &data);
1593 if (h == INVALID_HANDLE_VALUE) {
1594 PyErr_Format(PyExc_NameError,
1595 "Can't find file for module %.100s\n(filename %.300s)",
1596 name, buf);
1597 return 0;
1598 }
1599 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001600 return strncmp(data.cFileName, name, namelen) == 0;
1601
1602/* DJGPP */
1603#elif defined(DJGPP)
1604 struct ffblk ffblk;
1605 int done;
1606
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001607 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001608 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001609
1610 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1611 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001612 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001613 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001614 name, buf);
1615 return 0;
1616 }
Tim Peters50d8d372001-02-28 05:34:27 +00001617 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001618
Jason Tishler7961aa62005-05-20 00:56:54 +00001619/* new-fangled macintosh (macosx) or Cygwin */
1620#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001621 DIR *dirp;
1622 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001623 char dirname[MAXPATHLEN + 1];
1624 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001625
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001626 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001627 return 1;
1628
Tim Petersd1e87a82001-03-01 18:12:00 +00001629 /* Copy the dir component into dirname; substitute "." if empty */
1630 if (dirlen <= 0) {
1631 dirname[0] = '.';
1632 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001633 }
1634 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001635 assert(dirlen <= MAXPATHLEN);
1636 memcpy(dirname, buf, dirlen);
1637 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001638 }
1639 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001640 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001641 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001642 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001643 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001644 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001645#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001646 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001647#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001648 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001649#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001650 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001651 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001652 (void)closedir(dirp);
1653 return 1; /* Found */
1654 }
1655 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001656 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001657 }
Tim Peters430f5d42001-03-01 01:30:56 +00001658 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001659
Andrew MacIntyred9400542002-02-26 11:41:34 +00001660/* OS/2 */
1661#elif defined(PYOS_OS2)
1662 HDIR hdir = 1;
1663 ULONG srchcnt = 1;
1664 FILEFINDBUF3 ffbuf;
1665 APIRET rc;
1666
Christian Heimes790c8232008-01-07 21:14:23 +00001667 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001668 return 1;
1669
1670 rc = DosFindFirst(buf,
1671 &hdir,
1672 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1673 &ffbuf, sizeof(ffbuf),
1674 &srchcnt,
1675 FIL_STANDARD);
1676 if (rc != NO_ERROR)
1677 return 0;
1678 return strncmp(ffbuf.achName, name, namelen) == 0;
1679
Tim Peters50d8d372001-02-28 05:34:27 +00001680/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1681#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001682 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001683
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001684#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001685}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001686
Guido van Rossum0980bd91998-02-13 17:18:36 +00001687
Guido van Rossum197346f1997-10-31 18:38:52 +00001688#ifdef HAVE_STAT
1689/* Helper to look for __init__.py or __init__.py[co] in potential package */
1690static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001691find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001692{
Tim Peters0f9431f2001-07-05 03:47:53 +00001693 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001694 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001695 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001696 struct stat statbuf;
1697
Tim Peters0f9431f2001-07-05 03:47:53 +00001698/* For calling case_ok(buf, len, namelen, name):
1699 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1700 * ^ ^ ^ ^
1701 * |--------------------- buf ---------------------|
1702 * |------------------- len ------------------|
1703 * |------ name -------|
1704 * |----- namelen -----|
1705 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001706 if (save_len + 13 >= MAXPATHLEN)
1707 return 0;
1708 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001709 pname = buf + i;
1710 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001711 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001712 if (case_ok(buf,
1713 save_len + 9, /* len("/__init__") */
1714 8, /* len("__init__") */
1715 pname)) {
1716 buf[save_len] = '\0';
1717 return 1;
1718 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001719 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001720 i += strlen(pname);
1721 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001722 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001723 if (case_ok(buf,
1724 save_len + 9, /* len("/__init__") */
1725 8, /* len("__init__") */
1726 pname)) {
1727 buf[save_len] = '\0';
1728 return 1;
1729 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001730 }
1731 buf[save_len] = '\0';
1732 return 0;
1733}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001734
Guido van Rossum197346f1997-10-31 18:38:52 +00001735#endif /* HAVE_STAT */
1736
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001737
Tim Petersdbd9ba62000-07-09 03:09:57 +00001738static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001739
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001741 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001744load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001745{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001746 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001748 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001749
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001750 /* First check that there's an open file (if we need one) */
1751 switch (type) {
1752 case PY_SOURCE:
1753 case PY_COMPILED:
1754 if (fp == NULL) {
1755 PyErr_Format(PyExc_ValueError,
1756 "file object required for import (type code %d)",
1757 type);
1758 return NULL;
1759 }
1760 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001761
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001762 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763
1764 case PY_SOURCE:
1765 m = load_source_module(name, buf, fp);
1766 break;
1767
1768 case PY_COMPILED:
1769 m = load_compiled_module(name, buf, fp);
1770 break;
1771
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001772#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001775 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001776#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001777
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001778 case PKG_DIRECTORY:
1779 m = load_package(name, buf);
1780 break;
1781
1782 case C_BUILTIN:
1783 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001784 if (buf != NULL && buf[0] != '\0')
1785 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001786 if (type == C_BUILTIN)
1787 err = init_builtin(name);
1788 else
1789 err = PyImport_ImportFrozenModule(name);
1790 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001791 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001792 if (err == 0) {
1793 PyErr_Format(PyExc_ImportError,
1794 "Purported %s module %.200s not found",
1795 type == C_BUILTIN ?
1796 "builtin" : "frozen",
1797 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001798 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001799 }
1800 modules = PyImport_GetModuleDict();
1801 m = PyDict_GetItemString(modules, name);
1802 if (m == NULL) {
1803 PyErr_Format(
1804 PyExc_ImportError,
1805 "%s module %.200s not properly initialized",
1806 type == C_BUILTIN ?
1807 "builtin" : "frozen",
1808 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001809 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001810 }
1811 Py_INCREF(m);
1812 break;
1813
Just van Rossum52e14d62002-12-30 22:08:05 +00001814 case IMP_HOOK: {
1815 if (loader == NULL) {
1816 PyErr_SetString(PyExc_ImportError,
1817 "import hook without loader");
1818 return NULL;
1819 }
1820 m = PyObject_CallMethod(loader, "load_module", "s", name);
1821 break;
1822 }
1823
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001824 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001825 PyErr_Format(PyExc_ImportError,
1826 "Don't know how to import %.200s (type code %d)",
1827 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001828 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001829
1830 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001831
1832 return m;
1833}
1834
1835
1836/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001837 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001838 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001839
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001840static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001841init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001842{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001843 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001844
Greg Ward201baee2001-10-04 14:52:06 +00001845 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001846 return 1;
1847
Guido van Rossum771c6c81997-10-31 18:37:24 +00001848 for (p = PyImport_Inittab; p->name != NULL; p++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001849 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001850 if (strcmp(name, p->name) == 0) {
1851 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001852 PyErr_Format(PyExc_ImportError,
1853 "Cannot re-init internal module %.200s",
1854 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001855 return -1;
1856 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001858 PySys_WriteStderr("import %s # builtin\n", name);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001859 mod = (*p->initfunc)();
1860 if (mod == 0)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001861 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001862 if (_PyImport_FixupExtension(mod, name, name) < 0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001863 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001864 /* FixupExtension has put the module into sys.modules,
1865 so we can release our own reference. */
1866 Py_DECREF(mod);
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001867 return 1;
1868 }
1869 }
1870 return 0;
1871}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001872
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001873
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001874/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001875
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001876static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001877find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001878{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001879 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001880
Guido van Rossum79f25d91997-04-29 20:08:16 +00001881 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001882 if (p->name == NULL)
1883 return NULL;
1884 if (strcmp(p->name, name) == 0)
1885 break;
1886 }
1887 return p;
1888}
1889
Guido van Rossum79f25d91997-04-29 20:08:16 +00001890static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001891get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001892{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001893 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001894 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001895
1896 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001897 PyErr_Format(PyExc_ImportError,
1898 "No such frozen object named %.200s",
1899 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001900 return NULL;
1901 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001902 if (p->code == NULL) {
1903 PyErr_Format(PyExc_ImportError,
1904 "Excluded frozen object named %.200s",
1905 name);
1906 return NULL;
1907 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001908 size = p->size;
1909 if (size < 0)
1910 size = -size;
1911 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001912}
1913
1914/* Initialize a frozen module.
1915 Return 1 for succes, 0 if the module is not found, and -1 with
1916 an exception set if the initialization failed.
1917 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001918
1919int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001920PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001921{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001922 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923 PyObject *co;
1924 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001925 int ispackage;
1926 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001927
1928 if (p == NULL)
1929 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001930 if (p->code == NULL) {
1931 PyErr_Format(PyExc_ImportError,
1932 "Excluded frozen object named %.200s",
1933 name);
1934 return -1;
1935 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001936 size = p->size;
1937 ispackage = (size < 0);
1938 if (ispackage)
1939 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001940 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001941 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001942 name, ispackage ? " package" : "");
1943 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001944 if (co == NULL)
1945 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001946 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001947 PyErr_Format(PyExc_TypeError,
1948 "frozen object %.200s is not a code object",
1949 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001950 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001951 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001952 if (ispackage) {
1953 /* Set __path__ to the package name */
1954 PyObject *d, *s;
1955 int err;
1956 m = PyImport_AddModule(name);
1957 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001958 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001959 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001960 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001961 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001962 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001963 err = PyDict_SetItemString(d, "__path__", s);
1964 Py_DECREF(s);
1965 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001966 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001967 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001968 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001969 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001970 goto err_return;
1971 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001973 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001974err_return:
1975 Py_DECREF(co);
1976 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001977}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001978
1979
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001980/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001981 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001982
Guido van Rossum79f25d91997-04-29 20:08:16 +00001983PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001984PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001985{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001986 PyObject *pname;
1987 PyObject *result;
1988
Martin v. Löwis5b222132007-06-10 09:51:05 +00001989 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001990 if (pname == NULL)
1991 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001992 result = PyImport_Import(pname);
1993 Py_DECREF(pname);
1994 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001995}
1996
Christian Heimes072c0f12008-01-03 23:01:04 +00001997/* Import a module without blocking
1998 *
1999 * At first it tries to fetch the module from sys.modules. If the module was
2000 * never loaded before it loads it with PyImport_ImportModule() unless another
2001 * thread holds the import lock. In the latter case the function raises an
2002 * ImportError instead of blocking.
2003 *
2004 * Returns the module object with incremented ref count.
2005 */
2006PyObject *
2007PyImport_ImportModuleNoBlock(const char *name)
2008{
2009 PyObject *result;
2010 PyObject *modules;
2011 long me;
2012
2013 /* Try to get the module from sys.modules[name] */
2014 modules = PyImport_GetModuleDict();
2015 if (modules == NULL)
2016 return NULL;
2017
2018 result = PyDict_GetItemString(modules, name);
2019 if (result != NULL) {
2020 Py_INCREF(result);
2021 return result;
2022 }
2023 else {
2024 PyErr_Clear();
2025 }
2026
2027 /* check the import lock
2028 * me might be -1 but I ignore the error here, the lock function
2029 * takes care of the problem */
2030 me = PyThread_get_thread_ident();
2031 if (import_lock_thread == -1 || import_lock_thread == me) {
2032 /* no thread or me is holding the lock */
2033 return PyImport_ImportModule(name);
2034 }
2035 else {
2036 PyErr_Format(PyExc_ImportError,
2037 "Failed to import %.200s because the import lock"
2038 "is held by another thread.",
2039 name);
2040 return NULL;
2041 }
2042}
2043
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002044/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002045static PyObject *get_parent(PyObject *globals, char *buf,
2046 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002047static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002048 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002049static int mark_miss(char *name);
2050static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002051 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002052static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002053
2054/* The Magnum Opus of dotted-name import :-) */
2055
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002056static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002057import_module_level(char *name, PyObject *globals, PyObject *locals,
2058 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002059{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002060 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002061 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002062 PyObject *parent, *head, *next, *tail;
2063
Christian Heimes454f37b2008-01-10 00:10:02 +00002064 if (strchr(name, '/') != NULL
2065#ifdef MS_WINDOWS
2066 || strchr(name, '\\') != NULL
2067#endif
2068 ) {
2069 PyErr_SetString(PyExc_ImportError,
2070 "Import by filename is not supported.");
2071 return NULL;
2072 }
2073
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002074 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002075 if (parent == NULL)
2076 return NULL;
2077
2078 head = load_next(parent, Py_None, &name, buf, &buflen);
2079 if (head == NULL)
2080 return NULL;
2081
2082 tail = head;
2083 Py_INCREF(tail);
2084 while (name) {
2085 next = load_next(tail, tail, &name, buf, &buflen);
2086 Py_DECREF(tail);
2087 if (next == NULL) {
2088 Py_DECREF(head);
2089 return NULL;
2090 }
2091 tail = next;
2092 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002093 if (tail == Py_None) {
2094 /* If tail is Py_None, both get_parent and load_next found
2095 an empty module name: someone called __import__("") or
2096 doctored faulty bytecode */
2097 Py_DECREF(tail);
2098 Py_DECREF(head);
2099 PyErr_SetString(PyExc_ValueError,
2100 "Empty module name");
2101 return NULL;
2102 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002103
2104 if (fromlist != NULL) {
2105 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2106 fromlist = NULL;
2107 }
2108
2109 if (fromlist == NULL) {
2110 Py_DECREF(tail);
2111 return head;
2112 }
2113
2114 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002115 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002116 Py_DECREF(tail);
2117 return NULL;
2118 }
2119
2120 return tail;
2121}
2122
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002123PyObject *
2124PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2125 PyObject *fromlist, int level)
2126{
2127 PyObject *result;
2128 lock_import();
2129 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002130 if (unlock_import() < 0) {
2131 Py_XDECREF(result);
2132 PyErr_SetString(PyExc_RuntimeError,
2133 "not holding the import lock");
2134 return NULL;
2135 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002136 return result;
2137}
2138
Fred Drake87590902004-05-28 20:21:36 +00002139/* Return the package that an import is being performed in. If globals comes
2140 from the module foo.bar.bat (not itself a package), this returns the
2141 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002142 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002143
2144 The *name* of the returned package is returned in buf, with the length of
2145 the name in *p_buflen.
2146
2147 If globals doesn't come from a package or a module in a package, or a
2148 corresponding entry is not found in sys.modules, Py_None is returned.
2149*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002150static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002151get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002152{
2153 static PyObject *namestr = NULL;
2154 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002155 static PyObject *pkgstr = NULL;
2156 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002157
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002158 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002159 return Py_None;
2160
2161 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002162 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002163 if (namestr == NULL)
2164 return NULL;
2165 }
2166 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002167 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002168 if (pathstr == NULL)
2169 return NULL;
2170 }
Nick Coghlande10c852007-12-04 12:22:52 +00002171 if (pkgstr == NULL) {
2172 pkgstr = PyUnicode_InternFromString("__package__");
2173 if (pkgstr == NULL)
2174 return NULL;
2175 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002176
2177 *buf = '\0';
2178 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002179 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002180
Nick Coghlande10c852007-12-04 12:22:52 +00002181 if ((pkgname != NULL) && (pkgname != Py_None)) {
2182 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002183 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002184 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002185
Nick Coghlande10c852007-12-04 12:22:52 +00002186 if (!PyUnicode_Check(pkgname)) {
2187 PyErr_SetString(PyExc_ValueError,
2188 "__package__ set to non-string");
2189 return NULL;
2190 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002191 pkgname_str = PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002192 if (len == 0) {
2193 if (level > 0) {
2194 PyErr_SetString(PyExc_ValueError,
2195 "Attempted relative import in non-package");
2196 return NULL;
2197 }
2198 return Py_None;
2199 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002200 if (len > MAXPATHLEN) {
2201 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002202 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002203 return NULL;
2204 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002205 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002206 } else {
2207 /* __package__ not set, so figure it out and set it */
2208 modname = PyDict_GetItem(globals, namestr);
2209 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002210 return Py_None;
Nick Coghlande10c852007-12-04 12:22:52 +00002211
2212 modpath = PyDict_GetItem(globals, pathstr);
2213 if (modpath != NULL) {
2214 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002215 char *modname_str;
2216 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002217 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002218
2219 modname_str = PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002220 if (len > MAXPATHLEN) {
2221 PyErr_SetString(PyExc_ValueError,
2222 "Module name too long");
2223 return NULL;
2224 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002225 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002226 error = PyDict_SetItem(globals, pkgstr, modname);
2227 if (error) {
2228 PyErr_SetString(PyExc_ValueError,
2229 "Could not set __package__");
2230 return NULL;
2231 }
2232 } else {
2233 /* Normal module, so work out the package name if any */
2234 char *start = PyUnicode_AsString(modname);
2235 char *lastdot = strrchr(start, '.');
2236 size_t len;
2237 int error;
2238 if (lastdot == NULL && level > 0) {
2239 PyErr_SetString(PyExc_ValueError,
2240 "Attempted relative import in non-package");
2241 return NULL;
2242 }
2243 if (lastdot == NULL) {
2244 error = PyDict_SetItem(globals, pkgstr, Py_None);
2245 if (error) {
2246 PyErr_SetString(PyExc_ValueError,
2247 "Could not set __package__");
2248 return NULL;
2249 }
2250 return Py_None;
2251 }
2252 len = lastdot - start;
2253 if (len >= MAXPATHLEN) {
2254 PyErr_SetString(PyExc_ValueError,
2255 "Module name too long");
2256 return NULL;
2257 }
2258 strncpy(buf, start, len);
2259 buf[len] = '\0';
2260 pkgname = PyUnicode_FromString(buf);
2261 if (pkgname == NULL) {
2262 return NULL;
2263 }
2264 error = PyDict_SetItem(globals, pkgstr, pkgname);
2265 Py_DECREF(pkgname);
2266 if (error) {
2267 PyErr_SetString(PyExc_ValueError,
2268 "Could not set __package__");
2269 return NULL;
2270 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002271 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002272 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002273 while (--level > 0) {
2274 char *dot = strrchr(buf, '.');
2275 if (dot == NULL) {
2276 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002277 "Attempted relative import beyond "
2278 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002279 return NULL;
2280 }
2281 *dot = '\0';
2282 }
2283 *p_buflen = strlen(buf);
2284
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002285 modules = PyImport_GetModuleDict();
2286 parent = PyDict_GetItemString(modules, buf);
2287 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002288 PyErr_Format(PyExc_SystemError,
2289 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002290 return parent;
2291 /* We expect, but can't guarantee, if parent != None, that:
2292 - parent.__name__ == buf
2293 - parent.__dict__ is globals
2294 If this is violated... Who cares? */
2295}
2296
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002297/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002298static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002299load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002300 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002301{
2302 char *name = *p_name;
2303 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002304 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002305 char *p;
2306 PyObject *result;
2307
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002308 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002309 /* completely empty module name should only happen in
2310 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002311 Py_INCREF(mod);
2312 *p_name = NULL;
2313 return mod;
2314 }
2315
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002316 if (dot == NULL) {
2317 *p_name = NULL;
2318 len = strlen(name);
2319 }
2320 else {
2321 *p_name = dot+1;
2322 len = dot-name;
2323 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002324 if (len == 0) {
2325 PyErr_SetString(PyExc_ValueError,
2326 "Empty module name");
2327 return NULL;
2328 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002329
2330 p = buf + *p_buflen;
2331 if (p != buf)
2332 *p++ = '.';
2333 if (p+len-buf >= MAXPATHLEN) {
2334 PyErr_SetString(PyExc_ValueError,
2335 "Module name too long");
2336 return NULL;
2337 }
2338 strncpy(p, name, len);
2339 p[len] = '\0';
2340 *p_buflen = p+len-buf;
2341
2342 result = import_submodule(mod, p, buf);
2343 if (result == Py_None && altmod != mod) {
2344 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002345 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002346 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002347 if (result != NULL && result != Py_None) {
2348 if (mark_miss(buf) != 0) {
2349 Py_DECREF(result);
2350 return NULL;
2351 }
2352 strncpy(buf, name, len);
2353 buf[len] = '\0';
2354 *p_buflen = len;
2355 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002356 }
2357 if (result == NULL)
2358 return NULL;
2359
2360 if (result == Py_None) {
2361 Py_DECREF(result);
2362 PyErr_Format(PyExc_ImportError,
2363 "No module named %.200s", name);
2364 return NULL;
2365 }
2366
2367 return result;
2368}
2369
2370static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002371mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002372{
2373 PyObject *modules = PyImport_GetModuleDict();
2374 return PyDict_SetItemString(modules, name, Py_None);
2375}
2376
2377static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002378ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002379 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002380{
2381 int i;
2382
2383 if (!PyObject_HasAttrString(mod, "__path__"))
2384 return 1;
2385
2386 for (i = 0; ; i++) {
2387 PyObject *item = PySequence_GetItem(fromlist, i);
2388 int hasit;
2389 if (item == NULL) {
2390 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2391 PyErr_Clear();
2392 return 1;
2393 }
2394 return 0;
2395 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002396 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002397 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002398 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002399 Py_DECREF(item);
2400 return 0;
2401 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002402 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002403 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002404 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002405 /* See if the package defines __all__ */
2406 if (recursive)
2407 continue; /* Avoid endless recursion */
2408 all = PyObject_GetAttrString(mod, "__all__");
2409 if (all == NULL)
2410 PyErr_Clear();
2411 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002412 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002413 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002414 if (!ret)
2415 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002416 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002417 continue;
2418 }
2419 hasit = PyObject_HasAttr(mod, item);
2420 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002421 PyObject *item8;
2422 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002423 PyObject *submod;
2424 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002425 if (!Py_FileSystemDefaultEncoding) {
2426 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2427 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002428 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002429 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002430 item8 = PyUnicode_AsEncodedString(item,
2431 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002432 }
2433 if (!item8) {
2434 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2435 return 0;
2436 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002437 subname = PyBytes_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002438 if (buflen + strlen(subname) >= MAXPATHLEN) {
2439 PyErr_SetString(PyExc_ValueError,
2440 "Module name too long");
2441 Py_DECREF(item);
2442 return 0;
2443 }
2444 p = buf + buflen;
2445 *p++ = '.';
2446 strcpy(p, subname);
2447 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002448 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002449 Py_XDECREF(submod);
2450 if (submod == NULL) {
2451 Py_DECREF(item);
2452 return 0;
2453 }
2454 }
2455 Py_DECREF(item);
2456 }
2457
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002458 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002459}
2460
Neil Schemenauer00b09662003-06-16 21:03:07 +00002461static int
2462add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2463 PyObject *modules)
2464{
2465 if (mod == Py_None)
2466 return 1;
2467 /* Irrespective of the success of this load, make a
2468 reference to it in the parent package module. A copy gets
2469 saved in the modules dictionary under the full name, so get a
2470 reference from there, if need be. (The exception is when the
2471 load failed with a SyntaxError -- then there's no trace in
2472 sys.modules. In that case, of course, do nothing extra.) */
2473 if (submod == NULL) {
2474 submod = PyDict_GetItemString(modules, fullname);
2475 if (submod == NULL)
2476 return 1;
2477 }
2478 if (PyModule_Check(mod)) {
2479 /* We can't use setattr here since it can give a
2480 * spurious warning if the submodule name shadows a
2481 * builtin name */
2482 PyObject *dict = PyModule_GetDict(mod);
2483 if (!dict)
2484 return 0;
2485 if (PyDict_SetItemString(dict, subname, submod) < 0)
2486 return 0;
2487 }
2488 else {
2489 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2490 return 0;
2491 }
2492 return 1;
2493}
2494
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002495static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002496import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002497{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002498 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002499 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002500
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002501 /* Require:
2502 if mod == None: subname == fullname
2503 else: mod.__name__ + "." + subname == fullname
2504 */
2505
Tim Peters50d8d372001-02-28 05:34:27 +00002506 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002507 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002508 }
2509 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002510 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002511 char buf[MAXPATHLEN+1];
2512 struct filedescr *fdp;
2513 FILE *fp = NULL;
2514
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002515 if (mod == Py_None)
2516 path = NULL;
2517 else {
2518 path = PyObject_GetAttrString(mod, "__path__");
2519 if (path == NULL) {
2520 PyErr_Clear();
2521 Py_INCREF(Py_None);
2522 return Py_None;
2523 }
2524 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002525
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002526 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002527 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2528 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002529 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002530 if (fdp == NULL) {
2531 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2532 return NULL;
2533 PyErr_Clear();
2534 Py_INCREF(Py_None);
2535 return Py_None;
2536 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002537 m = load_module(fullname, fp, buf, fdp->type, loader);
2538 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002539 if (fp)
2540 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002541 if (!add_submodule(mod, m, fullname, subname, modules)) {
2542 Py_XDECREF(m);
2543 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002544 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002545 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002546
2547 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002548}
2549
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002550
2551/* Re-import a module of any kind and return its module object, WITH
2552 INCREMENTED REFERENCE COUNT */
2553
Guido van Rossum79f25d91997-04-29 20:08:16 +00002554PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002555PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002556{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002557 PyInterpreterState *interp = PyThreadState_Get()->interp;
2558 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002559 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002560 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002561 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002562 char buf[MAXPATHLEN+1];
2563 struct filedescr *fdp;
2564 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002565 PyObject *newm;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002566
2567 if (modules_reloading == NULL) {
2568 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002569 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002570 return NULL;
2571 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002572
Guido van Rossum79f25d91997-04-29 20:08:16 +00002573 if (m == NULL || !PyModule_Check(m)) {
2574 PyErr_SetString(PyExc_TypeError,
2575 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002576 return NULL;
2577 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002578 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002579 if (name == NULL)
2580 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002581 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002582 PyErr_Format(PyExc_ImportError,
2583 "reload(): module %.200s not in sys.modules",
2584 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002585 return NULL;
2586 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002587 existing_m = PyDict_GetItemString(modules_reloading, name);
2588 if (existing_m != NULL) {
2589 /* Due to a recursive reload, this module is already
2590 being reloaded. */
2591 Py_INCREF(existing_m);
2592 return existing_m;
2593 }
2594 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2595 return NULL;
2596
Guido van Rossum222ef561997-09-06 19:41:09 +00002597 subname = strrchr(name, '.');
2598 if (subname == NULL)
2599 subname = name;
2600 else {
2601 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002602 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002603 if (parentname == NULL) {
2604 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002605 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002606 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002607 parent = PyDict_GetItem(modules, parentname);
2608 if (parent == NULL) {
2609 PyErr_Format(PyExc_ImportError,
2610 "reload(): parent %.200s not in sys.modules",
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002611 PyUnicode_AsString(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002612 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002613 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002614 return NULL;
2615 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002616 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002617 subname++;
2618 path = PyObject_GetAttrString(parent, "__path__");
2619 if (path == NULL)
2620 PyErr_Clear();
2621 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002622 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002623 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002624 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002625
2626 if (fdp == NULL) {
2627 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002628 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002629 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002630 }
2631
2632 newm = load_module(name, fp, buf, fdp->type, loader);
2633 Py_XDECREF(loader);
2634
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002635 if (fp)
2636 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002637 if (newm == NULL) {
2638 /* load_module probably removed name from modules because of
2639 * the error. Put back the original module object. We're
2640 * going to return NULL in this case regardless of whether
2641 * replacing name succeeds, so the return value is ignored.
2642 */
2643 PyDict_SetItemString(modules, name, m);
2644 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002645 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002646 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002647}
2648
2649
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002650/* Higher-level import emulator which emulates the "import" statement
2651 more accurately -- it invokes the __import__() function from the
2652 builtins of the current globals. This means that the import is
2653 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002654 environment, e.g. by "rexec".
2655 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002656 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002657 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002658
2659PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002660PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002661{
2662 static PyObject *silly_list = NULL;
2663 static PyObject *builtins_str = NULL;
2664 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002665 PyObject *globals = NULL;
2666 PyObject *import = NULL;
2667 PyObject *builtins = NULL;
2668 PyObject *r = NULL;
2669
2670 /* Initialize constant string objects */
2671 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002672 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002673 if (import_str == NULL)
2674 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002675 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002676 if (builtins_str == NULL)
2677 return NULL;
2678 silly_list = Py_BuildValue("[s]", "__doc__");
2679 if (silly_list == NULL)
2680 return NULL;
2681 }
2682
2683 /* Get the builtins from current globals */
2684 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002685 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002686 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002687 builtins = PyObject_GetItem(globals, builtins_str);
2688 if (builtins == NULL)
2689 goto err;
2690 }
2691 else {
2692 /* No globals -- use standard builtins, and fake globals */
2693 PyErr_Clear();
2694
Georg Brandl1a3284e2007-12-02 09:40:06 +00002695 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002696 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002697 if (builtins == NULL)
2698 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002699 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2700 if (globals == NULL)
2701 goto err;
2702 }
2703
2704 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002705 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002706 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002707 if (import == NULL)
2708 PyErr_SetObject(PyExc_KeyError, import_str);
2709 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002710 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002711 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002712 if (import == NULL)
2713 goto err;
2714
Christian Heimes072c0f12008-01-03 23:01:04 +00002715 /* Call the __import__ function with the proper argument list
2716 * Always use absolute import here. */
2717 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2718 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002719
2720 err:
2721 Py_XDECREF(globals);
2722 Py_XDECREF(builtins);
2723 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002724
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002725 return r;
2726}
2727
2728
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002729/* Module 'imp' provides Python access to the primitives used for
2730 importing modules.
2731*/
2732
Guido van Rossum79f25d91997-04-29 20:08:16 +00002733static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002734imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002735{
2736 char buf[4];
2737
Guido van Rossum96774c12000-05-01 20:19:08 +00002738 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2739 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2740 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2741 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002742
Christian Heimes72b710a2008-05-26 13:28:38 +00002743 return PyBytes_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002744}
2745
Guido van Rossum79f25d91997-04-29 20:08:16 +00002746static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002747imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002748{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002749 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002750 struct filedescr *fdp;
2751
Guido van Rossum79f25d91997-04-29 20:08:16 +00002752 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002753 if (list == NULL)
2754 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002755 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2756 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002757 fdp->suffix, fdp->mode, fdp->type);
2758 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002759 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002760 return NULL;
2761 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002762 if (PyList_Append(list, item) < 0) {
2763 Py_DECREF(list);
2764 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002765 return NULL;
2766 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002767 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002768 }
2769 return list;
2770}
2771
Guido van Rossum79f25d91997-04-29 20:08:16 +00002772static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002773call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002774{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002775 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002776 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002777 struct filedescr *fdp;
2778 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002779 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002780 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002781 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002782 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002783
2784 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002785 if (path == Py_None)
2786 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002787 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002788 if (fdp == NULL)
2789 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002790 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002791 fd = fileno(fp);
2792 if (fd != -1)
2793 fd = dup(fd);
2794 fclose(fp);
2795 fp = NULL;
2796 }
2797 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002798 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002799 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2800 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002801 found_encoding = PyTokenizer_FindEncoding(fd);
2802 lseek(fd, 0, 0); /* Reset position */
Brett Cannon3bb42d92007-10-20 03:43:15 +00002803 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002804 (char*)PyUnicode_GetDefaultEncoding();
2805 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002806 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002807 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002808 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002809 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002810 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002811 return NULL;
2812 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002813 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002814 else {
2815 fob = Py_None;
2816 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002817 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002818 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002819 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002820 Py_DECREF(fob);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002821 PyMem_FREE(found_encoding);
2822
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002823 return ret;
2824}
2825
Guido van Rossum79f25d91997-04-29 20:08:16 +00002826static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002827imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002828{
2829 char *name;
2830 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002831 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002832 return NULL;
2833 return call_find_module(name, path);
2834}
2835
2836static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002837imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002838{
2839 char *name;
2840 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002841 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002842 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002843 return NULL;
2844 ret = init_builtin(name);
2845 if (ret < 0)
2846 return NULL;
2847 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002848 Py_INCREF(Py_None);
2849 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002850 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002851 m = PyImport_AddModule(name);
2852 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002853 return m;
2854}
2855
Guido van Rossum79f25d91997-04-29 20:08:16 +00002856static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002857imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002858{
2859 char *name;
2860 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002861 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002862 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002863 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002864 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002865 if (ret < 0)
2866 return NULL;
2867 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002868 Py_INCREF(Py_None);
2869 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002870 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002871 m = PyImport_AddModule(name);
2872 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002873 return m;
2874}
2875
Guido van Rossum79f25d91997-04-29 20:08:16 +00002876static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002877imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002878{
2879 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002880
Guido van Rossum43713e52000-02-29 13:59:29 +00002881 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002882 return NULL;
2883 return get_frozen_object(name);
2884}
2885
Guido van Rossum79f25d91997-04-29 20:08:16 +00002886static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002887imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002888{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002889 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002890 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002891 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002892 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002893}
2894
Guido van Rossum79f25d91997-04-29 20:08:16 +00002895static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002896imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002897{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002898 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002899 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002900 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002901 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002902 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002903 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002904}
2905
2906static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002907get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002908{
2909 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002910 if (mode[0] == 'U')
2911 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002912 if (fob == NULL) {
2913 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002914 }
2915 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002916 int fd = PyObject_AsFileDescriptor(fob);
2917 if (fd == -1)
2918 return NULL;
2919 /* XXX This will leak a FILE struct. Fix this!!!!
2920 (But it doesn't leak a file descrioptor!) */
2921 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002922 }
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002923 if (fp == NULL)
2924 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002925 return fp;
2926}
2927
Guido van Rossum79f25d91997-04-29 20:08:16 +00002928static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002929imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002930{
2931 char *name;
2932 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002933 PyObject *fob = NULL;
2934 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002935 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002936 if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
2937 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002938 return NULL;
2939 fp = get_file(pathname, fob, "rb");
2940 if (fp == NULL)
2941 return NULL;
2942 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002943 if (fob == NULL)
2944 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002945 return m;
2946}
2947
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002948#ifdef HAVE_DYNAMIC_LOADING
2949
Guido van Rossum79f25d91997-04-29 20:08:16 +00002950static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002951imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002952{
2953 char *name;
2954 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002955 PyObject *fob = NULL;
2956 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002957 FILE *fp = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002958 if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
2959 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002960 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002961 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002962 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002963 if (fp == NULL)
2964 return NULL;
2965 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002966 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002967 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002968}
2969
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002970#endif /* HAVE_DYNAMIC_LOADING */
2971
Guido van Rossum79f25d91997-04-29 20:08:16 +00002972static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002973imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002974{
2975 char *name;
2976 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002977 PyObject *fob = NULL;
2978 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002979 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002980 if (!PyArg_ParseTuple(args, "ss|O:load_source",
2981 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002982 return NULL;
2983 fp = get_file(pathname, fob, "r");
2984 if (fp == NULL)
2985 return NULL;
2986 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002987 if (fob == NULL)
2988 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002989 return m;
2990}
2991
Guido van Rossum79f25d91997-04-29 20:08:16 +00002992static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002993imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002994{
2995 char *name;
2996 PyObject *fob;
2997 char *pathname;
2998 char *suffix; /* Unused */
2999 char *mode;
3000 int type;
3001 FILE *fp;
3002
Guido van Rossum43713e52000-02-29 13:59:29 +00003003 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003004 &name, &fob, &pathname,
3005 &suffix, &mode, &type))
3006 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003007 if (*mode) {
3008 /* Mode must start with 'r' or 'U' and must not contain '+'.
3009 Implicit in this test is the assumption that the mode
3010 may contain other modifiers like 'b' or 't'. */
3011
3012 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003013 PyErr_Format(PyExc_ValueError,
3014 "invalid file open mode %.200s", mode);
3015 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003016 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003017 }
3018 if (fob == Py_None)
3019 fp = NULL;
3020 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003021 fp = get_file(NULL, fob, mode);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003022 if (fp == NULL)
3023 return NULL;
3024 }
Just van Rossum52e14d62002-12-30 22:08:05 +00003025 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003026}
3027
3028static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003029imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003030{
3031 char *name;
3032 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00003033 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003034 return NULL;
3035 return load_package(name, pathname);
3036}
3037
3038static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003039imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003040{
3041 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003042 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003043 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003044 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003045}
3046
Christian Heimes13a7a212008-01-07 17:13:09 +00003047static PyObject *
3048imp_reload(PyObject *self, PyObject *v)
3049{
3050 return PyImport_ReloadModule(v);
3051}
3052
3053PyDoc_STRVAR(doc_reload,
3054"reload(module) -> module\n\
3055\n\
3056Reload the module. The module must have been successfully imported before.");
3057
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003058/* Doc strings */
3059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003060PyDoc_STRVAR(doc_imp,
3061"This module provides the components needed to build your own\n\
3062__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003064PyDoc_STRVAR(doc_find_module,
3065"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003066Search for a module. If path is omitted or None, search for a\n\
3067built-in, frozen or special module and continue search in sys.path.\n\
3068The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003069package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003071PyDoc_STRVAR(doc_load_module,
3072"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003073Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003074The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003076PyDoc_STRVAR(doc_get_magic,
3077"get_magic() -> string\n\
3078Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003080PyDoc_STRVAR(doc_get_suffixes,
3081"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003082Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003083that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003085PyDoc_STRVAR(doc_new_module,
3086"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003087Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003088The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003090PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003091"lock_held() -> boolean\n\
3092Return True if the import lock is currently held, else False.\n\
3093On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003094
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003095PyDoc_STRVAR(doc_acquire_lock,
3096"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003097Acquires the interpreter's import lock for the current thread.\n\
3098This lock should be used by import hooks to ensure thread-safety\n\
3099when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003100On platforms without threads, this function does nothing.");
3101
3102PyDoc_STRVAR(doc_release_lock,
3103"release_lock() -> None\n\
3104Release the interpreter's import lock.\n\
3105On platforms without threads, this function does nothing.");
3106
Guido van Rossum79f25d91997-04-29 20:08:16 +00003107static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003108 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3109 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3110 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3111 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3112 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3113 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3114 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3115 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003116 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003117 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003118 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3119 {"init_builtin", imp_init_builtin, METH_VARARGS},
3120 {"init_frozen", imp_init_frozen, METH_VARARGS},
3121 {"is_builtin", imp_is_builtin, METH_VARARGS},
3122 {"is_frozen", imp_is_frozen, METH_VARARGS},
3123 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003124#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003125 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003126#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003127 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003128 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003129 {NULL, NULL} /* sentinel */
3130};
3131
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003132static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003133setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003134{
3135 PyObject *v;
3136 int err;
3137
Christian Heimes217cfd12007-12-02 14:31:20 +00003138 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003139 err = PyDict_SetItemString(d, name, v);
3140 Py_XDECREF(v);
3141 return err;
3142}
3143
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003144typedef struct {
3145 PyObject_HEAD
3146} NullImporter;
3147
3148static int
3149NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3150{
3151 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003152 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003153
3154 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3155 return -1;
3156
3157 if (!PyArg_ParseTuple(args, "s:NullImporter",
3158 &path))
3159 return -1;
3160
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003161 pathlen = strlen(path);
3162 if (pathlen == 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003163 PyErr_SetString(PyExc_ImportError, "empty pathname");
3164 return -1;
3165 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003166 struct stat statbuf;
3167 int rv;
3168
3169 rv = stat(path, &statbuf);
Guido van Rossumc1bdbc32007-11-07 17:46:34 +00003170#ifdef MS_WINDOWS
3171 /* MS Windows stat() chokes on paths like C:\path\. Try to
3172 * recover *one* time by stripping off a trailing slash or
3173 * backslash. http://bugs.python.org/issue1293
3174 */
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003175 if (rv != 0 && pathlen <= MAXPATHLEN &&
3176 (path[pathlen-1] == '/' || path[pathlen-1] == '\\')) {
3177 char mangled[MAXPATHLEN+1];
3178
3179 strcpy(mangled, path);
3180 mangled[pathlen-1] = '\0';
3181 rv = stat(mangled, &statbuf);
3182 }
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003183#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003184 if (rv == 0) {
3185 /* it exists */
3186 if (S_ISDIR(statbuf.st_mode)) {
3187 /* it's a directory */
3188 PyErr_SetString(PyExc_ImportError,
3189 "existing directory");
3190 return -1;
3191 }
3192 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003193 }
3194 return 0;
3195}
3196
3197static PyObject *
3198NullImporter_find_module(NullImporter *self, PyObject *args)
3199{
3200 Py_RETURN_NONE;
3201}
3202
3203static PyMethodDef NullImporter_methods[] = {
3204 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3205 "Always return None"
3206 },
3207 {NULL} /* Sentinel */
3208};
3209
3210
Christian Heimes9cd17752007-11-18 19:35:23 +00003211PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003212 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003213 "imp.NullImporter", /*tp_name*/
3214 sizeof(NullImporter), /*tp_basicsize*/
3215 0, /*tp_itemsize*/
3216 0, /*tp_dealloc*/
3217 0, /*tp_print*/
3218 0, /*tp_getattr*/
3219 0, /*tp_setattr*/
3220 0, /*tp_compare*/
3221 0, /*tp_repr*/
3222 0, /*tp_as_number*/
3223 0, /*tp_as_sequence*/
3224 0, /*tp_as_mapping*/
3225 0, /*tp_hash */
3226 0, /*tp_call*/
3227 0, /*tp_str*/
3228 0, /*tp_getattro*/
3229 0, /*tp_setattro*/
3230 0, /*tp_as_buffer*/
3231 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3232 "Null importer object", /* tp_doc */
3233 0, /* tp_traverse */
3234 0, /* tp_clear */
3235 0, /* tp_richcompare */
3236 0, /* tp_weaklistoffset */
3237 0, /* tp_iter */
3238 0, /* tp_iternext */
3239 NullImporter_methods, /* tp_methods */
3240 0, /* tp_members */
3241 0, /* tp_getset */
3242 0, /* tp_base */
3243 0, /* tp_dict */
3244 0, /* tp_descr_get */
3245 0, /* tp_descr_set */
3246 0, /* tp_dictoffset */
3247 (initproc)NullImporter_init, /* tp_init */
3248 0, /* tp_alloc */
3249 PyType_GenericNew /* tp_new */
3250};
3251
Martin v. Löwis1a214512008-06-11 05:26:20 +00003252static struct PyModuleDef impmodule = {
3253 PyModuleDef_HEAD_INIT,
3254 "imp",
3255 doc_imp,
3256 0,
3257 imp_methods,
3258 NULL,
3259 NULL,
3260 NULL,
3261 NULL
3262};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003263
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003264PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003265PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003266{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003267 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003268
Christian Heimes9cd17752007-11-18 19:35:23 +00003269 if (PyType_Ready(&PyNullImporter_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003270 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003271
Martin v. Löwis1a214512008-06-11 05:26:20 +00003272 m = PyModule_Create(&impmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003273 if (m == NULL)
3274 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003275 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003276 if (d == NULL)
3277 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003278
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003279 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3280 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3281 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3282 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3283 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3284 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3285 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3286 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003287 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003288 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003289
Christian Heimes9cd17752007-11-18 19:35:23 +00003290 Py_INCREF(&PyNullImporter_Type);
3291 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +00003292 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003293 failure:
Martin v. Löwis1a214512008-06-11 05:26:20 +00003294 Py_XDECREF(m);
3295 return NULL;
3296
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003297}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003298
3299
Guido van Rossumb18618d2000-05-03 23:44:39 +00003300/* API for embedding applications that want to add their own entries
3301 to the table of built-in modules. This should normally be called
3302 *before* Py_Initialize(). When the table resize fails, -1 is
3303 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003304
3305 After a similar function by Just van Rossum. */
3306
3307int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003308PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003309{
3310 static struct _inittab *our_copy = NULL;
3311 struct _inittab *p;
3312 int i, n;
3313
3314 /* Count the number of entries in both tables */
3315 for (n = 0; newtab[n].name != NULL; n++)
3316 ;
3317 if (n == 0)
3318 return 0; /* Nothing to do */
3319 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3320 ;
3321
3322 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003323 p = our_copy;
3324 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003325 if (p == NULL)
3326 return -1;
3327
3328 /* Copy the tables into the new memory */
3329 if (our_copy != PyImport_Inittab)
3330 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3331 PyImport_Inittab = our_copy = p;
3332 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3333
3334 return 0;
3335}
3336
3337/* Shorthand to add a single entry given a name and a function */
3338
3339int
Martin v. Löwis1a214512008-06-11 05:26:20 +00003340PyImport_AppendInittab(char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003341{
3342 struct _inittab newtab[2];
3343
3344 memset(newtab, '\0', sizeof newtab);
3345
3346 newtab[0].name = name;
3347 newtab[0].initfunc = initfunc;
3348
3349 return PyImport_ExtendInittab(newtab);
3350}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003351
3352#ifdef __cplusplus
3353}
3354#endif