blob: 5dabd9f6432c2a10b3a1ac943973dea0033ed810 [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
544 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000547_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000549 PyObject *modules, *mod, *dict, *copy;
550 if (extensions == NULL) {
551 extensions = PyDict_New();
552 if (extensions == NULL)
553 return NULL;
554 }
555 modules = PyImport_GetModuleDict();
556 mod = PyDict_GetItemString(modules, name);
557 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000558 PyErr_Format(PyExc_SystemError,
559 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 return NULL;
561 }
562 dict = PyModule_GetDict(mod);
563 if (dict == NULL)
564 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000565 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566 if (copy == NULL)
567 return NULL;
568 PyDict_SetItemString(extensions, filename, copy);
569 Py_DECREF(copy);
570 return copy;
571}
572
573PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000574_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000575{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000576 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000577 if (extensions == NULL)
578 return NULL;
579 dict = PyDict_GetItemString(extensions, filename);
580 if (dict == NULL)
581 return NULL;
582 mod = PyImport_AddModule(name);
583 if (mod == NULL)
584 return NULL;
585 mdict = PyModule_GetDict(mod);
586 if (mdict == NULL)
587 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000588 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000591 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 name, filename);
593 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594}
595
596
597/* Get the module object corresponding to a module name.
598 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000599 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000600 Because the former action is most common, THIS DOES NOT RETURN A
601 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000602
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000604PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608
Guido van Rossum25ce5661997-08-02 03:10:38 +0000609 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 if (m == NULL)
614 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000615 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000617 return NULL;
618 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620
621 return m;
622}
623
Tim Peters1cd70172004-08-02 03:52:12 +0000624/* Remove name from sys.modules, if it's there. */
625static void
626_RemoveModule(const char *name)
627{
628 PyObject *modules = PyImport_GetModuleDict();
629 if (PyDict_GetItemString(modules, name) == NULL)
630 return;
631 if (PyDict_DelItemString(modules, name) < 0)
632 Py_FatalError("import: deleting existing key in"
633 "sys.modules failed");
634}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635
Christian Heimes3b06e532008-01-07 20:12:44 +0000636static PyObject * get_sourcefile(const char *file);
637
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000638/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000639 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
640 * removed from sys.modules, to avoid leaving damaged module objects
641 * in sys.modules. The caller may wish to restore the original
642 * module object (if any) in this case; PyImport_ReloadModule is an
643 * example.
644 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000646PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000648 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
649}
650
651PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000652PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000653{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000654 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000655 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000656
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000658 if (m == NULL)
659 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000660 /* If the module is being reloaded, we get the old module back
661 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 d = PyModule_GetDict(m);
663 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
664 if (PyDict_SetItemString(d, "__builtins__",
665 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000666 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000667 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000668 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000669 v = NULL;
670 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000671 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000672 if (v == NULL)
673 PyErr_Clear();
674 }
675 if (v == NULL) {
676 v = ((PyCodeObject *)co)->co_filename;
677 Py_INCREF(v);
678 }
679 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000681 Py_DECREF(v);
682
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000683 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000685 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000687
Guido van Rossum25ce5661997-08-02 03:10:38 +0000688 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000689 PyErr_Format(PyExc_ImportError,
690 "Loaded module %.200s not found in sys.modules",
691 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000692 return NULL;
693 }
694
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696
697 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000698
699 error:
700 _RemoveModule(name);
701 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702}
703
704
705/* Given a pathname for a Python source file, fill a buffer with the
706 pathname for the corresponding compiled file. Return the pathname
707 for the compiled file, or NULL if there's no space in the buffer.
708 Doesn't set an exception. */
709
710static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000711make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712{
Tim Petersc1731372001-08-04 08:12:36 +0000713 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 if (len+2 > buflen)
715 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000716
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000717#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000718 /* Treat .pyw as if it were .py. The case of ".pyw" must match
719 that used in _PyImport_StandardFiletab. */
720 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
721 --len; /* pretend 'w' isn't there */
722#endif
723 memcpy(buf, pathname, len);
724 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
725 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726
727 return buf;
728}
729
730
731/* Given a pathname for a Python source file, its time of last
732 modification, and a pathname for a compiled file, check whether the
733 compiled file represents the same version of the source. If so,
734 return a FILE pointer for the compiled file, positioned just after
735 the header; if not, return NULL.
736 Doesn't set an exception. */
737
738static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000739check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740{
741 FILE *fp;
742 long magic;
743 long pyc_mtime;
744
745 fp = fopen(cpathname, "rb");
746 if (fp == NULL)
747 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000749 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000751 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000752 fclose(fp);
753 return NULL;
754 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000758 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759 fclose(fp);
760 return NULL;
761 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000763 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000764 return fp;
765}
766
767
768/* Read a code object from a file and check it for validity */
769
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000771read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774
Tim Petersd9b9ac82001-01-28 00:27:39 +0000775 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000776 if (co == NULL)
777 return NULL;
778 if (!PyCode_Check(co)) {
779 PyErr_Format(PyExc_ImportError,
780 "Non-code object in %.200s", cpathname);
781 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000782 return NULL;
783 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785}
786
787
788/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000789 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000790
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000792load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793{
794 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 PyCodeObject *co;
796 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000799 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000800 PyErr_Format(PyExc_ImportError,
801 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802 return NULL;
803 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000805 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806 if (co == NULL)
807 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000809 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000811 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000813
814 return m;
815}
816
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817/* Parse a source file and return the corresponding code object */
818
Guido van Rossum79f25d91997-04-29 20:08:16 +0000819static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000821{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 PyCodeObject *co = NULL;
823 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000824 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000825 PyArena *arena = PyArena_New();
826 if (arena == NULL)
827 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000829 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000830 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Christian Heimes4d6ec852008-03-26 22:34:47 +0000831 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000832 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000834 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000836 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837 return co;
838}
839
840
Guido van Rossum55a83382000-09-20 20:31:38 +0000841/* Helper to open a bytecode file for writing in exclusive mode */
842
843static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000844open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000845{
846#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
847 /* Use O_EXCL to avoid a race condition when another process tries to
848 write the same file. When that happens, our open() call fails,
849 which is just fine (since it's only a cache).
850 XXX If the file exists and is writable but the directory is not
851 writable, the file will never be written. Oh well.
852 */
853 int fd;
854 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000855 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
856#ifdef O_BINARY
857 |O_BINARY /* necessary for Windows */
858#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000859#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000860 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000861#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000862 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000863#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000864 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000865 if (fd < 0)
866 return NULL;
867 return fdopen(fd, "wb");
868#else
869 /* Best we can do -- on Windows this can't happen anyway */
870 return fopen(filename, "wb");
871#endif
872}
873
874
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000875/* Write a compiled module to a file, placing the time of last
876 modification of its source into the header.
877 Errors are ignored, if a write error occurs an attempt is made to
878 remove the file. */
879
880static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000881write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882{
883 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000884 time_t mtime = srcstat->st_mtime;
885 mode_t mode = srcstat->st_mode;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886
Christian Heimes05e8be12008-02-23 18:30:17 +0000887 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000888 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000890 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 "# can't create %s\n", cpathname);
892 return;
893 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000894 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000895 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000896 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
897 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000898 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000899 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000900 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901 /* Don't keep partial file */
902 fclose(fp);
903 (void) unlink(cpathname);
904 return;
905 }
906 /* Now write the true mtime */
907 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000908 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000909 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910 fflush(fp);
911 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000912 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000913 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000914}
915
916
917/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000918 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
919 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000920
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000922load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923{
Christian Heimes05e8be12008-02-23 18:30:17 +0000924 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000925 FILE *fpc;
926 char buf[MAXPATHLEN+1];
927 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000928 PyCodeObject *co;
929 PyObject *m;
Christian Heimes05e8be12008-02-23 18:30:17 +0000930
931 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +0000932 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +0000933 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +0000934 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000935 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000936 }
Fred Drake4c82b232000-06-30 16:18:57 +0000937#if SIZEOF_TIME_T > 4
938 /* Python's .pyc timestamp handling presumes that the timestamp fits
939 in 4 bytes. This will be fine until sometime in the year 2038,
940 when a 4-byte signed time_t will overflow.
941 */
Christian Heimes05e8be12008-02-23 18:30:17 +0000942 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +0000943 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000944 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000945 return NULL;
946 }
947#endif
Tim Peters36515e22001-11-18 04:06:29 +0000948 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000949 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000950 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +0000951 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000952 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000953 fclose(fpc);
954 if (co == NULL)
955 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000956 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000957 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000959 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000960 }
961 else {
962 co = parse_source_module(pathname, fp);
963 if (co == NULL)
964 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000966 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +0000968 if (cpathname) {
969 PyObject *ro = PySys_GetObject("dont_write_bytecode");
970 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +0000971 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +0000972 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000973 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000974 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000976
977 return m;
978}
979
Christian Heimes3b06e532008-01-07 20:12:44 +0000980/* Get source file -> unicode or None
981 * Returns the path to the py file if available, else the given path
982 */
983static PyObject *
984get_sourcefile(const char *file)
985{
986 char py[MAXPATHLEN + 1];
987 Py_ssize_t len;
988 PyObject *u;
989 struct stat statbuf;
990
991 if (!file || !*file) {
992 Py_RETURN_NONE;
993 }
994
995 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +0000996 /* match '*.py?' */
997 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000998 return PyUnicode_DecodeFSDefault(file);
999 }
1000
1001 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001002 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001003 if (stat(py, &statbuf) == 0 &&
1004 S_ISREG(statbuf.st_mode)) {
1005 u = PyUnicode_DecodeFSDefault(py);
1006 }
1007 else {
1008 u = PyUnicode_DecodeFSDefault(file);
1009 }
1010 return u;
1011}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001013/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001014static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1015static struct filedescr *find_module(char *, char *, PyObject *,
1016 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001017static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001018
1019/* Load a package and return its module object WITH INCREMENTED
1020 REFERENCE COUNT */
1021
1022static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001023load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001024{
Tim Peters1cd70172004-08-02 03:52:12 +00001025 PyObject *m, *d;
1026 PyObject *file = NULL;
1027 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001028 int err;
1029 char buf[MAXPATHLEN+1];
1030 FILE *fp = NULL;
1031 struct filedescr *fdp;
1032
1033 m = PyImport_AddModule(name);
1034 if (m == NULL)
1035 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001036 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001037 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001038 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001039 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001040 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001041 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001042 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001043 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001044 if (path == NULL)
1045 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001046 err = PyDict_SetItemString(d, "__file__", file);
1047 if (err == 0)
1048 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001049 if (err != 0)
1050 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001051 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001052 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001053 if (fdp == NULL) {
1054 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1055 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001056 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001057 }
1058 else
1059 m = NULL;
1060 goto cleanup;
1061 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001062 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001063 if (fp != NULL)
1064 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001065 goto cleanup;
1066
1067 error:
1068 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001069 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001070 Py_XDECREF(path);
1071 Py_XDECREF(file);
1072 return m;
1073}
1074
1075
1076/* Helper to test for built-in module */
1077
1078static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001079is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001080{
1081 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001082 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1083 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1084 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001085 return -1;
1086 else
1087 return 1;
1088 }
1089 }
1090 return 0;
1091}
1092
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001093
Just van Rossum52e14d62002-12-30 22:08:05 +00001094/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1095 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001096 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001097 that can handle the path item. Return None if no hook could;
1098 this tells our caller it should fall back to the builtin
1099 import mechanism. Cache the result in path_importer_cache.
1100 Returns a borrowed reference. */
1101
1102static PyObject *
1103get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1104 PyObject *p)
1105{
1106 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001107 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001108
1109 /* These conditions are the caller's responsibility: */
1110 assert(PyList_Check(path_hooks));
1111 assert(PyDict_Check(path_importer_cache));
1112
1113 nhooks = PyList_Size(path_hooks);
1114 if (nhooks < 0)
1115 return NULL; /* Shouldn't happen */
1116
1117 importer = PyDict_GetItem(path_importer_cache, p);
1118 if (importer != NULL)
1119 return importer;
1120
1121 /* set path_importer_cache[p] to None to avoid recursion */
1122 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1123 return NULL;
1124
1125 for (j = 0; j < nhooks; j++) {
1126 PyObject *hook = PyList_GetItem(path_hooks, j);
1127 if (hook == NULL)
1128 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001129 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001130 if (importer != NULL)
1131 break;
1132
1133 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1134 return NULL;
1135 }
1136 PyErr_Clear();
1137 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001138 if (importer == NULL) {
1139 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001140 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001141 );
1142 if (importer == NULL) {
1143 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1144 PyErr_Clear();
1145 return Py_None;
1146 }
1147 }
1148 }
1149 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001150 int err = PyDict_SetItem(path_importer_cache, p, importer);
1151 Py_DECREF(importer);
1152 if (err != 0)
1153 return NULL;
1154 }
1155 return importer;
1156}
1157
Christian Heimes9cd17752007-11-18 19:35:23 +00001158PyAPI_FUNC(PyObject *)
1159PyImport_GetImporter(PyObject *path) {
1160 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1161
1162 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1163 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1164 importer = get_path_importer(path_importer_cache,
1165 path_hooks, path);
1166 }
1167 }
1168 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1169 return importer;
1170}
1171
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001172/* Search the path (default sys.path) for a module. Return the
1173 corresponding filedescr struct, and (via return arguments) the
1174 pathname and an open file. Return NULL if the module is not found. */
1175
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001176#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001177extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001178 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001179#endif
1180
Martin v. Löwis18e16552006-02-15 17:27:45 +00001181static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001182static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001183static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001184
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001185static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001186find_module(char *fullname, char *subname, PyObject *path, char *buf,
1187 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001188{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001189 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001190 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001191 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001192 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001193 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001194 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001195 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001196 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1197 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1198 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001199 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001200#if defined(PYOS_OS2)
1201 size_t saved_len;
1202 size_t saved_namelen;
1203 char *saved_buf = NULL;
1204#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001205 if (p_loader != NULL)
1206 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001207
Just van Rossum52e14d62002-12-30 22:08:05 +00001208 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001209 PyErr_SetString(PyExc_OverflowError,
1210 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001211 return NULL;
1212 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001213 strcpy(name, subname);
1214
1215 /* sys.meta_path import hook */
1216 if (p_loader != NULL) {
1217 PyObject *meta_path;
1218
1219 meta_path = PySys_GetObject("meta_path");
1220 if (meta_path == NULL || !PyList_Check(meta_path)) {
1221 PyErr_SetString(PyExc_ImportError,
1222 "sys.meta_path must be a list of "
1223 "import hooks");
1224 return NULL;
1225 }
1226 Py_INCREF(meta_path); /* zap guard */
1227 npath = PyList_Size(meta_path);
1228 for (i = 0; i < npath; i++) {
1229 PyObject *loader;
1230 PyObject *hook = PyList_GetItem(meta_path, i);
1231 loader = PyObject_CallMethod(hook, "find_module",
1232 "sO", fullname,
1233 path != NULL ?
1234 path : Py_None);
1235 if (loader == NULL) {
1236 Py_DECREF(meta_path);
1237 return NULL; /* true error */
1238 }
1239 if (loader != Py_None) {
1240 /* a loader was found */
1241 *p_loader = loader;
1242 Py_DECREF(meta_path);
1243 return &importhookdescr;
1244 }
1245 Py_DECREF(loader);
1246 }
1247 Py_DECREF(meta_path);
1248 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001249
Guido van Rossum13d77992007-07-23 03:16:50 +00001250 if (path != NULL && PyUnicode_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001251 /* The only type of submodule allowed inside a "frozen"
1252 package are other frozen modules or packages. */
Guido van Rossum13d77992007-07-23 03:16:50 +00001253 char *p = PyUnicode_AsString(path);
1254 if (strlen(p) + 1 + strlen(name) >= (size_t)buflen) {
Guido van Rossum0506a431998-08-11 15:07:39 +00001255 PyErr_SetString(PyExc_ImportError,
1256 "full frozen module name too long");
1257 return NULL;
1258 }
Guido van Rossum13d77992007-07-23 03:16:50 +00001259 strcpy(buf, p);
Guido van Rossum0506a431998-08-11 15:07:39 +00001260 strcat(buf, ".");
1261 strcat(buf, name);
1262 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001263 if (find_frozen(name) != NULL) {
1264 strcpy(buf, name);
1265 return &fd_frozen;
1266 }
1267 PyErr_Format(PyExc_ImportError,
1268 "No frozen submodule named %.200s", name);
1269 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001270 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001271 if (path == NULL) {
1272 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001273 strcpy(buf, name);
1274 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001275 }
Greg Ward201baee2001-10-04 14:52:06 +00001276 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001277 strcpy(buf, name);
1278 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001279 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001280
Guido van Rossumac279101996-08-22 23:10:58 +00001281#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001282 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1283 if (fp != NULL) {
1284 *p_fp = fp;
1285 return fdp;
1286 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001287#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001288 path = PySys_GetObject("path");
1289 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001290 if (path == NULL || !PyList_Check(path)) {
1291 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001292 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001293 return NULL;
1294 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001295
1296 path_hooks = PySys_GetObject("path_hooks");
1297 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1298 PyErr_SetString(PyExc_ImportError,
1299 "sys.path_hooks must be a list of "
1300 "import hooks");
1301 return NULL;
1302 }
1303 path_importer_cache = PySys_GetObject("path_importer_cache");
1304 if (path_importer_cache == NULL ||
1305 !PyDict_Check(path_importer_cache)) {
1306 PyErr_SetString(PyExc_ImportError,
1307 "sys.path_importer_cache must be a dict");
1308 return NULL;
1309 }
1310
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001312 namelen = strlen(name);
1313 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001315 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001316 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001317 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001318 if (!v)
1319 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001320 if (PyUnicode_Check(v)) {
1321 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1322 if (v == NULL)
1323 return NULL;
1324 }
1325 if (!PyString_Check(v))
1326 continue;
1327 base = PyString_AS_STRING(v);
1328 size = PyString_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001329 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001330 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001331 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001332 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001333 strcpy(buf, base);
Walter Dörwald3430d702002-06-17 10:43:59 +00001334 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001336 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001337
1338 /* sys.path_hooks import hook */
1339 if (p_loader != NULL) {
1340 PyObject *importer;
1341
1342 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001343 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001344 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001345 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001346 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001347 /* Note: importer is a borrowed reference */
1348 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001349 PyObject *loader;
1350 loader = PyObject_CallMethod(importer,
1351 "find_module",
1352 "s", fullname);
1353 if (loader == NULL)
1354 return NULL; /* error */
1355 if (loader != Py_None) {
1356 /* a loader was found */
1357 *p_loader = loader;
1358 return &importhookdescr;
1359 }
1360 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001361 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001362 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001363 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001364 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001365
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001366 if (len > 0 && buf[len-1] != SEP
1367#ifdef ALTSEP
1368 && buf[len-1] != ALTSEP
1369#endif
1370 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001371 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001372 strcpy(buf+len, name);
1373 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001374
1375 /* Check for package import (buf holds a directory name,
1376 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001377#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001378 if (stat(buf, &statbuf) == 0 && /* it exists */
1379 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380 case_ok(buf, len, namelen, name)) { /* case matches */
1381 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382 return &fd_package;
1383 }
1384 else {
1385 char warnstr[MAXPATHLEN+80];
1386 sprintf(warnstr, "Not importing directory "
1387 "'%.*s': missing __init__.py",
1388 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001389 if (PyErr_WarnEx(PyExc_ImportWarning,
1390 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001391 return NULL;
1392 }
1393 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001394 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001395#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001396#if defined(PYOS_OS2)
1397 /* take a snapshot of the module spec for restoration
1398 * after the 8 character DLL hackery
1399 */
1400 saved_buf = strdup(buf);
1401 saved_len = len;
1402 saved_namelen = namelen;
1403#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001405#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001406 /* OS/2 limits DLLs to 8 character names (w/o
1407 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001408 * so if the name is longer than that and its a
1409 * dynamically loaded module we're going to try,
1410 * truncate the name before trying
1411 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001412 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001413 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001414 const struct filedescr *scan;
1415 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001416 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001417 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001418 break;
1419 else
1420 scan++;
1421 }
1422 if (scan->suffix != NULL) {
1423 /* yes, so truncate the name */
1424 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001425 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001426 buf[len] = '\0';
1427 }
1428 }
1429#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001430 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001431 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001432 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001433 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001434 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001435 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001436 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001437 if (fp != NULL) {
1438 if (case_ok(buf, len, namelen, name))
1439 break;
1440 else { /* continue search */
1441 fclose(fp);
1442 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001443 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001444 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001445#if defined(PYOS_OS2)
1446 /* restore the saved snapshot */
1447 strcpy(buf, saved_buf);
1448 len = saved_len;
1449 namelen = saved_namelen;
1450#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001451 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001452#if defined(PYOS_OS2)
1453 /* don't need/want the module name snapshot anymore */
1454 if (saved_buf)
1455 {
1456 free(saved_buf);
1457 saved_buf = NULL;
1458 }
1459#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001460 if (fp != NULL)
1461 break;
1462 }
1463 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001464 PyErr_Format(PyExc_ImportError,
1465 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001466 return NULL;
1467 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001468 *p_fp = fp;
1469 return fdp;
1470}
1471
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001472/* Helpers for main.c
1473 * Find the source file corresponding to a named module
1474 */
1475struct filedescr *
1476_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1477 size_t buflen, FILE **p_fp, PyObject **p_loader)
1478{
1479 return find_module((char *) name, (char *) name, path,
1480 buf, buflen, p_fp, p_loader);
1481}
1482
1483PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1484{
1485 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1486}
1487
Martin v. Löwis18e16552006-02-15 17:27:45 +00001488/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001489 * The arguments here are tricky, best shown by example:
1490 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1491 * ^ ^ ^ ^
1492 * |--------------------- buf ---------------------|
1493 * |------------------- len ------------------|
1494 * |------ name -------|
1495 * |----- namelen -----|
1496 * buf is the full path, but len only counts up to (& exclusive of) the
1497 * extension. name is the module name, also exclusive of extension.
1498 *
1499 * We've already done a successful stat() or fopen() on buf, so know that
1500 * there's some match, possibly case-insensitive.
1501 *
Tim Peters50d8d372001-02-28 05:34:27 +00001502 * case_ok() is to return 1 if there's a case-sensitive match for
1503 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1504 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001505 *
Tim Peters50d8d372001-02-28 05:34:27 +00001506 * case_ok() is used to implement case-sensitive import semantics even
1507 * on platforms with case-insensitive filesystems. It's trivial to implement
1508 * for case-sensitive filesystems. It's pretty much a cross-platform
1509 * nightmare for systems with case-insensitive filesystems.
1510 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001511
Tim Peters50d8d372001-02-28 05:34:27 +00001512/* First we may need a pile of platform-specific header files; the sequence
1513 * of #if's here should match the sequence in the body of case_ok().
1514 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001515#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001516#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001517
Tim Peters50d8d372001-02-28 05:34:27 +00001518#elif defined(DJGPP)
1519#include <dir.h>
1520
Jason Tishler7961aa62005-05-20 00:56:54 +00001521#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001522#include <sys/types.h>
1523#include <dirent.h>
1524
Andrew MacIntyred9400542002-02-26 11:41:34 +00001525#elif defined(PYOS_OS2)
1526#define INCL_DOS
1527#define INCL_DOSERRORS
1528#define INCL_NOPMAPI
1529#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001530#endif
1531
Guido van Rossum0980bd91998-02-13 17:18:36 +00001532static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001533case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001534{
Tim Peters50d8d372001-02-28 05:34:27 +00001535/* Pick a platform-specific implementation; the sequence of #if's here should
1536 * match the sequence just above.
1537 */
1538
Jason Tishler7961aa62005-05-20 00:56:54 +00001539/* MS_WINDOWS */
1540#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001541 WIN32_FIND_DATA data;
1542 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001543
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001544 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001545 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001546
Guido van Rossum0980bd91998-02-13 17:18:36 +00001547 h = FindFirstFile(buf, &data);
1548 if (h == INVALID_HANDLE_VALUE) {
1549 PyErr_Format(PyExc_NameError,
1550 "Can't find file for module %.100s\n(filename %.300s)",
1551 name, buf);
1552 return 0;
1553 }
1554 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001555 return strncmp(data.cFileName, name, namelen) == 0;
1556
1557/* DJGPP */
1558#elif defined(DJGPP)
1559 struct ffblk ffblk;
1560 int done;
1561
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001562 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001563 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001564
1565 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1566 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001567 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001568 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001569 name, buf);
1570 return 0;
1571 }
Tim Peters50d8d372001-02-28 05:34:27 +00001572 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001573
Jason Tishler7961aa62005-05-20 00:56:54 +00001574/* new-fangled macintosh (macosx) or Cygwin */
1575#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001576 DIR *dirp;
1577 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001578 char dirname[MAXPATHLEN + 1];
1579 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001580
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001581 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001582 return 1;
1583
Tim Petersd1e87a82001-03-01 18:12:00 +00001584 /* Copy the dir component into dirname; substitute "." if empty */
1585 if (dirlen <= 0) {
1586 dirname[0] = '.';
1587 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001588 }
1589 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001590 assert(dirlen <= MAXPATHLEN);
1591 memcpy(dirname, buf, dirlen);
1592 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001593 }
1594 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001595 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001596 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001597 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001598 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001599 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001600#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001601 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001602#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001603 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001604#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001605 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001606 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001607 (void)closedir(dirp);
1608 return 1; /* Found */
1609 }
1610 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001611 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001612 }
Tim Peters430f5d42001-03-01 01:30:56 +00001613 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001614
Andrew MacIntyred9400542002-02-26 11:41:34 +00001615/* OS/2 */
1616#elif defined(PYOS_OS2)
1617 HDIR hdir = 1;
1618 ULONG srchcnt = 1;
1619 FILEFINDBUF3 ffbuf;
1620 APIRET rc;
1621
Christian Heimes790c8232008-01-07 21:14:23 +00001622 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001623 return 1;
1624
1625 rc = DosFindFirst(buf,
1626 &hdir,
1627 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1628 &ffbuf, sizeof(ffbuf),
1629 &srchcnt,
1630 FIL_STANDARD);
1631 if (rc != NO_ERROR)
1632 return 0;
1633 return strncmp(ffbuf.achName, name, namelen) == 0;
1634
Tim Peters50d8d372001-02-28 05:34:27 +00001635/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1636#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001637 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001638
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001639#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001640}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001641
Guido van Rossum0980bd91998-02-13 17:18:36 +00001642
Guido van Rossum197346f1997-10-31 18:38:52 +00001643#ifdef HAVE_STAT
1644/* Helper to look for __init__.py or __init__.py[co] in potential package */
1645static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001646find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001647{
Tim Peters0f9431f2001-07-05 03:47:53 +00001648 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001649 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001650 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001651 struct stat statbuf;
1652
Tim Peters0f9431f2001-07-05 03:47:53 +00001653/* For calling case_ok(buf, len, namelen, name):
1654 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1655 * ^ ^ ^ ^
1656 * |--------------------- buf ---------------------|
1657 * |------------------- len ------------------|
1658 * |------ name -------|
1659 * |----- namelen -----|
1660 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001661 if (save_len + 13 >= MAXPATHLEN)
1662 return 0;
1663 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001664 pname = buf + i;
1665 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001666 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001667 if (case_ok(buf,
1668 save_len + 9, /* len("/__init__") */
1669 8, /* len("__init__") */
1670 pname)) {
1671 buf[save_len] = '\0';
1672 return 1;
1673 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001674 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001675 i += strlen(pname);
1676 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001677 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001678 if (case_ok(buf,
1679 save_len + 9, /* len("/__init__") */
1680 8, /* len("__init__") */
1681 pname)) {
1682 buf[save_len] = '\0';
1683 return 1;
1684 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001685 }
1686 buf[save_len] = '\0';
1687 return 0;
1688}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001689
Guido van Rossum197346f1997-10-31 18:38:52 +00001690#endif /* HAVE_STAT */
1691
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001692
Tim Petersdbd9ba62000-07-09 03:09:57 +00001693static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001694
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001695/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001696 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001697
Guido van Rossum79f25d91997-04-29 20:08:16 +00001698static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001699load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001700{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001701 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001703 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001705 /* First check that there's an open file (if we need one) */
1706 switch (type) {
1707 case PY_SOURCE:
1708 case PY_COMPILED:
1709 if (fp == NULL) {
1710 PyErr_Format(PyExc_ValueError,
1711 "file object required for import (type code %d)",
1712 type);
1713 return NULL;
1714 }
1715 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001716
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001717 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718
1719 case PY_SOURCE:
1720 m = load_source_module(name, buf, fp);
1721 break;
1722
1723 case PY_COMPILED:
1724 m = load_compiled_module(name, buf, fp);
1725 break;
1726
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001727#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001728 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001730 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001731#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001732
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001733 case PKG_DIRECTORY:
1734 m = load_package(name, buf);
1735 break;
1736
1737 case C_BUILTIN:
1738 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001739 if (buf != NULL && buf[0] != '\0')
1740 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001741 if (type == C_BUILTIN)
1742 err = init_builtin(name);
1743 else
1744 err = PyImport_ImportFrozenModule(name);
1745 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001746 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001747 if (err == 0) {
1748 PyErr_Format(PyExc_ImportError,
1749 "Purported %s module %.200s not found",
1750 type == C_BUILTIN ?
1751 "builtin" : "frozen",
1752 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001753 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001754 }
1755 modules = PyImport_GetModuleDict();
1756 m = PyDict_GetItemString(modules, name);
1757 if (m == NULL) {
1758 PyErr_Format(
1759 PyExc_ImportError,
1760 "%s module %.200s not properly initialized",
1761 type == C_BUILTIN ?
1762 "builtin" : "frozen",
1763 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001764 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001765 }
1766 Py_INCREF(m);
1767 break;
1768
Just van Rossum52e14d62002-12-30 22:08:05 +00001769 case IMP_HOOK: {
1770 if (loader == NULL) {
1771 PyErr_SetString(PyExc_ImportError,
1772 "import hook without loader");
1773 return NULL;
1774 }
1775 m = PyObject_CallMethod(loader, "load_module", "s", name);
1776 break;
1777 }
1778
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001780 PyErr_Format(PyExc_ImportError,
1781 "Don't know how to import %.200s (type code %d)",
1782 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001783 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001784
1785 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001786
1787 return m;
1788}
1789
1790
1791/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001792 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001794
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001795static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001796init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001797{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001798 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001799
Greg Ward201baee2001-10-04 14:52:06 +00001800 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001801 return 1;
1802
Guido van Rossum771c6c81997-10-31 18:37:24 +00001803 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001804 if (strcmp(name, p->name) == 0) {
1805 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001806 PyErr_Format(PyExc_ImportError,
1807 "Cannot re-init internal module %.200s",
1808 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001809 return -1;
1810 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001812 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001813 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001815 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001816 if (_PyImport_FixupExtension(name, name) == NULL)
1817 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001818 return 1;
1819 }
1820 }
1821 return 0;
1822}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001823
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001824
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001825/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001827static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001828find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001829{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001830 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001831
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001833 if (p->name == NULL)
1834 return NULL;
1835 if (strcmp(p->name, name) == 0)
1836 break;
1837 }
1838 return p;
1839}
1840
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001842get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001843{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001844 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001845 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001846
1847 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001848 PyErr_Format(PyExc_ImportError,
1849 "No such frozen object named %.200s",
1850 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001851 return NULL;
1852 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001853 if (p->code == NULL) {
1854 PyErr_Format(PyExc_ImportError,
1855 "Excluded frozen object named %.200s",
1856 name);
1857 return NULL;
1858 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001859 size = p->size;
1860 if (size < 0)
1861 size = -size;
1862 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001863}
1864
1865/* Initialize a frozen module.
1866 Return 1 for succes, 0 if the module is not found, and -1 with
1867 an exception set if the initialization failed.
1868 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001869
1870int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001871PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001872{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001873 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001874 PyObject *co;
1875 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001876 int ispackage;
1877 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001878
1879 if (p == NULL)
1880 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001881 if (p->code == NULL) {
1882 PyErr_Format(PyExc_ImportError,
1883 "Excluded frozen object named %.200s",
1884 name);
1885 return -1;
1886 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001887 size = p->size;
1888 ispackage = (size < 0);
1889 if (ispackage)
1890 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001892 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001893 name, ispackage ? " package" : "");
1894 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001895 if (co == NULL)
1896 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001898 PyErr_Format(PyExc_TypeError,
1899 "frozen object %.200s is not a code object",
1900 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001901 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001902 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001903 if (ispackage) {
1904 /* Set __path__ to the package name */
1905 PyObject *d, *s;
1906 int err;
1907 m = PyImport_AddModule(name);
1908 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001909 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001910 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001911 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001912 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001913 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001914 err = PyDict_SetItemString(d, "__path__", s);
1915 Py_DECREF(s);
1916 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001917 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001918 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001919 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001920 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001921 goto err_return;
1922 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001924 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001925err_return:
1926 Py_DECREF(co);
1927 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001928}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001929
1930
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001931/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001932 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001933
Guido van Rossum79f25d91997-04-29 20:08:16 +00001934PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001935PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001936{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001937 PyObject *pname;
1938 PyObject *result;
1939
Martin v. Löwis5b222132007-06-10 09:51:05 +00001940 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001941 if (pname == NULL)
1942 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001943 result = PyImport_Import(pname);
1944 Py_DECREF(pname);
1945 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001946}
1947
Christian Heimes072c0f12008-01-03 23:01:04 +00001948/* Import a module without blocking
1949 *
1950 * At first it tries to fetch the module from sys.modules. If the module was
1951 * never loaded before it loads it with PyImport_ImportModule() unless another
1952 * thread holds the import lock. In the latter case the function raises an
1953 * ImportError instead of blocking.
1954 *
1955 * Returns the module object with incremented ref count.
1956 */
1957PyObject *
1958PyImport_ImportModuleNoBlock(const char *name)
1959{
1960 PyObject *result;
1961 PyObject *modules;
1962 long me;
1963
1964 /* Try to get the module from sys.modules[name] */
1965 modules = PyImport_GetModuleDict();
1966 if (modules == NULL)
1967 return NULL;
1968
1969 result = PyDict_GetItemString(modules, name);
1970 if (result != NULL) {
1971 Py_INCREF(result);
1972 return result;
1973 }
1974 else {
1975 PyErr_Clear();
1976 }
1977
1978 /* check the import lock
1979 * me might be -1 but I ignore the error here, the lock function
1980 * takes care of the problem */
1981 me = PyThread_get_thread_ident();
1982 if (import_lock_thread == -1 || import_lock_thread == me) {
1983 /* no thread or me is holding the lock */
1984 return PyImport_ImportModule(name);
1985 }
1986 else {
1987 PyErr_Format(PyExc_ImportError,
1988 "Failed to import %.200s because the import lock"
1989 "is held by another thread.",
1990 name);
1991 return NULL;
1992 }
1993}
1994
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001995/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001996static PyObject *get_parent(PyObject *globals, char *buf,
1997 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001998static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001999 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002000static int mark_miss(char *name);
2001static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002002 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002003static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002004
2005/* The Magnum Opus of dotted-name import :-) */
2006
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002007static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002008import_module_level(char *name, PyObject *globals, PyObject *locals,
2009 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002010{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002011 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002012 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002013 PyObject *parent, *head, *next, *tail;
2014
Christian Heimes454f37b2008-01-10 00:10:02 +00002015 if (strchr(name, '/') != NULL
2016#ifdef MS_WINDOWS
2017 || strchr(name, '\\') != NULL
2018#endif
2019 ) {
2020 PyErr_SetString(PyExc_ImportError,
2021 "Import by filename is not supported.");
2022 return NULL;
2023 }
2024
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002025 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002026 if (parent == NULL)
2027 return NULL;
2028
2029 head = load_next(parent, Py_None, &name, buf, &buflen);
2030 if (head == NULL)
2031 return NULL;
2032
2033 tail = head;
2034 Py_INCREF(tail);
2035 while (name) {
2036 next = load_next(tail, tail, &name, buf, &buflen);
2037 Py_DECREF(tail);
2038 if (next == NULL) {
2039 Py_DECREF(head);
2040 return NULL;
2041 }
2042 tail = next;
2043 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002044 if (tail == Py_None) {
2045 /* If tail is Py_None, both get_parent and load_next found
2046 an empty module name: someone called __import__("") or
2047 doctored faulty bytecode */
2048 Py_DECREF(tail);
2049 Py_DECREF(head);
2050 PyErr_SetString(PyExc_ValueError,
2051 "Empty module name");
2052 return NULL;
2053 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002054
2055 if (fromlist != NULL) {
2056 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2057 fromlist = NULL;
2058 }
2059
2060 if (fromlist == NULL) {
2061 Py_DECREF(tail);
2062 return head;
2063 }
2064
2065 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002066 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002067 Py_DECREF(tail);
2068 return NULL;
2069 }
2070
2071 return tail;
2072}
2073
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002074PyObject *
2075PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2076 PyObject *fromlist, int level)
2077{
2078 PyObject *result;
2079 lock_import();
2080 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002081 if (unlock_import() < 0) {
2082 Py_XDECREF(result);
2083 PyErr_SetString(PyExc_RuntimeError,
2084 "not holding the import lock");
2085 return NULL;
2086 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002087 return result;
2088}
2089
Fred Drake87590902004-05-28 20:21:36 +00002090/* Return the package that an import is being performed in. If globals comes
2091 from the module foo.bar.bat (not itself a package), this returns the
2092 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002093 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002094
2095 The *name* of the returned package is returned in buf, with the length of
2096 the name in *p_buflen.
2097
2098 If globals doesn't come from a package or a module in a package, or a
2099 corresponding entry is not found in sys.modules, Py_None is returned.
2100*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002101static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002102get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002103{
2104 static PyObject *namestr = NULL;
2105 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002106 static PyObject *pkgstr = NULL;
2107 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002108
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002109 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002110 return Py_None;
2111
2112 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002113 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002114 if (namestr == NULL)
2115 return NULL;
2116 }
2117 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002118 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002119 if (pathstr == NULL)
2120 return NULL;
2121 }
Nick Coghlande10c852007-12-04 12:22:52 +00002122 if (pkgstr == NULL) {
2123 pkgstr = PyUnicode_InternFromString("__package__");
2124 if (pkgstr == NULL)
2125 return NULL;
2126 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002127
2128 *buf = '\0';
2129 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002130 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002131
Nick Coghlande10c852007-12-04 12:22:52 +00002132 if ((pkgname != NULL) && (pkgname != Py_None)) {
2133 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002134 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002135 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002136
Nick Coghlande10c852007-12-04 12:22:52 +00002137 if (!PyUnicode_Check(pkgname)) {
2138 PyErr_SetString(PyExc_ValueError,
2139 "__package__ set to non-string");
2140 return NULL;
2141 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002142 pkgname_str = PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002143 if (len == 0) {
2144 if (level > 0) {
2145 PyErr_SetString(PyExc_ValueError,
2146 "Attempted relative import in non-package");
2147 return NULL;
2148 }
2149 return Py_None;
2150 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002151 if (len > MAXPATHLEN) {
2152 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002153 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002154 return NULL;
2155 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002156 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002157 } else {
2158 /* __package__ not set, so figure it out and set it */
2159 modname = PyDict_GetItem(globals, namestr);
2160 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002161 return Py_None;
Nick Coghlande10c852007-12-04 12:22:52 +00002162
2163 modpath = PyDict_GetItem(globals, pathstr);
2164 if (modpath != NULL) {
2165 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002166 char *modname_str;
2167 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002168 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002169
2170 modname_str = PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002171 if (len > MAXPATHLEN) {
2172 PyErr_SetString(PyExc_ValueError,
2173 "Module name too long");
2174 return NULL;
2175 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002176 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002177 error = PyDict_SetItem(globals, pkgstr, modname);
2178 if (error) {
2179 PyErr_SetString(PyExc_ValueError,
2180 "Could not set __package__");
2181 return NULL;
2182 }
2183 } else {
2184 /* Normal module, so work out the package name if any */
2185 char *start = PyUnicode_AsString(modname);
2186 char *lastdot = strrchr(start, '.');
2187 size_t len;
2188 int error;
2189 if (lastdot == NULL && level > 0) {
2190 PyErr_SetString(PyExc_ValueError,
2191 "Attempted relative import in non-package");
2192 return NULL;
2193 }
2194 if (lastdot == NULL) {
2195 error = PyDict_SetItem(globals, pkgstr, Py_None);
2196 if (error) {
2197 PyErr_SetString(PyExc_ValueError,
2198 "Could not set __package__");
2199 return NULL;
2200 }
2201 return Py_None;
2202 }
2203 len = lastdot - start;
2204 if (len >= MAXPATHLEN) {
2205 PyErr_SetString(PyExc_ValueError,
2206 "Module name too long");
2207 return NULL;
2208 }
2209 strncpy(buf, start, len);
2210 buf[len] = '\0';
2211 pkgname = PyUnicode_FromString(buf);
2212 if (pkgname == NULL) {
2213 return NULL;
2214 }
2215 error = PyDict_SetItem(globals, pkgstr, pkgname);
2216 Py_DECREF(pkgname);
2217 if (error) {
2218 PyErr_SetString(PyExc_ValueError,
2219 "Could not set __package__");
2220 return NULL;
2221 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002222 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002223 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002224 while (--level > 0) {
2225 char *dot = strrchr(buf, '.');
2226 if (dot == NULL) {
2227 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002228 "Attempted relative import beyond "
2229 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002230 return NULL;
2231 }
2232 *dot = '\0';
2233 }
2234 *p_buflen = strlen(buf);
2235
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002236 modules = PyImport_GetModuleDict();
2237 parent = PyDict_GetItemString(modules, buf);
2238 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002239 PyErr_Format(PyExc_SystemError,
2240 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002241 return parent;
2242 /* We expect, but can't guarantee, if parent != None, that:
2243 - parent.__name__ == buf
2244 - parent.__dict__ is globals
2245 If this is violated... Who cares? */
2246}
2247
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002248/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002249static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002250load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002251 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002252{
2253 char *name = *p_name;
2254 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002255 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002256 char *p;
2257 PyObject *result;
2258
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002259 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002260 /* completely empty module name should only happen in
2261 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002262 Py_INCREF(mod);
2263 *p_name = NULL;
2264 return mod;
2265 }
2266
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002267 if (dot == NULL) {
2268 *p_name = NULL;
2269 len = strlen(name);
2270 }
2271 else {
2272 *p_name = dot+1;
2273 len = dot-name;
2274 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002275 if (len == 0) {
2276 PyErr_SetString(PyExc_ValueError,
2277 "Empty module name");
2278 return NULL;
2279 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002280
2281 p = buf + *p_buflen;
2282 if (p != buf)
2283 *p++ = '.';
2284 if (p+len-buf >= MAXPATHLEN) {
2285 PyErr_SetString(PyExc_ValueError,
2286 "Module name too long");
2287 return NULL;
2288 }
2289 strncpy(p, name, len);
2290 p[len] = '\0';
2291 *p_buflen = p+len-buf;
2292
2293 result = import_submodule(mod, p, buf);
2294 if (result == Py_None && altmod != mod) {
2295 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002296 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002297 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002298 if (result != NULL && result != Py_None) {
2299 if (mark_miss(buf) != 0) {
2300 Py_DECREF(result);
2301 return NULL;
2302 }
2303 strncpy(buf, name, len);
2304 buf[len] = '\0';
2305 *p_buflen = len;
2306 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002307 }
2308 if (result == NULL)
2309 return NULL;
2310
2311 if (result == Py_None) {
2312 Py_DECREF(result);
2313 PyErr_Format(PyExc_ImportError,
2314 "No module named %.200s", name);
2315 return NULL;
2316 }
2317
2318 return result;
2319}
2320
2321static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002322mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002323{
2324 PyObject *modules = PyImport_GetModuleDict();
2325 return PyDict_SetItemString(modules, name, Py_None);
2326}
2327
2328static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002329ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002330 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002331{
2332 int i;
2333
2334 if (!PyObject_HasAttrString(mod, "__path__"))
2335 return 1;
2336
2337 for (i = 0; ; i++) {
2338 PyObject *item = PySequence_GetItem(fromlist, i);
2339 int hasit;
2340 if (item == NULL) {
2341 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2342 PyErr_Clear();
2343 return 1;
2344 }
2345 return 0;
2346 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002347 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002348 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002349 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002350 Py_DECREF(item);
2351 return 0;
2352 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002353 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002354 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002355 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002356 /* See if the package defines __all__ */
2357 if (recursive)
2358 continue; /* Avoid endless recursion */
2359 all = PyObject_GetAttrString(mod, "__all__");
2360 if (all == NULL)
2361 PyErr_Clear();
2362 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002363 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002364 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002365 if (!ret)
2366 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002367 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002368 continue;
2369 }
2370 hasit = PyObject_HasAttr(mod, item);
2371 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002372 PyObject *item8;
2373 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002374 PyObject *submod;
2375 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002376 if (!Py_FileSystemDefaultEncoding) {
2377 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2378 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002379 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002380 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002381 item8 = PyUnicode_AsEncodedString(item,
2382 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002383 }
2384 if (!item8) {
2385 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2386 return 0;
2387 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002388 subname = PyString_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002389 if (buflen + strlen(subname) >= MAXPATHLEN) {
2390 PyErr_SetString(PyExc_ValueError,
2391 "Module name too long");
2392 Py_DECREF(item);
2393 return 0;
2394 }
2395 p = buf + buflen;
2396 *p++ = '.';
2397 strcpy(p, subname);
2398 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002399 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002400 Py_XDECREF(submod);
2401 if (submod == NULL) {
2402 Py_DECREF(item);
2403 return 0;
2404 }
2405 }
2406 Py_DECREF(item);
2407 }
2408
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002409 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002410}
2411
Neil Schemenauer00b09662003-06-16 21:03:07 +00002412static int
2413add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2414 PyObject *modules)
2415{
2416 if (mod == Py_None)
2417 return 1;
2418 /* Irrespective of the success of this load, make a
2419 reference to it in the parent package module. A copy gets
2420 saved in the modules dictionary under the full name, so get a
2421 reference from there, if need be. (The exception is when the
2422 load failed with a SyntaxError -- then there's no trace in
2423 sys.modules. In that case, of course, do nothing extra.) */
2424 if (submod == NULL) {
2425 submod = PyDict_GetItemString(modules, fullname);
2426 if (submod == NULL)
2427 return 1;
2428 }
2429 if (PyModule_Check(mod)) {
2430 /* We can't use setattr here since it can give a
2431 * spurious warning if the submodule name shadows a
2432 * builtin name */
2433 PyObject *dict = PyModule_GetDict(mod);
2434 if (!dict)
2435 return 0;
2436 if (PyDict_SetItemString(dict, subname, submod) < 0)
2437 return 0;
2438 }
2439 else {
2440 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2441 return 0;
2442 }
2443 return 1;
2444}
2445
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002446static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002447import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002448{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002449 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002450 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002451
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002452 /* Require:
2453 if mod == None: subname == fullname
2454 else: mod.__name__ + "." + subname == fullname
2455 */
2456
Tim Peters50d8d372001-02-28 05:34:27 +00002457 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002458 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002459 }
2460 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002461 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002462 char buf[MAXPATHLEN+1];
2463 struct filedescr *fdp;
2464 FILE *fp = NULL;
2465
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002466 if (mod == Py_None)
2467 path = NULL;
2468 else {
2469 path = PyObject_GetAttrString(mod, "__path__");
2470 if (path == NULL) {
2471 PyErr_Clear();
2472 Py_INCREF(Py_None);
2473 return Py_None;
2474 }
2475 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002476
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002477 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002478 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2479 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002480 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002481 if (fdp == NULL) {
2482 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2483 return NULL;
2484 PyErr_Clear();
2485 Py_INCREF(Py_None);
2486 return Py_None;
2487 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002488 m = load_module(fullname, fp, buf, fdp->type, loader);
2489 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002490 if (fp)
2491 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002492 if (!add_submodule(mod, m, fullname, subname, modules)) {
2493 Py_XDECREF(m);
2494 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002495 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002496 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002497
2498 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002499}
2500
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002501
2502/* Re-import a module of any kind and return its module object, WITH
2503 INCREMENTED REFERENCE COUNT */
2504
Guido van Rossum79f25d91997-04-29 20:08:16 +00002505PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002506PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002507{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002508 PyInterpreterState *interp = PyThreadState_Get()->interp;
2509 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002510 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002511 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002512 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002513 char buf[MAXPATHLEN+1];
2514 struct filedescr *fdp;
2515 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002516 PyObject *newm;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002517
2518 if (modules_reloading == NULL) {
2519 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002520 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002521 return NULL;
2522 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002523
Guido van Rossum79f25d91997-04-29 20:08:16 +00002524 if (m == NULL || !PyModule_Check(m)) {
2525 PyErr_SetString(PyExc_TypeError,
2526 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002527 return NULL;
2528 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002529 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002530 if (name == NULL)
2531 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002532 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002533 PyErr_Format(PyExc_ImportError,
2534 "reload(): module %.200s not in sys.modules",
2535 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002536 return NULL;
2537 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002538 existing_m = PyDict_GetItemString(modules_reloading, name);
2539 if (existing_m != NULL) {
2540 /* Due to a recursive reload, this module is already
2541 being reloaded. */
2542 Py_INCREF(existing_m);
2543 return existing_m;
2544 }
2545 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2546 return NULL;
2547
Guido van Rossum222ef561997-09-06 19:41:09 +00002548 subname = strrchr(name, '.');
2549 if (subname == NULL)
2550 subname = name;
2551 else {
2552 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002553 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002554 if (parentname == NULL) {
2555 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002556 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002557 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002558 parent = PyDict_GetItem(modules, parentname);
2559 if (parent == NULL) {
2560 PyErr_Format(PyExc_ImportError,
2561 "reload(): parent %.200s not in sys.modules",
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002562 PyUnicode_AsString(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002563 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002564 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002565 return NULL;
2566 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002567 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002568 subname++;
2569 path = PyObject_GetAttrString(parent, "__path__");
2570 if (path == NULL)
2571 PyErr_Clear();
2572 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002573 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002574 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002575 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002576
2577 if (fdp == NULL) {
2578 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002579 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002580 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002581 }
2582
2583 newm = load_module(name, fp, buf, fdp->type, loader);
2584 Py_XDECREF(loader);
2585
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002586 if (fp)
2587 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002588 if (newm == NULL) {
2589 /* load_module probably removed name from modules because of
2590 * the error. Put back the original module object. We're
2591 * going to return NULL in this case regardless of whether
2592 * replacing name succeeds, so the return value is ignored.
2593 */
2594 PyDict_SetItemString(modules, name, m);
2595 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002596 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002597 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002598}
2599
2600
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002601/* Higher-level import emulator which emulates the "import" statement
2602 more accurately -- it invokes the __import__() function from the
2603 builtins of the current globals. This means that the import is
2604 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002605 environment, e.g. by "rexec".
2606 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002607 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002608 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002609
2610PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002611PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002612{
2613 static PyObject *silly_list = NULL;
2614 static PyObject *builtins_str = NULL;
2615 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002616 PyObject *globals = NULL;
2617 PyObject *import = NULL;
2618 PyObject *builtins = NULL;
2619 PyObject *r = NULL;
2620
2621 /* Initialize constant string objects */
2622 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002623 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002624 if (import_str == NULL)
2625 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002626 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002627 if (builtins_str == NULL)
2628 return NULL;
2629 silly_list = Py_BuildValue("[s]", "__doc__");
2630 if (silly_list == NULL)
2631 return NULL;
2632 }
2633
2634 /* Get the builtins from current globals */
2635 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002636 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002637 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002638 builtins = PyObject_GetItem(globals, builtins_str);
2639 if (builtins == NULL)
2640 goto err;
2641 }
2642 else {
2643 /* No globals -- use standard builtins, and fake globals */
2644 PyErr_Clear();
2645
Georg Brandl1a3284e2007-12-02 09:40:06 +00002646 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002647 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002648 if (builtins == NULL)
2649 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002650 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2651 if (globals == NULL)
2652 goto err;
2653 }
2654
2655 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002656 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002657 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002658 if (import == NULL)
2659 PyErr_SetObject(PyExc_KeyError, import_str);
2660 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002661 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002662 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002663 if (import == NULL)
2664 goto err;
2665
Christian Heimes072c0f12008-01-03 23:01:04 +00002666 /* Call the __import__ function with the proper argument list
2667 * Always use absolute import here. */
2668 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2669 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002670
2671 err:
2672 Py_XDECREF(globals);
2673 Py_XDECREF(builtins);
2674 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002675
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002676 return r;
2677}
2678
2679
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002680/* Module 'imp' provides Python access to the primitives used for
2681 importing modules.
2682*/
2683
Guido van Rossum79f25d91997-04-29 20:08:16 +00002684static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002685imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002686{
2687 char buf[4];
2688
Guido van Rossum96774c12000-05-01 20:19:08 +00002689 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2690 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2691 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2692 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002693
Amaury Forgeot d'Arcae0b0882008-04-24 18:23:22 +00002694 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002695}
2696
Guido van Rossum79f25d91997-04-29 20:08:16 +00002697static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002698imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002699{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002700 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002701 struct filedescr *fdp;
2702
Guido van Rossum79f25d91997-04-29 20:08:16 +00002703 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002704 if (list == NULL)
2705 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002706 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2707 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002708 fdp->suffix, fdp->mode, fdp->type);
2709 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002710 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002711 return NULL;
2712 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002713 if (PyList_Append(list, item) < 0) {
2714 Py_DECREF(list);
2715 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002716 return NULL;
2717 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002718 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002719 }
2720 return list;
2721}
2722
Guido van Rossum79f25d91997-04-29 20:08:16 +00002723static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002724call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002725{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002726 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002727 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002728 struct filedescr *fdp;
2729 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002730 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002731 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002732 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002733 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002734
2735 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002736 if (path == Py_None)
2737 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002738 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002739 if (fdp == NULL)
2740 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002741 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002742 fd = fileno(fp);
2743 if (fd != -1)
2744 fd = dup(fd);
2745 fclose(fp);
2746 fp = NULL;
2747 }
2748 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002749 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002750 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2751 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002752 found_encoding = PyTokenizer_FindEncoding(fd);
2753 lseek(fd, 0, 0); /* Reset position */
Brett Cannon3bb42d92007-10-20 03:43:15 +00002754 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002755 (char*)PyUnicode_GetDefaultEncoding();
2756 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002757 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002758 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002759 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002760 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002761 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002762 return NULL;
2763 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002764 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002765 else {
2766 fob = Py_None;
2767 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002768 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002769 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002770 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002771 Py_DECREF(fob);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002772 PyMem_FREE(found_encoding);
2773
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002774 return ret;
2775}
2776
Guido van Rossum79f25d91997-04-29 20:08:16 +00002777static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002778imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002779{
2780 char *name;
2781 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002782 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002783 return NULL;
2784 return call_find_module(name, path);
2785}
2786
2787static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002788imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002789{
2790 char *name;
2791 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002792 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002793 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002794 return NULL;
2795 ret = init_builtin(name);
2796 if (ret < 0)
2797 return NULL;
2798 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002799 Py_INCREF(Py_None);
2800 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002801 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002802 m = PyImport_AddModule(name);
2803 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002804 return m;
2805}
2806
Guido van Rossum79f25d91997-04-29 20:08:16 +00002807static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002808imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002809{
2810 char *name;
2811 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002812 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002813 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002814 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002815 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002816 if (ret < 0)
2817 return NULL;
2818 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002819 Py_INCREF(Py_None);
2820 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002821 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002822 m = PyImport_AddModule(name);
2823 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002824 return m;
2825}
2826
Guido van Rossum79f25d91997-04-29 20:08:16 +00002827static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002828imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002829{
2830 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002831
Guido van Rossum43713e52000-02-29 13:59:29 +00002832 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002833 return NULL;
2834 return get_frozen_object(name);
2835}
2836
Guido van Rossum79f25d91997-04-29 20:08:16 +00002837static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002838imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002839{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002840 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002841 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002842 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002843 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002844}
2845
Guido van Rossum79f25d91997-04-29 20:08:16 +00002846static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002847imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002848{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002849 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002850 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002851 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002852 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002853 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002854 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002855}
2856
2857static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002858get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002859{
2860 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002861 if (mode[0] == 'U')
2862 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002863 if (fob == NULL) {
2864 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002865 }
2866 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002867 int fd = PyObject_AsFileDescriptor(fob);
2868 if (fd == -1)
2869 return NULL;
2870 /* XXX This will leak a FILE struct. Fix this!!!!
2871 (But it doesn't leak a file descrioptor!) */
2872 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002873 }
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002874 if (fp == NULL)
2875 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002876 return fp;
2877}
2878
Guido van Rossum79f25d91997-04-29 20:08:16 +00002879static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002880imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002881{
2882 char *name;
2883 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002884 PyObject *fob = NULL;
2885 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002886 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002887 if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
2888 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002889 return NULL;
2890 fp = get_file(pathname, fob, "rb");
2891 if (fp == NULL)
2892 return NULL;
2893 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002894 if (fob == NULL)
2895 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002896 return m;
2897}
2898
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002899#ifdef HAVE_DYNAMIC_LOADING
2900
Guido van Rossum79f25d91997-04-29 20:08:16 +00002901static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002902imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002903{
2904 char *name;
2905 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002906 PyObject *fob = NULL;
2907 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002908 FILE *fp = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002909 if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
2910 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002911 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002912 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002913 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002914 if (fp == NULL)
2915 return NULL;
2916 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002917 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002918 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002919}
2920
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002921#endif /* HAVE_DYNAMIC_LOADING */
2922
Guido van Rossum79f25d91997-04-29 20:08:16 +00002923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002924imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002925{
2926 char *name;
2927 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002928 PyObject *fob = NULL;
2929 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002930 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002931 if (!PyArg_ParseTuple(args, "ss|O:load_source",
2932 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002933 return NULL;
2934 fp = get_file(pathname, fob, "r");
2935 if (fp == NULL)
2936 return NULL;
2937 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002938 if (fob == NULL)
2939 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002940 return m;
2941}
2942
Guido van Rossum79f25d91997-04-29 20:08:16 +00002943static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002944imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002945{
2946 char *name;
2947 PyObject *fob;
2948 char *pathname;
2949 char *suffix; /* Unused */
2950 char *mode;
2951 int type;
2952 FILE *fp;
2953
Guido van Rossum43713e52000-02-29 13:59:29 +00002954 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002955 &name, &fob, &pathname,
2956 &suffix, &mode, &type))
2957 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002958 if (*mode) {
2959 /* Mode must start with 'r' or 'U' and must not contain '+'.
2960 Implicit in this test is the assumption that the mode
2961 may contain other modifiers like 'b' or 't'. */
2962
2963 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002964 PyErr_Format(PyExc_ValueError,
2965 "invalid file open mode %.200s", mode);
2966 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002967 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002968 }
2969 if (fob == Py_None)
2970 fp = NULL;
2971 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002972 fp = get_file(NULL, fob, mode);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002973 if (fp == NULL)
2974 return NULL;
2975 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002976 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002977}
2978
2979static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002980imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002981{
2982 char *name;
2983 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002984 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002985 return NULL;
2986 return load_package(name, pathname);
2987}
2988
2989static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002990imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002991{
2992 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002993 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002994 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002995 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002996}
2997
Christian Heimes13a7a212008-01-07 17:13:09 +00002998static PyObject *
2999imp_reload(PyObject *self, PyObject *v)
3000{
3001 return PyImport_ReloadModule(v);
3002}
3003
3004PyDoc_STRVAR(doc_reload,
3005"reload(module) -> module\n\
3006\n\
3007Reload the module. The module must have been successfully imported before.");
3008
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003009/* Doc strings */
3010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003011PyDoc_STRVAR(doc_imp,
3012"This module provides the components needed to build your own\n\
3013__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003015PyDoc_STRVAR(doc_find_module,
3016"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003017Search for a module. If path is omitted or None, search for a\n\
3018built-in, frozen or special module and continue search in sys.path.\n\
3019The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003020package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003022PyDoc_STRVAR(doc_load_module,
3023"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003024Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003025The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027PyDoc_STRVAR(doc_get_magic,
3028"get_magic() -> string\n\
3029Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003031PyDoc_STRVAR(doc_get_suffixes,
3032"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003033Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003034that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(doc_new_module,
3037"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003038Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003039The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003041PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003042"lock_held() -> boolean\n\
3043Return True if the import lock is currently held, else False.\n\
3044On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003045
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003046PyDoc_STRVAR(doc_acquire_lock,
3047"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003048Acquires the interpreter's import lock for the current thread.\n\
3049This lock should be used by import hooks to ensure thread-safety\n\
3050when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003051On platforms without threads, this function does nothing.");
3052
3053PyDoc_STRVAR(doc_release_lock,
3054"release_lock() -> None\n\
3055Release the interpreter's import lock.\n\
3056On platforms without threads, this function does nothing.");
3057
Guido van Rossum79f25d91997-04-29 20:08:16 +00003058static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003059 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3060 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3061 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3062 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3063 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3064 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3065 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3066 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003067 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003068 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003069 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3070 {"init_builtin", imp_init_builtin, METH_VARARGS},
3071 {"init_frozen", imp_init_frozen, METH_VARARGS},
3072 {"is_builtin", imp_is_builtin, METH_VARARGS},
3073 {"is_frozen", imp_is_frozen, METH_VARARGS},
3074 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003075#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003076 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003077#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003078 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003079 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003080 {NULL, NULL} /* sentinel */
3081};
3082
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003083static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003084setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003085{
3086 PyObject *v;
3087 int err;
3088
Christian Heimes217cfd12007-12-02 14:31:20 +00003089 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003090 err = PyDict_SetItemString(d, name, v);
3091 Py_XDECREF(v);
3092 return err;
3093}
3094
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003095typedef struct {
3096 PyObject_HEAD
3097} NullImporter;
3098
3099static int
3100NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3101{
3102 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003103 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003104
3105 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3106 return -1;
3107
3108 if (!PyArg_ParseTuple(args, "s:NullImporter",
3109 &path))
3110 return -1;
3111
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003112 pathlen = strlen(path);
3113 if (pathlen == 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003114 PyErr_SetString(PyExc_ImportError, "empty pathname");
3115 return -1;
3116 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003117 struct stat statbuf;
3118 int rv;
3119
3120 rv = stat(path, &statbuf);
Guido van Rossumc1bdbc32007-11-07 17:46:34 +00003121#ifdef MS_WINDOWS
3122 /* MS Windows stat() chokes on paths like C:\path\. Try to
3123 * recover *one* time by stripping off a trailing slash or
3124 * backslash. http://bugs.python.org/issue1293
3125 */
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003126 if (rv != 0 && pathlen <= MAXPATHLEN &&
3127 (path[pathlen-1] == '/' || path[pathlen-1] == '\\')) {
3128 char mangled[MAXPATHLEN+1];
3129
3130 strcpy(mangled, path);
3131 mangled[pathlen-1] = '\0';
3132 rv = stat(mangled, &statbuf);
3133 }
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003134#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003135 if (rv == 0) {
3136 /* it exists */
3137 if (S_ISDIR(statbuf.st_mode)) {
3138 /* it's a directory */
3139 PyErr_SetString(PyExc_ImportError,
3140 "existing directory");
3141 return -1;
3142 }
3143 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003144 }
3145 return 0;
3146}
3147
3148static PyObject *
3149NullImporter_find_module(NullImporter *self, PyObject *args)
3150{
3151 Py_RETURN_NONE;
3152}
3153
3154static PyMethodDef NullImporter_methods[] = {
3155 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3156 "Always return None"
3157 },
3158 {NULL} /* Sentinel */
3159};
3160
3161
Christian Heimes9cd17752007-11-18 19:35:23 +00003162PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003163 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003164 "imp.NullImporter", /*tp_name*/
3165 sizeof(NullImporter), /*tp_basicsize*/
3166 0, /*tp_itemsize*/
3167 0, /*tp_dealloc*/
3168 0, /*tp_print*/
3169 0, /*tp_getattr*/
3170 0, /*tp_setattr*/
3171 0, /*tp_compare*/
3172 0, /*tp_repr*/
3173 0, /*tp_as_number*/
3174 0, /*tp_as_sequence*/
3175 0, /*tp_as_mapping*/
3176 0, /*tp_hash */
3177 0, /*tp_call*/
3178 0, /*tp_str*/
3179 0, /*tp_getattro*/
3180 0, /*tp_setattro*/
3181 0, /*tp_as_buffer*/
3182 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3183 "Null importer object", /* tp_doc */
3184 0, /* tp_traverse */
3185 0, /* tp_clear */
3186 0, /* tp_richcompare */
3187 0, /* tp_weaklistoffset */
3188 0, /* tp_iter */
3189 0, /* tp_iternext */
3190 NullImporter_methods, /* tp_methods */
3191 0, /* tp_members */
3192 0, /* tp_getset */
3193 0, /* tp_base */
3194 0, /* tp_dict */
3195 0, /* tp_descr_get */
3196 0, /* tp_descr_set */
3197 0, /* tp_dictoffset */
3198 (initproc)NullImporter_init, /* tp_init */
3199 0, /* tp_alloc */
3200 PyType_GenericNew /* tp_new */
3201};
3202
3203
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003204PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003205initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003206{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003207 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003208
Christian Heimes9cd17752007-11-18 19:35:23 +00003209 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003210 goto failure;
3211
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003212 m = Py_InitModule4("imp", imp_methods, doc_imp,
3213 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003214 if (m == NULL)
3215 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003216 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003217 if (d == NULL)
3218 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003219
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003220 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3221 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3222 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3223 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3224 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3225 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3226 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3227 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003228 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003229 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003230
Christian Heimes9cd17752007-11-18 19:35:23 +00003231 Py_INCREF(&PyNullImporter_Type);
3232 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003233 failure:
3234 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003235}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003236
3237
Guido van Rossumb18618d2000-05-03 23:44:39 +00003238/* API for embedding applications that want to add their own entries
3239 to the table of built-in modules. This should normally be called
3240 *before* Py_Initialize(). When the table resize fails, -1 is
3241 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003242
3243 After a similar function by Just van Rossum. */
3244
3245int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003246PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003247{
3248 static struct _inittab *our_copy = NULL;
3249 struct _inittab *p;
3250 int i, n;
3251
3252 /* Count the number of entries in both tables */
3253 for (n = 0; newtab[n].name != NULL; n++)
3254 ;
3255 if (n == 0)
3256 return 0; /* Nothing to do */
3257 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3258 ;
3259
3260 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003261 p = our_copy;
3262 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003263 if (p == NULL)
3264 return -1;
3265
3266 /* Copy the tables into the new memory */
3267 if (our_copy != PyImport_Inittab)
3268 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3269 PyImport_Inittab = our_copy = p;
3270 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3271
3272 return 0;
3273}
3274
3275/* Shorthand to add a single entry given a name and a function */
3276
3277int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003278PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003279{
3280 struct _inittab newtab[2];
3281
3282 memset(newtab, '\0', sizeof newtab);
3283
3284 newtab[0].name = name;
3285 newtab[0].initfunc = initfunc;
3286
3287 return PyImport_ExtendInittab(newtab);
3288}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003289
3290#ifdef __cplusplus
3291}
3292#endif